ixgbe_fcoe.c 24 KB

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