en_netdev.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. /*
  2. * Copyright (c) 2007 Mellanox Technologies. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/etherdevice.h>
  34. #include <linux/tcp.h>
  35. #include <linux/if_vlan.h>
  36. #include <linux/delay.h>
  37. #include <linux/slab.h>
  38. #include <linux/mlx4/driver.h>
  39. #include <linux/mlx4/device.h>
  40. #include <linux/mlx4/cmd.h>
  41. #include <linux/mlx4/cq.h>
  42. #include "mlx4_en.h"
  43. #include "en_port.h"
  44. static int mlx4_en_setup_tc(struct net_device *dev, u8 up)
  45. {
  46. struct mlx4_en_priv *priv = netdev_priv(dev);
  47. int i;
  48. unsigned int q, offset = 0;
  49. if (up && up != MLX4_EN_NUM_UP)
  50. return -EINVAL;
  51. netdev_set_num_tc(dev, up);
  52. /* Partition Tx queues evenly amongst UP's */
  53. q = priv->tx_ring_num / up;
  54. for (i = 0; i < up; i++) {
  55. netdev_set_tc_queue(dev, i, q, offset);
  56. offset += q;
  57. }
  58. return 0;
  59. }
  60. static int mlx4_en_vlan_rx_add_vid(struct net_device *dev, unsigned short vid)
  61. {
  62. struct mlx4_en_priv *priv = netdev_priv(dev);
  63. struct mlx4_en_dev *mdev = priv->mdev;
  64. int err;
  65. int idx;
  66. en_dbg(HW, priv, "adding VLAN:%d\n", vid);
  67. set_bit(vid, priv->active_vlans);
  68. /* Add VID to port VLAN filter */
  69. mutex_lock(&mdev->state_lock);
  70. if (mdev->device_up && priv->port_up) {
  71. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  72. if (err)
  73. en_err(priv, "Failed configuring VLAN filter\n");
  74. }
  75. if (mlx4_register_vlan(mdev->dev, priv->port, vid, &idx))
  76. en_err(priv, "failed adding vlan %d\n", vid);
  77. mutex_unlock(&mdev->state_lock);
  78. return 0;
  79. }
  80. static int mlx4_en_vlan_rx_kill_vid(struct net_device *dev, unsigned short vid)
  81. {
  82. struct mlx4_en_priv *priv = netdev_priv(dev);
  83. struct mlx4_en_dev *mdev = priv->mdev;
  84. int err;
  85. int idx;
  86. en_dbg(HW, priv, "Killing VID:%d\n", vid);
  87. clear_bit(vid, priv->active_vlans);
  88. /* Remove VID from port VLAN filter */
  89. mutex_lock(&mdev->state_lock);
  90. if (!mlx4_find_cached_vlan(mdev->dev, priv->port, vid, &idx))
  91. mlx4_unregister_vlan(mdev->dev, priv->port, idx);
  92. else
  93. en_err(priv, "could not find vid %d in cache\n", vid);
  94. if (mdev->device_up && priv->port_up) {
  95. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  96. if (err)
  97. en_err(priv, "Failed configuring VLAN filter\n");
  98. }
  99. mutex_unlock(&mdev->state_lock);
  100. return 0;
  101. }
  102. u64 mlx4_en_mac_to_u64(u8 *addr)
  103. {
  104. u64 mac = 0;
  105. int i;
  106. for (i = 0; i < ETH_ALEN; i++) {
  107. mac <<= 8;
  108. mac |= addr[i];
  109. }
  110. return mac;
  111. }
  112. static int mlx4_en_set_mac(struct net_device *dev, void *addr)
  113. {
  114. struct mlx4_en_priv *priv = netdev_priv(dev);
  115. struct mlx4_en_dev *mdev = priv->mdev;
  116. struct sockaddr *saddr = addr;
  117. if (!is_valid_ether_addr(saddr->sa_data))
  118. return -EADDRNOTAVAIL;
  119. memcpy(dev->dev_addr, saddr->sa_data, ETH_ALEN);
  120. priv->mac = mlx4_en_mac_to_u64(dev->dev_addr);
  121. queue_work(mdev->workqueue, &priv->mac_task);
  122. return 0;
  123. }
  124. static void mlx4_en_do_set_mac(struct work_struct *work)
  125. {
  126. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  127. mac_task);
  128. struct mlx4_en_dev *mdev = priv->mdev;
  129. int err = 0;
  130. mutex_lock(&mdev->state_lock);
  131. if (priv->port_up) {
  132. /* Remove old MAC and insert the new one */
  133. err = mlx4_replace_mac(mdev->dev, priv->port,
  134. priv->base_qpn, priv->mac);
  135. if (err)
  136. en_err(priv, "Failed changing HW MAC address\n");
  137. } else
  138. en_dbg(HW, priv, "Port is down while "
  139. "registering mac, exiting...\n");
  140. mutex_unlock(&mdev->state_lock);
  141. }
  142. static void mlx4_en_clear_list(struct net_device *dev)
  143. {
  144. struct mlx4_en_priv *priv = netdev_priv(dev);
  145. struct mlx4_en_mc_list *tmp, *mc_to_del;
  146. list_for_each_entry_safe(mc_to_del, tmp, &priv->mc_list, list) {
  147. list_del(&mc_to_del->list);
  148. kfree(mc_to_del);
  149. }
  150. }
  151. static void mlx4_en_cache_mclist(struct net_device *dev)
  152. {
  153. struct mlx4_en_priv *priv = netdev_priv(dev);
  154. struct netdev_hw_addr *ha;
  155. struct mlx4_en_mc_list *tmp;
  156. mlx4_en_clear_list(dev);
  157. netdev_for_each_mc_addr(ha, dev) {
  158. tmp = kzalloc(sizeof(struct mlx4_en_mc_list), GFP_ATOMIC);
  159. if (!tmp) {
  160. en_err(priv, "failed to allocate multicast list\n");
  161. mlx4_en_clear_list(dev);
  162. return;
  163. }
  164. memcpy(tmp->addr, ha->addr, ETH_ALEN);
  165. list_add_tail(&tmp->list, &priv->mc_list);
  166. }
  167. }
  168. static void update_mclist_flags(struct mlx4_en_priv *priv,
  169. struct list_head *dst,
  170. struct list_head *src)
  171. {
  172. struct mlx4_en_mc_list *dst_tmp, *src_tmp, *new_mc;
  173. bool found;
  174. /* Find all the entries that should be removed from dst,
  175. * These are the entries that are not found in src
  176. */
  177. list_for_each_entry(dst_tmp, dst, list) {
  178. found = false;
  179. list_for_each_entry(src_tmp, src, list) {
  180. if (!memcmp(dst_tmp->addr, src_tmp->addr, ETH_ALEN)) {
  181. found = true;
  182. break;
  183. }
  184. }
  185. if (!found)
  186. dst_tmp->action = MCLIST_REM;
  187. }
  188. /* Add entries that exist in src but not in dst
  189. * mark them as need to add
  190. */
  191. list_for_each_entry(src_tmp, src, list) {
  192. found = false;
  193. list_for_each_entry(dst_tmp, dst, list) {
  194. if (!memcmp(dst_tmp->addr, src_tmp->addr, ETH_ALEN)) {
  195. dst_tmp->action = MCLIST_NONE;
  196. found = true;
  197. break;
  198. }
  199. }
  200. if (!found) {
  201. new_mc = kmalloc(sizeof(struct mlx4_en_mc_list),
  202. GFP_KERNEL);
  203. if (!new_mc) {
  204. en_err(priv, "Failed to allocate current multicast list\n");
  205. return;
  206. }
  207. memcpy(new_mc, src_tmp,
  208. sizeof(struct mlx4_en_mc_list));
  209. new_mc->action = MCLIST_ADD;
  210. list_add_tail(&new_mc->list, dst);
  211. }
  212. }
  213. }
  214. static void mlx4_en_set_multicast(struct net_device *dev)
  215. {
  216. struct mlx4_en_priv *priv = netdev_priv(dev);
  217. if (!priv->port_up)
  218. return;
  219. queue_work(priv->mdev->workqueue, &priv->mcast_task);
  220. }
  221. static void mlx4_en_do_set_multicast(struct work_struct *work)
  222. {
  223. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  224. mcast_task);
  225. struct mlx4_en_dev *mdev = priv->mdev;
  226. struct net_device *dev = priv->dev;
  227. struct mlx4_en_mc_list *mclist, *tmp;
  228. u64 mcast_addr = 0;
  229. u8 mc_list[16] = {0};
  230. int err = 0;
  231. mutex_lock(&mdev->state_lock);
  232. if (!mdev->device_up) {
  233. en_dbg(HW, priv, "Card is not up, "
  234. "ignoring multicast change.\n");
  235. goto out;
  236. }
  237. if (!priv->port_up) {
  238. en_dbg(HW, priv, "Port is down, "
  239. "ignoring multicast change.\n");
  240. goto out;
  241. }
  242. if (!netif_carrier_ok(dev)) {
  243. if (!mlx4_en_QUERY_PORT(mdev, priv->port)) {
  244. if (priv->port_state.link_state) {
  245. priv->last_link_state = MLX4_DEV_EVENT_PORT_UP;
  246. netif_carrier_on(dev);
  247. en_dbg(LINK, priv, "Link Up\n");
  248. }
  249. }
  250. }
  251. /*
  252. * Promsicuous mode: disable all filters
  253. */
  254. if (dev->flags & IFF_PROMISC) {
  255. if (!(priv->flags & MLX4_EN_FLAG_PROMISC)) {
  256. if (netif_msg_rx_status(priv))
  257. en_warn(priv, "Entering promiscuous mode\n");
  258. priv->flags |= MLX4_EN_FLAG_PROMISC;
  259. /* Enable promiscouos mode */
  260. switch (mdev->dev->caps.steering_mode) {
  261. case MLX4_STEERING_MODE_B0:
  262. err = mlx4_unicast_promisc_add(mdev->dev,
  263. priv->base_qpn,
  264. priv->port);
  265. if (err)
  266. en_err(priv, "Failed enabling unicast promiscuous mode\n");
  267. /* Add the default qp number as multicast
  268. * promisc
  269. */
  270. if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
  271. err = mlx4_multicast_promisc_add(mdev->dev,
  272. priv->base_qpn,
  273. priv->port);
  274. if (err)
  275. en_err(priv, "Failed enabling multicast promiscuous mode\n");
  276. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  277. }
  278. break;
  279. case MLX4_STEERING_MODE_A0:
  280. err = mlx4_SET_PORT_qpn_calc(mdev->dev,
  281. priv->port,
  282. priv->base_qpn,
  283. 1);
  284. if (err)
  285. en_err(priv, "Failed enabling promiscuous mode\n");
  286. break;
  287. }
  288. /* Disable port multicast filter (unconditionally) */
  289. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  290. 0, MLX4_MCAST_DISABLE);
  291. if (err)
  292. en_err(priv, "Failed disabling "
  293. "multicast filter\n");
  294. /* Disable port VLAN filter */
  295. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  296. if (err)
  297. en_err(priv, "Failed disabling VLAN filter\n");
  298. }
  299. goto out;
  300. }
  301. /*
  302. * Not in promiscuous mode
  303. */
  304. if (priv->flags & MLX4_EN_FLAG_PROMISC) {
  305. if (netif_msg_rx_status(priv))
  306. en_warn(priv, "Leaving promiscuous mode\n");
  307. priv->flags &= ~MLX4_EN_FLAG_PROMISC;
  308. /* Disable promiscouos mode */
  309. switch (mdev->dev->caps.steering_mode) {
  310. case MLX4_STEERING_MODE_B0:
  311. err = mlx4_unicast_promisc_remove(mdev->dev,
  312. priv->base_qpn,
  313. priv->port);
  314. if (err)
  315. en_err(priv, "Failed disabling unicast promiscuous mode\n");
  316. /* Disable Multicast promisc */
  317. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  318. err = mlx4_multicast_promisc_remove(mdev->dev,
  319. priv->base_qpn,
  320. priv->port);
  321. if (err)
  322. en_err(priv, "Failed disabling multicast promiscuous mode\n");
  323. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  324. }
  325. break;
  326. case MLX4_STEERING_MODE_A0:
  327. err = mlx4_SET_PORT_qpn_calc(mdev->dev,
  328. priv->port,
  329. priv->base_qpn, 0);
  330. if (err)
  331. en_err(priv, "Failed disabling promiscuous mode\n");
  332. break;
  333. }
  334. /* Enable port VLAN filter */
  335. err = mlx4_SET_VLAN_FLTR(mdev->dev, priv);
  336. if (err)
  337. en_err(priv, "Failed enabling VLAN filter\n");
  338. }
  339. /* Enable/disable the multicast filter according to IFF_ALLMULTI */
  340. if (dev->flags & IFF_ALLMULTI) {
  341. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  342. 0, MLX4_MCAST_DISABLE);
  343. if (err)
  344. en_err(priv, "Failed disabling multicast filter\n");
  345. /* Add the default qp number as multicast promisc */
  346. if (!(priv->flags & MLX4_EN_FLAG_MC_PROMISC)) {
  347. switch (mdev->dev->caps.steering_mode) {
  348. case MLX4_STEERING_MODE_B0:
  349. err = mlx4_multicast_promisc_add(mdev->dev,
  350. priv->base_qpn,
  351. priv->port);
  352. break;
  353. case MLX4_STEERING_MODE_A0:
  354. break;
  355. }
  356. if (err)
  357. en_err(priv, "Failed entering multicast promisc mode\n");
  358. priv->flags |= MLX4_EN_FLAG_MC_PROMISC;
  359. }
  360. } else {
  361. /* Disable Multicast promisc */
  362. if (priv->flags & MLX4_EN_FLAG_MC_PROMISC) {
  363. switch (mdev->dev->caps.steering_mode) {
  364. case MLX4_STEERING_MODE_B0:
  365. err = mlx4_multicast_promisc_remove(mdev->dev,
  366. priv->base_qpn,
  367. priv->port);
  368. break;
  369. case MLX4_STEERING_MODE_A0:
  370. break;
  371. }
  372. if (err)
  373. en_err(priv, "Failed disabling multicast promiscuous mode\n");
  374. priv->flags &= ~MLX4_EN_FLAG_MC_PROMISC;
  375. }
  376. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  377. 0, MLX4_MCAST_DISABLE);
  378. if (err)
  379. en_err(priv, "Failed disabling multicast filter\n");
  380. /* Flush mcast filter and init it with broadcast address */
  381. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, ETH_BCAST,
  382. 1, MLX4_MCAST_CONFIG);
  383. /* Update multicast list - we cache all addresses so they won't
  384. * change while HW is updated holding the command semaphor */
  385. netif_tx_lock_bh(dev);
  386. mlx4_en_cache_mclist(dev);
  387. netif_tx_unlock_bh(dev);
  388. list_for_each_entry(mclist, &priv->mc_list, list) {
  389. mcast_addr = mlx4_en_mac_to_u64(mclist->addr);
  390. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port,
  391. mcast_addr, 0, MLX4_MCAST_CONFIG);
  392. }
  393. err = mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0,
  394. 0, MLX4_MCAST_ENABLE);
  395. if (err)
  396. en_err(priv, "Failed enabling multicast filter\n");
  397. update_mclist_flags(priv, &priv->curr_list, &priv->mc_list);
  398. list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
  399. if (mclist->action == MCLIST_REM) {
  400. /* detach this address and delete from list */
  401. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  402. mc_list[5] = priv->port;
  403. err = mlx4_multicast_detach(mdev->dev,
  404. &priv->rss_map.indir_qp,
  405. mc_list,
  406. MLX4_PROT_ETH,
  407. mclist->reg_id);
  408. if (err)
  409. en_err(priv, "Fail to detach multicast address\n");
  410. /* remove from list */
  411. list_del(&mclist->list);
  412. kfree(mclist);
  413. }
  414. if (mclist->action == MCLIST_ADD) {
  415. /* attach the address */
  416. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  417. /* needed for B0 steering support */
  418. mc_list[5] = priv->port;
  419. err = mlx4_multicast_attach(mdev->dev,
  420. &priv->rss_map.indir_qp,
  421. mc_list,
  422. priv->port, 0,
  423. MLX4_PROT_ETH,
  424. &mclist->reg_id);
  425. if (err)
  426. en_err(priv, "Fail to attach multicast address\n");
  427. }
  428. }
  429. }
  430. out:
  431. mutex_unlock(&mdev->state_lock);
  432. }
  433. #ifdef CONFIG_NET_POLL_CONTROLLER
  434. static void mlx4_en_netpoll(struct net_device *dev)
  435. {
  436. struct mlx4_en_priv *priv = netdev_priv(dev);
  437. struct mlx4_en_cq *cq;
  438. unsigned long flags;
  439. int i;
  440. for (i = 0; i < priv->rx_ring_num; i++) {
  441. cq = &priv->rx_cq[i];
  442. spin_lock_irqsave(&cq->lock, flags);
  443. napi_synchronize(&cq->napi);
  444. mlx4_en_process_rx_cq(dev, cq, 0);
  445. spin_unlock_irqrestore(&cq->lock, flags);
  446. }
  447. }
  448. #endif
  449. static void mlx4_en_tx_timeout(struct net_device *dev)
  450. {
  451. struct mlx4_en_priv *priv = netdev_priv(dev);
  452. struct mlx4_en_dev *mdev = priv->mdev;
  453. if (netif_msg_timer(priv))
  454. en_warn(priv, "Tx timeout called on port:%d\n", priv->port);
  455. priv->port_stats.tx_timeout++;
  456. en_dbg(DRV, priv, "Scheduling watchdog\n");
  457. queue_work(mdev->workqueue, &priv->watchdog_task);
  458. }
  459. static struct net_device_stats *mlx4_en_get_stats(struct net_device *dev)
  460. {
  461. struct mlx4_en_priv *priv = netdev_priv(dev);
  462. spin_lock_bh(&priv->stats_lock);
  463. memcpy(&priv->ret_stats, &priv->stats, sizeof(priv->stats));
  464. spin_unlock_bh(&priv->stats_lock);
  465. return &priv->ret_stats;
  466. }
  467. static void mlx4_en_set_default_moderation(struct mlx4_en_priv *priv)
  468. {
  469. struct mlx4_en_cq *cq;
  470. int i;
  471. /* If we haven't received a specific coalescing setting
  472. * (module param), we set the moderation parameters as follows:
  473. * - moder_cnt is set to the number of mtu sized packets to
  474. * satisfy our coelsing target.
  475. * - moder_time is set to a fixed value.
  476. */
  477. priv->rx_frames = MLX4_EN_RX_COAL_TARGET;
  478. priv->rx_usecs = MLX4_EN_RX_COAL_TIME;
  479. priv->tx_frames = MLX4_EN_TX_COAL_PKTS;
  480. priv->tx_usecs = MLX4_EN_TX_COAL_TIME;
  481. en_dbg(INTR, priv, "Default coalesing params for mtu:%d - "
  482. "rx_frames:%d rx_usecs:%d\n",
  483. priv->dev->mtu, priv->rx_frames, priv->rx_usecs);
  484. /* Setup cq moderation params */
  485. for (i = 0; i < priv->rx_ring_num; i++) {
  486. cq = &priv->rx_cq[i];
  487. cq->moder_cnt = priv->rx_frames;
  488. cq->moder_time = priv->rx_usecs;
  489. priv->last_moder_time[i] = MLX4_EN_AUTO_CONF;
  490. priv->last_moder_packets[i] = 0;
  491. priv->last_moder_bytes[i] = 0;
  492. }
  493. for (i = 0; i < priv->tx_ring_num; i++) {
  494. cq = &priv->tx_cq[i];
  495. cq->moder_cnt = priv->tx_frames;
  496. cq->moder_time = priv->tx_usecs;
  497. }
  498. /* Reset auto-moderation params */
  499. priv->pkt_rate_low = MLX4_EN_RX_RATE_LOW;
  500. priv->rx_usecs_low = MLX4_EN_RX_COAL_TIME_LOW;
  501. priv->pkt_rate_high = MLX4_EN_RX_RATE_HIGH;
  502. priv->rx_usecs_high = MLX4_EN_RX_COAL_TIME_HIGH;
  503. priv->sample_interval = MLX4_EN_SAMPLE_INTERVAL;
  504. priv->adaptive_rx_coal = 1;
  505. priv->last_moder_jiffies = 0;
  506. priv->last_moder_tx_packets = 0;
  507. }
  508. static void mlx4_en_auto_moderation(struct mlx4_en_priv *priv)
  509. {
  510. unsigned long period = (unsigned long) (jiffies - priv->last_moder_jiffies);
  511. struct mlx4_en_cq *cq;
  512. unsigned long packets;
  513. unsigned long rate;
  514. unsigned long avg_pkt_size;
  515. unsigned long rx_packets;
  516. unsigned long rx_bytes;
  517. unsigned long rx_pkt_diff;
  518. int moder_time;
  519. int ring, err;
  520. if (!priv->adaptive_rx_coal || period < priv->sample_interval * HZ)
  521. return;
  522. for (ring = 0; ring < priv->rx_ring_num; ring++) {
  523. spin_lock_bh(&priv->stats_lock);
  524. rx_packets = priv->rx_ring[ring].packets;
  525. rx_bytes = priv->rx_ring[ring].bytes;
  526. spin_unlock_bh(&priv->stats_lock);
  527. rx_pkt_diff = ((unsigned long) (rx_packets -
  528. priv->last_moder_packets[ring]));
  529. packets = rx_pkt_diff;
  530. rate = packets * HZ / period;
  531. avg_pkt_size = packets ? ((unsigned long) (rx_bytes -
  532. priv->last_moder_bytes[ring])) / packets : 0;
  533. /* Apply auto-moderation only when packet rate
  534. * exceeds a rate that it matters */
  535. if (rate > (MLX4_EN_RX_RATE_THRESH / priv->rx_ring_num) &&
  536. avg_pkt_size > MLX4_EN_AVG_PKT_SMALL) {
  537. if (rate < priv->pkt_rate_low)
  538. moder_time = priv->rx_usecs_low;
  539. else if (rate > priv->pkt_rate_high)
  540. moder_time = priv->rx_usecs_high;
  541. else
  542. moder_time = (rate - priv->pkt_rate_low) *
  543. (priv->rx_usecs_high - priv->rx_usecs_low) /
  544. (priv->pkt_rate_high - priv->pkt_rate_low) +
  545. priv->rx_usecs_low;
  546. } else {
  547. moder_time = priv->rx_usecs_low;
  548. }
  549. if (moder_time != priv->last_moder_time[ring]) {
  550. priv->last_moder_time[ring] = moder_time;
  551. cq = &priv->rx_cq[ring];
  552. cq->moder_time = moder_time;
  553. err = mlx4_en_set_cq_moder(priv, cq);
  554. if (err)
  555. en_err(priv, "Failed modifying moderation "
  556. "for cq:%d\n", ring);
  557. }
  558. priv->last_moder_packets[ring] = rx_packets;
  559. priv->last_moder_bytes[ring] = rx_bytes;
  560. }
  561. priv->last_moder_jiffies = jiffies;
  562. }
  563. static void mlx4_en_do_get_stats(struct work_struct *work)
  564. {
  565. struct delayed_work *delay = to_delayed_work(work);
  566. struct mlx4_en_priv *priv = container_of(delay, struct mlx4_en_priv,
  567. stats_task);
  568. struct mlx4_en_dev *mdev = priv->mdev;
  569. int err;
  570. err = mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 0);
  571. if (err)
  572. en_dbg(HW, priv, "Could not update stats\n");
  573. mutex_lock(&mdev->state_lock);
  574. if (mdev->device_up) {
  575. if (priv->port_up)
  576. mlx4_en_auto_moderation(priv);
  577. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  578. }
  579. if (mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port]) {
  580. queue_work(mdev->workqueue, &priv->mac_task);
  581. mdev->mac_removed[MLX4_MAX_PORTS + 1 - priv->port] = 0;
  582. }
  583. mutex_unlock(&mdev->state_lock);
  584. }
  585. static void mlx4_en_linkstate(struct work_struct *work)
  586. {
  587. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  588. linkstate_task);
  589. struct mlx4_en_dev *mdev = priv->mdev;
  590. int linkstate = priv->link_state;
  591. mutex_lock(&mdev->state_lock);
  592. /* If observable port state changed set carrier state and
  593. * report to system log */
  594. if (priv->last_link_state != linkstate) {
  595. if (linkstate == MLX4_DEV_EVENT_PORT_DOWN) {
  596. en_info(priv, "Link Down\n");
  597. netif_carrier_off(priv->dev);
  598. } else {
  599. en_info(priv, "Link Up\n");
  600. netif_carrier_on(priv->dev);
  601. }
  602. }
  603. priv->last_link_state = linkstate;
  604. mutex_unlock(&mdev->state_lock);
  605. }
  606. int mlx4_en_start_port(struct net_device *dev)
  607. {
  608. struct mlx4_en_priv *priv = netdev_priv(dev);
  609. struct mlx4_en_dev *mdev = priv->mdev;
  610. struct mlx4_en_cq *cq;
  611. struct mlx4_en_tx_ring *tx_ring;
  612. int rx_index = 0;
  613. int tx_index = 0;
  614. int err = 0;
  615. int i;
  616. int j;
  617. u8 mc_list[16] = {0};
  618. if (priv->port_up) {
  619. en_dbg(DRV, priv, "start port called while port already up\n");
  620. return 0;
  621. }
  622. INIT_LIST_HEAD(&priv->mc_list);
  623. INIT_LIST_HEAD(&priv->curr_list);
  624. /* Calculate Rx buf size */
  625. dev->mtu = min(dev->mtu, priv->max_mtu);
  626. mlx4_en_calc_rx_buf(dev);
  627. en_dbg(DRV, priv, "Rx buf size:%d\n", priv->rx_skb_size);
  628. /* Configure rx cq's and rings */
  629. err = mlx4_en_activate_rx_rings(priv);
  630. if (err) {
  631. en_err(priv, "Failed to activate RX rings\n");
  632. return err;
  633. }
  634. for (i = 0; i < priv->rx_ring_num; i++) {
  635. cq = &priv->rx_cq[i];
  636. err = mlx4_en_activate_cq(priv, cq, i);
  637. if (err) {
  638. en_err(priv, "Failed activating Rx CQ\n");
  639. goto cq_err;
  640. }
  641. for (j = 0; j < cq->size; j++)
  642. cq->buf[j].owner_sr_opcode = MLX4_CQE_OWNER_MASK;
  643. err = mlx4_en_set_cq_moder(priv, cq);
  644. if (err) {
  645. en_err(priv, "Failed setting cq moderation parameters");
  646. mlx4_en_deactivate_cq(priv, cq);
  647. goto cq_err;
  648. }
  649. mlx4_en_arm_cq(priv, cq);
  650. priv->rx_ring[i].cqn = cq->mcq.cqn;
  651. ++rx_index;
  652. }
  653. /* Set qp number */
  654. en_dbg(DRV, priv, "Getting qp number for port %d\n", priv->port);
  655. err = mlx4_get_eth_qp(mdev->dev, priv->port,
  656. priv->mac, &priv->base_qpn);
  657. if (err) {
  658. en_err(priv, "Failed getting eth qp\n");
  659. goto cq_err;
  660. }
  661. mdev->mac_removed[priv->port] = 0;
  662. err = mlx4_en_config_rss_steer(priv);
  663. if (err) {
  664. en_err(priv, "Failed configuring rss steering\n");
  665. goto mac_err;
  666. }
  667. /* Configure tx cq's and rings */
  668. for (i = 0; i < priv->tx_ring_num; i++) {
  669. /* Configure cq */
  670. cq = &priv->tx_cq[i];
  671. err = mlx4_en_activate_cq(priv, cq, i);
  672. if (err) {
  673. en_err(priv, "Failed allocating Tx CQ\n");
  674. goto tx_err;
  675. }
  676. err = mlx4_en_set_cq_moder(priv, cq);
  677. if (err) {
  678. en_err(priv, "Failed setting cq moderation parameters");
  679. mlx4_en_deactivate_cq(priv, cq);
  680. goto tx_err;
  681. }
  682. en_dbg(DRV, priv, "Resetting index of collapsed CQ:%d to -1\n", i);
  683. cq->buf->wqe_index = cpu_to_be16(0xffff);
  684. /* Configure ring */
  685. tx_ring = &priv->tx_ring[i];
  686. err = mlx4_en_activate_tx_ring(priv, tx_ring, cq->mcq.cqn,
  687. i / priv->mdev->profile.num_tx_rings_p_up);
  688. if (err) {
  689. en_err(priv, "Failed allocating Tx ring\n");
  690. mlx4_en_deactivate_cq(priv, cq);
  691. goto tx_err;
  692. }
  693. tx_ring->tx_queue = netdev_get_tx_queue(dev, i);
  694. /* Arm CQ for TX completions */
  695. mlx4_en_arm_cq(priv, cq);
  696. /* Set initial ownership of all Tx TXBBs to SW (1) */
  697. for (j = 0; j < tx_ring->buf_size; j += STAMP_STRIDE)
  698. *((u32 *) (tx_ring->buf + j)) = 0xffffffff;
  699. ++tx_index;
  700. }
  701. /* Configure port */
  702. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  703. priv->rx_skb_size + ETH_FCS_LEN,
  704. priv->prof->tx_pause,
  705. priv->prof->tx_ppp,
  706. priv->prof->rx_pause,
  707. priv->prof->rx_ppp);
  708. if (err) {
  709. en_err(priv, "Failed setting port general configurations "
  710. "for port %d, with error %d\n", priv->port, err);
  711. goto tx_err;
  712. }
  713. /* Set default qp number */
  714. err = mlx4_SET_PORT_qpn_calc(mdev->dev, priv->port, priv->base_qpn, 0);
  715. if (err) {
  716. en_err(priv, "Failed setting default qp numbers\n");
  717. goto tx_err;
  718. }
  719. /* Init port */
  720. en_dbg(HW, priv, "Initializing port\n");
  721. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  722. if (err) {
  723. en_err(priv, "Failed Initializing port\n");
  724. goto tx_err;
  725. }
  726. /* Attach rx QP to bradcast address */
  727. memset(&mc_list[10], 0xff, ETH_ALEN);
  728. mc_list[5] = priv->port; /* needed for B0 steering support */
  729. if (mlx4_multicast_attach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
  730. priv->port, 0, MLX4_PROT_ETH,
  731. &priv->broadcast_id))
  732. mlx4_warn(mdev, "Failed Attaching Broadcast\n");
  733. /* Must redo promiscuous mode setup. */
  734. priv->flags &= ~(MLX4_EN_FLAG_PROMISC | MLX4_EN_FLAG_MC_PROMISC);
  735. /* Schedule multicast task to populate multicast list */
  736. queue_work(mdev->workqueue, &priv->mcast_task);
  737. mlx4_set_stats_bitmap(mdev->dev, &priv->stats_bitmap);
  738. priv->port_up = true;
  739. netif_tx_start_all_queues(dev);
  740. return 0;
  741. tx_err:
  742. while (tx_index--) {
  743. mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[tx_index]);
  744. mlx4_en_deactivate_cq(priv, &priv->tx_cq[tx_index]);
  745. }
  746. mlx4_en_release_rss_steer(priv);
  747. mac_err:
  748. mlx4_put_eth_qp(mdev->dev, priv->port, priv->mac, priv->base_qpn);
  749. cq_err:
  750. while (rx_index--)
  751. mlx4_en_deactivate_cq(priv, &priv->rx_cq[rx_index]);
  752. for (i = 0; i < priv->rx_ring_num; i++)
  753. mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
  754. return err; /* need to close devices */
  755. }
  756. void mlx4_en_stop_port(struct net_device *dev)
  757. {
  758. struct mlx4_en_priv *priv = netdev_priv(dev);
  759. struct mlx4_en_dev *mdev = priv->mdev;
  760. struct mlx4_en_mc_list *mclist, *tmp;
  761. int i;
  762. u8 mc_list[16] = {0};
  763. if (!priv->port_up) {
  764. en_dbg(DRV, priv, "stop port called while port already down\n");
  765. return;
  766. }
  767. /* Synchronize with tx routine */
  768. netif_tx_lock_bh(dev);
  769. netif_tx_stop_all_queues(dev);
  770. netif_tx_unlock_bh(dev);
  771. /* Set port as not active */
  772. priv->port_up = false;
  773. /* Detach All multicasts */
  774. memset(&mc_list[10], 0xff, ETH_ALEN);
  775. mc_list[5] = priv->port; /* needed for B0 steering support */
  776. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp, mc_list,
  777. MLX4_PROT_ETH, priv->broadcast_id);
  778. list_for_each_entry(mclist, &priv->curr_list, list) {
  779. memcpy(&mc_list[10], mclist->addr, ETH_ALEN);
  780. mc_list[5] = priv->port;
  781. mlx4_multicast_detach(mdev->dev, &priv->rss_map.indir_qp,
  782. mc_list, MLX4_PROT_ETH, mclist->reg_id);
  783. }
  784. mlx4_en_clear_list(dev);
  785. list_for_each_entry_safe(mclist, tmp, &priv->curr_list, list) {
  786. list_del(&mclist->list);
  787. kfree(mclist);
  788. }
  789. /* Flush multicast filter */
  790. mlx4_SET_MCAST_FLTR(mdev->dev, priv->port, 0, 1, MLX4_MCAST_CONFIG);
  791. /* Free TX Rings */
  792. for (i = 0; i < priv->tx_ring_num; i++) {
  793. mlx4_en_deactivate_tx_ring(priv, &priv->tx_ring[i]);
  794. mlx4_en_deactivate_cq(priv, &priv->tx_cq[i]);
  795. }
  796. msleep(10);
  797. for (i = 0; i < priv->tx_ring_num; i++)
  798. mlx4_en_free_tx_buf(dev, &priv->tx_ring[i]);
  799. /* Free RSS qps */
  800. mlx4_en_release_rss_steer(priv);
  801. /* Unregister Mac address for the port */
  802. mlx4_put_eth_qp(mdev->dev, priv->port, priv->mac, priv->base_qpn);
  803. mdev->mac_removed[priv->port] = 1;
  804. /* Free RX Rings */
  805. for (i = 0; i < priv->rx_ring_num; i++) {
  806. mlx4_en_deactivate_rx_ring(priv, &priv->rx_ring[i]);
  807. while (test_bit(NAPI_STATE_SCHED, &priv->rx_cq[i].napi.state))
  808. msleep(1);
  809. mlx4_en_deactivate_cq(priv, &priv->rx_cq[i]);
  810. }
  811. /* close port*/
  812. mlx4_CLOSE_PORT(mdev->dev, priv->port);
  813. }
  814. static void mlx4_en_restart(struct work_struct *work)
  815. {
  816. struct mlx4_en_priv *priv = container_of(work, struct mlx4_en_priv,
  817. watchdog_task);
  818. struct mlx4_en_dev *mdev = priv->mdev;
  819. struct net_device *dev = priv->dev;
  820. int i;
  821. en_dbg(DRV, priv, "Watchdog task called for port %d\n", priv->port);
  822. mutex_lock(&mdev->state_lock);
  823. if (priv->port_up) {
  824. mlx4_en_stop_port(dev);
  825. for (i = 0; i < priv->tx_ring_num; i++)
  826. netdev_tx_reset_queue(priv->tx_ring[i].tx_queue);
  827. if (mlx4_en_start_port(dev))
  828. en_err(priv, "Failed restarting port %d\n", priv->port);
  829. }
  830. mutex_unlock(&mdev->state_lock);
  831. }
  832. static void mlx4_en_clear_stats(struct net_device *dev)
  833. {
  834. struct mlx4_en_priv *priv = netdev_priv(dev);
  835. struct mlx4_en_dev *mdev = priv->mdev;
  836. int i;
  837. if (mlx4_en_DUMP_ETH_STATS(mdev, priv->port, 1))
  838. en_dbg(HW, priv, "Failed dumping statistics\n");
  839. memset(&priv->stats, 0, sizeof(priv->stats));
  840. memset(&priv->pstats, 0, sizeof(priv->pstats));
  841. memset(&priv->pkstats, 0, sizeof(priv->pkstats));
  842. memset(&priv->port_stats, 0, sizeof(priv->port_stats));
  843. for (i = 0; i < priv->tx_ring_num; i++) {
  844. priv->tx_ring[i].bytes = 0;
  845. priv->tx_ring[i].packets = 0;
  846. priv->tx_ring[i].tx_csum = 0;
  847. }
  848. for (i = 0; i < priv->rx_ring_num; i++) {
  849. priv->rx_ring[i].bytes = 0;
  850. priv->rx_ring[i].packets = 0;
  851. priv->rx_ring[i].csum_ok = 0;
  852. priv->rx_ring[i].csum_none = 0;
  853. }
  854. }
  855. static int mlx4_en_open(struct net_device *dev)
  856. {
  857. struct mlx4_en_priv *priv = netdev_priv(dev);
  858. struct mlx4_en_dev *mdev = priv->mdev;
  859. int err = 0;
  860. mutex_lock(&mdev->state_lock);
  861. if (!mdev->device_up) {
  862. en_err(priv, "Cannot open - device down/disabled\n");
  863. err = -EBUSY;
  864. goto out;
  865. }
  866. /* Reset HW statistics and SW counters */
  867. mlx4_en_clear_stats(dev);
  868. err = mlx4_en_start_port(dev);
  869. if (err)
  870. en_err(priv, "Failed starting port:%d\n", priv->port);
  871. out:
  872. mutex_unlock(&mdev->state_lock);
  873. return err;
  874. }
  875. static int mlx4_en_close(struct net_device *dev)
  876. {
  877. struct mlx4_en_priv *priv = netdev_priv(dev);
  878. struct mlx4_en_dev *mdev = priv->mdev;
  879. en_dbg(IFDOWN, priv, "Close port called\n");
  880. mutex_lock(&mdev->state_lock);
  881. mlx4_en_stop_port(dev);
  882. netif_carrier_off(dev);
  883. mutex_unlock(&mdev->state_lock);
  884. return 0;
  885. }
  886. void mlx4_en_free_resources(struct mlx4_en_priv *priv)
  887. {
  888. int i;
  889. for (i = 0; i < priv->tx_ring_num; i++) {
  890. if (priv->tx_ring[i].tx_info)
  891. mlx4_en_destroy_tx_ring(priv, &priv->tx_ring[i]);
  892. if (priv->tx_cq[i].buf)
  893. mlx4_en_destroy_cq(priv, &priv->tx_cq[i]);
  894. }
  895. for (i = 0; i < priv->rx_ring_num; i++) {
  896. if (priv->rx_ring[i].rx_info)
  897. mlx4_en_destroy_rx_ring(priv, &priv->rx_ring[i],
  898. priv->prof->rx_ring_size, priv->stride);
  899. if (priv->rx_cq[i].buf)
  900. mlx4_en_destroy_cq(priv, &priv->rx_cq[i]);
  901. }
  902. if (priv->base_tx_qpn) {
  903. mlx4_qp_release_range(priv->mdev->dev, priv->base_tx_qpn, priv->tx_ring_num);
  904. priv->base_tx_qpn = 0;
  905. }
  906. }
  907. int mlx4_en_alloc_resources(struct mlx4_en_priv *priv)
  908. {
  909. struct mlx4_en_port_profile *prof = priv->prof;
  910. int i;
  911. int err;
  912. err = mlx4_qp_reserve_range(priv->mdev->dev, priv->tx_ring_num, 256, &priv->base_tx_qpn);
  913. if (err) {
  914. en_err(priv, "failed reserving range for TX rings\n");
  915. return err;
  916. }
  917. /* Create tx Rings */
  918. for (i = 0; i < priv->tx_ring_num; i++) {
  919. if (mlx4_en_create_cq(priv, &priv->tx_cq[i],
  920. prof->tx_ring_size, i, TX))
  921. goto err;
  922. if (mlx4_en_create_tx_ring(priv, &priv->tx_ring[i], priv->base_tx_qpn + i,
  923. prof->tx_ring_size, TXBB_SIZE))
  924. goto err;
  925. }
  926. /* Create rx Rings */
  927. for (i = 0; i < priv->rx_ring_num; i++) {
  928. if (mlx4_en_create_cq(priv, &priv->rx_cq[i],
  929. prof->rx_ring_size, i, RX))
  930. goto err;
  931. if (mlx4_en_create_rx_ring(priv, &priv->rx_ring[i],
  932. prof->rx_ring_size, priv->stride))
  933. goto err;
  934. }
  935. return 0;
  936. err:
  937. en_err(priv, "Failed to allocate NIC resources\n");
  938. return -ENOMEM;
  939. }
  940. void mlx4_en_destroy_netdev(struct net_device *dev)
  941. {
  942. struct mlx4_en_priv *priv = netdev_priv(dev);
  943. struct mlx4_en_dev *mdev = priv->mdev;
  944. en_dbg(DRV, priv, "Destroying netdev on port:%d\n", priv->port);
  945. /* Unregister device - this will close the port if it was up */
  946. if (priv->registered)
  947. unregister_netdev(dev);
  948. if (priv->allocated)
  949. mlx4_free_hwq_res(mdev->dev, &priv->res, MLX4_EN_PAGE_SIZE);
  950. cancel_delayed_work(&priv->stats_task);
  951. /* flush any pending task for this netdev */
  952. flush_workqueue(mdev->workqueue);
  953. /* Detach the netdev so tasks would not attempt to access it */
  954. mutex_lock(&mdev->state_lock);
  955. mdev->pndev[priv->port] = NULL;
  956. mutex_unlock(&mdev->state_lock);
  957. mlx4_en_free_resources(priv);
  958. kfree(priv->tx_ring);
  959. kfree(priv->tx_cq);
  960. free_netdev(dev);
  961. }
  962. static int mlx4_en_change_mtu(struct net_device *dev, int new_mtu)
  963. {
  964. struct mlx4_en_priv *priv = netdev_priv(dev);
  965. struct mlx4_en_dev *mdev = priv->mdev;
  966. int err = 0;
  967. en_dbg(DRV, priv, "Change MTU called - current:%d new:%d\n",
  968. dev->mtu, new_mtu);
  969. if ((new_mtu < MLX4_EN_MIN_MTU) || (new_mtu > priv->max_mtu)) {
  970. en_err(priv, "Bad MTU size:%d.\n", new_mtu);
  971. return -EPERM;
  972. }
  973. dev->mtu = new_mtu;
  974. if (netif_running(dev)) {
  975. mutex_lock(&mdev->state_lock);
  976. if (!mdev->device_up) {
  977. /* NIC is probably restarting - let watchdog task reset
  978. * the port */
  979. en_dbg(DRV, priv, "Change MTU called with card down!?\n");
  980. } else {
  981. mlx4_en_stop_port(dev);
  982. err = mlx4_en_start_port(dev);
  983. if (err) {
  984. en_err(priv, "Failed restarting port:%d\n",
  985. priv->port);
  986. queue_work(mdev->workqueue, &priv->watchdog_task);
  987. }
  988. }
  989. mutex_unlock(&mdev->state_lock);
  990. }
  991. return 0;
  992. }
  993. static int mlx4_en_set_features(struct net_device *netdev,
  994. netdev_features_t features)
  995. {
  996. struct mlx4_en_priv *priv = netdev_priv(netdev);
  997. if (features & NETIF_F_LOOPBACK)
  998. priv->ctrl_flags |= cpu_to_be32(MLX4_WQE_CTRL_FORCE_LOOPBACK);
  999. else
  1000. priv->ctrl_flags &=
  1001. cpu_to_be32(~MLX4_WQE_CTRL_FORCE_LOOPBACK);
  1002. return 0;
  1003. }
  1004. static const struct net_device_ops mlx4_netdev_ops = {
  1005. .ndo_open = mlx4_en_open,
  1006. .ndo_stop = mlx4_en_close,
  1007. .ndo_start_xmit = mlx4_en_xmit,
  1008. .ndo_select_queue = mlx4_en_select_queue,
  1009. .ndo_get_stats = mlx4_en_get_stats,
  1010. .ndo_set_rx_mode = mlx4_en_set_multicast,
  1011. .ndo_set_mac_address = mlx4_en_set_mac,
  1012. .ndo_validate_addr = eth_validate_addr,
  1013. .ndo_change_mtu = mlx4_en_change_mtu,
  1014. .ndo_tx_timeout = mlx4_en_tx_timeout,
  1015. .ndo_vlan_rx_add_vid = mlx4_en_vlan_rx_add_vid,
  1016. .ndo_vlan_rx_kill_vid = mlx4_en_vlan_rx_kill_vid,
  1017. #ifdef CONFIG_NET_POLL_CONTROLLER
  1018. .ndo_poll_controller = mlx4_en_netpoll,
  1019. #endif
  1020. .ndo_set_features = mlx4_en_set_features,
  1021. .ndo_setup_tc = mlx4_en_setup_tc,
  1022. };
  1023. int mlx4_en_init_netdev(struct mlx4_en_dev *mdev, int port,
  1024. struct mlx4_en_port_profile *prof)
  1025. {
  1026. struct net_device *dev;
  1027. struct mlx4_en_priv *priv;
  1028. int i;
  1029. int err;
  1030. dev = alloc_etherdev_mqs(sizeof(struct mlx4_en_priv),
  1031. prof->tx_ring_num, prof->rx_ring_num);
  1032. if (dev == NULL)
  1033. return -ENOMEM;
  1034. SET_NETDEV_DEV(dev, &mdev->dev->pdev->dev);
  1035. dev->dev_id = port - 1;
  1036. /*
  1037. * Initialize driver private data
  1038. */
  1039. priv = netdev_priv(dev);
  1040. memset(priv, 0, sizeof(struct mlx4_en_priv));
  1041. priv->dev = dev;
  1042. priv->mdev = mdev;
  1043. priv->ddev = &mdev->pdev->dev;
  1044. priv->prof = prof;
  1045. priv->port = port;
  1046. priv->port_up = false;
  1047. priv->flags = prof->flags;
  1048. priv->ctrl_flags = cpu_to_be32(MLX4_WQE_CTRL_CQ_UPDATE |
  1049. MLX4_WQE_CTRL_SOLICITED);
  1050. priv->tx_ring_num = prof->tx_ring_num;
  1051. priv->tx_ring = kzalloc(sizeof(struct mlx4_en_tx_ring) *
  1052. priv->tx_ring_num, GFP_KERNEL);
  1053. if (!priv->tx_ring) {
  1054. err = -ENOMEM;
  1055. goto out;
  1056. }
  1057. priv->tx_cq = kzalloc(sizeof(struct mlx4_en_cq) * priv->tx_ring_num,
  1058. GFP_KERNEL);
  1059. if (!priv->tx_cq) {
  1060. err = -ENOMEM;
  1061. goto out;
  1062. }
  1063. priv->rx_ring_num = prof->rx_ring_num;
  1064. priv->mac_index = -1;
  1065. priv->msg_enable = MLX4_EN_MSG_LEVEL;
  1066. spin_lock_init(&priv->stats_lock);
  1067. INIT_WORK(&priv->mcast_task, mlx4_en_do_set_multicast);
  1068. INIT_WORK(&priv->mac_task, mlx4_en_do_set_mac);
  1069. INIT_WORK(&priv->watchdog_task, mlx4_en_restart);
  1070. INIT_WORK(&priv->linkstate_task, mlx4_en_linkstate);
  1071. INIT_DELAYED_WORK(&priv->stats_task, mlx4_en_do_get_stats);
  1072. #ifdef CONFIG_MLX4_EN_DCB
  1073. if (!mlx4_is_slave(priv->mdev->dev))
  1074. dev->dcbnl_ops = &mlx4_en_dcbnl_ops;
  1075. #endif
  1076. /* Query for default mac and max mtu */
  1077. priv->max_mtu = mdev->dev->caps.eth_mtu_cap[priv->port];
  1078. priv->mac = mdev->dev->caps.def_mac[priv->port];
  1079. if (ILLEGAL_MAC(priv->mac)) {
  1080. en_err(priv, "Port: %d, invalid mac burned: 0x%llx, quiting\n",
  1081. priv->port, priv->mac);
  1082. err = -EINVAL;
  1083. goto out;
  1084. }
  1085. priv->stride = roundup_pow_of_two(sizeof(struct mlx4_en_rx_desc) +
  1086. DS_SIZE * MLX4_EN_MAX_RX_FRAGS);
  1087. err = mlx4_en_alloc_resources(priv);
  1088. if (err)
  1089. goto out;
  1090. /* Allocate page for receive rings */
  1091. err = mlx4_alloc_hwq_res(mdev->dev, &priv->res,
  1092. MLX4_EN_PAGE_SIZE, MLX4_EN_PAGE_SIZE);
  1093. if (err) {
  1094. en_err(priv, "Failed to allocate page for rx qps\n");
  1095. goto out;
  1096. }
  1097. priv->allocated = 1;
  1098. /*
  1099. * Initialize netdev entry points
  1100. */
  1101. dev->netdev_ops = &mlx4_netdev_ops;
  1102. dev->watchdog_timeo = MLX4_EN_WATCHDOG_TIMEOUT;
  1103. netif_set_real_num_tx_queues(dev, priv->tx_ring_num);
  1104. netif_set_real_num_rx_queues(dev, priv->rx_ring_num);
  1105. SET_ETHTOOL_OPS(dev, &mlx4_en_ethtool_ops);
  1106. /* Set defualt MAC */
  1107. dev->addr_len = ETH_ALEN;
  1108. for (i = 0; i < ETH_ALEN; i++) {
  1109. dev->dev_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
  1110. dev->perm_addr[ETH_ALEN - 1 - i] = (u8) (priv->mac >> (8 * i));
  1111. }
  1112. /*
  1113. * Set driver features
  1114. */
  1115. dev->hw_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_IPV6_CSUM;
  1116. if (mdev->LSO_support)
  1117. dev->hw_features |= NETIF_F_TSO | NETIF_F_TSO6;
  1118. dev->vlan_features = dev->hw_features;
  1119. dev->hw_features |= NETIF_F_RXCSUM | NETIF_F_RXHASH;
  1120. dev->features = dev->hw_features | NETIF_F_HIGHDMA |
  1121. NETIF_F_HW_VLAN_TX | NETIF_F_HW_VLAN_RX |
  1122. NETIF_F_HW_VLAN_FILTER;
  1123. dev->hw_features |= NETIF_F_LOOPBACK;
  1124. mdev->pndev[port] = dev;
  1125. netif_carrier_off(dev);
  1126. err = register_netdev(dev);
  1127. if (err) {
  1128. en_err(priv, "Netdev registration failed for port %d\n", port);
  1129. goto out;
  1130. }
  1131. priv->registered = 1;
  1132. en_warn(priv, "Using %d TX rings\n", prof->tx_ring_num);
  1133. en_warn(priv, "Using %d RX rings\n", prof->rx_ring_num);
  1134. /* Configure port */
  1135. mlx4_en_calc_rx_buf(dev);
  1136. err = mlx4_SET_PORT_general(mdev->dev, priv->port,
  1137. priv->rx_skb_size + ETH_FCS_LEN,
  1138. prof->tx_pause, prof->tx_ppp,
  1139. prof->rx_pause, prof->rx_ppp);
  1140. if (err) {
  1141. en_err(priv, "Failed setting port general configurations "
  1142. "for port %d, with error %d\n", priv->port, err);
  1143. goto out;
  1144. }
  1145. /* Init port */
  1146. en_warn(priv, "Initializing port\n");
  1147. err = mlx4_INIT_PORT(mdev->dev, priv->port);
  1148. if (err) {
  1149. en_err(priv, "Failed Initializing port\n");
  1150. goto out;
  1151. }
  1152. mlx4_en_set_default_moderation(priv);
  1153. queue_delayed_work(mdev->workqueue, &priv->stats_task, STATS_DELAY);
  1154. return 0;
  1155. out:
  1156. mlx4_en_destroy_netdev(dev);
  1157. return err;
  1158. }