ixgbe_fcoe.c 29 KB

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