ixgbe_fcoe.c 28 KB

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