ixgbe_fcoe.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2012 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. unsigned int cpu;
  131. if (!netdev || !sgl)
  132. return 0;
  133. adapter = netdev_priv(netdev);
  134. if (xid >= IXGBE_FCOE_DDP_MAX) {
  135. e_warn(drv, "xid=0x%x out-of-range\n", xid);
  136. return 0;
  137. }
  138. /* no DDP if we are already down or resetting */
  139. if (test_bit(__IXGBE_DOWN, &adapter->state) ||
  140. test_bit(__IXGBE_RESETTING, &adapter->state))
  141. return 0;
  142. fcoe = &adapter->fcoe;
  143. if (!fcoe->pool) {
  144. e_warn(drv, "xid=0x%x no ddp pool for fcoe\n", xid);
  145. return 0;
  146. }
  147. ddp = &fcoe->ddp[xid];
  148. if (ddp->sgl) {
  149. e_err(drv, "xid 0x%x w/ non-null sgl=%p nents=%d\n",
  150. xid, ddp->sgl, ddp->sgc);
  151. return 0;
  152. }
  153. ixgbe_fcoe_clear_ddp(ddp);
  154. /* setup dma from scsi command sgl */
  155. dmacount = pci_map_sg(adapter->pdev, sgl, sgc, DMA_FROM_DEVICE);
  156. if (dmacount == 0) {
  157. e_err(drv, "xid 0x%x DMA map error\n", xid);
  158. return 0;
  159. }
  160. /* alloc the udl from per cpu ddp pool */
  161. cpu = get_cpu();
  162. pool = *per_cpu_ptr(fcoe->pool, cpu);
  163. ddp->udl = pci_pool_alloc(pool, GFP_ATOMIC, &ddp->udp);
  164. if (!ddp->udl) {
  165. e_err(drv, "failed allocated ddp context\n");
  166. goto out_noddp_unmap;
  167. }
  168. ddp->pool = pool;
  169. ddp->sgl = sgl;
  170. ddp->sgc = sgc;
  171. j = 0;
  172. for_each_sg(sgl, sg, dmacount, i) {
  173. addr = sg_dma_address(sg);
  174. len = sg_dma_len(sg);
  175. while (len) {
  176. /* max number of buffers allowed in one DDP context */
  177. if (j >= IXGBE_BUFFCNT_MAX) {
  178. *per_cpu_ptr(fcoe->pcpu_noddp, cpu) += 1;
  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. *per_cpu_ptr(fcoe->pcpu_noddp_ext_buff, cpu) += 1;
  216. goto out_noddp_free;
  217. }
  218. ddp->udl[j] = (u64)(fcoe->extra_ddp_buffer_dma);
  219. j++;
  220. lastsize = 1;
  221. }
  222. put_cpu();
  223. fcbuff = (IXGBE_FCBUFF_4KB << IXGBE_FCBUFF_BUFFSIZE_SHIFT);
  224. fcbuff |= ((j & 0xff) << IXGBE_FCBUFF_BUFFCNT_SHIFT);
  225. fcbuff |= (firstoff << IXGBE_FCBUFF_OFFSET_SHIFT);
  226. /* Set WRCONTX bit to allow DDP for target */
  227. if (target_mode)
  228. fcbuff |= (IXGBE_FCBUFF_WRCONTX);
  229. fcbuff |= (IXGBE_FCBUFF_VALID);
  230. fcdmarw = xid;
  231. fcdmarw |= IXGBE_FCDMARW_WE;
  232. fcdmarw |= (lastsize << IXGBE_FCDMARW_LASTSIZE_SHIFT);
  233. fcfltrw = xid;
  234. fcfltrw |= IXGBE_FCFLTRW_WE;
  235. /* program DMA context */
  236. hw = &adapter->hw;
  237. spin_lock_bh(&fcoe->lock);
  238. /* turn on last frame indication for target mode as FCP_RSPtarget is
  239. * supposed to send FCP_RSP when it is done. */
  240. if (target_mode && !test_bit(__IXGBE_FCOE_TARGET, &fcoe->mode)) {
  241. set_bit(__IXGBE_FCOE_TARGET, &fcoe->mode);
  242. fcrxctl = IXGBE_READ_REG(hw, IXGBE_FCRXCTRL);
  243. fcrxctl |= IXGBE_FCRXCTRL_LASTSEQH;
  244. IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL, fcrxctl);
  245. }
  246. IXGBE_WRITE_REG(hw, IXGBE_FCPTRL, ddp->udp & DMA_BIT_MASK(32));
  247. IXGBE_WRITE_REG(hw, IXGBE_FCPTRH, (u64)ddp->udp >> 32);
  248. IXGBE_WRITE_REG(hw, IXGBE_FCBUFF, fcbuff);
  249. IXGBE_WRITE_REG(hw, IXGBE_FCDMARW, fcdmarw);
  250. /* program filter context */
  251. IXGBE_WRITE_REG(hw, IXGBE_FCPARAM, 0);
  252. IXGBE_WRITE_REG(hw, IXGBE_FCFLT, IXGBE_FCFLT_VALID);
  253. IXGBE_WRITE_REG(hw, IXGBE_FCFLTRW, fcfltrw);
  254. spin_unlock_bh(&fcoe->lock);
  255. return 1;
  256. out_noddp_free:
  257. pci_pool_free(pool, ddp->udl, ddp->udp);
  258. ixgbe_fcoe_clear_ddp(ddp);
  259. out_noddp_unmap:
  260. pci_unmap_sg(adapter->pdev, sgl, sgc, DMA_FROM_DEVICE);
  261. put_cpu();
  262. return 0;
  263. }
  264. /**
  265. * ixgbe_fcoe_ddp_get - called to set up ddp context in initiator mode
  266. * @netdev: the corresponding net_device
  267. * @xid: the exchange id requesting ddp
  268. * @sgl: the scatter-gather list for this request
  269. * @sgc: the number of scatter-gather items
  270. *
  271. * This is the implementation of net_device_ops.ndo_fcoe_ddp_setup
  272. * and is expected to be called from ULD, e.g., FCP layer of libfc
  273. * to set up ddp for the corresponding xid of the given sglist for
  274. * the corresponding I/O.
  275. *
  276. * Returns : 1 for success and 0 for no ddp
  277. */
  278. int ixgbe_fcoe_ddp_get(struct net_device *netdev, u16 xid,
  279. struct scatterlist *sgl, unsigned int sgc)
  280. {
  281. return ixgbe_fcoe_ddp_setup(netdev, xid, sgl, sgc, 0);
  282. }
  283. /**
  284. * ixgbe_fcoe_ddp_target - called to set up ddp context in target mode
  285. * @netdev: the corresponding net_device
  286. * @xid: the exchange id requesting ddp
  287. * @sgl: the scatter-gather list for this request
  288. * @sgc: the number of scatter-gather items
  289. *
  290. * This is the implementation of net_device_ops.ndo_fcoe_ddp_target
  291. * and is expected to be called from ULD, e.g., FCP layer of libfc
  292. * to set up ddp for the corresponding xid of the given sglist for
  293. * the corresponding I/O. The DDP in target mode is a write I/O request
  294. * from the initiator.
  295. *
  296. * Returns : 1 for success and 0 for no ddp
  297. */
  298. int ixgbe_fcoe_ddp_target(struct net_device *netdev, u16 xid,
  299. struct scatterlist *sgl, unsigned int sgc)
  300. {
  301. return ixgbe_fcoe_ddp_setup(netdev, xid, sgl, sgc, 1);
  302. }
  303. /**
  304. * ixgbe_fcoe_ddp - check ddp status and mark it done
  305. * @adapter: ixgbe adapter
  306. * @rx_desc: advanced rx descriptor
  307. * @skb: the skb holding the received data
  308. *
  309. * This checks ddp status.
  310. *
  311. * Returns : < 0 indicates an error or not a FCiE ddp, 0 indicates
  312. * not passing the skb to ULD, > 0 indicates is the length of data
  313. * being ddped.
  314. */
  315. int ixgbe_fcoe_ddp(struct ixgbe_adapter *adapter,
  316. union ixgbe_adv_rx_desc *rx_desc,
  317. struct sk_buff *skb,
  318. u32 staterr)
  319. {
  320. u16 xid;
  321. u32 fctl;
  322. u32 fceofe, fcerr, fcstat;
  323. int rc = -EINVAL;
  324. struct ixgbe_fcoe *fcoe;
  325. struct ixgbe_fcoe_ddp *ddp;
  326. struct fc_frame_header *fh;
  327. struct fcoe_crc_eof *crc;
  328. fcerr = (staterr & IXGBE_RXDADV_ERR_FCERR);
  329. fceofe = (staterr & IXGBE_RXDADV_ERR_FCEOFE);
  330. if (fcerr == IXGBE_FCERR_BADCRC)
  331. skb_checksum_none_assert(skb);
  332. else
  333. skb->ip_summed = CHECKSUM_UNNECESSARY;
  334. if (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
  335. fh = (struct fc_frame_header *)(skb->data +
  336. sizeof(struct vlan_hdr) + sizeof(struct fcoe_hdr));
  337. else
  338. fh = (struct fc_frame_header *)(skb->data +
  339. sizeof(struct fcoe_hdr));
  340. fctl = ntoh24(fh->fh_f_ctl);
  341. if (fctl & FC_FC_EX_CTX)
  342. xid = be16_to_cpu(fh->fh_ox_id);
  343. else
  344. xid = be16_to_cpu(fh->fh_rx_id);
  345. if (xid >= IXGBE_FCOE_DDP_MAX)
  346. goto ddp_out;
  347. fcoe = &adapter->fcoe;
  348. ddp = &fcoe->ddp[xid];
  349. if (!ddp->udl)
  350. goto ddp_out;
  351. if (fcerr | fceofe)
  352. goto ddp_out;
  353. fcstat = (staterr & IXGBE_RXDADV_STAT_FCSTAT);
  354. if (fcstat) {
  355. /* update length of DDPed data */
  356. ddp->len = le32_to_cpu(rx_desc->wb.lower.hi_dword.rss);
  357. /* unmap the sg list when FCP_RSP is received */
  358. if (fcstat == IXGBE_RXDADV_STAT_FCSTAT_FCPRSP) {
  359. pci_unmap_sg(adapter->pdev, ddp->sgl,
  360. ddp->sgc, DMA_FROM_DEVICE);
  361. ddp->err = (fcerr | fceofe);
  362. ddp->sgl = NULL;
  363. ddp->sgc = 0;
  364. }
  365. /* return 0 to bypass going to ULD for DDPed data */
  366. if (fcstat == IXGBE_RXDADV_STAT_FCSTAT_DDP)
  367. rc = 0;
  368. else if (ddp->len)
  369. rc = ddp->len;
  370. }
  371. /* In target mode, check the last data frame of the sequence.
  372. * For DDP in target mode, data is already DDPed but the header
  373. * indication of the last data frame ould allow is to tell if we
  374. * got all the data and the ULP can send FCP_RSP back, as this is
  375. * not a full fcoe frame, we fill the trailer here so it won't be
  376. * dropped by the ULP stack.
  377. */
  378. if ((fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA) &&
  379. (fctl & FC_FC_END_SEQ)) {
  380. crc = (struct fcoe_crc_eof *)skb_put(skb, sizeof(*crc));
  381. crc->fcoe_eof = FC_EOF_T;
  382. }
  383. ddp_out:
  384. return rc;
  385. }
  386. /**
  387. * ixgbe_fso - ixgbe FCoE Sequence Offload (FSO)
  388. * @tx_ring: tx desc ring
  389. * @skb: associated skb
  390. * @tx_flags: tx flags
  391. * @hdr_len: hdr_len to be returned
  392. *
  393. * This sets up large send offload for FCoE
  394. *
  395. * Returns : 0 indicates no FSO, > 0 for FSO, < 0 for error
  396. */
  397. int ixgbe_fso(struct ixgbe_ring *tx_ring, struct sk_buff *skb,
  398. u32 tx_flags, u8 *hdr_len)
  399. {
  400. struct fc_frame_header *fh;
  401. u32 vlan_macip_lens;
  402. u32 fcoe_sof_eof = 0;
  403. u32 mss_l4len_idx;
  404. u8 sof, eof;
  405. if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_type != SKB_GSO_FCOE)) {
  406. dev_err(tx_ring->dev, "Wrong gso type %d:expecting SKB_GSO_FCOE\n",
  407. skb_shinfo(skb)->gso_type);
  408. return -EINVAL;
  409. }
  410. /* resets the header to point fcoe/fc */
  411. skb_set_network_header(skb, skb->mac_len);
  412. skb_set_transport_header(skb, skb->mac_len +
  413. sizeof(struct fcoe_hdr));
  414. /* sets up SOF and ORIS */
  415. sof = ((struct fcoe_hdr *)skb_network_header(skb))->fcoe_sof;
  416. switch (sof) {
  417. case FC_SOF_I2:
  418. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_ORIS;
  419. break;
  420. case FC_SOF_I3:
  421. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_SOF |
  422. IXGBE_ADVTXD_FCOEF_ORIS;
  423. break;
  424. case FC_SOF_N2:
  425. break;
  426. case FC_SOF_N3:
  427. fcoe_sof_eof = IXGBE_ADVTXD_FCOEF_SOF;
  428. break;
  429. default:
  430. dev_warn(tx_ring->dev, "unknown sof = 0x%x\n", sof);
  431. return -EINVAL;
  432. }
  433. /* the first byte of the last dword is EOF */
  434. skb_copy_bits(skb, skb->len - 4, &eof, 1);
  435. /* sets up EOF and ORIE */
  436. switch (eof) {
  437. case FC_EOF_N:
  438. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N;
  439. break;
  440. case FC_EOF_T:
  441. /* lso needs ORIE */
  442. if (skb_is_gso(skb))
  443. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_N |
  444. IXGBE_ADVTXD_FCOEF_ORIE;
  445. else
  446. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_T;
  447. break;
  448. case FC_EOF_NI:
  449. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_NI;
  450. break;
  451. case FC_EOF_A:
  452. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_EOF_A;
  453. break;
  454. default:
  455. dev_warn(tx_ring->dev, "unknown eof = 0x%x\n", eof);
  456. return -EINVAL;
  457. }
  458. /* sets up PARINC indicating data offset */
  459. fh = (struct fc_frame_header *)skb_transport_header(skb);
  460. if (fh->fh_f_ctl[2] & FC_FC_REL_OFF)
  461. fcoe_sof_eof |= IXGBE_ADVTXD_FCOEF_PARINC;
  462. /* include trailer in headlen as it is replicated per frame */
  463. *hdr_len = sizeof(struct fcoe_crc_eof);
  464. /* hdr_len includes fc_hdr if FCoE LSO is enabled */
  465. if (skb_is_gso(skb))
  466. *hdr_len += (skb_transport_offset(skb) +
  467. sizeof(struct fc_frame_header));
  468. /* mss_l4len_id: use 1 for FSO as TSO, no need for L4LEN */
  469. mss_l4len_idx = skb_shinfo(skb)->gso_size << IXGBE_ADVTXD_MSS_SHIFT;
  470. mss_l4len_idx |= 1 << IXGBE_ADVTXD_IDX_SHIFT;
  471. /* vlan_macip_lens: HEADLEN, MACLEN, VLAN tag */
  472. vlan_macip_lens = skb_transport_offset(skb) +
  473. sizeof(struct fc_frame_header);
  474. vlan_macip_lens |= (skb_transport_offset(skb) - 4)
  475. << IXGBE_ADVTXD_MACLEN_SHIFT;
  476. vlan_macip_lens |= tx_flags & IXGBE_TX_FLAGS_VLAN_MASK;
  477. /* write context desc */
  478. ixgbe_tx_ctxtdesc(tx_ring, vlan_macip_lens, fcoe_sof_eof,
  479. IXGBE_ADVTXT_TUCMD_FCOE, mss_l4len_idx);
  480. return skb_is_gso(skb);
  481. }
  482. static void ixgbe_fcoe_ddp_pools_free(struct ixgbe_fcoe *fcoe)
  483. {
  484. unsigned int cpu;
  485. struct pci_pool **pool;
  486. for_each_possible_cpu(cpu) {
  487. pool = per_cpu_ptr(fcoe->pool, cpu);
  488. if (*pool)
  489. pci_pool_destroy(*pool);
  490. }
  491. free_percpu(fcoe->pool);
  492. fcoe->pool = NULL;
  493. }
  494. static void ixgbe_fcoe_ddp_pools_alloc(struct ixgbe_adapter *adapter)
  495. {
  496. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  497. unsigned int cpu;
  498. struct pci_pool **pool;
  499. char pool_name[32];
  500. fcoe->pool = alloc_percpu(struct pci_pool *);
  501. if (!fcoe->pool)
  502. return;
  503. /* allocate pci pool for each cpu */
  504. for_each_possible_cpu(cpu) {
  505. snprintf(pool_name, 32, "ixgbe_fcoe_ddp_%d", cpu);
  506. pool = per_cpu_ptr(fcoe->pool, cpu);
  507. *pool = pci_pool_create(pool_name,
  508. adapter->pdev, IXGBE_FCPTR_MAX,
  509. IXGBE_FCPTR_ALIGN, PAGE_SIZE);
  510. if (!*pool) {
  511. e_err(drv, "failed to alloc DDP pool on cpu:%d\n", cpu);
  512. ixgbe_fcoe_ddp_pools_free(fcoe);
  513. return;
  514. }
  515. }
  516. }
  517. /**
  518. * ixgbe_configure_fcoe - configures registers for fcoe at start
  519. * @adapter: ptr to ixgbe adapter
  520. *
  521. * This sets up FCoE related registers
  522. *
  523. * Returns : none
  524. */
  525. void ixgbe_configure_fcoe(struct ixgbe_adapter *adapter)
  526. {
  527. int i, fcoe_q, fcoe_i;
  528. struct ixgbe_hw *hw = &adapter->hw;
  529. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  530. struct ixgbe_ring_feature *f = &adapter->ring_feature[RING_F_FCOE];
  531. unsigned int cpu;
  532. if (!fcoe->pool) {
  533. spin_lock_init(&fcoe->lock);
  534. ixgbe_fcoe_ddp_pools_alloc(adapter);
  535. if (!fcoe->pool) {
  536. e_err(drv, "failed to alloc percpu fcoe DDP pools\n");
  537. return;
  538. }
  539. /* Extra buffer to be shared by all DDPs for HW work around */
  540. fcoe->extra_ddp_buffer = kmalloc(IXGBE_FCBUFF_MIN, GFP_ATOMIC);
  541. if (fcoe->extra_ddp_buffer == NULL) {
  542. e_err(drv, "failed to allocated extra DDP buffer\n");
  543. goto out_ddp_pools;
  544. }
  545. fcoe->extra_ddp_buffer_dma =
  546. dma_map_single(&adapter->pdev->dev,
  547. fcoe->extra_ddp_buffer,
  548. IXGBE_FCBUFF_MIN,
  549. DMA_FROM_DEVICE);
  550. if (dma_mapping_error(&adapter->pdev->dev,
  551. fcoe->extra_ddp_buffer_dma)) {
  552. e_err(drv, "failed to map extra DDP buffer\n");
  553. goto out_extra_ddp_buffer;
  554. }
  555. /* Alloc per cpu mem to count the ddp alloc failure number */
  556. fcoe->pcpu_noddp = alloc_percpu(u64);
  557. if (!fcoe->pcpu_noddp) {
  558. e_err(drv, "failed to alloc noddp counter\n");
  559. goto out_pcpu_noddp_alloc_fail;
  560. }
  561. fcoe->pcpu_noddp_ext_buff = alloc_percpu(u64);
  562. if (!fcoe->pcpu_noddp_ext_buff) {
  563. e_err(drv, "failed to alloc noddp extra buff cnt\n");
  564. goto out_pcpu_noddp_extra_buff_alloc_fail;
  565. }
  566. for_each_possible_cpu(cpu) {
  567. *per_cpu_ptr(fcoe->pcpu_noddp, cpu) = 0;
  568. *per_cpu_ptr(fcoe->pcpu_noddp_ext_buff, cpu) = 0;
  569. }
  570. }
  571. /* Enable L2 eth type filter for FCoE */
  572. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FCOE),
  573. (ETH_P_FCOE | IXGBE_ETQF_FCOE | IXGBE_ETQF_FILTER_EN));
  574. /* Enable L2 eth type filter for FIP */
  575. IXGBE_WRITE_REG(hw, IXGBE_ETQF(IXGBE_ETQF_FILTER_FIP),
  576. (ETH_P_FIP | IXGBE_ETQF_FILTER_EN));
  577. if (adapter->ring_feature[RING_F_FCOE].indices) {
  578. /* Use multiple rx queues for FCoE by redirection table */
  579. for (i = 0; i < IXGBE_FCRETA_SIZE; i++) {
  580. fcoe_i = f->mask + i % f->indices;
  581. fcoe_i &= IXGBE_FCRETA_ENTRY_MASK;
  582. fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
  583. IXGBE_WRITE_REG(hw, IXGBE_FCRETA(i), fcoe_q);
  584. }
  585. IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, IXGBE_FCRECTL_ENA);
  586. IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE), 0);
  587. } else {
  588. /* Use single rx queue for FCoE */
  589. fcoe_i = f->mask;
  590. fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
  591. IXGBE_WRITE_REG(hw, IXGBE_FCRECTL, 0);
  592. IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FCOE),
  593. IXGBE_ETQS_QUEUE_EN |
  594. (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
  595. }
  596. /* send FIP frames to the first FCoE queue */
  597. fcoe_i = f->mask;
  598. fcoe_q = adapter->rx_ring[fcoe_i]->reg_idx;
  599. IXGBE_WRITE_REG(hw, IXGBE_ETQS(IXGBE_ETQF_FILTER_FIP),
  600. IXGBE_ETQS_QUEUE_EN |
  601. (fcoe_q << IXGBE_ETQS_RX_QUEUE_SHIFT));
  602. IXGBE_WRITE_REG(hw, IXGBE_FCRXCTRL, IXGBE_FCRXCTRL_FCCRCBO |
  603. (FC_FCOE_VER << IXGBE_FCRXCTRL_FCOEVER_SHIFT));
  604. return;
  605. out_pcpu_noddp_extra_buff_alloc_fail:
  606. free_percpu(fcoe->pcpu_noddp);
  607. out_pcpu_noddp_alloc_fail:
  608. dma_unmap_single(&adapter->pdev->dev,
  609. fcoe->extra_ddp_buffer_dma,
  610. IXGBE_FCBUFF_MIN,
  611. DMA_FROM_DEVICE);
  612. out_extra_ddp_buffer:
  613. kfree(fcoe->extra_ddp_buffer);
  614. out_ddp_pools:
  615. ixgbe_fcoe_ddp_pools_free(fcoe);
  616. }
  617. /**
  618. * ixgbe_cleanup_fcoe - release all fcoe ddp context resources
  619. * @adapter : ixgbe adapter
  620. *
  621. * Cleans up outstanding ddp context resources
  622. *
  623. * Returns : none
  624. */
  625. void ixgbe_cleanup_fcoe(struct ixgbe_adapter *adapter)
  626. {
  627. int i;
  628. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  629. if (!fcoe->pool)
  630. return;
  631. for (i = 0; i < IXGBE_FCOE_DDP_MAX; i++)
  632. ixgbe_fcoe_ddp_put(adapter->netdev, i);
  633. dma_unmap_single(&adapter->pdev->dev,
  634. fcoe->extra_ddp_buffer_dma,
  635. IXGBE_FCBUFF_MIN,
  636. DMA_FROM_DEVICE);
  637. free_percpu(fcoe->pcpu_noddp);
  638. free_percpu(fcoe->pcpu_noddp_ext_buff);
  639. kfree(fcoe->extra_ddp_buffer);
  640. ixgbe_fcoe_ddp_pools_free(fcoe);
  641. }
  642. /**
  643. * ixgbe_fcoe_enable - turn on FCoE offload feature
  644. * @netdev: the corresponding netdev
  645. *
  646. * Turns on FCoE offload feature in 82599.
  647. *
  648. * Returns : 0 indicates success or -EINVAL on failure
  649. */
  650. int ixgbe_fcoe_enable(struct net_device *netdev)
  651. {
  652. int rc = -EINVAL;
  653. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  654. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  655. if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
  656. goto out_enable;
  657. atomic_inc(&fcoe->refcnt);
  658. if (adapter->flags & IXGBE_FLAG_FCOE_ENABLED)
  659. goto out_enable;
  660. e_info(drv, "Enabling FCoE offload features.\n");
  661. if (netif_running(netdev))
  662. netdev->netdev_ops->ndo_stop(netdev);
  663. ixgbe_clear_interrupt_scheme(adapter);
  664. adapter->flags |= IXGBE_FLAG_FCOE_ENABLED;
  665. adapter->ring_feature[RING_F_FCOE].indices = IXGBE_FCRETA_SIZE;
  666. netdev->features |= NETIF_F_FCOE_CRC;
  667. netdev->features |= NETIF_F_FSO;
  668. netdev->features |= NETIF_F_FCOE_MTU;
  669. netdev->fcoe_ddp_xid = IXGBE_FCOE_DDP_MAX - 1;
  670. ixgbe_init_interrupt_scheme(adapter);
  671. netdev_features_change(netdev);
  672. if (netif_running(netdev))
  673. netdev->netdev_ops->ndo_open(netdev);
  674. rc = 0;
  675. out_enable:
  676. return rc;
  677. }
  678. /**
  679. * ixgbe_fcoe_disable - turn off FCoE offload feature
  680. * @netdev: the corresponding netdev
  681. *
  682. * Turns off FCoE offload feature in 82599.
  683. *
  684. * Returns : 0 indicates success or -EINVAL on failure
  685. */
  686. int ixgbe_fcoe_disable(struct net_device *netdev)
  687. {
  688. int rc = -EINVAL;
  689. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  690. struct ixgbe_fcoe *fcoe = &adapter->fcoe;
  691. if (!(adapter->flags & IXGBE_FLAG_FCOE_CAPABLE))
  692. goto out_disable;
  693. if (!(adapter->flags & IXGBE_FLAG_FCOE_ENABLED))
  694. goto out_disable;
  695. if (!atomic_dec_and_test(&fcoe->refcnt))
  696. goto out_disable;
  697. e_info(drv, "Disabling FCoE offload features.\n");
  698. netdev->features &= ~NETIF_F_FCOE_CRC;
  699. netdev->features &= ~NETIF_F_FSO;
  700. netdev->features &= ~NETIF_F_FCOE_MTU;
  701. netdev->fcoe_ddp_xid = 0;
  702. netdev_features_change(netdev);
  703. if (netif_running(netdev))
  704. netdev->netdev_ops->ndo_stop(netdev);
  705. ixgbe_clear_interrupt_scheme(adapter);
  706. adapter->flags &= ~IXGBE_FLAG_FCOE_ENABLED;
  707. adapter->ring_feature[RING_F_FCOE].indices = 0;
  708. ixgbe_cleanup_fcoe(adapter);
  709. ixgbe_init_interrupt_scheme(adapter);
  710. if (netif_running(netdev))
  711. netdev->netdev_ops->ndo_open(netdev);
  712. rc = 0;
  713. out_disable:
  714. return rc;
  715. }
  716. /**
  717. * ixgbe_fcoe_get_wwn - get world wide name for the node or the port
  718. * @netdev : ixgbe adapter
  719. * @wwn : the world wide name
  720. * @type: the type of world wide name
  721. *
  722. * Returns the node or port world wide name if both the prefix and the san
  723. * mac address are valid, then the wwn is formed based on the NAA-2 for
  724. * IEEE Extended name identifier (ref. to T10 FC-LS Spec., Sec. 15.3).
  725. *
  726. * Returns : 0 on success
  727. */
  728. int ixgbe_fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
  729. {
  730. int rc = -EINVAL;
  731. u16 prefix = 0xffff;
  732. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  733. struct ixgbe_mac_info *mac = &adapter->hw.mac;
  734. switch (type) {
  735. case NETDEV_FCOE_WWNN:
  736. prefix = mac->wwnn_prefix;
  737. break;
  738. case NETDEV_FCOE_WWPN:
  739. prefix = mac->wwpn_prefix;
  740. break;
  741. default:
  742. break;
  743. }
  744. if ((prefix != 0xffff) &&
  745. is_valid_ether_addr(mac->san_addr)) {
  746. *wwn = ((u64) prefix << 48) |
  747. ((u64) mac->san_addr[0] << 40) |
  748. ((u64) mac->san_addr[1] << 32) |
  749. ((u64) mac->san_addr[2] << 24) |
  750. ((u64) mac->san_addr[3] << 16) |
  751. ((u64) mac->san_addr[4] << 8) |
  752. ((u64) mac->san_addr[5]);
  753. rc = 0;
  754. }
  755. return rc;
  756. }
  757. /**
  758. * ixgbe_fcoe_get_hbainfo - get FCoE HBA information
  759. * @netdev : ixgbe adapter
  760. * @info : HBA information
  761. *
  762. * Returns ixgbe HBA information
  763. *
  764. * Returns : 0 on success
  765. */
  766. int ixgbe_fcoe_get_hbainfo(struct net_device *netdev,
  767. struct netdev_fcoe_hbainfo *info)
  768. {
  769. struct ixgbe_adapter *adapter = netdev_priv(netdev);
  770. struct ixgbe_hw *hw = &adapter->hw;
  771. int i, pos;
  772. u8 buf[8];
  773. if (!info)
  774. return -EINVAL;
  775. /* Don't return information on unsupported devices */
  776. if (hw->mac.type != ixgbe_mac_82599EB &&
  777. hw->mac.type != ixgbe_mac_X540)
  778. return -EINVAL;
  779. /* Manufacturer */
  780. snprintf(info->manufacturer, sizeof(info->manufacturer),
  781. "Intel Corporation");
  782. /* Serial Number */
  783. /* Get the PCI-e Device Serial Number Capability */
  784. pos = pci_find_ext_capability(adapter->pdev, PCI_EXT_CAP_ID_DSN);
  785. if (pos) {
  786. pos += 4;
  787. for (i = 0; i < 8; i++)
  788. pci_read_config_byte(adapter->pdev, pos + i, &buf[i]);
  789. snprintf(info->serial_number, sizeof(info->serial_number),
  790. "%02X%02X%02X%02X%02X%02X%02X%02X",
  791. buf[7], buf[6], buf[5], buf[4],
  792. buf[3], buf[2], buf[1], buf[0]);
  793. } else
  794. snprintf(info->serial_number, sizeof(info->serial_number),
  795. "Unknown");
  796. /* Hardware Version */
  797. snprintf(info->hardware_version,
  798. sizeof(info->hardware_version),
  799. "Rev %d", hw->revision_id);
  800. /* Driver Name/Version */
  801. snprintf(info->driver_version,
  802. sizeof(info->driver_version),
  803. "%s v%s",
  804. ixgbe_driver_name,
  805. ixgbe_driver_version);
  806. /* Firmware Version */
  807. snprintf(info->firmware_version,
  808. sizeof(info->firmware_version),
  809. "0x%08x",
  810. (adapter->eeprom_verh << 16) |
  811. adapter->eeprom_verl);
  812. /* Model */
  813. if (hw->mac.type == ixgbe_mac_82599EB) {
  814. snprintf(info->model,
  815. sizeof(info->model),
  816. "Intel 82599");
  817. } else {
  818. snprintf(info->model,
  819. sizeof(info->model),
  820. "Intel X540");
  821. }
  822. /* Model Description */
  823. snprintf(info->model_description,
  824. sizeof(info->model_description),
  825. "%s",
  826. ixgbe_default_device_descr);
  827. return 0;
  828. }