nicvf_main.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. /*
  2. * Copyright (C) 2015 Cavium, Inc.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of version 2 of the GNU General Public License
  6. * as published by the Free Software Foundation.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/interrupt.h>
  10. #include <linux/pci.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/if_vlan.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/ethtool.h>
  15. #include <linux/log2.h>
  16. #include <linux/prefetch.h>
  17. #include <linux/irq.h>
  18. #include <linux/iommu.h>
  19. #include "nic_reg.h"
  20. #include "nic.h"
  21. #include "nicvf_queues.h"
  22. #include "thunder_bgx.h"
  23. #define DRV_NAME "thunder-nicvf"
  24. #define DRV_VERSION "1.0"
  25. /* Supported devices */
  26. static const struct pci_device_id nicvf_id_table[] = {
  27. { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
  28. PCI_DEVICE_ID_THUNDER_NIC_VF,
  29. PCI_VENDOR_ID_CAVIUM,
  30. PCI_SUBSYS_DEVID_88XX_NIC_VF) },
  31. { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
  32. PCI_DEVICE_ID_THUNDER_PASS1_NIC_VF,
  33. PCI_VENDOR_ID_CAVIUM,
  34. PCI_SUBSYS_DEVID_88XX_PASS1_NIC_VF) },
  35. { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
  36. PCI_DEVICE_ID_THUNDER_NIC_VF,
  37. PCI_VENDOR_ID_CAVIUM,
  38. PCI_SUBSYS_DEVID_81XX_NIC_VF) },
  39. { PCI_DEVICE_SUB(PCI_VENDOR_ID_CAVIUM,
  40. PCI_DEVICE_ID_THUNDER_NIC_VF,
  41. PCI_VENDOR_ID_CAVIUM,
  42. PCI_SUBSYS_DEVID_83XX_NIC_VF) },
  43. { 0, } /* end of table */
  44. };
  45. MODULE_AUTHOR("Sunil Goutham");
  46. MODULE_DESCRIPTION("Cavium Thunder NIC Virtual Function Driver");
  47. MODULE_LICENSE("GPL v2");
  48. MODULE_VERSION(DRV_VERSION);
  49. MODULE_DEVICE_TABLE(pci, nicvf_id_table);
  50. static int debug = 0x00;
  51. module_param(debug, int, 0644);
  52. MODULE_PARM_DESC(debug, "Debug message level bitmap");
  53. static int cpi_alg = CPI_ALG_NONE;
  54. module_param(cpi_alg, int, S_IRUGO);
  55. MODULE_PARM_DESC(cpi_alg,
  56. "PFC algorithm (0=none, 1=VLAN, 2=VLAN16, 3=IP Diffserv)");
  57. static inline u8 nicvf_netdev_qidx(struct nicvf *nic, u8 qidx)
  58. {
  59. if (nic->sqs_mode)
  60. return qidx + ((nic->sqs_id + 1) * MAX_CMP_QUEUES_PER_QS);
  61. else
  62. return qidx;
  63. }
  64. /* The Cavium ThunderX network controller can *only* be found in SoCs
  65. * containing the ThunderX ARM64 CPU implementation. All accesses to the device
  66. * registers on this platform are implicitly strongly ordered with respect
  67. * to memory accesses. So writeq_relaxed() and readq_relaxed() are safe to use
  68. * with no memory barriers in this driver. The readq()/writeq() functions add
  69. * explicit ordering operation which in this case are redundant, and only
  70. * add overhead.
  71. */
  72. /* Register read/write APIs */
  73. void nicvf_reg_write(struct nicvf *nic, u64 offset, u64 val)
  74. {
  75. writeq_relaxed(val, nic->reg_base + offset);
  76. }
  77. u64 nicvf_reg_read(struct nicvf *nic, u64 offset)
  78. {
  79. return readq_relaxed(nic->reg_base + offset);
  80. }
  81. void nicvf_queue_reg_write(struct nicvf *nic, u64 offset,
  82. u64 qidx, u64 val)
  83. {
  84. void __iomem *addr = nic->reg_base + offset;
  85. writeq_relaxed(val, addr + (qidx << NIC_Q_NUM_SHIFT));
  86. }
  87. u64 nicvf_queue_reg_read(struct nicvf *nic, u64 offset, u64 qidx)
  88. {
  89. void __iomem *addr = nic->reg_base + offset;
  90. return readq_relaxed(addr + (qidx << NIC_Q_NUM_SHIFT));
  91. }
  92. /* VF -> PF mailbox communication */
  93. static void nicvf_write_to_mbx(struct nicvf *nic, union nic_mbx *mbx)
  94. {
  95. u64 *msg = (u64 *)mbx;
  96. nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 0, msg[0]);
  97. nicvf_reg_write(nic, NIC_VF_PF_MAILBOX_0_1 + 8, msg[1]);
  98. }
  99. int nicvf_send_msg_to_pf(struct nicvf *nic, union nic_mbx *mbx)
  100. {
  101. int timeout = NIC_MBOX_MSG_TIMEOUT;
  102. int sleep = 10;
  103. nic->pf_acked = false;
  104. nic->pf_nacked = false;
  105. nicvf_write_to_mbx(nic, mbx);
  106. /* Wait for previous message to be acked, timeout 2sec */
  107. while (!nic->pf_acked) {
  108. if (nic->pf_nacked) {
  109. netdev_err(nic->netdev,
  110. "PF NACK to mbox msg 0x%02x from VF%d\n",
  111. (mbx->msg.msg & 0xFF), nic->vf_id);
  112. return -EINVAL;
  113. }
  114. msleep(sleep);
  115. if (nic->pf_acked)
  116. break;
  117. timeout -= sleep;
  118. if (!timeout) {
  119. netdev_err(nic->netdev,
  120. "PF didn't ACK to mbox msg 0x%02x from VF%d\n",
  121. (mbx->msg.msg & 0xFF), nic->vf_id);
  122. return -EBUSY;
  123. }
  124. }
  125. return 0;
  126. }
  127. /* Checks if VF is able to comminicate with PF
  128. * and also gets the VNIC number this VF is associated to.
  129. */
  130. static int nicvf_check_pf_ready(struct nicvf *nic)
  131. {
  132. union nic_mbx mbx = {};
  133. mbx.msg.msg = NIC_MBOX_MSG_READY;
  134. if (nicvf_send_msg_to_pf(nic, &mbx)) {
  135. netdev_err(nic->netdev,
  136. "PF didn't respond to READY msg\n");
  137. return 0;
  138. }
  139. return 1;
  140. }
  141. static void nicvf_read_bgx_stats(struct nicvf *nic, struct bgx_stats_msg *bgx)
  142. {
  143. if (bgx->rx)
  144. nic->bgx_stats.rx_stats[bgx->idx] = bgx->stats;
  145. else
  146. nic->bgx_stats.tx_stats[bgx->idx] = bgx->stats;
  147. }
  148. static void nicvf_handle_mbx_intr(struct nicvf *nic)
  149. {
  150. union nic_mbx mbx = {};
  151. u64 *mbx_data;
  152. u64 mbx_addr;
  153. int i;
  154. mbx_addr = NIC_VF_PF_MAILBOX_0_1;
  155. mbx_data = (u64 *)&mbx;
  156. for (i = 0; i < NIC_PF_VF_MAILBOX_SIZE; i++) {
  157. *mbx_data = nicvf_reg_read(nic, mbx_addr);
  158. mbx_data++;
  159. mbx_addr += sizeof(u64);
  160. }
  161. netdev_dbg(nic->netdev, "Mbox message: msg: 0x%x\n", mbx.msg.msg);
  162. switch (mbx.msg.msg) {
  163. case NIC_MBOX_MSG_READY:
  164. nic->pf_acked = true;
  165. nic->vf_id = mbx.nic_cfg.vf_id & 0x7F;
  166. nic->tns_mode = mbx.nic_cfg.tns_mode & 0x7F;
  167. nic->node = mbx.nic_cfg.node_id;
  168. if (!nic->set_mac_pending)
  169. ether_addr_copy(nic->netdev->dev_addr,
  170. mbx.nic_cfg.mac_addr);
  171. nic->sqs_mode = mbx.nic_cfg.sqs_mode;
  172. nic->loopback_supported = mbx.nic_cfg.loopback_supported;
  173. nic->link_up = false;
  174. nic->duplex = 0;
  175. nic->speed = 0;
  176. break;
  177. case NIC_MBOX_MSG_ACK:
  178. nic->pf_acked = true;
  179. break;
  180. case NIC_MBOX_MSG_NACK:
  181. nic->pf_nacked = true;
  182. break;
  183. case NIC_MBOX_MSG_RSS_SIZE:
  184. nic->rss_info.rss_size = mbx.rss_size.ind_tbl_size;
  185. nic->pf_acked = true;
  186. break;
  187. case NIC_MBOX_MSG_BGX_STATS:
  188. nicvf_read_bgx_stats(nic, &mbx.bgx_stats);
  189. nic->pf_acked = true;
  190. break;
  191. case NIC_MBOX_MSG_BGX_LINK_CHANGE:
  192. nic->pf_acked = true;
  193. nic->link_up = mbx.link_status.link_up;
  194. nic->duplex = mbx.link_status.duplex;
  195. nic->speed = mbx.link_status.speed;
  196. nic->mac_type = mbx.link_status.mac_type;
  197. if (nic->link_up) {
  198. netdev_info(nic->netdev, "%s: Link is Up %d Mbps %s\n",
  199. nic->netdev->name, nic->speed,
  200. nic->duplex == DUPLEX_FULL ?
  201. "Full duplex" : "Half duplex");
  202. netif_carrier_on(nic->netdev);
  203. netif_tx_start_all_queues(nic->netdev);
  204. } else {
  205. netdev_info(nic->netdev, "%s: Link is Down\n",
  206. nic->netdev->name);
  207. netif_carrier_off(nic->netdev);
  208. netif_tx_stop_all_queues(nic->netdev);
  209. }
  210. break;
  211. case NIC_MBOX_MSG_ALLOC_SQS:
  212. nic->sqs_count = mbx.sqs_alloc.qs_count;
  213. nic->pf_acked = true;
  214. break;
  215. case NIC_MBOX_MSG_SNICVF_PTR:
  216. /* Primary VF: make note of secondary VF's pointer
  217. * to be used while packet transmission.
  218. */
  219. nic->snicvf[mbx.nicvf.sqs_id] =
  220. (struct nicvf *)mbx.nicvf.nicvf;
  221. nic->pf_acked = true;
  222. break;
  223. case NIC_MBOX_MSG_PNICVF_PTR:
  224. /* Secondary VF/Qset: make note of primary VF's pointer
  225. * to be used while packet reception, to handover packet
  226. * to primary VF's netdev.
  227. */
  228. nic->pnicvf = (struct nicvf *)mbx.nicvf.nicvf;
  229. nic->pf_acked = true;
  230. break;
  231. case NIC_MBOX_MSG_PFC:
  232. nic->pfc.autoneg = mbx.pfc.autoneg;
  233. nic->pfc.fc_rx = mbx.pfc.fc_rx;
  234. nic->pfc.fc_tx = mbx.pfc.fc_tx;
  235. nic->pf_acked = true;
  236. break;
  237. default:
  238. netdev_err(nic->netdev,
  239. "Invalid message from PF, msg 0x%x\n", mbx.msg.msg);
  240. break;
  241. }
  242. nicvf_clear_intr(nic, NICVF_INTR_MBOX, 0);
  243. }
  244. static int nicvf_hw_set_mac_addr(struct nicvf *nic, struct net_device *netdev)
  245. {
  246. union nic_mbx mbx = {};
  247. mbx.mac.msg = NIC_MBOX_MSG_SET_MAC;
  248. mbx.mac.vf_id = nic->vf_id;
  249. ether_addr_copy(mbx.mac.mac_addr, netdev->dev_addr);
  250. return nicvf_send_msg_to_pf(nic, &mbx);
  251. }
  252. static void nicvf_config_cpi(struct nicvf *nic)
  253. {
  254. union nic_mbx mbx = {};
  255. mbx.cpi_cfg.msg = NIC_MBOX_MSG_CPI_CFG;
  256. mbx.cpi_cfg.vf_id = nic->vf_id;
  257. mbx.cpi_cfg.cpi_alg = nic->cpi_alg;
  258. mbx.cpi_cfg.rq_cnt = nic->qs->rq_cnt;
  259. nicvf_send_msg_to_pf(nic, &mbx);
  260. }
  261. static void nicvf_get_rss_size(struct nicvf *nic)
  262. {
  263. union nic_mbx mbx = {};
  264. mbx.rss_size.msg = NIC_MBOX_MSG_RSS_SIZE;
  265. mbx.rss_size.vf_id = nic->vf_id;
  266. nicvf_send_msg_to_pf(nic, &mbx);
  267. }
  268. void nicvf_config_rss(struct nicvf *nic)
  269. {
  270. union nic_mbx mbx = {};
  271. struct nicvf_rss_info *rss = &nic->rss_info;
  272. int ind_tbl_len = rss->rss_size;
  273. int i, nextq = 0;
  274. mbx.rss_cfg.vf_id = nic->vf_id;
  275. mbx.rss_cfg.hash_bits = rss->hash_bits;
  276. while (ind_tbl_len) {
  277. mbx.rss_cfg.tbl_offset = nextq;
  278. mbx.rss_cfg.tbl_len = min(ind_tbl_len,
  279. RSS_IND_TBL_LEN_PER_MBX_MSG);
  280. mbx.rss_cfg.msg = mbx.rss_cfg.tbl_offset ?
  281. NIC_MBOX_MSG_RSS_CFG_CONT : NIC_MBOX_MSG_RSS_CFG;
  282. for (i = 0; i < mbx.rss_cfg.tbl_len; i++)
  283. mbx.rss_cfg.ind_tbl[i] = rss->ind_tbl[nextq++];
  284. nicvf_send_msg_to_pf(nic, &mbx);
  285. ind_tbl_len -= mbx.rss_cfg.tbl_len;
  286. }
  287. }
  288. void nicvf_set_rss_key(struct nicvf *nic)
  289. {
  290. struct nicvf_rss_info *rss = &nic->rss_info;
  291. u64 key_addr = NIC_VNIC_RSS_KEY_0_4;
  292. int idx;
  293. for (idx = 0; idx < RSS_HASH_KEY_SIZE; idx++) {
  294. nicvf_reg_write(nic, key_addr, rss->key[idx]);
  295. key_addr += sizeof(u64);
  296. }
  297. }
  298. static int nicvf_rss_init(struct nicvf *nic)
  299. {
  300. struct nicvf_rss_info *rss = &nic->rss_info;
  301. int idx;
  302. nicvf_get_rss_size(nic);
  303. if (cpi_alg != CPI_ALG_NONE) {
  304. rss->enable = false;
  305. rss->hash_bits = 0;
  306. return 0;
  307. }
  308. rss->enable = true;
  309. netdev_rss_key_fill(rss->key, RSS_HASH_KEY_SIZE * sizeof(u64));
  310. nicvf_set_rss_key(nic);
  311. rss->cfg = RSS_IP_HASH_ENA | RSS_TCP_HASH_ENA | RSS_UDP_HASH_ENA;
  312. nicvf_reg_write(nic, NIC_VNIC_RSS_CFG, rss->cfg);
  313. rss->hash_bits = ilog2(rounddown_pow_of_two(rss->rss_size));
  314. for (idx = 0; idx < rss->rss_size; idx++)
  315. rss->ind_tbl[idx] = ethtool_rxfh_indir_default(idx,
  316. nic->rx_queues);
  317. nicvf_config_rss(nic);
  318. return 1;
  319. }
  320. /* Request PF to allocate additional Qsets */
  321. static void nicvf_request_sqs(struct nicvf *nic)
  322. {
  323. union nic_mbx mbx = {};
  324. int sqs;
  325. int sqs_count = nic->sqs_count;
  326. int rx_queues = 0, tx_queues = 0;
  327. /* Only primary VF should request */
  328. if (nic->sqs_mode || !nic->sqs_count)
  329. return;
  330. mbx.sqs_alloc.msg = NIC_MBOX_MSG_ALLOC_SQS;
  331. mbx.sqs_alloc.vf_id = nic->vf_id;
  332. mbx.sqs_alloc.qs_count = nic->sqs_count;
  333. if (nicvf_send_msg_to_pf(nic, &mbx)) {
  334. /* No response from PF */
  335. nic->sqs_count = 0;
  336. return;
  337. }
  338. /* Return if no Secondary Qsets available */
  339. if (!nic->sqs_count)
  340. return;
  341. if (nic->rx_queues > MAX_RCV_QUEUES_PER_QS)
  342. rx_queues = nic->rx_queues - MAX_RCV_QUEUES_PER_QS;
  343. if (nic->tx_queues > MAX_SND_QUEUES_PER_QS)
  344. tx_queues = nic->tx_queues - MAX_SND_QUEUES_PER_QS;
  345. /* Set no of Rx/Tx queues in each of the SQsets */
  346. for (sqs = 0; sqs < nic->sqs_count; sqs++) {
  347. mbx.nicvf.msg = NIC_MBOX_MSG_SNICVF_PTR;
  348. mbx.nicvf.vf_id = nic->vf_id;
  349. mbx.nicvf.sqs_id = sqs;
  350. nicvf_send_msg_to_pf(nic, &mbx);
  351. nic->snicvf[sqs]->sqs_id = sqs;
  352. if (rx_queues > MAX_RCV_QUEUES_PER_QS) {
  353. nic->snicvf[sqs]->qs->rq_cnt = MAX_RCV_QUEUES_PER_QS;
  354. rx_queues -= MAX_RCV_QUEUES_PER_QS;
  355. } else {
  356. nic->snicvf[sqs]->qs->rq_cnt = rx_queues;
  357. rx_queues = 0;
  358. }
  359. if (tx_queues > MAX_SND_QUEUES_PER_QS) {
  360. nic->snicvf[sqs]->qs->sq_cnt = MAX_SND_QUEUES_PER_QS;
  361. tx_queues -= MAX_SND_QUEUES_PER_QS;
  362. } else {
  363. nic->snicvf[sqs]->qs->sq_cnt = tx_queues;
  364. tx_queues = 0;
  365. }
  366. nic->snicvf[sqs]->qs->cq_cnt =
  367. max(nic->snicvf[sqs]->qs->rq_cnt, nic->snicvf[sqs]->qs->sq_cnt);
  368. /* Initialize secondary Qset's queues and its interrupts */
  369. nicvf_open(nic->snicvf[sqs]->netdev);
  370. }
  371. /* Update stack with actual Rx/Tx queue count allocated */
  372. if (sqs_count != nic->sqs_count)
  373. nicvf_set_real_num_queues(nic->netdev,
  374. nic->tx_queues, nic->rx_queues);
  375. }
  376. /* Send this Qset's nicvf pointer to PF.
  377. * PF inturn sends primary VF's nicvf struct to secondary Qsets/VFs
  378. * so that packets received by these Qsets can use primary VF's netdev
  379. */
  380. static void nicvf_send_vf_struct(struct nicvf *nic)
  381. {
  382. union nic_mbx mbx = {};
  383. mbx.nicvf.msg = NIC_MBOX_MSG_NICVF_PTR;
  384. mbx.nicvf.sqs_mode = nic->sqs_mode;
  385. mbx.nicvf.nicvf = (u64)nic;
  386. nicvf_send_msg_to_pf(nic, &mbx);
  387. }
  388. static void nicvf_get_primary_vf_struct(struct nicvf *nic)
  389. {
  390. union nic_mbx mbx = {};
  391. mbx.nicvf.msg = NIC_MBOX_MSG_PNICVF_PTR;
  392. nicvf_send_msg_to_pf(nic, &mbx);
  393. }
  394. int nicvf_set_real_num_queues(struct net_device *netdev,
  395. int tx_queues, int rx_queues)
  396. {
  397. int err = 0;
  398. err = netif_set_real_num_tx_queues(netdev, tx_queues);
  399. if (err) {
  400. netdev_err(netdev,
  401. "Failed to set no of Tx queues: %d\n", tx_queues);
  402. return err;
  403. }
  404. err = netif_set_real_num_rx_queues(netdev, rx_queues);
  405. if (err)
  406. netdev_err(netdev,
  407. "Failed to set no of Rx queues: %d\n", rx_queues);
  408. return err;
  409. }
  410. static int nicvf_init_resources(struct nicvf *nic)
  411. {
  412. int err;
  413. /* Enable Qset */
  414. nicvf_qset_config(nic, true);
  415. /* Initialize queues and HW for data transfer */
  416. err = nicvf_config_data_transfer(nic, true);
  417. if (err) {
  418. netdev_err(nic->netdev,
  419. "Failed to alloc/config VF's QSet resources\n");
  420. return err;
  421. }
  422. return 0;
  423. }
  424. static void nicvf_snd_pkt_handler(struct net_device *netdev,
  425. struct cqe_send_t *cqe_tx,
  426. int cqe_type, int budget,
  427. unsigned int *tx_pkts, unsigned int *tx_bytes)
  428. {
  429. struct sk_buff *skb = NULL;
  430. struct nicvf *nic = netdev_priv(netdev);
  431. struct snd_queue *sq;
  432. struct sq_hdr_subdesc *hdr;
  433. struct sq_hdr_subdesc *tso_sqe;
  434. sq = &nic->qs->sq[cqe_tx->sq_idx];
  435. hdr = (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, cqe_tx->sqe_ptr);
  436. if (hdr->subdesc_type != SQ_DESC_TYPE_HEADER)
  437. return;
  438. netdev_dbg(nic->netdev,
  439. "%s Qset #%d SQ #%d SQ ptr #%d subdesc count %d\n",
  440. __func__, cqe_tx->sq_qs, cqe_tx->sq_idx,
  441. cqe_tx->sqe_ptr, hdr->subdesc_cnt);
  442. nicvf_check_cqe_tx_errs(nic, cqe_tx);
  443. skb = (struct sk_buff *)sq->skbuff[cqe_tx->sqe_ptr];
  444. if (skb) {
  445. /* Check for dummy descriptor used for HW TSO offload on 88xx */
  446. if (hdr->dont_send) {
  447. /* Get actual TSO descriptors and free them */
  448. tso_sqe =
  449. (struct sq_hdr_subdesc *)GET_SQ_DESC(sq, hdr->rsvd2);
  450. nicvf_unmap_sndq_buffers(nic, sq, hdr->rsvd2,
  451. tso_sqe->subdesc_cnt);
  452. nicvf_put_sq_desc(sq, tso_sqe->subdesc_cnt + 1);
  453. } else {
  454. nicvf_unmap_sndq_buffers(nic, sq, cqe_tx->sqe_ptr,
  455. hdr->subdesc_cnt);
  456. }
  457. nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
  458. prefetch(skb);
  459. (*tx_pkts)++;
  460. *tx_bytes += skb->len;
  461. napi_consume_skb(skb, budget);
  462. sq->skbuff[cqe_tx->sqe_ptr] = (u64)NULL;
  463. } else {
  464. /* In case of SW TSO on 88xx, only last segment will have
  465. * a SKB attached, so just free SQEs here.
  466. */
  467. if (!nic->hw_tso)
  468. nicvf_put_sq_desc(sq, hdr->subdesc_cnt + 1);
  469. }
  470. }
  471. static inline void nicvf_set_rxhash(struct net_device *netdev,
  472. struct cqe_rx_t *cqe_rx,
  473. struct sk_buff *skb)
  474. {
  475. u8 hash_type;
  476. u32 hash;
  477. if (!(netdev->features & NETIF_F_RXHASH))
  478. return;
  479. switch (cqe_rx->rss_alg) {
  480. case RSS_ALG_TCP_IP:
  481. case RSS_ALG_UDP_IP:
  482. hash_type = PKT_HASH_TYPE_L4;
  483. hash = cqe_rx->rss_tag;
  484. break;
  485. case RSS_ALG_IP:
  486. hash_type = PKT_HASH_TYPE_L3;
  487. hash = cqe_rx->rss_tag;
  488. break;
  489. default:
  490. hash_type = PKT_HASH_TYPE_NONE;
  491. hash = 0;
  492. }
  493. skb_set_hash(skb, hash, hash_type);
  494. }
  495. static void nicvf_rcv_pkt_handler(struct net_device *netdev,
  496. struct napi_struct *napi,
  497. struct cqe_rx_t *cqe_rx)
  498. {
  499. struct sk_buff *skb;
  500. struct nicvf *nic = netdev_priv(netdev);
  501. struct nicvf *snic = nic;
  502. int err = 0;
  503. int rq_idx;
  504. rq_idx = nicvf_netdev_qidx(nic, cqe_rx->rq_idx);
  505. if (nic->sqs_mode) {
  506. /* Use primary VF's 'nicvf' struct */
  507. nic = nic->pnicvf;
  508. netdev = nic->netdev;
  509. }
  510. /* Check for errors */
  511. err = nicvf_check_cqe_rx_errs(nic, cqe_rx);
  512. if (err && !cqe_rx->rb_cnt)
  513. return;
  514. skb = nicvf_get_rcv_skb(snic, cqe_rx);
  515. if (!skb) {
  516. netdev_dbg(nic->netdev, "Packet not received\n");
  517. return;
  518. }
  519. if (netif_msg_pktdata(nic)) {
  520. netdev_info(nic->netdev, "%s: skb 0x%p, len=%d\n", netdev->name,
  521. skb, skb->len);
  522. print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET, 16, 1,
  523. skb->data, skb->len, true);
  524. }
  525. /* If error packet, drop it here */
  526. if (err) {
  527. dev_kfree_skb_any(skb);
  528. return;
  529. }
  530. nicvf_set_rxhash(netdev, cqe_rx, skb);
  531. skb_record_rx_queue(skb, rq_idx);
  532. if (netdev->hw_features & NETIF_F_RXCSUM) {
  533. /* HW by default verifies TCP/UDP/SCTP checksums */
  534. skb->ip_summed = CHECKSUM_UNNECESSARY;
  535. } else {
  536. skb_checksum_none_assert(skb);
  537. }
  538. skb->protocol = eth_type_trans(skb, netdev);
  539. /* Check for stripped VLAN */
  540. if (cqe_rx->vlan_found && cqe_rx->vlan_stripped)
  541. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
  542. ntohs((__force __be16)cqe_rx->vlan_tci));
  543. if (napi && (netdev->features & NETIF_F_GRO))
  544. napi_gro_receive(napi, skb);
  545. else
  546. netif_receive_skb(skb);
  547. }
  548. static int nicvf_cq_intr_handler(struct net_device *netdev, u8 cq_idx,
  549. struct napi_struct *napi, int budget)
  550. {
  551. int processed_cqe, work_done = 0, tx_done = 0;
  552. int cqe_count, cqe_head;
  553. struct nicvf *nic = netdev_priv(netdev);
  554. struct queue_set *qs = nic->qs;
  555. struct cmp_queue *cq = &qs->cq[cq_idx];
  556. struct cqe_rx_t *cq_desc;
  557. struct netdev_queue *txq;
  558. struct snd_queue *sq;
  559. unsigned int tx_pkts = 0, tx_bytes = 0;
  560. spin_lock_bh(&cq->lock);
  561. loop:
  562. processed_cqe = 0;
  563. /* Get no of valid CQ entries to process */
  564. cqe_count = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS, cq_idx);
  565. cqe_count &= CQ_CQE_COUNT;
  566. if (!cqe_count)
  567. goto done;
  568. /* Get head of the valid CQ entries */
  569. cqe_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD, cq_idx) >> 9;
  570. cqe_head &= 0xFFFF;
  571. netdev_dbg(nic->netdev, "%s CQ%d cqe_count %d cqe_head %d\n",
  572. __func__, cq_idx, cqe_count, cqe_head);
  573. while (processed_cqe < cqe_count) {
  574. /* Get the CQ descriptor */
  575. cq_desc = (struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head);
  576. cqe_head++;
  577. cqe_head &= (cq->dmem.q_len - 1);
  578. /* Initiate prefetch for next descriptor */
  579. prefetch((struct cqe_rx_t *)GET_CQ_DESC(cq, cqe_head));
  580. if ((work_done >= budget) && napi &&
  581. (cq_desc->cqe_type != CQE_TYPE_SEND)) {
  582. break;
  583. }
  584. netdev_dbg(nic->netdev, "CQ%d cq_desc->cqe_type %d\n",
  585. cq_idx, cq_desc->cqe_type);
  586. switch (cq_desc->cqe_type) {
  587. case CQE_TYPE_RX:
  588. nicvf_rcv_pkt_handler(netdev, napi, cq_desc);
  589. work_done++;
  590. break;
  591. case CQE_TYPE_SEND:
  592. nicvf_snd_pkt_handler(netdev,
  593. (void *)cq_desc, CQE_TYPE_SEND,
  594. budget, &tx_pkts, &tx_bytes);
  595. tx_done++;
  596. break;
  597. case CQE_TYPE_INVALID:
  598. case CQE_TYPE_RX_SPLIT:
  599. case CQE_TYPE_RX_TCP:
  600. case CQE_TYPE_SEND_PTP:
  601. /* Ignore for now */
  602. break;
  603. }
  604. processed_cqe++;
  605. }
  606. netdev_dbg(nic->netdev,
  607. "%s CQ%d processed_cqe %d work_done %d budget %d\n",
  608. __func__, cq_idx, processed_cqe, work_done, budget);
  609. /* Ring doorbell to inform H/W to reuse processed CQEs */
  610. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_DOOR,
  611. cq_idx, processed_cqe);
  612. if ((work_done < budget) && napi)
  613. goto loop;
  614. done:
  615. /* Wakeup TXQ if its stopped earlier due to SQ full */
  616. sq = &nic->qs->sq[cq_idx];
  617. if (tx_done ||
  618. (atomic_read(&sq->free_cnt) >= MIN_SQ_DESC_PER_PKT_XMIT)) {
  619. netdev = nic->pnicvf->netdev;
  620. txq = netdev_get_tx_queue(netdev,
  621. nicvf_netdev_qidx(nic, cq_idx));
  622. if (tx_pkts)
  623. netdev_tx_completed_queue(txq, tx_pkts, tx_bytes);
  624. /* To read updated queue and carrier status */
  625. smp_mb();
  626. if (netif_tx_queue_stopped(txq) && netif_carrier_ok(netdev)) {
  627. netif_tx_wake_queue(txq);
  628. nic = nic->pnicvf;
  629. this_cpu_inc(nic->drv_stats->txq_wake);
  630. if (netif_msg_tx_err(nic))
  631. netdev_warn(netdev,
  632. "%s: Transmit queue wakeup SQ%d\n",
  633. netdev->name, cq_idx);
  634. }
  635. }
  636. spin_unlock_bh(&cq->lock);
  637. return work_done;
  638. }
  639. static int nicvf_poll(struct napi_struct *napi, int budget)
  640. {
  641. u64 cq_head;
  642. int work_done = 0;
  643. struct net_device *netdev = napi->dev;
  644. struct nicvf *nic = netdev_priv(netdev);
  645. struct nicvf_cq_poll *cq;
  646. cq = container_of(napi, struct nicvf_cq_poll, napi);
  647. work_done = nicvf_cq_intr_handler(netdev, cq->cq_idx, napi, budget);
  648. if (work_done < budget) {
  649. /* Slow packet rate, exit polling */
  650. napi_complete_done(napi, work_done);
  651. /* Re-enable interrupts */
  652. cq_head = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_HEAD,
  653. cq->cq_idx);
  654. nicvf_clear_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
  655. nicvf_queue_reg_write(nic, NIC_QSET_CQ_0_7_HEAD,
  656. cq->cq_idx, cq_head);
  657. nicvf_enable_intr(nic, NICVF_INTR_CQ, cq->cq_idx);
  658. }
  659. return work_done;
  660. }
  661. /* Qset error interrupt handler
  662. *
  663. * As of now only CQ errors are handled
  664. */
  665. static void nicvf_handle_qs_err(unsigned long data)
  666. {
  667. struct nicvf *nic = (struct nicvf *)data;
  668. struct queue_set *qs = nic->qs;
  669. int qidx;
  670. u64 status;
  671. netif_tx_disable(nic->netdev);
  672. /* Check if it is CQ err */
  673. for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
  674. status = nicvf_queue_reg_read(nic, NIC_QSET_CQ_0_7_STATUS,
  675. qidx);
  676. if (!(status & CQ_ERR_MASK))
  677. continue;
  678. /* Process already queued CQEs and reconfig CQ */
  679. nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
  680. nicvf_sq_disable(nic, qidx);
  681. nicvf_cq_intr_handler(nic->netdev, qidx, NULL, 0);
  682. nicvf_cmp_queue_config(nic, qs, qidx, true);
  683. nicvf_sq_free_used_descs(nic->netdev, &qs->sq[qidx], qidx);
  684. nicvf_sq_enable(nic, &qs->sq[qidx], qidx);
  685. nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
  686. }
  687. netif_tx_start_all_queues(nic->netdev);
  688. /* Re-enable Qset error interrupt */
  689. nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
  690. }
  691. static void nicvf_dump_intr_status(struct nicvf *nic)
  692. {
  693. if (netif_msg_intr(nic))
  694. netdev_info(nic->netdev, "%s: interrupt status 0x%llx\n",
  695. nic->netdev->name, nicvf_reg_read(nic, NIC_VF_INT));
  696. }
  697. static irqreturn_t nicvf_misc_intr_handler(int irq, void *nicvf_irq)
  698. {
  699. struct nicvf *nic = (struct nicvf *)nicvf_irq;
  700. u64 intr;
  701. nicvf_dump_intr_status(nic);
  702. intr = nicvf_reg_read(nic, NIC_VF_INT);
  703. /* Check for spurious interrupt */
  704. if (!(intr & NICVF_INTR_MBOX_MASK))
  705. return IRQ_HANDLED;
  706. nicvf_handle_mbx_intr(nic);
  707. return IRQ_HANDLED;
  708. }
  709. static irqreturn_t nicvf_intr_handler(int irq, void *cq_irq)
  710. {
  711. struct nicvf_cq_poll *cq_poll = (struct nicvf_cq_poll *)cq_irq;
  712. struct nicvf *nic = cq_poll->nicvf;
  713. int qidx = cq_poll->cq_idx;
  714. nicvf_dump_intr_status(nic);
  715. /* Disable interrupts */
  716. nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
  717. /* Schedule NAPI */
  718. napi_schedule_irqoff(&cq_poll->napi);
  719. /* Clear interrupt */
  720. nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
  721. return IRQ_HANDLED;
  722. }
  723. static irqreturn_t nicvf_rbdr_intr_handler(int irq, void *nicvf_irq)
  724. {
  725. struct nicvf *nic = (struct nicvf *)nicvf_irq;
  726. u8 qidx;
  727. nicvf_dump_intr_status(nic);
  728. /* Disable RBDR interrupt and schedule softirq */
  729. for (qidx = 0; qidx < nic->qs->rbdr_cnt; qidx++) {
  730. if (!nicvf_is_intr_enabled(nic, NICVF_INTR_RBDR, qidx))
  731. continue;
  732. nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
  733. tasklet_hi_schedule(&nic->rbdr_task);
  734. /* Clear interrupt */
  735. nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
  736. }
  737. return IRQ_HANDLED;
  738. }
  739. static irqreturn_t nicvf_qs_err_intr_handler(int irq, void *nicvf_irq)
  740. {
  741. struct nicvf *nic = (struct nicvf *)nicvf_irq;
  742. nicvf_dump_intr_status(nic);
  743. /* Disable Qset err interrupt and schedule softirq */
  744. nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
  745. tasklet_hi_schedule(&nic->qs_err_task);
  746. nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
  747. return IRQ_HANDLED;
  748. }
  749. static int nicvf_enable_msix(struct nicvf *nic)
  750. {
  751. int ret, vec;
  752. nic->num_vec = NIC_VF_MSIX_VECTORS;
  753. for (vec = 0; vec < nic->num_vec; vec++)
  754. nic->msix_entries[vec].entry = vec;
  755. ret = pci_enable_msix(nic->pdev, nic->msix_entries, nic->num_vec);
  756. if (ret) {
  757. netdev_err(nic->netdev,
  758. "Req for #%d msix vectors failed\n", nic->num_vec);
  759. return 0;
  760. }
  761. nic->msix_enabled = 1;
  762. return 1;
  763. }
  764. static void nicvf_disable_msix(struct nicvf *nic)
  765. {
  766. if (nic->msix_enabled) {
  767. pci_disable_msix(nic->pdev);
  768. nic->msix_enabled = 0;
  769. nic->num_vec = 0;
  770. }
  771. }
  772. static void nicvf_set_irq_affinity(struct nicvf *nic)
  773. {
  774. int vec, cpu;
  775. int irqnum;
  776. for (vec = 0; vec < nic->num_vec; vec++) {
  777. if (!nic->irq_allocated[vec])
  778. continue;
  779. if (!zalloc_cpumask_var(&nic->affinity_mask[vec], GFP_KERNEL))
  780. return;
  781. /* CQ interrupts */
  782. if (vec < NICVF_INTR_ID_SQ)
  783. /* Leave CPU0 for RBDR and other interrupts */
  784. cpu = nicvf_netdev_qidx(nic, vec) + 1;
  785. else
  786. cpu = 0;
  787. cpumask_set_cpu(cpumask_local_spread(cpu, nic->node),
  788. nic->affinity_mask[vec]);
  789. irqnum = nic->msix_entries[vec].vector;
  790. irq_set_affinity_hint(irqnum, nic->affinity_mask[vec]);
  791. }
  792. }
  793. static int nicvf_register_interrupts(struct nicvf *nic)
  794. {
  795. int irq, ret = 0;
  796. int vector;
  797. for_each_cq_irq(irq)
  798. sprintf(nic->irq_name[irq], "%s-rxtx-%d",
  799. nic->pnicvf->netdev->name,
  800. nicvf_netdev_qidx(nic, irq));
  801. for_each_sq_irq(irq)
  802. sprintf(nic->irq_name[irq], "%s-sq-%d",
  803. nic->pnicvf->netdev->name,
  804. nicvf_netdev_qidx(nic, irq - NICVF_INTR_ID_SQ));
  805. for_each_rbdr_irq(irq)
  806. sprintf(nic->irq_name[irq], "%s-rbdr-%d",
  807. nic->pnicvf->netdev->name,
  808. nic->sqs_mode ? (nic->sqs_id + 1) : 0);
  809. /* Register CQ interrupts */
  810. for (irq = 0; irq < nic->qs->cq_cnt; irq++) {
  811. vector = nic->msix_entries[irq].vector;
  812. ret = request_irq(vector, nicvf_intr_handler,
  813. 0, nic->irq_name[irq], nic->napi[irq]);
  814. if (ret)
  815. goto err;
  816. nic->irq_allocated[irq] = true;
  817. }
  818. /* Register RBDR interrupt */
  819. for (irq = NICVF_INTR_ID_RBDR;
  820. irq < (NICVF_INTR_ID_RBDR + nic->qs->rbdr_cnt); irq++) {
  821. vector = nic->msix_entries[irq].vector;
  822. ret = request_irq(vector, nicvf_rbdr_intr_handler,
  823. 0, nic->irq_name[irq], nic);
  824. if (ret)
  825. goto err;
  826. nic->irq_allocated[irq] = true;
  827. }
  828. /* Register QS error interrupt */
  829. sprintf(nic->irq_name[NICVF_INTR_ID_QS_ERR], "%s-qset-err-%d",
  830. nic->pnicvf->netdev->name,
  831. nic->sqs_mode ? (nic->sqs_id + 1) : 0);
  832. irq = NICVF_INTR_ID_QS_ERR;
  833. ret = request_irq(nic->msix_entries[irq].vector,
  834. nicvf_qs_err_intr_handler,
  835. 0, nic->irq_name[irq], nic);
  836. if (ret)
  837. goto err;
  838. nic->irq_allocated[irq] = true;
  839. /* Set IRQ affinities */
  840. nicvf_set_irq_affinity(nic);
  841. err:
  842. if (ret)
  843. netdev_err(nic->netdev, "request_irq failed, vector %d\n", irq);
  844. return ret;
  845. }
  846. static void nicvf_unregister_interrupts(struct nicvf *nic)
  847. {
  848. int irq;
  849. /* Free registered interrupts */
  850. for (irq = 0; irq < nic->num_vec; irq++) {
  851. if (!nic->irq_allocated[irq])
  852. continue;
  853. irq_set_affinity_hint(nic->msix_entries[irq].vector, NULL);
  854. free_cpumask_var(nic->affinity_mask[irq]);
  855. if (irq < NICVF_INTR_ID_SQ)
  856. free_irq(nic->msix_entries[irq].vector, nic->napi[irq]);
  857. else
  858. free_irq(nic->msix_entries[irq].vector, nic);
  859. nic->irq_allocated[irq] = false;
  860. }
  861. /* Disable MSI-X */
  862. nicvf_disable_msix(nic);
  863. }
  864. /* Initialize MSIX vectors and register MISC interrupt.
  865. * Send READY message to PF to check if its alive
  866. */
  867. static int nicvf_register_misc_interrupt(struct nicvf *nic)
  868. {
  869. int ret = 0;
  870. int irq = NICVF_INTR_ID_MISC;
  871. /* Return if mailbox interrupt is already registered */
  872. if (nic->msix_enabled)
  873. return 0;
  874. /* Enable MSI-X */
  875. if (!nicvf_enable_msix(nic))
  876. return 1;
  877. sprintf(nic->irq_name[irq], "%s Mbox", "NICVF");
  878. /* Register Misc interrupt */
  879. ret = request_irq(nic->msix_entries[irq].vector,
  880. nicvf_misc_intr_handler, 0, nic->irq_name[irq], nic);
  881. if (ret)
  882. return ret;
  883. nic->irq_allocated[irq] = true;
  884. /* Enable mailbox interrupt */
  885. nicvf_enable_intr(nic, NICVF_INTR_MBOX, 0);
  886. /* Check if VF is able to communicate with PF */
  887. if (!nicvf_check_pf_ready(nic)) {
  888. nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
  889. nicvf_unregister_interrupts(nic);
  890. return 1;
  891. }
  892. return 0;
  893. }
  894. static netdev_tx_t nicvf_xmit(struct sk_buff *skb, struct net_device *netdev)
  895. {
  896. struct nicvf *nic = netdev_priv(netdev);
  897. int qid = skb_get_queue_mapping(skb);
  898. struct netdev_queue *txq = netdev_get_tx_queue(netdev, qid);
  899. struct nicvf *snic;
  900. struct snd_queue *sq;
  901. int tmp;
  902. /* Check for minimum packet length */
  903. if (skb->len <= ETH_HLEN) {
  904. dev_kfree_skb(skb);
  905. return NETDEV_TX_OK;
  906. }
  907. snic = nic;
  908. /* Get secondary Qset's SQ structure */
  909. if (qid >= MAX_SND_QUEUES_PER_QS) {
  910. tmp = qid / MAX_SND_QUEUES_PER_QS;
  911. snic = (struct nicvf *)nic->snicvf[tmp - 1];
  912. if (!snic) {
  913. netdev_warn(nic->netdev,
  914. "Secondary Qset#%d's ptr not initialized\n",
  915. tmp - 1);
  916. dev_kfree_skb(skb);
  917. return NETDEV_TX_OK;
  918. }
  919. qid = qid % MAX_SND_QUEUES_PER_QS;
  920. }
  921. sq = &snic->qs->sq[qid];
  922. if (!netif_tx_queue_stopped(txq) &&
  923. !nicvf_sq_append_skb(snic, sq, skb, qid)) {
  924. netif_tx_stop_queue(txq);
  925. /* Barrier, so that stop_queue visible to other cpus */
  926. smp_mb();
  927. /* Check again, incase another cpu freed descriptors */
  928. if (atomic_read(&sq->free_cnt) > MIN_SQ_DESC_PER_PKT_XMIT) {
  929. netif_tx_wake_queue(txq);
  930. } else {
  931. this_cpu_inc(nic->drv_stats->txq_stop);
  932. if (netif_msg_tx_err(nic))
  933. netdev_warn(netdev,
  934. "%s: Transmit ring full, stopping SQ%d\n",
  935. netdev->name, qid);
  936. }
  937. return NETDEV_TX_BUSY;
  938. }
  939. return NETDEV_TX_OK;
  940. }
  941. static inline void nicvf_free_cq_poll(struct nicvf *nic)
  942. {
  943. struct nicvf_cq_poll *cq_poll;
  944. int qidx;
  945. for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
  946. cq_poll = nic->napi[qidx];
  947. if (!cq_poll)
  948. continue;
  949. nic->napi[qidx] = NULL;
  950. kfree(cq_poll);
  951. }
  952. }
  953. int nicvf_stop(struct net_device *netdev)
  954. {
  955. int irq, qidx;
  956. struct nicvf *nic = netdev_priv(netdev);
  957. struct queue_set *qs = nic->qs;
  958. struct nicvf_cq_poll *cq_poll = NULL;
  959. union nic_mbx mbx = {};
  960. mbx.msg.msg = NIC_MBOX_MSG_SHUTDOWN;
  961. nicvf_send_msg_to_pf(nic, &mbx);
  962. netif_carrier_off(netdev);
  963. netif_tx_stop_all_queues(nic->netdev);
  964. nic->link_up = false;
  965. /* Teardown secondary qsets first */
  966. if (!nic->sqs_mode) {
  967. for (qidx = 0; qidx < nic->sqs_count; qidx++) {
  968. if (!nic->snicvf[qidx])
  969. continue;
  970. nicvf_stop(nic->snicvf[qidx]->netdev);
  971. nic->snicvf[qidx] = NULL;
  972. }
  973. }
  974. /* Disable RBDR & QS error interrupts */
  975. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++) {
  976. nicvf_disable_intr(nic, NICVF_INTR_RBDR, qidx);
  977. nicvf_clear_intr(nic, NICVF_INTR_RBDR, qidx);
  978. }
  979. nicvf_disable_intr(nic, NICVF_INTR_QS_ERR, 0);
  980. nicvf_clear_intr(nic, NICVF_INTR_QS_ERR, 0);
  981. /* Wait for pending IRQ handlers to finish */
  982. for (irq = 0; irq < nic->num_vec; irq++)
  983. synchronize_irq(nic->msix_entries[irq].vector);
  984. tasklet_kill(&nic->rbdr_task);
  985. tasklet_kill(&nic->qs_err_task);
  986. if (nic->rb_work_scheduled)
  987. cancel_delayed_work_sync(&nic->rbdr_work);
  988. for (qidx = 0; qidx < nic->qs->cq_cnt; qidx++) {
  989. cq_poll = nic->napi[qidx];
  990. if (!cq_poll)
  991. continue;
  992. napi_synchronize(&cq_poll->napi);
  993. /* CQ intr is enabled while napi_complete,
  994. * so disable it now
  995. */
  996. nicvf_disable_intr(nic, NICVF_INTR_CQ, qidx);
  997. nicvf_clear_intr(nic, NICVF_INTR_CQ, qidx);
  998. napi_disable(&cq_poll->napi);
  999. netif_napi_del(&cq_poll->napi);
  1000. }
  1001. netif_tx_disable(netdev);
  1002. for (qidx = 0; qidx < netdev->num_tx_queues; qidx++)
  1003. netdev_tx_reset_queue(netdev_get_tx_queue(netdev, qidx));
  1004. /* Free resources */
  1005. nicvf_config_data_transfer(nic, false);
  1006. /* Disable HW Qset */
  1007. nicvf_qset_config(nic, false);
  1008. /* disable mailbox interrupt */
  1009. nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
  1010. nicvf_unregister_interrupts(nic);
  1011. nicvf_free_cq_poll(nic);
  1012. /* Clear multiqset info */
  1013. nic->pnicvf = nic;
  1014. return 0;
  1015. }
  1016. static int nicvf_update_hw_max_frs(struct nicvf *nic, int mtu)
  1017. {
  1018. union nic_mbx mbx = {};
  1019. mbx.frs.msg = NIC_MBOX_MSG_SET_MAX_FRS;
  1020. mbx.frs.max_frs = mtu;
  1021. mbx.frs.vf_id = nic->vf_id;
  1022. return nicvf_send_msg_to_pf(nic, &mbx);
  1023. }
  1024. int nicvf_open(struct net_device *netdev)
  1025. {
  1026. int cpu, err, qidx;
  1027. struct nicvf *nic = netdev_priv(netdev);
  1028. struct queue_set *qs = nic->qs;
  1029. struct nicvf_cq_poll *cq_poll = NULL;
  1030. union nic_mbx mbx = {};
  1031. netif_carrier_off(netdev);
  1032. err = nicvf_register_misc_interrupt(nic);
  1033. if (err)
  1034. return err;
  1035. /* Register NAPI handler for processing CQEs */
  1036. for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
  1037. cq_poll = kzalloc(sizeof(*cq_poll), GFP_KERNEL);
  1038. if (!cq_poll) {
  1039. err = -ENOMEM;
  1040. goto napi_del;
  1041. }
  1042. cq_poll->cq_idx = qidx;
  1043. cq_poll->nicvf = nic;
  1044. netif_napi_add(netdev, &cq_poll->napi, nicvf_poll,
  1045. NAPI_POLL_WEIGHT);
  1046. napi_enable(&cq_poll->napi);
  1047. nic->napi[qidx] = cq_poll;
  1048. }
  1049. /* Check if we got MAC address from PF or else generate a radom MAC */
  1050. if (!nic->sqs_mode && is_zero_ether_addr(netdev->dev_addr)) {
  1051. eth_hw_addr_random(netdev);
  1052. nicvf_hw_set_mac_addr(nic, netdev);
  1053. }
  1054. if (nic->set_mac_pending) {
  1055. nic->set_mac_pending = false;
  1056. nicvf_hw_set_mac_addr(nic, netdev);
  1057. }
  1058. /* Init tasklet for handling Qset err interrupt */
  1059. tasklet_init(&nic->qs_err_task, nicvf_handle_qs_err,
  1060. (unsigned long)nic);
  1061. /* Init RBDR tasklet which will refill RBDR */
  1062. tasklet_init(&nic->rbdr_task, nicvf_rbdr_task,
  1063. (unsigned long)nic);
  1064. INIT_DELAYED_WORK(&nic->rbdr_work, nicvf_rbdr_work);
  1065. /* Configure CPI alorithm */
  1066. nic->cpi_alg = cpi_alg;
  1067. if (!nic->sqs_mode)
  1068. nicvf_config_cpi(nic);
  1069. nicvf_request_sqs(nic);
  1070. if (nic->sqs_mode)
  1071. nicvf_get_primary_vf_struct(nic);
  1072. /* Configure receive side scaling and MTU */
  1073. if (!nic->sqs_mode) {
  1074. nicvf_rss_init(nic);
  1075. err = nicvf_update_hw_max_frs(nic, netdev->mtu);
  1076. if (err)
  1077. goto cleanup;
  1078. /* Clear percpu stats */
  1079. for_each_possible_cpu(cpu)
  1080. memset(per_cpu_ptr(nic->drv_stats, cpu), 0,
  1081. sizeof(struct nicvf_drv_stats));
  1082. }
  1083. err = nicvf_register_interrupts(nic);
  1084. if (err)
  1085. goto cleanup;
  1086. /* Initialize the queues */
  1087. err = nicvf_init_resources(nic);
  1088. if (err)
  1089. goto cleanup;
  1090. /* Make sure queue initialization is written */
  1091. wmb();
  1092. nicvf_reg_write(nic, NIC_VF_INT, -1);
  1093. /* Enable Qset err interrupt */
  1094. nicvf_enable_intr(nic, NICVF_INTR_QS_ERR, 0);
  1095. /* Enable completion queue interrupt */
  1096. for (qidx = 0; qidx < qs->cq_cnt; qidx++)
  1097. nicvf_enable_intr(nic, NICVF_INTR_CQ, qidx);
  1098. /* Enable RBDR threshold interrupt */
  1099. for (qidx = 0; qidx < qs->rbdr_cnt; qidx++)
  1100. nicvf_enable_intr(nic, NICVF_INTR_RBDR, qidx);
  1101. /* Send VF config done msg to PF */
  1102. mbx.msg.msg = NIC_MBOX_MSG_CFG_DONE;
  1103. nicvf_write_to_mbx(nic, &mbx);
  1104. return 0;
  1105. cleanup:
  1106. nicvf_disable_intr(nic, NICVF_INTR_MBOX, 0);
  1107. nicvf_unregister_interrupts(nic);
  1108. tasklet_kill(&nic->qs_err_task);
  1109. tasklet_kill(&nic->rbdr_task);
  1110. napi_del:
  1111. for (qidx = 0; qidx < qs->cq_cnt; qidx++) {
  1112. cq_poll = nic->napi[qidx];
  1113. if (!cq_poll)
  1114. continue;
  1115. napi_disable(&cq_poll->napi);
  1116. netif_napi_del(&cq_poll->napi);
  1117. }
  1118. nicvf_free_cq_poll(nic);
  1119. return err;
  1120. }
  1121. static int nicvf_change_mtu(struct net_device *netdev, int new_mtu)
  1122. {
  1123. struct nicvf *nic = netdev_priv(netdev);
  1124. int orig_mtu = netdev->mtu;
  1125. netdev->mtu = new_mtu;
  1126. if (!netif_running(netdev))
  1127. return 0;
  1128. if (nicvf_update_hw_max_frs(nic, new_mtu)) {
  1129. netdev->mtu = orig_mtu;
  1130. return -EINVAL;
  1131. }
  1132. return 0;
  1133. }
  1134. static int nicvf_set_mac_address(struct net_device *netdev, void *p)
  1135. {
  1136. struct sockaddr *addr = p;
  1137. struct nicvf *nic = netdev_priv(netdev);
  1138. if (!is_valid_ether_addr(addr->sa_data))
  1139. return -EADDRNOTAVAIL;
  1140. memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
  1141. if (nic->msix_enabled) {
  1142. if (nicvf_hw_set_mac_addr(nic, netdev))
  1143. return -EBUSY;
  1144. } else {
  1145. nic->set_mac_pending = true;
  1146. }
  1147. return 0;
  1148. }
  1149. void nicvf_update_lmac_stats(struct nicvf *nic)
  1150. {
  1151. int stat = 0;
  1152. union nic_mbx mbx = {};
  1153. if (!netif_running(nic->netdev))
  1154. return;
  1155. mbx.bgx_stats.msg = NIC_MBOX_MSG_BGX_STATS;
  1156. mbx.bgx_stats.vf_id = nic->vf_id;
  1157. /* Rx stats */
  1158. mbx.bgx_stats.rx = 1;
  1159. while (stat < BGX_RX_STATS_COUNT) {
  1160. mbx.bgx_stats.idx = stat;
  1161. if (nicvf_send_msg_to_pf(nic, &mbx))
  1162. return;
  1163. stat++;
  1164. }
  1165. stat = 0;
  1166. /* Tx stats */
  1167. mbx.bgx_stats.rx = 0;
  1168. while (stat < BGX_TX_STATS_COUNT) {
  1169. mbx.bgx_stats.idx = stat;
  1170. if (nicvf_send_msg_to_pf(nic, &mbx))
  1171. return;
  1172. stat++;
  1173. }
  1174. }
  1175. void nicvf_update_stats(struct nicvf *nic)
  1176. {
  1177. int qidx, cpu;
  1178. u64 tmp_stats = 0;
  1179. struct nicvf_hw_stats *stats = &nic->hw_stats;
  1180. struct nicvf_drv_stats *drv_stats;
  1181. struct queue_set *qs = nic->qs;
  1182. #define GET_RX_STATS(reg) \
  1183. nicvf_reg_read(nic, NIC_VNIC_RX_STAT_0_13 | (reg << 3))
  1184. #define GET_TX_STATS(reg) \
  1185. nicvf_reg_read(nic, NIC_VNIC_TX_STAT_0_4 | (reg << 3))
  1186. stats->rx_bytes = GET_RX_STATS(RX_OCTS);
  1187. stats->rx_ucast_frames = GET_RX_STATS(RX_UCAST);
  1188. stats->rx_bcast_frames = GET_RX_STATS(RX_BCAST);
  1189. stats->rx_mcast_frames = GET_RX_STATS(RX_MCAST);
  1190. stats->rx_fcs_errors = GET_RX_STATS(RX_FCS);
  1191. stats->rx_l2_errors = GET_RX_STATS(RX_L2ERR);
  1192. stats->rx_drop_red = GET_RX_STATS(RX_RED);
  1193. stats->rx_drop_red_bytes = GET_RX_STATS(RX_RED_OCTS);
  1194. stats->rx_drop_overrun = GET_RX_STATS(RX_ORUN);
  1195. stats->rx_drop_overrun_bytes = GET_RX_STATS(RX_ORUN_OCTS);
  1196. stats->rx_drop_bcast = GET_RX_STATS(RX_DRP_BCAST);
  1197. stats->rx_drop_mcast = GET_RX_STATS(RX_DRP_MCAST);
  1198. stats->rx_drop_l3_bcast = GET_RX_STATS(RX_DRP_L3BCAST);
  1199. stats->rx_drop_l3_mcast = GET_RX_STATS(RX_DRP_L3MCAST);
  1200. stats->tx_bytes = GET_TX_STATS(TX_OCTS);
  1201. stats->tx_ucast_frames = GET_TX_STATS(TX_UCAST);
  1202. stats->tx_bcast_frames = GET_TX_STATS(TX_BCAST);
  1203. stats->tx_mcast_frames = GET_TX_STATS(TX_MCAST);
  1204. stats->tx_drops = GET_TX_STATS(TX_DROP);
  1205. /* On T88 pass 2.0, the dummy SQE added for TSO notification
  1206. * via CQE has 'dont_send' set. Hence HW drops the pkt pointed
  1207. * pointed by dummy SQE and results in tx_drops counter being
  1208. * incremented. Subtracting it from tx_tso counter will give
  1209. * exact tx_drops counter.
  1210. */
  1211. if (nic->t88 && nic->hw_tso) {
  1212. for_each_possible_cpu(cpu) {
  1213. drv_stats = per_cpu_ptr(nic->drv_stats, cpu);
  1214. tmp_stats += drv_stats->tx_tso;
  1215. }
  1216. stats->tx_drops = tmp_stats - stats->tx_drops;
  1217. }
  1218. stats->tx_frames = stats->tx_ucast_frames +
  1219. stats->tx_bcast_frames +
  1220. stats->tx_mcast_frames;
  1221. stats->rx_frames = stats->rx_ucast_frames +
  1222. stats->rx_bcast_frames +
  1223. stats->rx_mcast_frames;
  1224. stats->rx_drops = stats->rx_drop_red +
  1225. stats->rx_drop_overrun;
  1226. /* Update RQ and SQ stats */
  1227. for (qidx = 0; qidx < qs->rq_cnt; qidx++)
  1228. nicvf_update_rq_stats(nic, qidx);
  1229. for (qidx = 0; qidx < qs->sq_cnt; qidx++)
  1230. nicvf_update_sq_stats(nic, qidx);
  1231. }
  1232. static void nicvf_get_stats64(struct net_device *netdev,
  1233. struct rtnl_link_stats64 *stats)
  1234. {
  1235. struct nicvf *nic = netdev_priv(netdev);
  1236. struct nicvf_hw_stats *hw_stats = &nic->hw_stats;
  1237. nicvf_update_stats(nic);
  1238. stats->rx_bytes = hw_stats->rx_bytes;
  1239. stats->rx_packets = hw_stats->rx_frames;
  1240. stats->rx_dropped = hw_stats->rx_drops;
  1241. stats->multicast = hw_stats->rx_mcast_frames;
  1242. stats->tx_bytes = hw_stats->tx_bytes;
  1243. stats->tx_packets = hw_stats->tx_frames;
  1244. stats->tx_dropped = hw_stats->tx_drops;
  1245. }
  1246. static void nicvf_tx_timeout(struct net_device *dev)
  1247. {
  1248. struct nicvf *nic = netdev_priv(dev);
  1249. if (netif_msg_tx_err(nic))
  1250. netdev_warn(dev, "%s: Transmit timed out, resetting\n",
  1251. dev->name);
  1252. this_cpu_inc(nic->drv_stats->tx_timeout);
  1253. schedule_work(&nic->reset_task);
  1254. }
  1255. static void nicvf_reset_task(struct work_struct *work)
  1256. {
  1257. struct nicvf *nic;
  1258. nic = container_of(work, struct nicvf, reset_task);
  1259. if (!netif_running(nic->netdev))
  1260. return;
  1261. nicvf_stop(nic->netdev);
  1262. nicvf_open(nic->netdev);
  1263. netif_trans_update(nic->netdev);
  1264. }
  1265. static int nicvf_config_loopback(struct nicvf *nic,
  1266. netdev_features_t features)
  1267. {
  1268. union nic_mbx mbx = {};
  1269. mbx.lbk.msg = NIC_MBOX_MSG_LOOPBACK;
  1270. mbx.lbk.vf_id = nic->vf_id;
  1271. mbx.lbk.enable = (features & NETIF_F_LOOPBACK) != 0;
  1272. return nicvf_send_msg_to_pf(nic, &mbx);
  1273. }
  1274. static netdev_features_t nicvf_fix_features(struct net_device *netdev,
  1275. netdev_features_t features)
  1276. {
  1277. struct nicvf *nic = netdev_priv(netdev);
  1278. if ((features & NETIF_F_LOOPBACK) &&
  1279. netif_running(netdev) && !nic->loopback_supported)
  1280. features &= ~NETIF_F_LOOPBACK;
  1281. return features;
  1282. }
  1283. static int nicvf_set_features(struct net_device *netdev,
  1284. netdev_features_t features)
  1285. {
  1286. struct nicvf *nic = netdev_priv(netdev);
  1287. netdev_features_t changed = features ^ netdev->features;
  1288. if (changed & NETIF_F_HW_VLAN_CTAG_RX)
  1289. nicvf_config_vlan_stripping(nic, features);
  1290. if ((changed & NETIF_F_LOOPBACK) && netif_running(netdev))
  1291. return nicvf_config_loopback(nic, features);
  1292. return 0;
  1293. }
  1294. static const struct net_device_ops nicvf_netdev_ops = {
  1295. .ndo_open = nicvf_open,
  1296. .ndo_stop = nicvf_stop,
  1297. .ndo_start_xmit = nicvf_xmit,
  1298. .ndo_change_mtu = nicvf_change_mtu,
  1299. .ndo_set_mac_address = nicvf_set_mac_address,
  1300. .ndo_get_stats64 = nicvf_get_stats64,
  1301. .ndo_tx_timeout = nicvf_tx_timeout,
  1302. .ndo_fix_features = nicvf_fix_features,
  1303. .ndo_set_features = nicvf_set_features,
  1304. };
  1305. static int nicvf_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  1306. {
  1307. struct device *dev = &pdev->dev;
  1308. struct net_device *netdev;
  1309. struct nicvf *nic;
  1310. int err, qcount;
  1311. u16 sdevid;
  1312. err = pci_enable_device(pdev);
  1313. if (err) {
  1314. dev_err(dev, "Failed to enable PCI device\n");
  1315. return err;
  1316. }
  1317. err = pci_request_regions(pdev, DRV_NAME);
  1318. if (err) {
  1319. dev_err(dev, "PCI request regions failed 0x%x\n", err);
  1320. goto err_disable_device;
  1321. }
  1322. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(48));
  1323. if (err) {
  1324. dev_err(dev, "Unable to get usable DMA configuration\n");
  1325. goto err_release_regions;
  1326. }
  1327. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(48));
  1328. if (err) {
  1329. dev_err(dev, "unable to get 48-bit DMA for consistent allocations\n");
  1330. goto err_release_regions;
  1331. }
  1332. qcount = netif_get_num_default_rss_queues();
  1333. /* Restrict multiqset support only for host bound VFs */
  1334. if (pdev->is_virtfn) {
  1335. /* Set max number of queues per VF */
  1336. qcount = min_t(int, num_online_cpus(),
  1337. (MAX_SQS_PER_VF + 1) * MAX_CMP_QUEUES_PER_QS);
  1338. }
  1339. netdev = alloc_etherdev_mqs(sizeof(struct nicvf), qcount, qcount);
  1340. if (!netdev) {
  1341. err = -ENOMEM;
  1342. goto err_release_regions;
  1343. }
  1344. pci_set_drvdata(pdev, netdev);
  1345. SET_NETDEV_DEV(netdev, &pdev->dev);
  1346. nic = netdev_priv(netdev);
  1347. nic->netdev = netdev;
  1348. nic->pdev = pdev;
  1349. nic->pnicvf = nic;
  1350. nic->max_queues = qcount;
  1351. /* MAP VF's configuration registers */
  1352. nic->reg_base = pcim_iomap(pdev, PCI_CFG_REG_BAR_NUM, 0);
  1353. if (!nic->reg_base) {
  1354. dev_err(dev, "Cannot map config register space, aborting\n");
  1355. err = -ENOMEM;
  1356. goto err_free_netdev;
  1357. }
  1358. nic->drv_stats = netdev_alloc_pcpu_stats(struct nicvf_drv_stats);
  1359. if (!nic->drv_stats) {
  1360. err = -ENOMEM;
  1361. goto err_free_netdev;
  1362. }
  1363. err = nicvf_set_qset_resources(nic);
  1364. if (err)
  1365. goto err_free_netdev;
  1366. /* Check if PF is alive and get MAC address for this VF */
  1367. err = nicvf_register_misc_interrupt(nic);
  1368. if (err)
  1369. goto err_free_netdev;
  1370. nicvf_send_vf_struct(nic);
  1371. if (!pass1_silicon(nic->pdev))
  1372. nic->hw_tso = true;
  1373. /* Get iommu domain for iova to physical addr conversion */
  1374. nic->iommu_domain = iommu_get_domain_for_dev(dev);
  1375. pci_read_config_word(nic->pdev, PCI_SUBSYSTEM_ID, &sdevid);
  1376. if (sdevid == 0xA134)
  1377. nic->t88 = true;
  1378. /* Check if this VF is in QS only mode */
  1379. if (nic->sqs_mode)
  1380. return 0;
  1381. err = nicvf_set_real_num_queues(netdev, nic->tx_queues, nic->rx_queues);
  1382. if (err)
  1383. goto err_unregister_interrupts;
  1384. netdev->hw_features = (NETIF_F_RXCSUM | NETIF_F_IP_CSUM | NETIF_F_SG |
  1385. NETIF_F_TSO | NETIF_F_GRO |
  1386. NETIF_F_HW_VLAN_CTAG_RX);
  1387. netdev->hw_features |= NETIF_F_RXHASH;
  1388. netdev->features |= netdev->hw_features;
  1389. netdev->hw_features |= NETIF_F_LOOPBACK;
  1390. netdev->vlan_features = NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO;
  1391. netdev->netdev_ops = &nicvf_netdev_ops;
  1392. netdev->watchdog_timeo = NICVF_TX_TIMEOUT;
  1393. /* MTU range: 64 - 9200 */
  1394. netdev->min_mtu = NIC_HW_MIN_FRS;
  1395. netdev->max_mtu = NIC_HW_MAX_FRS;
  1396. INIT_WORK(&nic->reset_task, nicvf_reset_task);
  1397. err = register_netdev(netdev);
  1398. if (err) {
  1399. dev_err(dev, "Failed to register netdevice\n");
  1400. goto err_unregister_interrupts;
  1401. }
  1402. nic->msg_enable = debug;
  1403. nicvf_set_ethtool_ops(netdev);
  1404. return 0;
  1405. err_unregister_interrupts:
  1406. nicvf_unregister_interrupts(nic);
  1407. err_free_netdev:
  1408. pci_set_drvdata(pdev, NULL);
  1409. if (nic->drv_stats)
  1410. free_percpu(nic->drv_stats);
  1411. free_netdev(netdev);
  1412. err_release_regions:
  1413. pci_release_regions(pdev);
  1414. err_disable_device:
  1415. pci_disable_device(pdev);
  1416. return err;
  1417. }
  1418. static void nicvf_remove(struct pci_dev *pdev)
  1419. {
  1420. struct net_device *netdev = pci_get_drvdata(pdev);
  1421. struct nicvf *nic;
  1422. struct net_device *pnetdev;
  1423. if (!netdev)
  1424. return;
  1425. nic = netdev_priv(netdev);
  1426. pnetdev = nic->pnicvf->netdev;
  1427. /* Check if this Qset is assigned to different VF.
  1428. * If yes, clean primary and all secondary Qsets.
  1429. */
  1430. if (pnetdev && (pnetdev->reg_state == NETREG_REGISTERED))
  1431. unregister_netdev(pnetdev);
  1432. nicvf_unregister_interrupts(nic);
  1433. pci_set_drvdata(pdev, NULL);
  1434. if (nic->drv_stats)
  1435. free_percpu(nic->drv_stats);
  1436. free_netdev(netdev);
  1437. pci_release_regions(pdev);
  1438. pci_disable_device(pdev);
  1439. }
  1440. static void nicvf_shutdown(struct pci_dev *pdev)
  1441. {
  1442. nicvf_remove(pdev);
  1443. }
  1444. static struct pci_driver nicvf_driver = {
  1445. .name = DRV_NAME,
  1446. .id_table = nicvf_id_table,
  1447. .probe = nicvf_probe,
  1448. .remove = nicvf_remove,
  1449. .shutdown = nicvf_shutdown,
  1450. };
  1451. static int __init nicvf_init_module(void)
  1452. {
  1453. pr_info("%s, ver %s\n", DRV_NAME, DRV_VERSION);
  1454. return pci_register_driver(&nicvf_driver);
  1455. }
  1456. static void __exit nicvf_cleanup_module(void)
  1457. {
  1458. pci_unregister_driver(&nicvf_driver);
  1459. }
  1460. module_init(nicvf_init_module);
  1461. module_exit(nicvf_cleanup_module);