ixgbe_fcoe.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright(c) 1999 - 2018 Intel Corporation. */
  3. #include "ixgbe.h"
  4. #include <linux/if_ether.h>
  5. #include <linux/gfp.h>
  6. #include <linux/if_vlan.h>
  7. #include <generated/utsrelease.h>
  8. #include <scsi/scsi_cmnd.h>
  9. #include <scsi/scsi_device.h>
  10. #include <scsi/fc/fc_fs.h>
  11. #include <scsi/fc/fc_fcoe.h>
  12. #include <scsi/libfc.h>
  13. #include <scsi/libfcoe.h>
  14. /**
  15. * ixgbe_fcoe_clear_ddp - clear the given ddp context
  16. * @ddp: ptr to the ixgbe_fcoe_ddp
  17. *
  18. * Returns : none
  19. *
  20. */
  21. static inline void ixgbe_fcoe_clear_ddp(struct ixgbe_fcoe_ddp *ddp)
  22. {
  23. ddp->len = 0;
  24. ddp->err = 1;
  25. ddp->udl = NULL;
  26. ddp->udp = 0UL;
  27. ddp->sgl = NULL;
  28. ddp->sgc = 0;
  29. }
  30. /**
  31. * ixgbe_fcoe_ddp_put - free the ddp context for a given xid
  32. * @netdev: the corresponding net_device
  33. * @xid: the xid that corresponding ddp will be freed
  34. *
  35. * This is the implementation of net_device_ops.ndo_fcoe_ddp_done
  36. * and it is expected to be called by ULD, i.e., FCP layer of libfc
  37. * to release the corresponding ddp context when the I/O is done.
  38. *
  39. * Returns : data length already ddp-ed in bytes
  40. */
  41. int ixgbe_fcoe_ddp_put(struct net_device *netdev, u16 xid)
  42. {
  43. int len;
  44. struct ixgbe_fcoe *fcoe;
  45. struct ixgbe_adapter *adapter;
  46. struct ixgbe_fcoe_ddp *ddp;
  47. struct ixgbe_hw *hw;
  48. u32 fcbuff;
  49. if (!netdev)
  50. return 0;
  51. if (xid >= netdev->fcoe_ddp_xid)
  52. return 0;
  53. adapter = netdev_priv(netdev);
  54. fcoe = &adapter->fcoe;
  55. ddp = &fcoe->ddp[xid];
  56. if (!ddp->udl)
  57. return 0;
  58. hw = &adapter->hw;
  59. len = ddp->len;
  60. /* if no error then skip ddp context invalidation */
  61. if (!ddp->err)
  62. goto skip_ddpinv;
  63. if (hw->mac.type == ixgbe_mac_X550) {
  64. /* X550 does not require DDP FCoE lock */
  65. IXGBE_WRITE_REG(hw, IXGBE_FCDFC(0, xid), 0);
  66. IXGBE_WRITE_REG(hw, IXGBE_FCDFC(3, xid),
  67. (xid | IXGBE_FCFLTRW_WE));
  68. /* program FCBUFF */
  69. IXGBE_WRITE_REG(hw, IXGBE_FCDDC(2, xid), 0);
  70. /* program FCDMARW */
  71. IXGBE_WRITE_REG(hw, IXGBE_FCDDC(3, xid),
  72. (xid | IXGBE_FCDMARW_WE));
  73. /* read FCBUFF to check context invalidated */
  74. IXGBE_WRITE_REG(hw, IXGBE_FCDDC(3, xid),
  75. (xid | IXGBE_FCDMARW_RE));
  76. fcbuff = IXGBE_READ_REG(hw, IXGBE_FCDDC(2, xid));
  77. } else {
  78. /* other hardware requires DDP FCoE lock */
  79. spin_lock_bh(&fcoe->lock);
  80. IXGBE_WRITE_REG(hw, IXGBE_FCFLT, 0);
  81. IXGBE_WRITE_REG(hw, IXGBE_FCFLTRW,
  82. (xid | IXGBE_FCFLTRW_WE));
  83. IXGBE_WRITE_REG(hw, IXGBE_FCBUFF, 0);
  84. IXGBE_WRITE_REG(hw, IXGBE_FCDMARW,
  85. (xid | IXGBE_FCDMARW_WE));
  86. /* guaranteed to be invalidated after 100us */
  87. IXGBE_WRITE_REG(hw, IXGBE_FCDMARW,
  88. (xid | IXGBE_FCDMARW_RE));
  89. fcbuff = IXGBE_READ_REG(hw, IXGBE_FCBUFF);
  90. spin_unlock_bh(&fcoe->lock);
  91. }
  92. if (fcbuff & IXGBE_FCBUFF_VALID)
  93. usleep_range(100, 150);
  94. skip_ddpinv:
  95. if (ddp->sgl)
  96. dma_unmap_sg(&adapter->pdev->dev, ddp->sgl, ddp->sgc,
  97. DMA_FROM_DEVICE);
  98. if (ddp->pool) {
  99. dma_pool_free(ddp->pool, ddp->udl, ddp->udp);
  100. ddp->pool = NULL;
  101. }
  102. ixgbe_fcoe_clear_ddp(ddp);
  103. return len;
  104. }
  105. /**
  106. * ixgbe_fcoe_ddp_setup - called to set up ddp context
  107. * @netdev: the corresponding net_device
  108. * @xid: the exchange id requesting ddp
  109. * @sgl: the scatter-gather list for this request
  110. * @sgc: the number of scatter-gather items
  111. * @target_mode: 1 to setup target mode, 0 to setup initiator mode
  112. *
  113. * Returns : 1 for success and 0 for no ddp
  114. */
  115. static int ixgbe_fcoe_ddp_setup(struct net_device *netdev, u16 xid,
  116. struct scatterlist *sgl, unsigned int sgc,
  117. int target_mode)
  118. {
  119. struct ixgbe_adapter *adapter;
  120. struct ixgbe_hw *hw;
  121. struct ixgbe_fcoe *fcoe;
  122. struct ixgbe_fcoe_ddp *ddp;
  123. struct ixgbe_fcoe_ddp_pool *ddp_pool;
  124. struct scatterlist *sg;
  125. unsigned int i, j, dmacount;
  126. unsigned int len;
  127. static const unsigned int bufflen = IXGBE_FCBUFF_MIN;
  128. unsigned int firstoff = 0;
  129. unsigned int lastsize;
  130. unsigned int thisoff = 0;
  131. unsigned int thislen = 0;
  132. u32 fcbuff, fcdmarw, fcfltrw, fcrxctl;
  133. dma_addr_t addr = 0;
  134. if (!netdev || !sgl)
  135. return 0;
  136. adapter = netdev_priv(netdev);
  137. if (xid >= netdev->fcoe_ddp_xid) {
  138. e_warn(drv, "xid=0x%x out-of-range\n", xid);
  139. return 0;
  140. }
  141. /* no DDP if we are already down or resetting */
  142. if (test_bit(__IXGBE_DOWN, &adapter->state) ||
  143. test_bit(__IXGBE_RESETTING, &adapter->state))
  144. return 0;
  145. fcoe = &adapter->fcoe;
  146. ddp = &fcoe->ddp[xid];
  147. if (ddp->sgl) {
  148. e_err(drv, "xid 0x%x w/ non-null sgl=%p nents=%d\n",
  149. xid, ddp->sgl, ddp->sgc);
  150. return 0;
  151. }
  152. ixgbe_fcoe_clear_ddp(ddp);
  153. if (!fcoe->ddp_pool) {
  154. e_warn(drv, "No ddp_pool resources allocated\n");
  155. return 0;
  156. }
  157. ddp_pool = per_cpu_ptr(fcoe->ddp_pool, get_cpu());
  158. if (!ddp_pool->pool) {
  159. e_warn(drv, "xid=0x%x no ddp pool for fcoe\n", xid);
  160. goto out_noddp;
  161. }
  162. /* setup dma from scsi command sgl */
  163. dmacount = dma_map_sg(&adapter->pdev->dev, sgl, sgc, DMA_FROM_DEVICE);
  164. if (dmacount == 0) {
  165. e_err(drv, "xid 0x%x DMA map error\n", xid);
  166. goto out_noddp;
  167. }
  168. /* alloc the udl from per cpu ddp pool */
  169. ddp->udl = dma_pool_alloc(ddp_pool->pool, GFP_ATOMIC, &ddp->udp);
  170. if (!ddp->udl) {
  171. e_err(drv, "failed allocated ddp context\n");
  172. goto out_noddp_unmap;
  173. }
  174. ddp->pool = ddp_pool->pool;
  175. ddp->sgl = sgl;
  176. ddp->sgc = sgc;
  177. j = 0;
  178. for_each_sg(sgl, sg, dmacount, i) {
  179. addr = sg_dma_address(sg);
  180. len = sg_dma_len(sg);
  181. while (len) {
  182. /* max number of buffers allowed in one DDP context */
  183. if (j >= IXGBE_BUFFCNT_MAX) {
  184. ddp_pool->noddp++;
  185. goto out_noddp_free;
  186. }
  187. /* get the offset of length of current buffer */
  188. thisoff = addr & ((dma_addr_t)bufflen - 1);
  189. thislen = min((bufflen - thisoff), len);
  190. /*
  191. * all but the 1st buffer (j == 0)
  192. * must be aligned on bufflen
  193. */
  194. if ((j != 0) && (thisoff))
  195. goto out_noddp_free;
  196. /*
  197. * all but the last buffer
  198. * ((i == (dmacount - 1)) && (thislen == len))
  199. * must end at bufflen
  200. */
  201. if (((i != (dmacount - 1)) || (thislen != len))
  202. && ((thislen + thisoff) != bufflen))
  203. goto out_noddp_free;
  204. ddp->udl[j] = (u64)(addr - thisoff);
  205. /* only the first buffer may have none-zero offset */
  206. if (j == 0)
  207. firstoff = thisoff;
  208. len -= thislen;
  209. addr += thislen;
  210. j++;
  211. }
  212. }
  213. /* only the last buffer may have non-full bufflen */
  214. lastsize = thisoff + thislen;
  215. /*
  216. * lastsize can not be buffer len.
  217. * If it is then adding another buffer with lastsize = 1.
  218. */
  219. if (lastsize == bufflen) {
  220. if (j >= IXGBE_BUFFCNT_MAX) {
  221. ddp_pool->noddp_ext_buff++;
  222. goto out_noddp_free;
  223. }
  224. ddp->udl[j] = (u64)(fcoe->extra_ddp_buffer_dma);
  225. j++;
  226. lastsize = 1;
  227. }
  228. put_cpu();
  229. fcbuff = (IXGBE_FCBUFF_4KB << IXGBE_FCBUFF_BUFFSIZE_SHIFT);
  230. fcbuff |= ((j & 0xff) << IXGBE_FCBUFF_BUFFCNT_SHIFT);
  231. fcbuff |= (firstoff << IXGBE_FCBUFF_OFFSET_SHIFT);
  232. /* Set WRCONTX bit to allow DDP for target */
  233. if (target_mode)
  234. fcbuff |= (IXGBE_FCBUFF_WRCONTX);
  235. fcbuff |= (IXGBE_FCBUFF_VALID);
  236. fcdmarw = xid;
  237. fcdmarw |= IXGBE_FCDMARW_WE;
  238. fcdmarw |= (lastsize << IXGBE_FCDMARW_LASTSIZE_SHIFT);
  239. fcfltrw = xid;
  240. fcfltrw |= IXGBE_FCFLTRW_WE;
  241. /* program DMA context */
  242. hw = &adapter->hw;
  243. /* turn on last frame indication for target mode as FCP_RSPtarget is
  244. * supposed to send FCP_RSP when it is done. */
  245. if (target_mode && !test_bit(__IXGBE_FCOE_TARGET, &fcoe->mode)) {
  246. set_bit(__IXGBE_FCOE_TARGET, &fcoe->mode);
  247. fcrxctl = IXGBE_READ_REG(hw, IXGBE_FCRXCTRL);
  248. fcrxctl |= IXGBE_FCRXCTRL_LASTSEQH;
  249. IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL, fcrxctl);
  250. }
  251. if (hw->mac.type == ixgbe_mac_X550) {
  252. /* X550 does not require DDP lock */
  253. IXGBE_WRITE_REG(hw, IXGBE_FCDDC(0, xid),
  254. ddp->udp & DMA_BIT_MASK(32));
  255. IXGBE_WRITE_REG(hw, IXGBE_FCDDC(1, xid), (u64)ddp->udp >> 32);
  256. IXGBE_WRITE_REG(hw, IXGBE_FCDDC(2, xid), fcbuff);
  257. IXGBE_WRITE_REG(hw, IXGBE_FCDDC(3, xid), fcdmarw);
  258. /* program filter context */
  259. IXGBE_WRITE_REG(hw, IXGBE_FCDFC(0, xid), IXGBE_FCFLT_VALID);
  260. IXGBE_WRITE_REG(hw, IXGBE_FCDFC(1, xid), 0);
  261. IXGBE_WRITE_REG(hw, IXGBE_FCDFC(3, xid), fcfltrw);
  262. } else {
  263. /* DDP lock for indirect DDP context access */
  264. spin_lock_bh(&fcoe->lock);
  265. IXGBE_WRITE_REG(hw, IXGBE_FCPTRL, ddp->udp & DMA_BIT_MASK(32));
  266. IXGBE_WRITE_REG(hw, IXGBE_FCPTRH, (u64)ddp->udp >> 32);
  267. IXGBE_WRITE_REG(hw, IXGBE_FCBUFF, fcbuff);
  268. IXGBE_WRITE_REG(hw, IXGBE_FCDMARW, fcdmarw);
  269. /* program filter context */
  270. IXGBE_WRITE_REG(hw, IXGBE_FCPARAM, 0);
  271. IXGBE_WRITE_REG(hw, IXGBE_FCFLT, IXGBE_FCFLT_VALID);
  272. IXGBE_WRITE_REG(hw, IXGBE_FCFLTRW, fcfltrw);
  273. spin_unlock_bh(&fcoe->lock);
  274. }
  275. return 1;
  276. out_noddp_free:
  277. dma_pool_free(ddp->pool, ddp->udl, ddp->udp);
  278. ixgbe_fcoe_clear_ddp(ddp);
  279. out_noddp_unmap:
  280. dma_unmap_sg(&adapter->pdev->dev, sgl, sgc, DMA_FROM_DEVICE);
  281. out_noddp:
  282. put_cpu();
  283. return 0;
  284. }
  285. /**
  286. * ixgbe_fcoe_ddp_get - called to set up ddp context in initiator mode
  287. * @netdev: the corresponding net_device
  288. * @xid: the exchange id requesting ddp
  289. * @sgl: the scatter-gather list for this request
  290. * @sgc: the number of scatter-gather items
  291. *
  292. * This is the implementation of net_device_ops.ndo_fcoe_ddp_setup
  293. * and is expected to be called from ULD, e.g., FCP layer of libfc
  294. * to set up ddp for the corresponding xid of the given sglist for
  295. * the corresponding I/O.
  296. *
  297. * Returns : 1 for success and 0 for no ddp
  298. */
  299. int ixgbe_fcoe_ddp_get(struct net_device *netdev, u16 xid,
  300. struct scatterlist *sgl, unsigned int sgc)
  301. {
  302. return ixgbe_fcoe_ddp_setup(netdev, xid, sgl, sgc, 0);
  303. }
  304. /**
  305. * ixgbe_fcoe_ddp_target - called to set up ddp context in target mode
  306. * @netdev: the corresponding net_device
  307. * @xid: the exchange id requesting ddp
  308. * @sgl: the scatter-gather list for this request
  309. * @sgc: the number of scatter-gather items
  310. *
  311. * This is the implementation of net_device_ops.ndo_fcoe_ddp_target
  312. * and is expected to be called from ULD, e.g., FCP layer of libfc
  313. * to set up ddp for the corresponding xid of the given sglist for
  314. * the corresponding I/O. The DDP in target mode is a write I/O request
  315. * from the initiator.
  316. *
  317. * Returns : 1 for success and 0 for no ddp
  318. */
  319. int ixgbe_fcoe_ddp_target(struct net_device *netdev, u16 xid,
  320. struct scatterlist *sgl, unsigned int sgc)
  321. {
  322. return ixgbe_fcoe_ddp_setup(netdev, xid, sgl, sgc, 1);
  323. }
  324. /**
  325. * ixgbe_fcoe_ddp - check ddp status and mark it done
  326. * @adapter: ixgbe adapter
  327. * @rx_desc: advanced rx descriptor
  328. * @skb: the skb holding the received data
  329. *
  330. * This checks ddp status.
  331. *
  332. * Returns : < 0 indicates an error or not a FCiE ddp, 0 indicates
  333. * not passing the skb to ULD, > 0 indicates is the length of data
  334. * being ddped.
  335. */
  336. int ixgbe_fcoe_ddp(struct ixgbe_adapter *adapter,
  337. union ixgbe_adv_rx_desc *rx_desc,
  338. struct sk_buff *skb)
  339. {
  340. int rc = -EINVAL;
  341. struct ixgbe_fcoe *fcoe;
  342. struct ixgbe_fcoe_ddp *ddp;
  343. struct fc_frame_header *fh;
  344. struct fcoe_crc_eof *crc;
  345. __le32 fcerr = ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_ERR_FCERR);
  346. __le32 ddp_err;
  347. int ddp_max;
  348. u32 fctl;
  349. u16 xid;
  350. if (fcerr == cpu_to_le32(IXGBE_FCERR_BADCRC))
  351. skb->ip_summed = CHECKSUM_NONE;
  352. else
  353. skb->ip_summed = CHECKSUM_UNNECESSARY;
  354. if (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
  355. fh = (struct fc_frame_header *)(skb->data +
  356. sizeof(struct vlan_hdr) + sizeof(struct fcoe_hdr));
  357. else
  358. fh = (struct fc_frame_header *)(skb->data +
  359. sizeof(struct fcoe_hdr));
  360. fctl = ntoh24(fh->fh_f_ctl);
  361. if (fctl & FC_FC_EX_CTX)
  362. xid = be16_to_cpu(fh->fh_ox_id);
  363. else
  364. xid = be16_to_cpu(fh->fh_rx_id);
  365. ddp_max = IXGBE_FCOE_DDP_MAX;
  366. /* X550 has different DDP Max limit */
  367. if (adapter->hw.mac.type == ixgbe_mac_X550)
  368. ddp_max = IXGBE_FCOE_DDP_MAX_X550;
  369. if (xid >= ddp_max)
  370. return -EINVAL;
  371. fcoe = &adapter->fcoe;
  372. ddp = &fcoe->ddp[xid];
  373. if (!ddp->udl)
  374. return -EINVAL;
  375. ddp_err = ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_ERR_FCEOFE |
  376. IXGBE_RXDADV_ERR_FCERR);
  377. if (ddp_err)
  378. return -EINVAL;
  379. switch (ixgbe_test_staterr(rx_desc, IXGBE_RXDADV_STAT_FCSTAT)) {
  380. /* return 0 to bypass going to ULD for DDPed data */
  381. case cpu_to_le32(IXGBE_RXDADV_STAT_FCSTAT_DDP):
  382. /* update length of DDPed data */
  383. ddp->len = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
  384. rc = 0;
  385. break;
  386. /* unmap the sg list when FCPRSP is received */
  387. case cpu_to_le32(IXGBE_RXDADV_STAT_FCSTAT_FCPRSP):
  388. dma_unmap_sg(&adapter->pdev->dev, ddp->sgl,
  389. ddp->sgc, DMA_FROM_DEVICE);
  390. ddp->err = (__force u32)ddp_err;
  391. ddp->sgl = NULL;
  392. ddp->sgc = 0;
  393. fallthrough;
  394. /* if DDP length is present pass it through to ULD */
  395. case cpu_to_le32(IXGBE_RXDADV_STAT_FCSTAT_NODDP):
  396. /* update length of DDPed data */
  397. ddp->len = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
  398. if (ddp->len)
  399. rc = ddp->len;
  400. break;
  401. /* no match will return as an error */
  402. case cpu_to_le32(IXGBE_RXDADV_STAT_FCSTAT_NOMTCH):
  403. default:
  404. break;
  405. }
  406. /* In target mode, check the last data frame of the sequence.
  407. * For DDP in target mode, data is already DDPed but the header
  408. * indication of the last data frame ould allow is to tell if we
  409. * got all the data and the ULP can send FCP_RSP back, as this is
  410. * not a full fcoe frame, we fill the trailer here so it won't be
  411. * dropped by the ULP stack.
  412. */
  413. if ((fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA) &&
  414. (fctl & FC_FC_END_SEQ)) {
  415. skb_linearize(skb);
  416. crc = skb_put(skb, sizeof(*crc));
  417. crc->fcoe_eof = FC_EOF_T;
  418. }
  419. return rc;
  420. }
  421. /**
  422. * ixgbe_fso - ixgbe FCoE Sequence Offload (FSO)
  423. * @tx_ring: tx desc ring
  424. * @first: first tx_buffer structure containing skb, tx_flags, and protocol
  425. * @hdr_len: hdr_len to be returned
  426. *
  427. * This sets up large send offload for FCoE
  428. *
  429. * Returns : 0 indicates success, < 0 for error
  430. */
  431. int ixgbe_fso(struct ixgbe_ring *tx_ring,
  432. struct ixgbe_tx_buffer *first,
  433. u8 *hdr_len)
  434. {
  435. struct sk_buff *skb = first->skb;
  436. struct fc_frame_header *fh;
  437. u32 vlan_macip_lens;
  438. u32 fcoe_sof_eof = 0;
  439. u32 mss_l4len_idx;
  440. u32 type_tucmd = IXGBE_ADVTXT_TUCMD_FCOE;
  441. u8 sof, eof;
  442. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type != SKB_GSO_FCOE)) {
  443. dev_err(tx_ring->dev, "Wrong gso type %d:expecting SKB_GSO_FCOE\n",
  444. skb_shinfo(skb)->gso_type);
  445. return -EINVAL;
  446. }
  447. /* resets the header to point fcoe/fc */
  448. skb_set_network_header(skb, skb->mac_len);
  449. skb_set_transport_header(skb, skb->mac_len +
  450. sizeof(struct fcoe_hdr));
  451. /* sets up SOF and ORIS */
  452. sof = ((struct fcoe_hdr *)skb_network_header(skb))->fcoe_sof;
  453. switch (sof) {
  454. case FC_SOF_I2:
  455. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_ORIS;
  456. break;
  457. case FC_SOF_I3:
  458. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_SOF |
  459. IXGBE_ADVTXD_FCOEF_ORIS;
  460. break;
  461. case FC_SOF_N2:
  462. break;
  463. case FC_SOF_N3:
  464. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_SOF;
  465. break;
  466. default:
  467. dev_warn(tx_ring->dev, "unknown sof = 0x%x\n", sof);
  468. return -EINVAL;
  469. }
  470. /* the first byte of the last dword is EOF */
  471. skb_copy_bits(skb, skb->len - 4, &eof, 1);
  472. /* sets up EOF and ORIE */
  473. switch (eof) {
  474. case FC_EOF_N:
  475. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N;
  476. break;
  477. case FC_EOF_T:
  478. /* lso needs ORIE */
  479. if (skb_is_gso(skb))
  480. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N |
  481. IXGBE_ADVTXD_FCOEF_ORIE;
  482. else
  483. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_T;
  484. break;
  485. case FC_EOF_NI:
  486. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_NI;
  487. break;
  488. case FC_EOF_A:
  489. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_A;
  490. break;
  491. default:
  492. dev_warn(tx_ring->dev, "unknown eof = 0x%x\n", eof);
  493. return -EINVAL;
  494. }
  495. /* sets up PARINC indicating data offset */
  496. fh = (struct fc_frame_header *)skb_transport_header(skb);
  497. if (fh->fh_f_ctl[2] & FC_FC_REL_OFF)
  498. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_PARINC;
  499. /* include trailer in headlen as it is replicated per frame */
  500. *hdr_len = sizeof(struct fcoe_crc_eof);
  501. /* hdr_len includes fc_hdr if FCoE LSO is enabled */
  502. if (skb_is_gso(skb)) {
  503. *hdr_len += skb_transport_offset(skb) +
  504. sizeof(struct fc_frame_header);
  505. /* update gso_segs and bytecount */
  506. first->gso_segs = DIV_ROUND_UP(skb->len - *hdr_len,
  507. skb_shinfo(skb)->gso_size);
  508. first->bytecount += (first->gso_segs - 1) * *hdr_len;
  509. first->tx_flags |= IXGBE_TX_FLAGS_TSO;
  510. /* Hardware expects L4T to be RSV for FCoE TSO */
  511. type_tucmd |= IXGBE_ADVTXD_TUCMD_L4T_RSV;
  512. }
  513. /* set flag indicating FCOE to ixgbe_tx_map call */
  514. first->tx_flags |= IXGBE_TX_FLAGS_FCOE | IXGBE_TX_FLAGS_CC;
  515. /* mss_l4len_id: use 0 for FSO as TSO, no need for L4LEN */
  516. mss_l4len_idx = skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
  517. /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
  518. vlan_macip_lens = skb_transport_offset(skb) +
  519. sizeof(struct fc_frame_header);
  520. vlan_macip_lens |= (skb_transport_offset(skb) - 4)
  521. << IXGBE_ADVTXD_MACLEN_SHIFT;
  522. vlan_macip_lens |= first->tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  523. /* write context desc */
  524. ixgbe_tx_ctxtdesc(tx_ring, vlan_macip_lens, fcoe_sof_eof,
  525. type_tucmd, mss_l4len_idx);
  526. return 0;
  527. }
  528. static void ixgbe_fcoe_dma_pool_free(struct ixgbe_fcoe *fcoe, unsigned int cpu)
  529. {
  530. struct ixgbe_fcoe_ddp_pool *ddp_pool;
  531. ddp_pool = per_cpu_ptr(fcoe->ddp_pool, cpu);
  532. dma_pool_destroy(ddp_pool->pool);
  533. ddp_pool->pool = NULL;
  534. }
  535. static int ixgbe_fcoe_dma_pool_alloc(struct ixgbe_fcoe *fcoe,
  536. struct device *dev,
  537. unsigned int cpu)
  538. {
  539. struct ixgbe_fcoe_ddp_pool *ddp_pool;
  540. struct dma_pool *pool;
  541. char pool_name[32];
  542. snprintf(pool_name, 32, "ixgbe_fcoe_ddp_%u", cpu);
  543. pool = dma_pool_create(pool_name, dev, IXGBE_FCPTR_MAX,
  544. IXGBE_FCPTR_ALIGN, PAGE_SIZE);
  545. if (!pool)
  546. return -ENOMEM;
  547. ddp_pool = per_cpu_ptr(fcoe->ddp_pool, cpu);
  548. ddp_pool->pool = pool;
  549. ddp_pool->noddp = 0;
  550. ddp_pool->noddp_ext_buff = 0;
  551. return 0;
  552. }
  553. /**
  554. * ixgbe_configure_fcoe - configures registers for fcoe at start
  555. * @adapter: ptr to ixgbe adapter
  556. *
  557. * This sets up FCoE related registers
  558. *
  559. * Returns : none
  560. */
  561. void ixgbe_configure_fcoe(struct ixgbe_adapter *adapter)
  562. {
  563. struct ixgbe_ring_feature *fcoe = &adapter->ring_feature[RING_F_FCOE];
  564. struct ixgbe_hw *hw = &adapter->hw;
  565. int i, fcoe_q, fcoe_i, fcoe_q_h = 0;
  566. int fcreta_size;
  567. u32 etqf;
  568. /* Minimal functionality for FCoE requires at least CRC offloads */
  569. if (!(adapter->netdev->features & NETIF_F_FCOE_CRC))
  570. return;
  571. /* Enable L2 EtherType filter for FCoE, needed for FCoE CRC and DDP */
  572. etqf = ETH_P_FCOE | IXGBE_ETQF_FCOE | IXGBE_ETQF_FILTER_EN;
  573. if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
  574. etqf |= IXGBE_ETQF_POOL_ENABLE;
  575. etqf |= VMDQ_P(0) << IXGBE_ETQF_POOL_SHIFT;
  576. }
  577. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FCOE), etqf);
  578. IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE), 0);
  579. /* leave registers un-configured if FCoE is disabled */
  580. if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
  581. return;
  582. /* Use one or more Rx queues for FCoE by redirection table */
  583. fcreta_size = IXGBE_FCRETA_SIZE;
  584. if (adapter->hw.mac.type == ixgbe_mac_X550)
  585. fcreta_size = IXGBE_FCRETA_SIZE_X550;
  586. for (i = 0; i < fcreta_size; i++) {
  587. if (adapter->hw.mac.type == ixgbe_mac_X550) {
  588. int fcoe_i_h = fcoe->offset + ((i + fcreta_size) %
  589. fcoe->indices);
  590. fcoe_q_h = adapter->rx_ring[fcoe_i_h]->reg_idx;
  591. fcoe_q_h = FIELD_PREP(IXGBE_FCRETA_ENTRY_HIGH_MASK,
  592. fcoe_q_h);
  593. }
  594. fcoe_i = fcoe->offset + (i % fcoe->indices);
  595. fcoe_i &= IXGBE_FCRETA_ENTRY_MASK;
  596. fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
  597. fcoe_q |= fcoe_q_h;
  598. IXGBE_WRITE_REG(hw, IXGBE_FCRETA(i), fcoe_q);
  599. }
  600. IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, IXGBE_FCRECTL_ENA);
  601. /* Enable L2 EtherType filter for FIP */
  602. etqf = ETH_P_FIP | IXGBE_ETQF_FILTER_EN;
  603. if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED) {
  604. etqf |= IXGBE_ETQF_POOL_ENABLE;
  605. etqf |= VMDQ_P(0) << IXGBE_ETQF_POOL_SHIFT;
  606. }
  607. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FIP), etqf);
  608. /* Send FIP frames to the first FCoE queue */
  609. fcoe_q = adapter->rx_ring[fcoe->offset]->reg_idx;
  610. IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FIP),
  611. IXGBE_ETQS_QUEUE_EN |
  612. (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
  613. /* Configure FCoE Rx control */
  614. IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL,
  615. IXGBE_FCRXCTRL_FCCRCBO |
  616. (FC_FCOE_VER << IXGBE_FCRXCTRL_FCOEVER_SHIFT));
  617. }
  618. /**
  619. * ixgbe_free_fcoe_ddp_resources - release all fcoe ddp context resources
  620. * @adapter : ixgbe adapter
  621. *
  622. * Cleans up outstanding ddp context resources
  623. *
  624. * Returns : none
  625. */
  626. void ixgbe_free_fcoe_ddp_resources(struct ixgbe_adapter *adapter)
  627. {
  628. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  629. int cpu, i, ddp_max;
  630. /* do nothing if no DDP pools were allocated */
  631. if (!fcoe->ddp_pool)
  632. return;
  633. ddp_max = IXGBE_FCOE_DDP_MAX;
  634. /* X550 has different DDP Max limit */
  635. if (adapter->hw.mac.type == ixgbe_mac_X550)
  636. ddp_max = IXGBE_FCOE_DDP_MAX_X550;
  637. for (i = 0; i < ddp_max; i++)
  638. ixgbe_fcoe_ddp_put(adapter->netdev, i);
  639. for_each_possible_cpu(cpu)
  640. ixgbe_fcoe_dma_pool_free(fcoe, cpu);
  641. dma_unmap_single(&adapter->pdev->dev,
  642. fcoe->extra_ddp_buffer_dma,
  643. IXGBE_FCBUFF_MIN,
  644. DMA_FROM_DEVICE);
  645. kfree(fcoe->extra_ddp_buffer);
  646. fcoe->extra_ddp_buffer = NULL;
  647. fcoe->extra_ddp_buffer_dma = 0;
  648. }
  649. /**
  650. * ixgbe_setup_fcoe_ddp_resources - setup all fcoe ddp context resources
  651. * @adapter: ixgbe adapter
  652. *
  653. * Sets up ddp context resouces
  654. *
  655. * Returns : 0 indicates success or -EINVAL on failure
  656. */
  657. int ixgbe_setup_fcoe_ddp_resources(struct ixgbe_adapter *adapter)
  658. {
  659. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  660. struct device *dev = &adapter->pdev->dev;
  661. void *buffer;
  662. dma_addr_t dma;
  663. unsigned int cpu;
  664. /* do nothing if no DDP pools were allocated */
  665. if (!fcoe->ddp_pool)
  666. return 0;
  667. /* Extra buffer to be shared by all DDPs for HW work around */
  668. buffer = kmalloc(IXGBE_FCBUFF_MIN, GFP_KERNEL);
  669. if (!buffer)
  670. return -ENOMEM;
  671. dma = dma_map_single(dev, buffer, IXGBE_FCBUFF_MIN, DMA_FROM_DEVICE);
  672. if (dma_mapping_error(dev, dma)) {
  673. e_err(drv, "failed to map extra DDP buffer\n");
  674. kfree(buffer);
  675. return -ENOMEM;
  676. }
  677. fcoe->extra_ddp_buffer = buffer;
  678. fcoe->extra_ddp_buffer_dma = dma;
  679. /* allocate pci pool for each cpu */
  680. for_each_possible_cpu(cpu) {
  681. int err = ixgbe_fcoe_dma_pool_alloc(fcoe, dev, cpu);
  682. if (!err)
  683. continue;
  684. e_err(drv, "failed to alloc DDP pool on cpu:%d\n", cpu);
  685. ixgbe_free_fcoe_ddp_resources(adapter);
  686. return -ENOMEM;
  687. }
  688. return 0;
  689. }
  690. static int ixgbe_fcoe_ddp_enable(struct ixgbe_adapter *adapter)
  691. {
  692. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  693. if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
  694. return -EINVAL;
  695. fcoe->ddp_pool = alloc_percpu(struct ixgbe_fcoe_ddp_pool);
  696. if (!fcoe->ddp_pool) {
  697. e_err(drv, "failed to allocate percpu DDP resources\n");
  698. return -ENOMEM;
  699. }
  700. adapter->netdev->fcoe_ddp_xid = IXGBE_FCOE_DDP_MAX - 1;
  701. /* X550 has different DDP Max limit */
  702. if (adapter->hw.mac.type == ixgbe_mac_X550)
  703. adapter->netdev->fcoe_ddp_xid = IXGBE_FCOE_DDP_MAX_X550 - 1;
  704. return 0;
  705. }
  706. static void ixgbe_fcoe_ddp_disable(struct ixgbe_adapter *adapter)
  707. {
  708. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  709. adapter->netdev->fcoe_ddp_xid = 0;
  710. if (!fcoe->ddp_pool)
  711. return;
  712. free_percpu(fcoe->ddp_pool);
  713. fcoe->ddp_pool = NULL;
  714. }
  715. /**
  716. * ixgbe_fcoe_enable - turn on FCoE offload feature
  717. * @netdev: the corresponding netdev
  718. *
  719. * Turns on FCoE offload feature in 82599.
  720. *
  721. * Returns : 0 indicates success or -EINVAL on failure
  722. */
  723. int ixgbe_fcoe_enable(struct net_device *netdev)
  724. {
  725. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  726. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  727. atomic_inc(&fcoe->refcnt);
  728. if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
  729. return -EINVAL;
  730. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
  731. return -EINVAL;
  732. e_info(drv, "Enabling FCoE offload features.\n");
  733. if (adapter->flags & IXGBE_FLAG_SRIOV_ENABLED)
  734. e_warn(probe, "Enabling FCoE on PF will disable legacy VFs\n");
  735. if (netif_running(netdev))
  736. netdev->netdev_ops->ndo_stop(netdev);
  737. /* Allocate per CPU memory to track DDP pools */
  738. ixgbe_fcoe_ddp_enable(adapter);
  739. /* enable FCoE and notify stack */
  740. adapter->flags |= IXGBE_FLAG_FCOE_ENABLED;
  741. netdev->fcoe_mtu = true;
  742. netdev_features_change(netdev);
  743. /* release existing queues and reallocate them */
  744. ixgbe_clear_interrupt_scheme(adapter);
  745. ixgbe_init_interrupt_scheme(adapter);
  746. if (netif_running(netdev))
  747. netdev->netdev_ops->ndo_open(netdev);
  748. return 0;
  749. }
  750. /**
  751. * ixgbe_fcoe_disable - turn off FCoE offload feature
  752. * @netdev: the corresponding netdev
  753. *
  754. * Turns off FCoE offload feature in 82599.
  755. *
  756. * Returns : 0 indicates success or -EINVAL on failure
  757. */
  758. int ixgbe_fcoe_disable(struct net_device *netdev)
  759. {
  760. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  761. if (!atomic_dec_and_test(&adapter->fcoe.refcnt))
  762. return -EINVAL;
  763. if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
  764. return -EINVAL;
  765. e_info(drv, "Disabling FCoE offload features.\n");
  766. if (netif_running(netdev))
  767. netdev->netdev_ops->ndo_stop(netdev);
  768. /* Free per CPU memory to track DDP pools */
  769. ixgbe_fcoe_ddp_disable(adapter);
  770. /* disable FCoE and notify stack */
  771. adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
  772. netdev->fcoe_mtu = false;
  773. netdev_features_change(netdev);
  774. /* release existing queues and reallocate them */
  775. ixgbe_clear_interrupt_scheme(adapter);
  776. ixgbe_init_interrupt_scheme(adapter);
  777. if (netif_running(netdev))
  778. netdev->netdev_ops->ndo_open(netdev);
  779. return 0;
  780. }
  781. /**
  782. * ixgbe_fcoe_get_wwn - get world wide name for the node or the port
  783. * @netdev : ixgbe adapter
  784. * @wwn : the world wide name
  785. * @type: the type of world wide name
  786. *
  787. * Returns the node or port world wide name if both the prefix and the san
  788. * mac address are valid, then the wwn is formed based on the NAA-2 for
  789. * IEEE Extended name identifier (ref. to T10 FC-LS Spec., Sec. 15.3).
  790. *
  791. * Returns : 0 on success
  792. */
  793. int ixgbe_fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
  794. {
  795. u16 prefix = 0xffff;
  796. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  797. struct ixgbe_mac_info *mac = &adapter->hw.mac;
  798. switch (type) {
  799. case NETDEV_FCOE_WWNN:
  800. prefix = mac->wwnn_prefix;
  801. break;
  802. case NETDEV_FCOE_WWPN:
  803. prefix = mac->wwpn_prefix;
  804. break;
  805. default:
  806. break;
  807. }
  808. if ((prefix != 0xffff) &&
  809. is_valid_ether_addr(mac->san_addr)) {
  810. *wwn = ((u64) prefix << 48) |
  811. ((u64) mac->san_addr[0] << 40) |
  812. ((u64) mac->san_addr[1] << 32) |
  813. ((u64) mac->san_addr[2] << 24) |
  814. ((u64) mac->san_addr[3] << 16) |
  815. ((u64) mac->san_addr[4] << 8) |
  816. ((u64) mac->san_addr[5]);
  817. return 0;
  818. }
  819. return -EINVAL;
  820. }
  821. /**
  822. * ixgbe_fcoe_get_hbainfo - get FCoE HBA information
  823. * @netdev : ixgbe adapter
  824. * @info : HBA information
  825. *
  826. * Returns ixgbe HBA information
  827. *
  828. * Returns : 0 on success
  829. */
  830. int ixgbe_fcoe_get_hbainfo(struct net_device *netdev,
  831. struct netdev_fcoe_hbainfo *info)
  832. {
  833. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  834. struct ixgbe_hw *hw = &adapter->hw;
  835. u64 dsn;
  836. if (!info)
  837. return -EINVAL;
  838. /* Don't return information on unsupported devices */
  839. if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
  840. return -EINVAL;
  841. /* Manufacturer */
  842. snprintf(info->manufacturer, sizeof(info->manufacturer),
  843. "Intel Corporation");
  844. /* Serial Number */
  845. /* Get the PCI-e Device Serial Number Capability */
  846. dsn = pci_get_dsn(adapter->pdev);
  847. if (dsn)
  848. snprintf(info->serial_number, sizeof(info->serial_number),
  849. "%016llX", dsn);
  850. else
  851. snprintf(info->serial_number, sizeof(info->serial_number),
  852. "Unknown");
  853. /* Hardware Version */
  854. snprintf(info->hardware_version,
  855. sizeof(info->hardware_version),
  856. "Rev %d", hw->revision_id);
  857. /* Driver Name/Version */
  858. snprintf(info->driver_version,
  859. sizeof(info->driver_version),
  860. "%s v%s",
  861. ixgbe_driver_name,
  862. UTS_RELEASE);
  863. /* Firmware Version */
  864. strscpy(info->firmware_version, adapter->eeprom_id,
  865. sizeof(info->firmware_version));
  866. /* Model */
  867. if (hw->mac.type == ixgbe_mac_82599EB) {
  868. snprintf(info->model,
  869. sizeof(info->model),
  870. "Intel 82599");
  871. } else if (hw->mac.type == ixgbe_mac_X550) {
  872. snprintf(info->model,
  873. sizeof(info->model),
  874. "Intel X550");
  875. } else {
  876. snprintf(info->model,
  877. sizeof(info->model),
  878. "Intel X540");
  879. }
  880. /* Model Description */
  881. snprintf(info->model_description,
  882. sizeof(info->model_description),
  883. "%s",
  884. ixgbe_default_device_descr);
  885. return 0;
  886. }
  887. /**
  888. * ixgbe_fcoe_get_tc - get the current TC that fcoe is mapped to
  889. * @adapter: pointer to the device adapter structure
  890. *
  891. * Return : TC that FCoE is mapped to
  892. */
  893. u8 ixgbe_fcoe_get_tc(struct ixgbe_adapter *adapter)
  894. {
  895. #ifdef CONFIG_IXGBE_DCB
  896. return netdev_get_prio_tc_map(adapter->netdev, adapter->fcoe.up);
  897. #else
  898. return 0;
  899. #endif
  900. }