ixgbe_fcoe.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2011 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. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include "ixgbe.h"
  21. #include <linux/if_ether.h>
  22. #include <linux/gfp.h>
  23. #include <linux/if_vlan.h>
  24. #include <scsi/scsi_cmnd.h>
  25. #include <scsi/scsi_device.h>
  26. #include <scsi/fc/fc_fs.h>
  27. #include <scsi/fc/fc_fcoe.h>
  28. #include <scsi/libfc.h>
  29. #include <scsi/libfcoe.h>
  30. /**
  31. * ixgbe_fcoe_clear_ddp - clear the given ddp context
  32. * @ddp - ptr to the ixgbe_fcoe_ddp
  33. *
  34. * Returns : none
  35. *
  36. */
  37. static inline void ixgbe_fcoe_clear_ddp(struct ixgbe_fcoe_ddp *ddp)
  38. {
  39. ddp->len = 0;
  40. ddp->err = 1;
  41. ddp->udl = NULL;
  42. ddp->udp = 0UL;
  43. ddp->sgl = NULL;
  44. ddp->sgc = 0;
  45. }
  46. /**
  47. * ixgbe_fcoe_ddp_put - free the ddp context for a given xid
  48. * @netdev: the corresponding net_device
  49. * @xid: the xid that corresponding ddp will be freed
  50. *
  51. * This is the implementation of net_device_ops.ndo_fcoe_ddp_done
  52. * and it is expected to be called by ULD, i.e., FCP layer of libfc
  53. * to release the corresponding ddp context when the I/O is done.
  54. *
  55. * Returns : data length already ddp-ed in bytes
  56. */
  57. int ixgbe_fcoe_ddp_put(struct net_device *netdev, u16 xid)
  58. {
  59. int len = 0;
  60. struct ixgbe_fcoe *fcoe;
  61. struct ixgbe_adapter *adapter;
  62. struct ixgbe_fcoe_ddp *ddp;
  63. u32 fcbuff;
  64. if (!netdev)
  65. goto out_ddp_put;
  66. if (xid >= IXGBE_FCOE_DDP_MAX)
  67. goto out_ddp_put;
  68. adapter = netdev_priv(netdev);
  69. fcoe = &adapter->fcoe;
  70. ddp = &fcoe->ddp[xid];
  71. if (!ddp->udl)
  72. goto out_ddp_put;
  73. len = ddp->len;
  74. /* if there an error, force to invalidate ddp context */
  75. if (ddp->err) {
  76. spin_lock_bh(&fcoe->lock);
  77. IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCFLT, 0);
  78. IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCFLTRW,
  79. (xid | IXGBE_FCFLTRW_WE));
  80. IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCBUFF, 0);
  81. IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCDMARW,
  82. (xid | IXGBE_FCDMARW_WE));
  83. /* guaranteed to be invalidated after 100us */
  84. IXGBE_WRITE_REG(&adapter->hw, IXGBE_FCDMARW,
  85. (xid | IXGBE_FCDMARW_RE));
  86. fcbuff = IXGBE_READ_REG(&adapter->hw, IXGBE_FCBUFF);
  87. spin_unlock_bh(&fcoe->lock);
  88. if (fcbuff & IXGBE_FCBUFF_VALID)
  89. udelay(100);
  90. }
  91. if (ddp->sgl)
  92. pci_unmap_sg(adapter->pdev, ddp->sgl, ddp->sgc,
  93. DMA_FROM_DEVICE);
  94. if (ddp->pool) {
  95. pci_pool_free(ddp->pool, ddp->udl, ddp->udp);
  96. ddp->pool = NULL;
  97. }
  98. ixgbe_fcoe_clear_ddp(ddp);
  99. out_ddp_put:
  100. return len;
  101. }
  102. /**
  103. * ixgbe_fcoe_ddp_setup - called to set up ddp context
  104. * @netdev: the corresponding net_device
  105. * @xid: the exchange id requesting ddp
  106. * @sgl: the scatter-gather list for this request
  107. * @sgc: the number of scatter-gather items
  108. *
  109. * Returns : 1 for success and 0 for no ddp
  110. */
  111. static int ixgbe_fcoe_ddp_setup(struct net_device *netdev, u16 xid,
  112. struct scatterlist *sgl, unsigned int sgc,
  113. int target_mode)
  114. {
  115. struct ixgbe_adapter *adapter;
  116. struct ixgbe_hw *hw;
  117. struct ixgbe_fcoe *fcoe;
  118. struct ixgbe_fcoe_ddp *ddp;
  119. struct scatterlist *sg;
  120. unsigned int i, j, dmacount;
  121. unsigned int len;
  122. static const unsigned int bufflen = IXGBE_FCBUFF_MIN;
  123. unsigned int firstoff = 0;
  124. unsigned int lastsize;
  125. unsigned int thisoff = 0;
  126. unsigned int thislen = 0;
  127. u32 fcbuff, fcdmarw, fcfltrw, fcrxctl;
  128. dma_addr_t addr = 0;
  129. struct pci_pool *pool;
  130. if (!netdev || !sgl)
  131. return 0;
  132. adapter = netdev_priv(netdev);
  133. if (xid >= IXGBE_FCOE_DDP_MAX) {
  134. e_warn(drv, "xid=0x%x out-of-range\n", xid);
  135. return 0;
  136. }
  137. /* no DDP if we are already down or resetting */
  138. if (test_bit(__IXGBE_DOWN, &adapter->state) ||
  139. test_bit(__IXGBE_RESETTING, &adapter->state))
  140. return 0;
  141. fcoe = &adapter->fcoe;
  142. if (!fcoe->pool) {
  143. e_warn(drv, "xid=0x%x no ddp pool for fcoe\n", xid);
  144. return 0;
  145. }
  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. /* setup dma from scsi command sgl */
  154. dmacount = pci_map_sg(adapter->pdev, sgl, sgc, DMA_FROM_DEVICE);
  155. if (dmacount == 0) {
  156. e_err(drv, "xid 0x%x DMA map error\n", xid);
  157. return 0;
  158. }
  159. /* alloc the udl from per cpu ddp pool */
  160. pool = *per_cpu_ptr(fcoe->pool, get_cpu());
  161. ddp->udl = pci_pool_alloc(pool, GFP_ATOMIC, &ddp->udp);
  162. if (!ddp->udl) {
  163. e_err(drv, "failed allocated ddp context\n");
  164. goto out_noddp_unmap;
  165. }
  166. ddp->pool = pool;
  167. ddp->sgl = sgl;
  168. ddp->sgc = sgc;
  169. j = 0;
  170. for_each_sg(sgl, sg, dmacount, i) {
  171. addr = sg_dma_address(sg);
  172. len = sg_dma_len(sg);
  173. while (len) {
  174. /* max number of buffers allowed in one DDP context */
  175. if (j >= IXGBE_BUFFCNT_MAX) {
  176. e_err(drv, "xid=%x:%d,%d,%d:addr=%llx "
  177. "not enough descriptors\n",
  178. xid, i, j, dmacount, (u64)addr);
  179. goto out_noddp_free;
  180. }
  181. /* get the offset of length of current buffer */
  182. thisoff = addr & ((dma_addr_t)bufflen - 1);
  183. thislen = min((bufflen - thisoff), len);
  184. /*
  185. * all but the 1st buffer (j == 0)
  186. * must be aligned on bufflen
  187. */
  188. if ((j != 0) && (thisoff))
  189. goto out_noddp_free;
  190. /*
  191. * all but the last buffer
  192. * ((i == (dmacount - 1)) && (thislen == len))
  193. * must end at bufflen
  194. */
  195. if (((i != (dmacount - 1)) || (thislen != len))
  196. && ((thislen + thisoff) != bufflen))
  197. goto out_noddp_free;
  198. ddp->udl[j] = (u64)(addr - thisoff);
  199. /* only the first buffer may have none-zero offset */
  200. if (j == 0)
  201. firstoff = thisoff;
  202. len -= thislen;
  203. addr += thislen;
  204. j++;
  205. }
  206. }
  207. /* only the last buffer may have non-full bufflen */
  208. lastsize = thisoff + thislen;
  209. /*
  210. * lastsize can not be buffer len.
  211. * If it is then adding another buffer with lastsize = 1.
  212. */
  213. if (lastsize == bufflen) {
  214. if (j >= IXGBE_BUFFCNT_MAX) {
  215. e_err(drv, "xid=%x:%d,%d,%d:addr=%llx "
  216. "not enough user buffers. We need an extra "
  217. "buffer because lastsize is bufflen.\n",
  218. xid, i, j, dmacount, (u64)addr);
  219. goto out_noddp_free;
  220. }
  221. ddp->udl[j] = (u64)(fcoe->extra_ddp_buffer_dma);
  222. j++;
  223. lastsize = 1;
  224. }
  225. put_cpu();
  226. fcbuff = (IXGBE_FCBUFF_4KB << IXGBE_FCBUFF_BUFFSIZE_SHIFT);
  227. fcbuff |= ((j & 0xff) << IXGBE_FCBUFF_BUFFCNT_SHIFT);
  228. fcbuff |= (firstoff << IXGBE_FCBUFF_OFFSET_SHIFT);
  229. /* Set WRCONTX bit to allow DDP for target */
  230. if (target_mode)
  231. fcbuff |= (IXGBE_FCBUFF_WRCONTX);
  232. fcbuff |= (IXGBE_FCBUFF_VALID);
  233. fcdmarw = xid;
  234. fcdmarw |= IXGBE_FCDMARW_WE;
  235. fcdmarw |= (lastsize << IXGBE_FCDMARW_LASTSIZE_SHIFT);
  236. fcfltrw = xid;
  237. fcfltrw |= IXGBE_FCFLTRW_WE;
  238. /* program DMA context */
  239. hw = &adapter->hw;
  240. spin_lock_bh(&fcoe->lock);
  241. /* turn on last frame indication for target mode as FCP_RSPtarget is
  242. * supposed to send FCP_RSP when it is done. */
  243. if (target_mode && !test_bit(__IXGBE_FCOE_TARGET, &fcoe->mode)) {
  244. set_bit(__IXGBE_FCOE_TARGET, &fcoe->mode);
  245. fcrxctl = IXGBE_READ_REG(hw, IXGBE_FCRXCTRL);
  246. fcrxctl |= IXGBE_FCRXCTRL_LASTSEQH;
  247. IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL, fcrxctl);
  248. }
  249. IXGBE_WRITE_REG(hw, IXGBE_FCPTRL, ddp->udp & DMA_BIT_MASK(32));
  250. IXGBE_WRITE_REG(hw, IXGBE_FCPTRH, (u64)ddp->udp >> 32);
  251. IXGBE_WRITE_REG(hw, IXGBE_FCBUFF, fcbuff);
  252. IXGBE_WRITE_REG(hw, IXGBE_FCDMARW, fcdmarw);
  253. /* program filter context */
  254. IXGBE_WRITE_REG(hw, IXGBE_FCPARAM, 0);
  255. IXGBE_WRITE_REG(hw, IXGBE_FCFLT, IXGBE_FCFLT_VALID);
  256. IXGBE_WRITE_REG(hw, IXGBE_FCFLTRW, fcfltrw);
  257. spin_unlock_bh(&fcoe->lock);
  258. return 1;
  259. out_noddp_free:
  260. pci_pool_free(pool, ddp->udl, ddp->udp);
  261. ixgbe_fcoe_clear_ddp(ddp);
  262. out_noddp_unmap:
  263. pci_unmap_sg(adapter->pdev, sgl, sgc, DMA_FROM_DEVICE);
  264. put_cpu();
  265. return 0;
  266. }
  267. /**
  268. * ixgbe_fcoe_ddp_get - called to set up ddp context in initiator mode
  269. * @netdev: the corresponding net_device
  270. * @xid: the exchange id requesting ddp
  271. * @sgl: the scatter-gather list for this request
  272. * @sgc: the number of scatter-gather items
  273. *
  274. * This is the implementation of net_device_ops.ndo_fcoe_ddp_setup
  275. * and is expected to be called from ULD, e.g., FCP layer of libfc
  276. * to set up ddp for the corresponding xid of the given sglist for
  277. * the corresponding I/O.
  278. *
  279. * Returns : 1 for success and 0 for no ddp
  280. */
  281. int ixgbe_fcoe_ddp_get(struct net_device *netdev, u16 xid,
  282. struct scatterlist *sgl, unsigned int sgc)
  283. {
  284. return ixgbe_fcoe_ddp_setup(netdev, xid, sgl, sgc, 0);
  285. }
  286. /**
  287. * ixgbe_fcoe_ddp_target - called to set up ddp context in target mode
  288. * @netdev: the corresponding net_device
  289. * @xid: the exchange id requesting ddp
  290. * @sgl: the scatter-gather list for this request
  291. * @sgc: the number of scatter-gather items
  292. *
  293. * This is the implementation of net_device_ops.ndo_fcoe_ddp_target
  294. * and is expected to be called from ULD, e.g., FCP layer of libfc
  295. * to set up ddp for the corresponding xid of the given sglist for
  296. * the corresponding I/O. The DDP in target mode is a write I/O request
  297. * from the initiator.
  298. *
  299. * Returns : 1 for success and 0 for no ddp
  300. */
  301. int ixgbe_fcoe_ddp_target(struct net_device *netdev, u16 xid,
  302. struct scatterlist *sgl, unsigned int sgc)
  303. {
  304. return ixgbe_fcoe_ddp_setup(netdev, xid, sgl, sgc, 1);
  305. }
  306. /**
  307. * ixgbe_fcoe_ddp - check ddp status and mark it done
  308. * @adapter: ixgbe adapter
  309. * @rx_desc: advanced rx descriptor
  310. * @skb: the skb holding the received data
  311. *
  312. * This checks ddp status.
  313. *
  314. * Returns : < 0 indicates an error or not a FCiE ddp, 0 indicates
  315. * not passing the skb to ULD, > 0 indicates is the length of data
  316. * being ddped.
  317. */
  318. int ixgbe_fcoe_ddp(struct ixgbe_adapter *adapter,
  319. union ixgbe_adv_rx_desc *rx_desc,
  320. struct sk_buff *skb,
  321. u32 staterr)
  322. {
  323. u16 xid;
  324. u32 fctl;
  325. u32 fceofe, fcerr, fcstat;
  326. int rc = -EINVAL;
  327. struct ixgbe_fcoe *fcoe;
  328. struct ixgbe_fcoe_ddp *ddp;
  329. struct fc_frame_header *fh;
  330. struct fcoe_crc_eof *crc;
  331. fcerr = (staterr & IXGBE_RXDADV_ERR_FCERR);
  332. fceofe = (staterr & IXGBE_RXDADV_ERR_FCEOFE);
  333. if (fcerr == IXGBE_FCERR_BADCRC)
  334. skb_checksum_none_assert(skb);
  335. else
  336. skb->ip_summed = CHECKSUM_UNNECESSARY;
  337. if (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
  338. fh = (struct fc_frame_header *)(skb->data +
  339. sizeof(struct vlan_hdr) + sizeof(struct fcoe_hdr));
  340. else
  341. fh = (struct fc_frame_header *)(skb->data +
  342. sizeof(struct fcoe_hdr));
  343. fctl = ntoh24(fh->fh_f_ctl);
  344. if (fctl & FC_FC_EX_CTX)
  345. xid = be16_to_cpu(fh->fh_ox_id);
  346. else
  347. xid = be16_to_cpu(fh->fh_rx_id);
  348. if (xid >= IXGBE_FCOE_DDP_MAX)
  349. goto ddp_out;
  350. fcoe = &adapter->fcoe;
  351. ddp = &fcoe->ddp[xid];
  352. if (!ddp->udl)
  353. goto ddp_out;
  354. if (fcerr | fceofe)
  355. goto ddp_out;
  356. fcstat = (staterr & IXGBE_RXDADV_STAT_FCSTAT);
  357. if (fcstat) {
  358. /* update length of DDPed data */
  359. ddp->len = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
  360. /* unmap the sg list when FCP_RSP is received */
  361. if (fcstat == IXGBE_RXDADV_STAT_FCSTAT_FCPRSP) {
  362. pci_unmap_sg(adapter->pdev, ddp->sgl,
  363. ddp->sgc, DMA_FROM_DEVICE);
  364. ddp->err = (fcerr | fceofe);
  365. ddp->sgl = NULL;
  366. ddp->sgc = 0;
  367. }
  368. /* return 0 to bypass going to ULD for DDPed data */
  369. if (fcstat == IXGBE_RXDADV_STAT_FCSTAT_DDP)
  370. rc = 0;
  371. else if (ddp->len)
  372. rc = ddp->len;
  373. }
  374. /* In target mode, check the last data frame of the sequence.
  375. * For DDP in target mode, data is already DDPed but the header
  376. * indication of the last data frame ould allow is to tell if we
  377. * got all the data and the ULP can send FCP_RSP back, as this is
  378. * not a full fcoe frame, we fill the trailer here so it won't be
  379. * dropped by the ULP stack.
  380. */
  381. if ((fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA) &&
  382. (fctl & FC_FC_END_SEQ)) {
  383. crc = (struct fcoe_crc_eof *)skb_put(skb, sizeof(*crc));
  384. crc->fcoe_eof = FC_EOF_T;
  385. }
  386. ddp_out:
  387. return rc;
  388. }
  389. /**
  390. * ixgbe_fso - ixgbe FCoE Sequence Offload (FSO)
  391. * @adapter: ixgbe adapter
  392. * @tx_ring: tx desc ring
  393. * @skb: associated skb
  394. * @tx_flags: tx flags
  395. * @hdr_len: hdr_len to be returned
  396. *
  397. * This sets up large send offload for FCoE
  398. *
  399. * Returns : 0 indicates no FSO, > 0 for FSO, < 0 for error
  400. */
  401. int ixgbe_fso(struct ixgbe_ring *tx_ring, struct sk_buff *skb,
  402. u32 tx_flags, u8 *hdr_len)
  403. {
  404. struct fc_frame_header *fh;
  405. u32 vlan_macip_lens;
  406. u32 fcoe_sof_eof = 0;
  407. u32 mss_l4len_idx;
  408. u8 sof, eof;
  409. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type != SKB_GSO_FCOE)) {
  410. dev_err(tx_ring->dev, "Wrong gso type %d:expecting SKB_GSO_FCOE\n",
  411. skb_shinfo(skb)->gso_type);
  412. return -EINVAL;
  413. }
  414. /* resets the header to point fcoe/fc */
  415. skb_set_network_header(skb, skb->mac_len);
  416. skb_set_transport_header(skb, skb->mac_len +
  417. sizeof(struct fcoe_hdr));
  418. /* sets up SOF and ORIS */
  419. sof = ((struct fcoe_hdr *)skb_network_header(skb))->fcoe_sof;
  420. switch (sof) {
  421. case FC_SOF_I2:
  422. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_ORIS;
  423. break;
  424. case FC_SOF_I3:
  425. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_SOF |
  426. IXGBE_ADVTXD_FCOEF_ORIS;
  427. break;
  428. case FC_SOF_N2:
  429. break;
  430. case FC_SOF_N3:
  431. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_SOF;
  432. break;
  433. default:
  434. dev_warn(tx_ring->dev, "unknown sof = 0x%x\n", sof);
  435. return -EINVAL;
  436. }
  437. /* the first byte of the last dword is EOF */
  438. skb_copy_bits(skb, skb->len - 4, &eof, 1);
  439. /* sets up EOF and ORIE */
  440. switch (eof) {
  441. case FC_EOF_N:
  442. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N;
  443. break;
  444. case FC_EOF_T:
  445. /* lso needs ORIE */
  446. if (skb_is_gso(skb))
  447. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N |
  448. IXGBE_ADVTXD_FCOEF_ORIE;
  449. else
  450. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_T;
  451. break;
  452. case FC_EOF_NI:
  453. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_NI;
  454. break;
  455. case FC_EOF_A:
  456. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_A;
  457. break;
  458. default:
  459. dev_warn(tx_ring->dev, "unknown eof = 0x%x\n", eof);
  460. return -EINVAL;
  461. }
  462. /* sets up PARINC indicating data offset */
  463. fh = (struct fc_frame_header *)skb_transport_header(skb);
  464. if (fh->fh_f_ctl[2] & FC_FC_REL_OFF)
  465. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_PARINC;
  466. /* include trailer in headlen as it is replicated per frame */
  467. *hdr_len = sizeof(struct fcoe_crc_eof);
  468. /* hdr_len includes fc_hdr if FCoE LSO is enabled */
  469. if (skb_is_gso(skb))
  470. *hdr_len += (skb_transport_offset(skb) +
  471. sizeof(struct fc_frame_header));
  472. /* mss_l4len_id: use 1 for FSO as TSO, no need for L4LEN */
  473. mss_l4len_idx = skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
  474. mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
  475. /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
  476. vlan_macip_lens = skb_transport_offset(skb) +
  477. sizeof(struct fc_frame_header);
  478. vlan_macip_lens |= (skb_transport_offset(skb) - 4)
  479. << IXGBE_ADVTXD_MACLEN_SHIFT;
  480. vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  481. /* write context desc */
  482. ixgbe_tx_ctxtdesc(tx_ring, vlan_macip_lens, fcoe_sof_eof,
  483. IXGBE_ADVTXT_TUCMD_FCOE, mss_l4len_idx);
  484. return skb_is_gso(skb);
  485. }
  486. static void ixgbe_fcoe_ddp_pools_free(struct ixgbe_fcoe *fcoe)
  487. {
  488. unsigned int cpu;
  489. struct pci_pool **pool;
  490. for_each_possible_cpu(cpu) {
  491. pool = per_cpu_ptr(fcoe->pool, cpu);
  492. if (*pool)
  493. pci_pool_destroy(*pool);
  494. }
  495. free_percpu(fcoe->pool);
  496. fcoe->pool = NULL;
  497. }
  498. static void ixgbe_fcoe_ddp_pools_alloc(struct ixgbe_adapter *adapter)
  499. {
  500. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  501. unsigned int cpu;
  502. struct pci_pool **pool;
  503. char pool_name[32];
  504. fcoe->pool = alloc_percpu(struct pci_pool *);
  505. if (!fcoe->pool)
  506. return;
  507. /* allocate pci pool for each cpu */
  508. for_each_possible_cpu(cpu) {
  509. snprintf(pool_name, 32, "ixgbe_fcoe_ddp_%d", cpu);
  510. pool = per_cpu_ptr(fcoe->pool, cpu);
  511. *pool = pci_pool_create(pool_name,
  512. adapter->pdev, IXGBE_FCPTR_MAX,
  513. IXGBE_FCPTR_ALIGN, PAGE_SIZE);
  514. if (!*pool) {
  515. e_err(drv, "failed to alloc DDP pool on cpu:%d\n", cpu);
  516. ixgbe_fcoe_ddp_pools_free(fcoe);
  517. return;
  518. }
  519. }
  520. }
  521. /**
  522. * ixgbe_configure_fcoe - configures registers for fcoe at start
  523. * @adapter: ptr to ixgbe adapter
  524. *
  525. * This sets up FCoE related registers
  526. *
  527. * Returns : none
  528. */
  529. void ixgbe_configure_fcoe(struct ixgbe_adapter *adapter)
  530. {
  531. int i, fcoe_q, fcoe_i;
  532. struct ixgbe_hw *hw = &adapter->hw;
  533. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  534. struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_FCOE];
  535. if (!fcoe->pool) {
  536. spin_lock_init(&fcoe->lock);
  537. ixgbe_fcoe_ddp_pools_alloc(adapter);
  538. if (!fcoe->pool) {
  539. e_err(drv, "failed to alloc percpu fcoe DDP pools\n");
  540. return;
  541. }
  542. /* Extra buffer to be shared by all DDPs for HW work around */
  543. fcoe->extra_ddp_buffer = kmalloc(IXGBE_FCBUFF_MIN, GFP_ATOMIC);
  544. if (fcoe->extra_ddp_buffer == NULL) {
  545. e_err(drv, "failed to allocated extra DDP buffer\n");
  546. goto out_ddp_pools;
  547. }
  548. fcoe->extra_ddp_buffer_dma =
  549. dma_map_single(&adapter->pdev->dev,
  550. fcoe->extra_ddp_buffer,
  551. IXGBE_FCBUFF_MIN,
  552. DMA_FROM_DEVICE);
  553. if (dma_mapping_error(&adapter->pdev->dev,
  554. fcoe->extra_ddp_buffer_dma)) {
  555. e_err(drv, "failed to map extra DDP buffer\n");
  556. goto out_extra_ddp_buffer;
  557. }
  558. }
  559. /* Enable L2 eth type filter for FCoE */
  560. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FCOE),
  561. (ETH_P_FCOE | IXGBE_ETQF_FCOE | IXGBE_ETQF_FILTER_EN));
  562. /* Enable L2 eth type filter for FIP */
  563. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FIP),
  564. (ETH_P_FIP | IXGBE_ETQF_FILTER_EN));
  565. if (adapter->ring_feature[RING_F_FCOE].indices) {
  566. /* Use multiple rx queues for FCoE by redirection table */
  567. for (i = 0; i < IXGBE_FCRETA_SIZE; i++) {
  568. fcoe_i = f->mask + i % f->indices;
  569. fcoe_i &= IXGBE_FCRETA_ENTRY_MASK;
  570. fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
  571. IXGBE_WRITE_REG(hw, IXGBE_FCRETA(i), fcoe_q);
  572. }
  573. IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, IXGBE_FCRECTL_ENA);
  574. IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE), 0);
  575. } else {
  576. /* Use single rx queue for FCoE */
  577. fcoe_i = f->mask;
  578. fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
  579. IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, 0);
  580. IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE),
  581. IXGBE_ETQS_QUEUE_EN |
  582. (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
  583. }
  584. /* send FIP frames to the first FCoE queue */
  585. fcoe_i = f->mask;
  586. fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
  587. IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FIP),
  588. IXGBE_ETQS_QUEUE_EN |
  589. (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
  590. IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL,
  591. IXGBE_FCRXCTRL_FCOELLI |
  592. IXGBE_FCRXCTRL_FCCRCBO |
  593. (FC_FCOE_VER << IXGBE_FCRXCTRL_FCOEVER_SHIFT));
  594. return;
  595. out_extra_ddp_buffer:
  596. kfree(fcoe->extra_ddp_buffer);
  597. out_ddp_pools:
  598. ixgbe_fcoe_ddp_pools_free(fcoe);
  599. }
  600. /**
  601. * ixgbe_cleanup_fcoe - release all fcoe ddp context resources
  602. * @adapter : ixgbe adapter
  603. *
  604. * Cleans up outstanding ddp context resources
  605. *
  606. * Returns : none
  607. */
  608. void ixgbe_cleanup_fcoe(struct ixgbe_adapter *adapter)
  609. {
  610. int i;
  611. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  612. if (!fcoe->pool)
  613. return;
  614. for (i = 0; i < IXGBE_FCOE_DDP_MAX; i++)
  615. ixgbe_fcoe_ddp_put(adapter->netdev, i);
  616. dma_unmap_single(&adapter->pdev->dev,
  617. fcoe->extra_ddp_buffer_dma,
  618. IXGBE_FCBUFF_MIN,
  619. DMA_FROM_DEVICE);
  620. kfree(fcoe->extra_ddp_buffer);
  621. ixgbe_fcoe_ddp_pools_free(fcoe);
  622. }
  623. /**
  624. * ixgbe_fcoe_enable - turn on FCoE offload feature
  625. * @netdev: the corresponding netdev
  626. *
  627. * Turns on FCoE offload feature in 82599.
  628. *
  629. * Returns : 0 indicates success or -EINVAL on failure
  630. */
  631. int ixgbe_fcoe_enable(struct net_device *netdev)
  632. {
  633. int rc = -EINVAL;
  634. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  635. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  636. if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
  637. goto out_enable;
  638. atomic_inc(&fcoe->refcnt);
  639. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
  640. goto out_enable;
  641. e_info(drv, "Enabling FCoE offload features.\n");
  642. if (netif_running(netdev))
  643. netdev->netdev_ops->ndo_stop(netdev);
  644. ixgbe_clear_interrupt_scheme(adapter);
  645. adapter->flags |= IXGBE_FLAG_FCOE_ENABLED;
  646. adapter->ring_feature[RING_F_FCOE].indices = IXGBE_FCRETA_SIZE;
  647. netdev->features |= NETIF_F_FCOE_CRC;
  648. netdev->features |= NETIF_F_FSO;
  649. netdev->features |= NETIF_F_FCOE_MTU;
  650. netdev->fcoe_ddp_xid = IXGBE_FCOE_DDP_MAX - 1;
  651. ixgbe_init_interrupt_scheme(adapter);
  652. netdev_features_change(netdev);
  653. if (netif_running(netdev))
  654. netdev->netdev_ops->ndo_open(netdev);
  655. rc = 0;
  656. out_enable:
  657. return rc;
  658. }
  659. /**
  660. * ixgbe_fcoe_disable - turn off FCoE offload feature
  661. * @netdev: the corresponding netdev
  662. *
  663. * Turns off FCoE offload feature in 82599.
  664. *
  665. * Returns : 0 indicates success or -EINVAL on failure
  666. */
  667. int ixgbe_fcoe_disable(struct net_device *netdev)
  668. {
  669. int rc = -EINVAL;
  670. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  671. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  672. if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
  673. goto out_disable;
  674. if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
  675. goto out_disable;
  676. if (!atomic_dec_and_test(&fcoe->refcnt))
  677. goto out_disable;
  678. e_info(drv, "Disabling FCoE offload features.\n");
  679. netdev->features &= ~NETIF_F_FCOE_CRC;
  680. netdev->features &= ~NETIF_F_FSO;
  681. netdev->features &= ~NETIF_F_FCOE_MTU;
  682. netdev->fcoe_ddp_xid = 0;
  683. netdev_features_change(netdev);
  684. if (netif_running(netdev))
  685. netdev->netdev_ops->ndo_stop(netdev);
  686. ixgbe_clear_interrupt_scheme(adapter);
  687. adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
  688. adapter->ring_feature[RING_F_FCOE].indices = 0;
  689. ixgbe_cleanup_fcoe(adapter);
  690. ixgbe_init_interrupt_scheme(adapter);
  691. if (netif_running(netdev))
  692. netdev->netdev_ops->ndo_open(netdev);
  693. rc = 0;
  694. out_disable:
  695. return rc;
  696. }
  697. /**
  698. * ixgbe_fcoe_get_wwn - get world wide name for the node or the port
  699. * @netdev : ixgbe adapter
  700. * @wwn : the world wide name
  701. * @type: the type of world wide name
  702. *
  703. * Returns the node or port world wide name if both the prefix and the san
  704. * mac address are valid, then the wwn is formed based on the NAA-2 for
  705. * IEEE Extended name identifier (ref. to T10 FC-LS Spec., Sec. 15.3).
  706. *
  707. * Returns : 0 on success
  708. */
  709. int ixgbe_fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
  710. {
  711. int rc = -EINVAL;
  712. u16 prefix = 0xffff;
  713. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  714. struct ixgbe_mac_info *mac = &adapter->hw.mac;
  715. switch (type) {
  716. case NETDEV_FCOE_WWNN:
  717. prefix = mac->wwnn_prefix;
  718. break;
  719. case NETDEV_FCOE_WWPN:
  720. prefix = mac->wwpn_prefix;
  721. break;
  722. default:
  723. break;
  724. }
  725. if ((prefix != 0xffff) &&
  726. is_valid_ether_addr(mac->san_addr)) {
  727. *wwn = ((u64) prefix << 48) |
  728. ((u64) mac->san_addr[0] << 40) |
  729. ((u64) mac->san_addr[1] << 32) |
  730. ((u64) mac->san_addr[2] << 24) |
  731. ((u64) mac->san_addr[3] << 16) |
  732. ((u64) mac->san_addr[4] << 8) |
  733. ((u64) mac->san_addr[5]);
  734. rc = 0;
  735. }
  736. return rc;
  737. }