nicvf_main.c 44 KB

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