GNU Linux-libre 4.14.332-gnu1
[releases.git] / drivers / net / ethernet / intel / i40e / i40e_fcoe.c
1 /*******************************************************************************
2  *
3  * Intel Ethernet Controller XL710 Family Linux Driver
4  * Copyright(c) 2013 - 2016 Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program.  If not, see <http://www.gnu.org/licenses/>.
17  *
18  * The full GNU General Public License is included in this distribution in
19  * the file called "COPYING".
20  *
21  * Contact Information:
22  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
23  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
24  *
25  ******************************************************************************/
26
27 #include <linux/if_ether.h>
28 #include <scsi/scsi_cmnd.h>
29 #include <scsi/scsi_device.h>
30 #include <scsi/fc/fc_fs.h>
31 #include <scsi/fc/fc_fip.h>
32 #include <scsi/fc/fc_fcoe.h>
33 #include <scsi/libfc.h>
34 #include <scsi/libfcoe.h>
35 #include <uapi/linux/dcbnl.h>
36
37 #include "i40e.h"
38 #include "i40e_fcoe.h"
39
40 /**
41  * i40e_fcoe_sof_is_class2 - returns true if this is a FC Class 2 SOF
42  * @sof: the FCoE start of frame delimiter
43  **/
44 static inline bool i40e_fcoe_sof_is_class2(u8 sof)
45 {
46         return (sof == FC_SOF_I2) || (sof == FC_SOF_N2);
47 }
48
49 /**
50  * i40e_fcoe_sof_is_class3 - returns true if this is a FC Class 3 SOF
51  * @sof: the FCoE start of frame delimiter
52  **/
53 static inline bool i40e_fcoe_sof_is_class3(u8 sof)
54 {
55         return (sof == FC_SOF_I3) || (sof == FC_SOF_N3);
56 }
57
58 /**
59  * i40e_fcoe_sof_is_supported - returns true if the FC SOF is supported by HW
60  * @sof: the input SOF value from the frame
61  **/
62 static inline bool i40e_fcoe_sof_is_supported(u8 sof)
63 {
64         return i40e_fcoe_sof_is_class2(sof) ||
65                i40e_fcoe_sof_is_class3(sof);
66 }
67
68 /**
69  * i40e_fcoe_fc_sof - pull the SOF from FCoE header in the frame
70  * @skb: the frame whose EOF is to be pulled from
71  **/
72 static inline int i40e_fcoe_fc_sof(struct sk_buff *skb, u8 *sof)
73 {
74         *sof = ((struct fcoe_hdr *)skb_network_header(skb))->fcoe_sof;
75
76         if (!i40e_fcoe_sof_is_supported(*sof))
77                 return -EINVAL;
78         return 0;
79 }
80
81 /**
82  * i40e_fcoe_eof_is_supported - returns true if the EOF is supported by HW
83  * @eof:     the input EOF value from the frame
84  **/
85 static inline bool i40e_fcoe_eof_is_supported(u8 eof)
86 {
87         return (eof == FC_EOF_N) || (eof == FC_EOF_T) ||
88                (eof == FC_EOF_NI) || (eof == FC_EOF_A);
89 }
90
91 /**
92  * i40e_fcoe_fc_eof - pull EOF from FCoE trailer in the frame
93  * @skb: the frame whose EOF is to be pulled from
94  **/
95 static inline int i40e_fcoe_fc_eof(struct sk_buff *skb, u8 *eof)
96 {
97         /* the first byte of the last dword is EOF */
98         skb_copy_bits(skb, skb->len - 4, eof, 1);
99
100         if (!i40e_fcoe_eof_is_supported(*eof))
101                 return -EINVAL;
102         return 0;
103 }
104
105 /**
106  * i40e_fcoe_ctxt_eof - convert input FC EOF for descriptor programming
107  * @eof: the input eof value from the frame
108  *
109  * The FC EOF is converted to the value understood by HW for descriptor
110  * programming. Never call this w/o calling i40e_fcoe_eof_is_supported()
111  * first and that already checks for all supported valid eof values.
112  **/
113 static inline u32 i40e_fcoe_ctxt_eof(u8 eof)
114 {
115         switch (eof) {
116         case FC_EOF_N:
117                 return I40E_TX_DESC_CMD_L4T_EOFT_EOF_N;
118         case FC_EOF_T:
119                 return I40E_TX_DESC_CMD_L4T_EOFT_EOF_T;
120         case FC_EOF_NI:
121                 return I40E_TX_DESC_CMD_L4T_EOFT_EOF_NI;
122         case FC_EOF_A:
123                 return I40E_TX_DESC_CMD_L4T_EOFT_EOF_A;
124         default:
125                 /* Supported valid eof shall be already checked by
126                  * calling i40e_fcoe_eof_is_supported() first,
127                  * therefore this default case shall never hit.
128                  */
129                 WARN_ON(1);
130                 return -EINVAL;
131         }
132 }
133
134 /**
135  * i40e_fcoe_xid_is_valid - returns true if the exchange id is valid
136  * @xid: the exchange id
137  **/
138 static inline bool i40e_fcoe_xid_is_valid(u16 xid)
139 {
140         return (xid != FC_XID_UNKNOWN) && (xid < I40E_FCOE_DDP_MAX);
141 }
142
143 /**
144  * i40e_fcoe_ddp_unmap - unmap the mapped sglist associated
145  * @pf: pointer to PF
146  * @ddp: sw DDP context
147  *
148  * Unmap the scatter-gather list associated with the given SW DDP context
149  *
150  * Returns: data length already ddp-ed in bytes
151  *
152  **/
153 static inline void i40e_fcoe_ddp_unmap(struct i40e_pf *pf,
154                                        struct i40e_fcoe_ddp *ddp)
155 {
156         if (test_and_set_bit(__I40E_FCOE_DDP_UNMAPPED, &ddp->flags))
157                 return;
158
159         if (ddp->sgl) {
160                 dma_unmap_sg(&pf->pdev->dev, ddp->sgl, ddp->sgc,
161                              DMA_FROM_DEVICE);
162                 ddp->sgl = NULL;
163                 ddp->sgc = 0;
164         }
165
166         if (ddp->pool) {
167                 dma_pool_free(ddp->pool, ddp->udl, ddp->udp);
168                 ddp->pool = NULL;
169         }
170 }
171
172 /**
173  * i40e_fcoe_ddp_clear - clear the given SW DDP context
174  * @ddp - SW DDP context
175  **/
176 static inline void i40e_fcoe_ddp_clear(struct i40e_fcoe_ddp *ddp)
177 {
178         memset(ddp, 0, sizeof(struct i40e_fcoe_ddp));
179         ddp->xid = FC_XID_UNKNOWN;
180         ddp->flags = __I40E_FCOE_DDP_NONE;
181 }
182
183 /**
184  * i40e_fcoe_progid_is_fcoe - check if the prog_id is for FCoE
185  * @id: the prog id for the programming status Rx descriptor write-back
186  **/
187 static inline bool i40e_fcoe_progid_is_fcoe(u8 id)
188 {
189         return (id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_PROG_STATUS) ||
190                (id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_INVL_STATUS);
191 }
192
193 /**
194  * i40e_fcoe_fc_get_xid - get xid from the frame header
195  * @fh: the fc frame header
196  *
197  * In case the incoming frame's exchange is originated from
198  * the initiator, then received frame's exchange id is ANDed
199  * with fc_cpu_mask bits to get the same cpu on which exchange
200  * was originated, otherwise just use the current cpu.
201  *
202  * Returns ox_id if exchange originator, rx_id if responder
203  **/
204 static inline u16 i40e_fcoe_fc_get_xid(struct fc_frame_header *fh)
205 {
206         u32 f_ctl = ntoh24(fh->fh_f_ctl);
207
208         return (f_ctl & FC_FC_EX_CTX) ?
209                 be16_to_cpu(fh->fh_ox_id) :
210                 be16_to_cpu(fh->fh_rx_id);
211 }
212
213 /**
214  * i40e_fcoe_fc_frame_header - get fc frame header from skb
215  * @skb: packet
216  *
217  * This checks if there is a VLAN header and returns the data
218  * pointer to the start of the fc_frame_header.
219  *
220  * Returns pointer to the fc_frame_header
221  **/
222 static inline struct fc_frame_header *i40e_fcoe_fc_frame_header(
223         struct sk_buff *skb)
224 {
225         void *fh = skb->data + sizeof(struct fcoe_hdr);
226
227         if (eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
228                 fh += sizeof(struct vlan_hdr);
229
230         return (struct fc_frame_header *)fh;
231 }
232
233 /**
234  * i40e_fcoe_ddp_put - release the DDP context for a given exchange id
235  * @netdev: the corresponding net_device
236  * @xid: the exchange id that corresponding DDP context will be released
237  *
238  * This is the implementation of net_device_ops.ndo_fcoe_ddp_done
239  * and it is expected to be called by ULD, i.e., FCP layer of libfc
240  * to release the corresponding ddp context when the I/O is done.
241  *
242  * Returns : data length already ddp-ed in bytes
243  **/
244 static int i40e_fcoe_ddp_put(struct net_device *netdev, u16 xid)
245 {
246         struct i40e_netdev_priv *np = netdev_priv(netdev);
247         struct i40e_pf *pf = np->vsi->back;
248         struct i40e_fcoe *fcoe = &pf->fcoe;
249         int len = 0;
250         struct i40e_fcoe_ddp *ddp = &fcoe->ddp[xid];
251
252         if (!fcoe || !ddp)
253                 goto out;
254
255         if (test_bit(__I40E_FCOE_DDP_DONE, &ddp->flags))
256                 len = ddp->len;
257         i40e_fcoe_ddp_unmap(pf, ddp);
258 out:
259         return len;
260 }
261
262 /**
263  * i40e_fcoe_sw_init - sets up the HW for FCoE
264  * @pf: pointer to PF
265  **/
266 void i40e_init_pf_fcoe(struct i40e_pf *pf)
267 {
268         struct i40e_hw *hw = &pf->hw;
269         u32 val;
270
271         pf->flags &= ~I40E_FLAG_FCOE_ENABLED;
272         pf->num_fcoe_qps = 0;
273         pf->fcoe_hmc_cntx_num = 0;
274         pf->fcoe_hmc_filt_num = 0;
275
276         if (!pf->hw.func_caps.fcoe) {
277                 dev_dbg(&pf->pdev->dev, "FCoE capability is disabled\n");
278                 return;
279         }
280
281         if (!pf->hw.func_caps.dcb) {
282                 dev_warn(&pf->pdev->dev,
283                          "Hardware is not DCB capable not enabling FCoE.\n");
284                 return;
285         }
286
287         /* enable FCoE hash filter */
288         val = i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1));
289         val |= BIT(I40E_FILTER_PCTYPE_FCOE_OX - 32);
290         val |= BIT(I40E_FILTER_PCTYPE_FCOE_RX - 32);
291         val &= I40E_PFQF_HENA_PTYPE_ENA_MASK;
292         i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), val);
293
294         /* enable flag */
295         pf->flags |= I40E_FLAG_FCOE_ENABLED;
296         pf->num_fcoe_qps = I40E_DEFAULT_FCOE;
297
298         /* Reserve 4K DDP contexts and 20K filter size for FCoE */
299         pf->fcoe_hmc_cntx_num = BIT(I40E_DMA_CNTX_SIZE_4K) *
300                                 I40E_DMA_CNTX_BASE_SIZE;
301         pf->fcoe_hmc_filt_num = pf->fcoe_hmc_cntx_num +
302                                 BIT(I40E_HASH_FILTER_SIZE_16K) *
303                                 I40E_HASH_FILTER_BASE_SIZE;
304
305         /* FCoE object: max 16K filter buckets and 4K DMA contexts */
306         pf->filter_settings.fcoe_filt_num = I40E_HASH_FILTER_SIZE_16K;
307         pf->filter_settings.fcoe_cntx_num = I40E_DMA_CNTX_SIZE_4K;
308
309         /* Setup max frame with FCoE_MTU plus L2 overheads */
310         val = i40e_read_rx_ctl(hw, I40E_GLFCOE_RCTL);
311         val &= ~I40E_GLFCOE_RCTL_MAX_SIZE_MASK;
312         val |= ((FCOE_MTU + ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN)
313                  << I40E_GLFCOE_RCTL_MAX_SIZE_SHIFT);
314         i40e_write_rx_ctl(hw, I40E_GLFCOE_RCTL, val);
315
316         dev_info(&pf->pdev->dev, "FCoE is supported.\n");
317 }
318
319 /**
320  * i40e_get_fcoe_tc_map - Return TC map for FCoE APP
321  * @pf: pointer to PF
322  *
323  **/
324 u8 i40e_get_fcoe_tc_map(struct i40e_pf *pf)
325 {
326         struct i40e_dcb_app_priority_table app;
327         struct i40e_hw *hw = &pf->hw;
328         u8 enabled_tc = 0;
329         u8 tc, i;
330         /* Get the FCoE APP TLV */
331         struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
332
333         for (i = 0; i < dcbcfg->numapps; i++) {
334                 app = dcbcfg->app[i];
335                 if (app.selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
336                     app.protocolid == ETH_P_FCOE) {
337                         tc = dcbcfg->etscfg.prioritytable[app.priority];
338                         enabled_tc |= BIT(tc);
339                         break;
340                 }
341         }
342
343         /* TC0 if there is no TC defined for FCoE APP TLV */
344         enabled_tc = enabled_tc ? enabled_tc : 0x1;
345
346         return enabled_tc;
347 }
348
349 /**
350  * i40e_fcoe_vsi_init - prepares the VSI context for creating a FCoE VSI
351  * @vsi: pointer to the associated VSI struct
352  * @ctxt: pointer to the associated VSI context to be passed to HW
353  *
354  * Returns 0 on success or < 0 on error
355  **/
356 int i40e_fcoe_vsi_init(struct i40e_vsi *vsi, struct i40e_vsi_context *ctxt)
357 {
358         struct i40e_aqc_vsi_properties_data *info = &ctxt->info;
359         struct i40e_pf *pf = vsi->back;
360         struct i40e_hw *hw = &pf->hw;
361         u8 enabled_tc = 0;
362
363         if (!(pf->flags & I40E_FLAG_FCOE_ENABLED)) {
364                 dev_err(&pf->pdev->dev,
365                         "FCoE is not enabled for this device\n");
366                 return -EPERM;
367         }
368
369         /* initialize the hardware for FCoE */
370         ctxt->pf_num = hw->pf_id;
371         ctxt->vf_num = 0;
372         ctxt->uplink_seid = vsi->uplink_seid;
373         ctxt->connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
374         ctxt->flags = I40E_AQ_VSI_TYPE_PF;
375
376         /* FCoE VSI would need the following sections */
377         info->valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
378
379         /* FCoE VSI does not need these sections */
380         info->valid_sections &= cpu_to_le16(~(I40E_AQ_VSI_PROP_SECURITY_VALID |
381                                             I40E_AQ_VSI_PROP_VLAN_VALID |
382                                             I40E_AQ_VSI_PROP_CAS_PV_VALID |
383                                             I40E_AQ_VSI_PROP_INGRESS_UP_VALID |
384                                             I40E_AQ_VSI_PROP_EGRESS_UP_VALID));
385
386         if (i40e_is_vsi_uplink_mode_veb(vsi)) {
387                 info->valid_sections |=
388                                 cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
389                 info->switch_id =
390                                 cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
391         }
392         enabled_tc = i40e_get_fcoe_tc_map(pf);
393         i40e_vsi_setup_queue_map(vsi, ctxt, enabled_tc, true);
394
395         /* set up queue option section: only enable FCoE */
396         info->queueing_opt_flags = I40E_AQ_VSI_QUE_OPT_FCOE_ENA;
397
398         return 0;
399 }
400
401 /**
402  * i40e_fcoe_enable - this is the implementation of ndo_fcoe_enable,
403  * indicating the upper FCoE protocol stack is ready to use FCoE
404  * offload features.
405  *
406  * @netdev: pointer to the netdev that FCoE is created on
407  *
408  * Returns 0 on success
409  *
410  * in RTNL
411  *
412  **/
413 int i40e_fcoe_enable(struct net_device *netdev)
414 {
415         struct i40e_netdev_priv *np = netdev_priv(netdev);
416         struct i40e_vsi *vsi = np->vsi;
417         struct i40e_pf *pf = vsi->back;
418         struct i40e_fcoe *fcoe = &pf->fcoe;
419
420         if (!(pf->flags & I40E_FLAG_FCOE_ENABLED)) {
421                 netdev_err(netdev, "HW does not support FCoE.\n");
422                 return -ENODEV;
423         }
424
425         if (vsi->type != I40E_VSI_FCOE) {
426                 netdev_err(netdev, "interface does not support FCoE.\n");
427                 return -EBUSY;
428         }
429
430         atomic_inc(&fcoe->refcnt);
431
432         return 0;
433 }
434
435 /**
436  * i40e_fcoe_disable- disables FCoE for upper FCoE protocol stack.
437  * @dev: pointer to the netdev that FCoE is created on
438  *
439  * Returns 0 on success
440  *
441  **/
442 int i40e_fcoe_disable(struct net_device *netdev)
443 {
444         struct i40e_netdev_priv *np = netdev_priv(netdev);
445         struct i40e_vsi *vsi = np->vsi;
446         struct i40e_pf *pf = vsi->back;
447         struct i40e_fcoe *fcoe = &pf->fcoe;
448
449         if (!(pf->flags & I40E_FLAG_FCOE_ENABLED)) {
450                 netdev_err(netdev, "device does not support FCoE\n");
451                 return -ENODEV;
452         }
453         if (vsi->type != I40E_VSI_FCOE)
454                 return -EBUSY;
455
456         if (!atomic_dec_and_test(&fcoe->refcnt))
457                 return -EINVAL;
458
459         netdev_info(netdev, "FCoE disabled\n");
460
461         return 0;
462 }
463
464 /**
465  * i40e_fcoe_dma_pool_free - free the per cpu pool for FCoE DDP
466  * @fcoe: the FCoE sw object
467  * @dev: the device that the pool is associated with
468  * @cpu: the cpu for this pool
469  *
470  **/
471 static void i40e_fcoe_dma_pool_free(struct i40e_fcoe *fcoe,
472                                     struct device *dev,
473                                     unsigned int cpu)
474 {
475         struct i40e_fcoe_ddp_pool *ddp_pool;
476
477         ddp_pool = per_cpu_ptr(fcoe->ddp_pool, cpu);
478         if (!ddp_pool->pool) {
479                 dev_warn(dev, "DDP pool already freed for cpu %d\n", cpu);
480                 return;
481         }
482         dma_pool_destroy(ddp_pool->pool);
483         ddp_pool->pool = NULL;
484 }
485
486 /**
487  * i40e_fcoe_dma_pool_create - per cpu pool for FCoE DDP
488  * @fcoe: the FCoE sw object
489  * @dev: the device that the pool is associated with
490  * @cpu: the cpu for this pool
491  *
492  * Returns 0 on successful or non zero on failure
493  *
494  **/
495 static int i40e_fcoe_dma_pool_create(struct i40e_fcoe *fcoe,
496                                      struct device *dev,
497                                      unsigned int cpu)
498 {
499         struct i40e_fcoe_ddp_pool *ddp_pool;
500         struct dma_pool *pool;
501         char pool_name[32];
502
503         ddp_pool = per_cpu_ptr(fcoe->ddp_pool, cpu);
504         if (ddp_pool && ddp_pool->pool) {
505                 dev_warn(dev, "DDP pool already allocated for cpu %d\n", cpu);
506                 return 0;
507         }
508         snprintf(pool_name, sizeof(pool_name), "i40e_fcoe_ddp_%d", cpu);
509         pool = dma_pool_create(pool_name, dev, I40E_FCOE_DDP_PTR_MAX,
510                                I40E_FCOE_DDP_PTR_ALIGN, PAGE_SIZE);
511         if (!pool) {
512                 dev_err(dev, "dma_pool_create %s failed\n", pool_name);
513                 return -ENOMEM;
514         }
515         ddp_pool->pool = pool;
516         return 0;
517 }
518
519 /**
520  * i40e_fcoe_free_ddp_resources - release FCoE DDP resources
521  * @vsi: the vsi FCoE is associated with
522  *
523  **/
524 void i40e_fcoe_free_ddp_resources(struct i40e_vsi *vsi)
525 {
526         struct i40e_pf *pf = vsi->back;
527         struct i40e_fcoe *fcoe = &pf->fcoe;
528         int cpu, i;
529
530         /* do nothing if not FCoE VSI */
531         if (vsi->type != I40E_VSI_FCOE)
532                 return;
533
534         /* do nothing if no DDP pools were allocated */
535         if (!fcoe->ddp_pool)
536                 return;
537
538         for (i = 0; i < I40E_FCOE_DDP_MAX; i++)
539                 i40e_fcoe_ddp_put(vsi->netdev, i);
540
541         for_each_possible_cpu(cpu)
542                 i40e_fcoe_dma_pool_free(fcoe, &pf->pdev->dev, cpu);
543
544         free_percpu(fcoe->ddp_pool);
545         fcoe->ddp_pool = NULL;
546
547         netdev_info(vsi->netdev, "VSI %d,%d FCoE DDP resources released\n",
548                     vsi->id, vsi->seid);
549 }
550
551 /**
552  * i40e_fcoe_setup_ddp_resources - allocate per cpu DDP resources
553  * @vsi: the VSI FCoE is associated with
554  *
555  * Returns 0 on successful or non zero on failure
556  *
557  **/
558 int i40e_fcoe_setup_ddp_resources(struct i40e_vsi *vsi)
559 {
560         struct i40e_pf *pf = vsi->back;
561         struct device *dev = &pf->pdev->dev;
562         struct i40e_fcoe *fcoe = &pf->fcoe;
563         unsigned int cpu;
564         int i;
565
566         if (vsi->type != I40E_VSI_FCOE)
567                 return -ENODEV;
568
569         /* do nothing if no DDP pools were allocated */
570         if (fcoe->ddp_pool)
571                 return -EEXIST;
572
573         /* allocate per CPU memory to track DDP pools */
574         fcoe->ddp_pool = alloc_percpu(struct i40e_fcoe_ddp_pool);
575         if (!fcoe->ddp_pool) {
576                 dev_err(&pf->pdev->dev, "failed to allocate percpu DDP\n");
577                 return -ENOMEM;
578         }
579
580         /* allocate pci pool for each cpu */
581         for_each_possible_cpu(cpu) {
582                 if (!i40e_fcoe_dma_pool_create(fcoe, dev, cpu))
583                         continue;
584
585                 dev_err(dev, "failed to alloc DDP pool on cpu:%d\n", cpu);
586                 i40e_fcoe_free_ddp_resources(vsi);
587                 return -ENOMEM;
588         }
589
590         /* initialize the sw context */
591         for (i = 0; i < I40E_FCOE_DDP_MAX; i++)
592                 i40e_fcoe_ddp_clear(&fcoe->ddp[i]);
593
594         netdev_info(vsi->netdev, "VSI %d,%d FCoE DDP resources allocated\n",
595                     vsi->id, vsi->seid);
596
597         return 0;
598 }
599
600 /**
601  * i40e_fcoe_handle_status - check the Programming Status for FCoE
602  * @rx_ring: the Rx ring for this descriptor
603  * @rx_desc: the Rx descriptor for Programming Status, not a packet descriptor.
604  *
605  * Check if this is the Rx Programming Status descriptor write-back for FCoE.
606  * This is used to verify if the context/filter programming or invalidation
607  * requested by SW to the HW is successful or not and take actions accordingly.
608  **/
609 void i40e_fcoe_handle_status(struct i40e_ring *rx_ring,
610                              union i40e_rx_desc *rx_desc, u8 prog_id)
611 {
612         struct i40e_pf *pf = rx_ring->vsi->back;
613         struct i40e_fcoe *fcoe = &pf->fcoe;
614         struct i40e_fcoe_ddp *ddp;
615         u32 error;
616         u16 xid;
617         u64 qw;
618
619         /* we only care for FCoE here */
620         if (!i40e_fcoe_progid_is_fcoe(prog_id))
621                 return;
622
623         xid = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fcoe_param) &
624               (I40E_FCOE_DDP_MAX - 1);
625
626         if (!i40e_fcoe_xid_is_valid(xid))
627                 return;
628
629         ddp = &fcoe->ddp[xid];
630         WARN_ON(xid != ddp->xid);
631
632         qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
633         error = (qw & I40E_RX_PROG_STATUS_DESC_QW1_ERROR_MASK) >>
634                 I40E_RX_PROG_STATUS_DESC_QW1_ERROR_SHIFT;
635
636         /* DDP context programming status: failure or success */
637         if (prog_id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_PROG_STATUS) {
638                 if (I40E_RX_PROG_FCOE_ERROR_TBL_FULL(error)) {
639                         dev_err(&pf->pdev->dev, "xid %x ddp->xid %x TABLE FULL\n",
640                                 xid, ddp->xid);
641                         ddp->prerr |= I40E_RX_PROG_FCOE_ERROR_TBL_FULL_BIT;
642                 }
643                 if (I40E_RX_PROG_FCOE_ERROR_CONFLICT(error)) {
644                         dev_err(&pf->pdev->dev, "xid %x ddp->xid %x CONFLICT\n",
645                                 xid, ddp->xid);
646                         ddp->prerr |= I40E_RX_PROG_FCOE_ERROR_CONFLICT_BIT;
647                 }
648         }
649
650         /* DDP context invalidation status: failure or success */
651         if (prog_id == I40E_RX_PROG_STATUS_DESC_FCOE_CTXT_INVL_STATUS) {
652                 if (I40E_RX_PROG_FCOE_ERROR_INVLFAIL(error)) {
653                         dev_err(&pf->pdev->dev, "xid %x ddp->xid %x INVALIDATION FAILURE\n",
654                                 xid, ddp->xid);
655                         ddp->prerr |= I40E_RX_PROG_FCOE_ERROR_INVLFAIL_BIT;
656                 }
657                 /* clear the flag so we can retry invalidation */
658                 clear_bit(__I40E_FCOE_DDP_ABORTED, &ddp->flags);
659         }
660
661         /* unmap DMA */
662         i40e_fcoe_ddp_unmap(pf, ddp);
663         i40e_fcoe_ddp_clear(ddp);
664 }
665
666 /**
667  * i40e_fcoe_handle_offload - check ddp status and mark it done
668  * @adapter: i40e adapter
669  * @rx_desc: advanced rx descriptor
670  * @skb: the skb holding the received data
671  *
672  * This checks ddp status.
673  *
674  * Returns : < 0 indicates an error or not a FCOE ddp, 0 indicates
675  * not passing the skb to ULD, > 0 indicates is the length of data
676  * being ddped.
677  *
678  **/
679 int i40e_fcoe_handle_offload(struct i40e_ring *rx_ring,
680                              union i40e_rx_desc *rx_desc,
681                              struct sk_buff *skb)
682 {
683         struct i40e_pf *pf = rx_ring->vsi->back;
684         struct i40e_fcoe *fcoe = &pf->fcoe;
685         struct fc_frame_header *fh = NULL;
686         struct i40e_fcoe_ddp *ddp = NULL;
687         u32 status, fltstat;
688         u32 error, fcerr;
689         int rc = -EINVAL;
690         u16 ptype;
691         u16 xid;
692         u64 qw;
693
694         /* check this rxd is for programming status */
695         qw = le64_to_cpu(rx_desc->wb.qword1.status_error_len);
696         /* packet descriptor, check packet type */
697         ptype = (qw & I40E_RXD_QW1_PTYPE_MASK) >> I40E_RXD_QW1_PTYPE_SHIFT;
698         if (!i40e_rx_is_fcoe(ptype))
699                 goto out_no_ddp;
700
701         error = (qw & I40E_RXD_QW1_ERROR_MASK) >> I40E_RXD_QW1_ERROR_SHIFT;
702         fcerr = (error >> I40E_RX_DESC_ERROR_L3L4E_SHIFT) &
703                  I40E_RX_DESC_FCOE_ERROR_MASK;
704
705         /* check stateless offload error */
706         if (unlikely(fcerr == I40E_RX_DESC_ERROR_L3L4E_PROT)) {
707                 dev_err(&pf->pdev->dev, "Protocol Error\n");
708                 skb->ip_summed = CHECKSUM_NONE;
709         } else {
710                 skb->ip_summed = CHECKSUM_UNNECESSARY;
711         }
712
713         /* check hw status on ddp */
714         status = (qw & I40E_RXD_QW1_STATUS_MASK) >> I40E_RXD_QW1_STATUS_SHIFT;
715         fltstat = (status >> I40E_RX_DESC_STATUS_FLTSTAT_SHIFT) &
716                    I40E_RX_DESC_FLTSTAT_FCMASK;
717
718         /* now we are ready to check DDP */
719         fh = i40e_fcoe_fc_frame_header(skb);
720         xid = i40e_fcoe_fc_get_xid(fh);
721         if (!i40e_fcoe_xid_is_valid(xid))
722                 goto out_no_ddp;
723
724         /* non DDP normal receive, return to the protocol stack */
725         if (fltstat == I40E_RX_DESC_FLTSTAT_NOMTCH)
726                 goto out_no_ddp;
727
728         /* do we have a sw ddp context setup ? */
729         ddp = &fcoe->ddp[xid];
730         if (!ddp->sgl)
731                 goto out_no_ddp;
732
733         /* fetch xid from hw rxd wb, which should match up the sw ctxt */
734         xid = le16_to_cpu(rx_desc->wb.qword0.lo_dword.mirr_fcoe.fcoe_ctx_id);
735         if (ddp->xid != xid) {
736                 dev_err(&pf->pdev->dev, "xid 0x%x does not match ctx_xid 0x%x\n",
737                         ddp->xid, xid);
738                 goto out_put_ddp;
739         }
740
741         /* the same exchange has already errored out */
742         if (ddp->fcerr) {
743                 dev_err(&pf->pdev->dev, "xid 0x%x fcerr 0x%x reported fcer 0x%x\n",
744                         xid, ddp->fcerr, fcerr);
745                 goto out_put_ddp;
746         }
747
748         /* fcoe param is valid by now with correct DDPed length */
749         ddp->len = le32_to_cpu(rx_desc->wb.qword0.hi_dword.fcoe_param);
750         ddp->fcerr = fcerr;
751         /* header posting only, useful only for target mode and debugging */
752         if (fltstat == I40E_RX_DESC_FLTSTAT_DDP) {
753                 /* For target mode, we get header of the last packet but it
754                  * does not have the FCoE trailer field, i.e., CRC and EOF
755                  * Ordered Set since they are offloaded by the HW, so fill
756                  * it up correspondingly to allow the packet to pass through
757                  * to the upper protocol stack.
758                  */
759                 u32 f_ctl = ntoh24(fh->fh_f_ctl);
760
761                 if ((f_ctl & FC_FC_END_SEQ) &&
762                     (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA)) {
763                         struct fcoe_crc_eof *crc = NULL;
764
765                         crc = skb_put(skb, sizeof(*crc));
766                         crc->fcoe_eof = FC_EOF_T;
767                 } else {
768                         /* otherwise, drop the header only frame */
769                         rc = 0;
770                         goto out_no_ddp;
771                 }
772         }
773
774 out_put_ddp:
775         /* either we got RSP or we have an error, unmap DMA in both cases */
776         i40e_fcoe_ddp_unmap(pf, ddp);
777         if (ddp->len && !ddp->fcerr) {
778                 int pkts;
779
780                 rc = ddp->len;
781                 i40e_fcoe_ddp_clear(ddp);
782                 ddp->len = rc;
783                 pkts = DIV_ROUND_UP(rc, 2048);
784                 rx_ring->stats.bytes += rc;
785                 rx_ring->stats.packets += pkts;
786                 rx_ring->q_vector->rx.total_bytes += rc;
787                 rx_ring->q_vector->rx.total_packets += pkts;
788                 set_bit(__I40E_FCOE_DDP_DONE, &ddp->flags);
789         }
790
791 out_no_ddp:
792         return rc;
793 }
794
795 /**
796  * i40e_fcoe_ddp_setup - called to set up ddp context
797  * @netdev: the corresponding net_device
798  * @xid: the exchange id requesting ddp
799  * @sgl: the scatter-gather list for this request
800  * @sgc: the number of scatter-gather items
801  * @target_mode: indicates this is a DDP request for target
802  *
803  * Returns : 1 for success and 0 for no DDP on this I/O
804  **/
805 static int i40e_fcoe_ddp_setup(struct net_device *netdev, u16 xid,
806                                struct scatterlist *sgl, unsigned int sgc,
807                                int target_mode)
808 {
809         static const unsigned int bufflen = I40E_FCOE_DDP_BUF_MIN;
810         struct i40e_netdev_priv *np = netdev_priv(netdev);
811         struct i40e_fcoe_ddp_pool *ddp_pool;
812         struct i40e_pf *pf = np->vsi->back;
813         struct i40e_fcoe *fcoe = &pf->fcoe;
814         unsigned int i, j, dmacount;
815         struct i40e_fcoe_ddp *ddp;
816         unsigned int firstoff = 0;
817         unsigned int thisoff = 0;
818         unsigned int thislen = 0;
819         struct scatterlist *sg;
820         dma_addr_t addr = 0;
821         unsigned int len;
822
823         if (xid >= I40E_FCOE_DDP_MAX) {
824                 dev_warn(&pf->pdev->dev, "xid=0x%x out-of-range\n", xid);
825                 return 0;
826         }
827
828         /* no DDP if we are already down or resetting */
829         if (test_bit(__I40E_DOWN, &pf->state) ||
830             test_bit(__I40E_NEEDS_RESTART, &pf->state)) {
831                 dev_info(&pf->pdev->dev, "xid=0x%x device in reset/down\n",
832                          xid);
833                 return 0;
834         }
835
836         ddp = &fcoe->ddp[xid];
837         if (ddp->sgl) {
838                 dev_info(&pf->pdev->dev, "xid 0x%x w/ non-null sgl=%p nents=%d\n",
839                          xid, ddp->sgl, ddp->sgc);
840                 return 0;
841         }
842         i40e_fcoe_ddp_clear(ddp);
843
844         if (!fcoe->ddp_pool) {
845                 dev_info(&pf->pdev->dev, "No DDP pool, xid 0x%x\n", xid);
846                 return 0;
847         }
848
849         ddp_pool = per_cpu_ptr(fcoe->ddp_pool, get_cpu());
850         if (!ddp_pool->pool) {
851                 dev_info(&pf->pdev->dev, "No percpu ddp pool, xid 0x%x\n", xid);
852                 goto out_noddp;
853         }
854
855         /* setup dma from scsi command sgl */
856         dmacount = dma_map_sg(&pf->pdev->dev, sgl, sgc, DMA_FROM_DEVICE);
857         if (dmacount == 0) {
858                 dev_info(&pf->pdev->dev, "dma_map_sg for sgl %p, sgc %d failed\n",
859                          sgl, sgc);
860                 goto out_noddp_unmap;
861         }
862
863         /* alloc the udl from our ddp pool */
864         ddp->udl = dma_pool_alloc(ddp_pool->pool, GFP_ATOMIC, &ddp->udp);
865         if (!ddp->udl) {
866                 dev_info(&pf->pdev->dev,
867                          "Failed allocated ddp context, xid 0x%x\n", xid);
868                 goto out_noddp_unmap;
869         }
870
871         j = 0;
872         ddp->len = 0;
873         for_each_sg(sgl, sg, dmacount, i) {
874                 addr = sg_dma_address(sg);
875                 len = sg_dma_len(sg);
876                 ddp->len += len;
877                 while (len) {
878                         /* max number of buffers allowed in one DDP context */
879                         if (j >= I40E_FCOE_DDP_BUFFCNT_MAX) {
880                                 dev_info(&pf->pdev->dev,
881                                          "xid=%x:%d,%d,%d:addr=%llx not enough descriptors\n",
882                                          xid, i, j, dmacount, (u64)addr);
883                                 goto out_noddp_free;
884                         }
885
886                         /* get the offset of length of current buffer */
887                         thisoff = addr & ((dma_addr_t)bufflen - 1);
888                         thislen = min_t(unsigned int, (bufflen - thisoff), len);
889                         /* all but the 1st buffer (j == 0)
890                          * must be aligned on bufflen
891                          */
892                         if ((j != 0) && (thisoff))
893                                 goto out_noddp_free;
894
895                         /* all but the last buffer
896                          * ((i == (dmacount - 1)) && (thislen == len))
897                          * must end at bufflen
898                          */
899                         if (((i != (dmacount - 1)) || (thislen != len)) &&
900                             ((thislen + thisoff) != bufflen))
901                                 goto out_noddp_free;
902
903                         ddp->udl[j] = (u64)(addr - thisoff);
904                         /* only the first buffer may have none-zero offset */
905                         if (j == 0)
906                                 firstoff = thisoff;
907                         len -= thislen;
908                         addr += thislen;
909                         j++;
910                 }
911         }
912         /* only the last buffer may have non-full bufflen */
913         ddp->lastsize = thisoff + thislen;
914         ddp->firstoff = firstoff;
915         ddp->list_len = j;
916         ddp->pool = ddp_pool->pool;
917         ddp->sgl = sgl;
918         ddp->sgc = sgc;
919         ddp->xid = xid;
920         if (target_mode)
921                 set_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags);
922         set_bit(__I40E_FCOE_DDP_INITALIZED, &ddp->flags);
923
924         put_cpu();
925         return 1; /* Success */
926
927 out_noddp_free:
928         dma_pool_free(ddp->pool, ddp->udl, ddp->udp);
929         i40e_fcoe_ddp_clear(ddp);
930
931 out_noddp_unmap:
932         dma_unmap_sg(&pf->pdev->dev, sgl, sgc, DMA_FROM_DEVICE);
933 out_noddp:
934         put_cpu();
935         return 0;
936 }
937
938 /**
939  * i40e_fcoe_ddp_get - called to set up ddp context in initiator mode
940  * @netdev: the corresponding net_device
941  * @xid: the exchange id requesting ddp
942  * @sgl: the scatter-gather list for this request
943  * @sgc: the number of scatter-gather items
944  *
945  * This is the implementation of net_device_ops.ndo_fcoe_ddp_setup
946  * and is expected to be called from ULD, e.g., FCP layer of libfc
947  * to set up ddp for the corresponding xid of the given sglist for
948  * the corresponding I/O.
949  *
950  * Returns : 1 for success and 0 for no ddp
951  **/
952 static int i40e_fcoe_ddp_get(struct net_device *netdev, u16 xid,
953                              struct scatterlist *sgl, unsigned int sgc)
954 {
955         return i40e_fcoe_ddp_setup(netdev, xid, sgl, sgc, 0);
956 }
957
958 /**
959  * i40e_fcoe_ddp_target - called to set up ddp context in target mode
960  * @netdev: the corresponding net_device
961  * @xid: the exchange id requesting ddp
962  * @sgl: the scatter-gather list for this request
963  * @sgc: the number of scatter-gather items
964  *
965  * This is the implementation of net_device_ops.ndo_fcoe_ddp_target
966  * and is expected to be called from ULD, e.g., FCP layer of libfc
967  * to set up ddp for the corresponding xid of the given sglist for
968  * the corresponding I/O. The DDP in target mode is a write I/O request
969  * from the initiator.
970  *
971  * Returns : 1 for success and 0 for no ddp
972  **/
973 static int i40e_fcoe_ddp_target(struct net_device *netdev, u16 xid,
974                                 struct scatterlist *sgl, unsigned int sgc)
975 {
976         return i40e_fcoe_ddp_setup(netdev, xid, sgl, sgc, 1);
977 }
978
979 /**
980  * i40e_fcoe_program_ddp - programs the HW DDP related descriptors
981  * @tx_ring: transmit ring for this packet
982  * @skb:     the packet to be sent out
983  * @sof: the SOF to indicate class of service
984  *
985  * Determine if it is READ/WRITE command, and finds out if there is
986  * a matching SW DDP context for this command. DDP is applicable
987  * only in case of READ if initiator or WRITE in case of
988  * responder (via checking XFER_RDY).
989  *
990  * Note: caller checks sof and ddp sw context
991  *
992  * Returns : none
993  *
994  **/
995 static void i40e_fcoe_program_ddp(struct i40e_ring *tx_ring,
996                                   struct sk_buff *skb,
997                                   struct i40e_fcoe_ddp *ddp, u8 sof)
998 {
999         struct i40e_fcoe_filter_context_desc *filter_desc = NULL;
1000         struct i40e_fcoe_queue_context_desc *queue_desc = NULL;
1001         struct i40e_fcoe_ddp_context_desc *ddp_desc = NULL;
1002         struct i40e_pf *pf = tx_ring->vsi->back;
1003         u16 i = tx_ring->next_to_use;
1004         struct fc_frame_header *fh;
1005         u64 flags_rsvd_lanq = 0;
1006         bool target_mode;
1007
1008         /* check if abort is still pending */
1009         if (test_bit(__I40E_FCOE_DDP_ABORTED, &ddp->flags)) {
1010                 dev_warn(&pf->pdev->dev,
1011                          "DDP abort is still pending xid:%hx and ddp->flags:%lx:\n",
1012                          ddp->xid, ddp->flags);
1013                 return;
1014         }
1015
1016         /* set the flag to indicate this is programmed */
1017         if (test_and_set_bit(__I40E_FCOE_DDP_PROGRAMMED, &ddp->flags)) {
1018                 dev_warn(&pf->pdev->dev,
1019                          "DDP is already programmed for xid:%hx and ddp->flags:%lx:\n",
1020                          ddp->xid, ddp->flags);
1021                 return;
1022         }
1023
1024         /* Prepare the DDP context descriptor */
1025         ddp_desc = I40E_DDP_CONTEXT_DESC(tx_ring, i);
1026         i++;
1027         if (i == tx_ring->count)
1028                 i = 0;
1029
1030         ddp_desc->type_cmd_foff_lsize =
1031                                 cpu_to_le64(I40E_TX_DESC_DTYPE_DDP_CTX  |
1032                                 ((u64)I40E_FCOE_DDP_CTX_DESC_BSIZE_4K  <<
1033                                 I40E_FCOE_DDP_CTX_QW1_CMD_SHIFT)        |
1034                                 ((u64)ddp->firstoff                    <<
1035                                 I40E_FCOE_DDP_CTX_QW1_FOFF_SHIFT)       |
1036                                 ((u64)ddp->lastsize                    <<
1037                                 I40E_FCOE_DDP_CTX_QW1_LSIZE_SHIFT));
1038         ddp_desc->rsvd = cpu_to_le64(0);
1039
1040         /* target mode needs last packet in the sequence  */
1041         target_mode = test_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags);
1042         if (target_mode)
1043                 ddp_desc->type_cmd_foff_lsize |=
1044                         cpu_to_le64(I40E_FCOE_DDP_CTX_DESC_LASTSEQH);
1045
1046         /* Prepare queue_context descriptor */
1047         queue_desc = I40E_QUEUE_CONTEXT_DESC(tx_ring, i++);
1048         if (i == tx_ring->count)
1049                 i = 0;
1050         queue_desc->dmaindx_fbase = cpu_to_le64(ddp->xid | ((u64)ddp->udp));
1051         queue_desc->flen_tph = cpu_to_le64(ddp->list_len |
1052                                 ((u64)(I40E_FCOE_QUEUE_CTX_DESC_TPHRDESC |
1053                                 I40E_FCOE_QUEUE_CTX_DESC_TPHDATA) <<
1054                                 I40E_FCOE_QUEUE_CTX_QW1_TPH_SHIFT));
1055
1056         /* Prepare filter_context_desc */
1057         filter_desc = I40E_FILTER_CONTEXT_DESC(tx_ring, i);
1058         i++;
1059         if (i == tx_ring->count)
1060                 i = 0;
1061
1062         fh = (struct fc_frame_header *)skb_transport_header(skb);
1063         filter_desc->param = cpu_to_le32(ntohl(fh->fh_parm_offset));
1064         filter_desc->seqn = cpu_to_le16(ntohs(fh->fh_seq_cnt));
1065         filter_desc->rsvd_dmaindx = cpu_to_le16(ddp->xid <<
1066                                 I40E_FCOE_FILTER_CTX_QW0_DMAINDX_SHIFT);
1067
1068         flags_rsvd_lanq = I40E_FCOE_FILTER_CTX_DESC_CTYP_DDP;
1069         flags_rsvd_lanq |= (u64)(target_mode ?
1070                         I40E_FCOE_FILTER_CTX_DESC_ENODE_RSP :
1071                         I40E_FCOE_FILTER_CTX_DESC_ENODE_INIT);
1072
1073         flags_rsvd_lanq |= (u64)((sof == FC_SOF_I2 || sof == FC_SOF_N2) ?
1074                         I40E_FCOE_FILTER_CTX_DESC_FC_CLASS2 :
1075                         I40E_FCOE_FILTER_CTX_DESC_FC_CLASS3);
1076
1077         flags_rsvd_lanq |= ((u64)skb->queue_mapping <<
1078                                 I40E_FCOE_FILTER_CTX_QW1_LANQINDX_SHIFT);
1079         filter_desc->flags_rsvd_lanq = cpu_to_le64(flags_rsvd_lanq);
1080
1081         /* By this time, all offload related descriptors has been programmed */
1082         tx_ring->next_to_use = i;
1083 }
1084
1085 /**
1086  * i40e_fcoe_invalidate_ddp - invalidates DDP in case of abort
1087  * @tx_ring: transmit ring for this packet
1088  * @skb: the packet associated w/ this DDP invalidation, i.e., ABTS
1089  * @ddp: the SW DDP context for this DDP
1090  *
1091  * Programs the Tx context descriptor to do DDP invalidation.
1092  **/
1093 static void i40e_fcoe_invalidate_ddp(struct i40e_ring *tx_ring,
1094                                      struct sk_buff *skb,
1095                                      struct i40e_fcoe_ddp *ddp)
1096 {
1097         struct i40e_tx_context_desc *context_desc;
1098         int i;
1099
1100         if (test_and_set_bit(__I40E_FCOE_DDP_ABORTED, &ddp->flags))
1101                 return;
1102
1103         i = tx_ring->next_to_use;
1104         context_desc = I40E_TX_CTXTDESC(tx_ring, i);
1105         i++;
1106         if (i == tx_ring->count)
1107                 i = 0;
1108
1109         context_desc->tunneling_params = cpu_to_le32(0);
1110         context_desc->l2tag2 = cpu_to_le16(0);
1111         context_desc->rsvd = cpu_to_le16(0);
1112         context_desc->type_cmd_tso_mss = cpu_to_le64(
1113                 I40E_TX_DESC_DTYPE_FCOE_CTX |
1114                 (I40E_FCOE_TX_CTX_DESC_OPCODE_DDP_CTX_INVL <<
1115                 I40E_TXD_CTX_QW1_CMD_SHIFT) |
1116                 (I40E_FCOE_TX_CTX_DESC_OPCODE_SINGLE_SEND <<
1117                 I40E_TXD_CTX_QW1_CMD_SHIFT));
1118         tx_ring->next_to_use = i;
1119 }
1120
1121 /**
1122  * i40e_fcoe_handle_ddp - check we should setup or invalidate DDP
1123  * @tx_ring: transmit ring for this packet
1124  * @skb: the packet to be sent out
1125  * @sof: the SOF to indicate class of service
1126  *
1127  * Determine if it is ABTS/READ/XFER_RDY, and finds out if there is
1128  * a matching SW DDP context for this command. DDP is applicable
1129  * only in case of READ if initiator or WRITE in case of
1130  * responder (via checking XFER_RDY). In case this is an ABTS, send
1131  * just invalidate the context.
1132  **/
1133 static void i40e_fcoe_handle_ddp(struct i40e_ring *tx_ring,
1134                                  struct sk_buff *skb, u8 sof)
1135 {
1136         struct i40e_pf *pf = tx_ring->vsi->back;
1137         struct i40e_fcoe *fcoe = &pf->fcoe;
1138         struct fc_frame_header *fh;
1139         struct i40e_fcoe_ddp *ddp;
1140         u32 f_ctl;
1141         u8 r_ctl;
1142         u16 xid;
1143
1144         fh = (struct fc_frame_header *)skb_transport_header(skb);
1145         f_ctl = ntoh24(fh->fh_f_ctl);
1146         r_ctl = fh->fh_r_ctl;
1147         ddp = NULL;
1148
1149         if ((r_ctl == FC_RCTL_DD_DATA_DESC) && (f_ctl & FC_FC_EX_CTX)) {
1150                 /* exchange responder? if so, XFER_RDY for write */
1151                 xid = ntohs(fh->fh_rx_id);
1152                 if (i40e_fcoe_xid_is_valid(xid)) {
1153                         ddp = &fcoe->ddp[xid];
1154                         if ((ddp->xid == xid) &&
1155                             (test_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags)))
1156                                 i40e_fcoe_program_ddp(tx_ring, skb, ddp, sof);
1157                 }
1158         } else if (r_ctl == FC_RCTL_DD_UNSOL_CMD) {
1159                 /* exchange originator, check READ cmd */
1160                 xid = ntohs(fh->fh_ox_id);
1161                 if (i40e_fcoe_xid_is_valid(xid)) {
1162                         ddp = &fcoe->ddp[xid];
1163                         if ((ddp->xid == xid) &&
1164                             (!test_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags)))
1165                                 i40e_fcoe_program_ddp(tx_ring, skb, ddp, sof);
1166                 }
1167         } else if (r_ctl == FC_RCTL_BA_ABTS) {
1168                 /* exchange originator, check ABTS */
1169                 xid = ntohs(fh->fh_ox_id);
1170                 if (i40e_fcoe_xid_is_valid(xid)) {
1171                         ddp = &fcoe->ddp[xid];
1172                         if ((ddp->xid == xid) &&
1173                             (!test_bit(__I40E_FCOE_DDP_TARGET, &ddp->flags)))
1174                                 i40e_fcoe_invalidate_ddp(tx_ring, skb, ddp);
1175                 }
1176         }
1177 }
1178
1179 /**
1180  * i40e_fcoe_tso - set up FCoE TSO
1181  * @tx_ring:  ring to send buffer on
1182  * @skb:      send buffer
1183  * @tx_flags: collected send information
1184  * @hdr_len:  the tso header length
1185  * @sof: the SOF to indicate class of service
1186  *
1187  * Note must already have sof checked to be either class 2 or class 3 before
1188  * calling this function.
1189  *
1190  * Returns 1 to indicate sequence segmentation offload is properly setup
1191  * or returns 0 to indicate no tso is needed, otherwise returns error
1192  * code to drop the frame.
1193  **/
1194 static int i40e_fcoe_tso(struct i40e_ring *tx_ring,
1195                          struct sk_buff *skb,
1196                          u32 tx_flags, u8 *hdr_len, u8 sof)
1197 {
1198         struct i40e_tx_context_desc *context_desc;
1199         u32 cd_type, cd_cmd, cd_tso_len, cd_mss;
1200         struct fc_frame_header *fh;
1201         u64 cd_type_cmd_tso_mss;
1202
1203         /* must match gso type as FCoE */
1204         if (!skb_is_gso(skb))
1205                 return 0;
1206
1207         /* is it the expected gso type for FCoE ?*/
1208         if (skb_shinfo(skb)->gso_type != SKB_GSO_FCOE) {
1209                 netdev_err(skb->dev,
1210                            "wrong gso type %d:expecting SKB_GSO_FCOE\n",
1211                            skb_shinfo(skb)->gso_type);
1212                 return -EINVAL;
1213         }
1214
1215         /* header and trailer are inserted by hw */
1216         *hdr_len = skb_transport_offset(skb) + sizeof(struct fc_frame_header) +
1217                    sizeof(struct fcoe_crc_eof);
1218
1219         /* check sof to decide a class 2 or 3 TSO */
1220         if (likely(i40e_fcoe_sof_is_class3(sof)))
1221                 cd_cmd = I40E_FCOE_TX_CTX_DESC_OPCODE_TSO_FC_CLASS3;
1222         else
1223                 cd_cmd = I40E_FCOE_TX_CTX_DESC_OPCODE_TSO_FC_CLASS2;
1224
1225         /* param field valid? */
1226         fh = (struct fc_frame_header *)skb_transport_header(skb);
1227         if (fh->fh_f_ctl[2] & FC_FC_REL_OFF)
1228                 cd_cmd |= I40E_FCOE_TX_CTX_DESC_RELOFF;
1229
1230         /* fill the field values */
1231         cd_type = I40E_TX_DESC_DTYPE_FCOE_CTX;
1232         cd_tso_len = skb->len - *hdr_len;
1233         cd_mss = skb_shinfo(skb)->gso_size;
1234         cd_type_cmd_tso_mss =
1235                 ((u64)cd_type  << I40E_TXD_CTX_QW1_DTYPE_SHIFT)     |
1236                 ((u64)cd_cmd     << I40E_TXD_CTX_QW1_CMD_SHIFT)     |
1237                 ((u64)cd_tso_len << I40E_TXD_CTX_QW1_TSO_LEN_SHIFT) |
1238                 ((u64)cd_mss     << I40E_TXD_CTX_QW1_MSS_SHIFT);
1239
1240         /* grab the next descriptor */
1241         context_desc = I40E_TX_CTXTDESC(tx_ring, tx_ring->next_to_use);
1242         tx_ring->next_to_use++;
1243         if (tx_ring->next_to_use == tx_ring->count)
1244                 tx_ring->next_to_use = 0;
1245
1246         context_desc->tunneling_params = 0;
1247         context_desc->l2tag2 = cpu_to_le16((tx_flags & I40E_TX_FLAGS_VLAN_MASK)
1248                                             >> I40E_TX_FLAGS_VLAN_SHIFT);
1249         context_desc->type_cmd_tso_mss = cpu_to_le64(cd_type_cmd_tso_mss);
1250
1251         return 1;
1252 }
1253
1254 /**
1255  * i40e_fcoe_tx_map - build the tx descriptor
1256  * @tx_ring:  ring to send buffer on
1257  * @skb:      send buffer
1258  * @first:    first buffer info buffer to use
1259  * @tx_flags: collected send information
1260  * @hdr_len:  ptr to the size of the packet header
1261  * @eof:      the frame eof value
1262  *
1263  * Note, for FCoE, sof and eof are already checked
1264  **/
1265 static void i40e_fcoe_tx_map(struct i40e_ring *tx_ring,
1266                              struct sk_buff *skb,
1267                              struct i40e_tx_buffer *first,
1268                              u32 tx_flags, u8 hdr_len, u8 eof)
1269 {
1270         u32 td_offset = 0;
1271         u32 td_cmd = 0;
1272         u32 maclen;
1273
1274         /* insert CRC */
1275         td_cmd = I40E_TX_DESC_CMD_ICRC;
1276
1277         /* setup MACLEN */
1278         maclen = skb_network_offset(skb);
1279         if (tx_flags & I40E_TX_FLAGS_SW_VLAN)
1280                 maclen += sizeof(struct vlan_hdr);
1281
1282         if (skb->protocol == htons(ETH_P_FCOE)) {
1283                 /* for FCoE, maclen should exclude ether type */
1284                 maclen -= 2;
1285                 /* setup type as FCoE and EOF insertion */
1286                 td_cmd |= (I40E_TX_DESC_CMD_FCOET | i40e_fcoe_ctxt_eof(eof));
1287                 /* setup FCoELEN and FCLEN */
1288                 td_offset |= ((((sizeof(struct fcoe_hdr) + 2) >> 2) <<
1289                                 I40E_TX_DESC_LENGTH_IPLEN_SHIFT) |
1290                               ((sizeof(struct fc_frame_header) >> 2) <<
1291                                 I40E_TX_DESC_LENGTH_L4_FC_LEN_SHIFT));
1292                 /* trim to exclude trailer */
1293                 pskb_trim(skb, skb->len - sizeof(struct fcoe_crc_eof));
1294         }
1295
1296         /* MACLEN is ether header length in words not bytes */
1297         td_offset |= (maclen >> 1) << I40E_TX_DESC_LENGTH_MACLEN_SHIFT;
1298
1299         i40e_tx_map(tx_ring, skb, first, tx_flags, hdr_len, td_cmd, td_offset);
1300 }
1301
1302 /**
1303  * i40e_fcoe_set_skb_header - adjust skb header point for FIP/FCoE/FC
1304  * @skb: the skb to be adjusted
1305  *
1306  * Returns true if this skb is a FCoE/FIP or VLAN carried FCoE/FIP and then
1307  * adjusts the skb header pointers correspondingly. Otherwise, returns false.
1308  **/
1309 static inline int i40e_fcoe_set_skb_header(struct sk_buff *skb)
1310 {
1311         __be16 protocol = skb->protocol;
1312
1313         skb_reset_mac_header(skb);
1314         skb->mac_len = sizeof(struct ethhdr);
1315         if (protocol == htons(ETH_P_8021Q)) {
1316                 struct vlan_ethhdr *veth = (struct vlan_ethhdr *)eth_hdr(skb);
1317
1318                 protocol = veth->h_vlan_encapsulated_proto;
1319                 skb->mac_len += sizeof(struct vlan_hdr);
1320         }
1321
1322         /* FCoE or FIP only */
1323         if ((protocol != htons(ETH_P_FIP)) &&
1324             (protocol != htons(ETH_P_FCOE)))
1325                 return -EINVAL;
1326
1327         /* set header to L2 of FCoE/FIP */
1328         skb_set_network_header(skb, skb->mac_len);
1329         if (protocol == htons(ETH_P_FIP))
1330                 return 0;
1331
1332         /* set header to L3 of FC */
1333         skb_set_transport_header(skb, skb->mac_len + sizeof(struct fcoe_hdr));
1334         return 0;
1335 }
1336
1337 /**
1338  * i40e_fcoe_xmit_frame - transmit buffer
1339  * @skb:     send buffer
1340  * @netdev:  the fcoe netdev
1341  *
1342  * Returns 0 if sent, else an error code
1343  **/
1344 static netdev_tx_t i40e_fcoe_xmit_frame(struct sk_buff *skb,
1345                                         struct net_device *netdev)
1346 {
1347         struct i40e_netdev_priv *np = netdev_priv(skb->dev);
1348         struct i40e_vsi *vsi = np->vsi;
1349         struct i40e_ring *tx_ring = vsi->tx_rings[skb->queue_mapping];
1350         struct i40e_tx_buffer *first;
1351         u32 tx_flags = 0;
1352         int fso, count;
1353         u8 hdr_len = 0;
1354         u8 sof = 0;
1355         u8 eof = 0;
1356
1357         if (i40e_fcoe_set_skb_header(skb))
1358                 goto out_drop;
1359
1360         count = i40e_xmit_descriptor_count(skb);
1361         if (i40e_chk_linearize(skb, count)) {
1362                 if (__skb_linearize(skb))
1363                         goto out_drop;
1364                 count = i40e_txd_use_count(skb->len);
1365                 tx_ring->tx_stats.tx_linearize++;
1366         }
1367
1368         /* need: 1 descriptor per page * PAGE_SIZE/I40E_MAX_DATA_PER_TXD,
1369          *       + 1 desc for skb_head_len/I40E_MAX_DATA_PER_TXD,
1370          *       + 4 desc gap to avoid the cache line where head is,
1371          *       + 1 desc for context descriptor,
1372          * otherwise try next time
1373          */
1374         if (i40e_maybe_stop_tx(tx_ring, count + 4 + 1)) {
1375                 tx_ring->tx_stats.tx_busy++;
1376                 return NETDEV_TX_BUSY;
1377         }
1378
1379         /* prepare the xmit flags */
1380         if (i40e_tx_prepare_vlan_flags(skb, tx_ring, &tx_flags))
1381                 goto out_drop;
1382
1383         /* record the location of the first descriptor for this packet */
1384         first = &tx_ring->tx_bi[tx_ring->next_to_use];
1385
1386         /* FIP is a regular L2 traffic w/o offload */
1387         if (skb->protocol == htons(ETH_P_FIP))
1388                 goto out_send;
1389
1390         /* check sof and eof, only supports FC Class 2 or 3 */
1391         if (i40e_fcoe_fc_sof(skb, &sof) || i40e_fcoe_fc_eof(skb, &eof)) {
1392                 netdev_err(netdev, "SOF/EOF error:%02x - %02x\n", sof, eof);
1393                 goto out_drop;
1394         }
1395
1396         /* always do FCCRC for FCoE */
1397         tx_flags |= I40E_TX_FLAGS_FCCRC;
1398
1399         /* check we should do sequence offload */
1400         fso = i40e_fcoe_tso(tx_ring, skb, tx_flags, &hdr_len, sof);
1401         if (fso < 0)
1402                 goto out_drop;
1403         else if (fso)
1404                 tx_flags |= I40E_TX_FLAGS_FSO;
1405         else
1406                 i40e_fcoe_handle_ddp(tx_ring, skb, sof);
1407
1408 out_send:
1409         /* send out the packet */
1410         i40e_fcoe_tx_map(tx_ring, skb, first, tx_flags, hdr_len, eof);
1411
1412         i40e_maybe_stop_tx(tx_ring, DESC_NEEDED);
1413         return NETDEV_TX_OK;
1414
1415 out_drop:
1416         dev_kfree_skb_any(skb);
1417         return NETDEV_TX_OK;
1418 }
1419
1420 /**
1421  * i40e_fcoe_change_mtu - NDO callback to change the Maximum Transfer Unit
1422  * @netdev: network interface device structure
1423  * @new_mtu: new value for maximum frame size
1424  *
1425  * Returns error as operation not permitted
1426  *
1427  **/
1428 static int i40e_fcoe_change_mtu(struct net_device *netdev, int new_mtu)
1429 {
1430         netdev_warn(netdev, "MTU change is not supported on FCoE interfaces\n");
1431         return -EPERM;
1432 }
1433
1434 /**
1435  * i40e_fcoe_set_features - set the netdev feature flags
1436  * @netdev: ptr to the netdev being adjusted
1437  * @features: the feature set that the stack is suggesting
1438  *
1439  **/
1440 static int i40e_fcoe_set_features(struct net_device *netdev,
1441                                   netdev_features_t features)
1442 {
1443         struct i40e_netdev_priv *np = netdev_priv(netdev);
1444         struct i40e_vsi *vsi = np->vsi;
1445
1446         if (features & NETIF_F_HW_VLAN_CTAG_RX)
1447                 i40e_vlan_stripping_enable(vsi);
1448         else
1449                 i40e_vlan_stripping_disable(vsi);
1450
1451         return 0;
1452 }
1453
1454 static const struct net_device_ops i40e_fcoe_netdev_ops = {
1455         .ndo_open               = i40e_open,
1456         .ndo_stop               = i40e_close,
1457         .ndo_get_stats64        = i40e_get_netdev_stats_struct,
1458         .ndo_set_rx_mode        = i40e_set_rx_mode,
1459         .ndo_validate_addr      = eth_validate_addr,
1460         .ndo_set_mac_address    = i40e_set_mac,
1461         .ndo_change_mtu         = i40e_fcoe_change_mtu,
1462         .ndo_do_ioctl           = i40e_ioctl,
1463         .ndo_tx_timeout         = i40e_tx_timeout,
1464         .ndo_vlan_rx_add_vid    = i40e_vlan_rx_add_vid,
1465         .ndo_vlan_rx_kill_vid   = i40e_vlan_rx_kill_vid,
1466         .ndo_setup_tc           = __i40e_setup_tc,
1467
1468 #ifdef CONFIG_NET_POLL_CONTROLLER
1469         .ndo_poll_controller    = i40e_netpoll,
1470 #endif
1471         .ndo_start_xmit         = i40e_fcoe_xmit_frame,
1472         .ndo_fcoe_enable        = i40e_fcoe_enable,
1473         .ndo_fcoe_disable       = i40e_fcoe_disable,
1474         .ndo_fcoe_ddp_setup     = i40e_fcoe_ddp_get,
1475         .ndo_fcoe_ddp_done      = i40e_fcoe_ddp_put,
1476         .ndo_fcoe_ddp_target    = i40e_fcoe_ddp_target,
1477         .ndo_set_features       = i40e_fcoe_set_features,
1478 };
1479
1480 /* fcoe network device type */
1481 static struct device_type fcoe_netdev_type = {
1482         .name = "fcoe",
1483 };
1484
1485 /**
1486  * i40e_fcoe_config_netdev - prepares the VSI context for creating a FCoE VSI
1487  * @vsi: pointer to the associated VSI struct
1488  * @ctxt: pointer to the associated VSI context to be passed to HW
1489  *
1490  * Returns 0 on success or < 0 on error
1491  **/
1492 void i40e_fcoe_config_netdev(struct net_device *netdev, struct i40e_vsi *vsi)
1493 {
1494         struct i40e_hw *hw = &vsi->back->hw;
1495         struct i40e_pf *pf = vsi->back;
1496
1497         if (vsi->type != I40E_VSI_FCOE)
1498                 return;
1499
1500         netdev->features = (NETIF_F_HW_VLAN_CTAG_TX |
1501                             NETIF_F_HW_VLAN_CTAG_RX |
1502                             NETIF_F_HW_VLAN_CTAG_FILTER);
1503
1504         netdev->vlan_features = netdev->features;
1505         netdev->vlan_features &= ~(NETIF_F_HW_VLAN_CTAG_TX |
1506                                    NETIF_F_HW_VLAN_CTAG_RX |
1507                                    NETIF_F_HW_VLAN_CTAG_FILTER);
1508         netdev->fcoe_ddp_xid = I40E_FCOE_DDP_MAX - 1;
1509         netdev->features |= NETIF_F_ALL_FCOE;
1510         netdev->vlan_features |= NETIF_F_ALL_FCOE;
1511         netdev->hw_features |= netdev->features;
1512         netdev->priv_flags |= IFF_UNICAST_FLT;
1513         netdev->priv_flags |= IFF_SUPP_NOFCS;
1514
1515         strlcpy(netdev->name, "fcoe%d", IFNAMSIZ-1);
1516         netdev->mtu = FCOE_MTU;
1517         SET_NETDEV_DEV(netdev, &pf->pdev->dev);
1518         SET_NETDEV_DEVTYPE(netdev, &fcoe_netdev_type);
1519         /* set different dev_port value 1 for FCoE netdev than the default
1520          * zero dev_port value for PF netdev, this helps biosdevname user
1521          * tool to differentiate them correctly while both attached to the
1522          * same PCI function.
1523          */
1524         netdev->dev_port = 1;
1525         spin_lock_bh(&vsi->mac_filter_hash_lock);
1526         i40e_add_filter(vsi, hw->mac.san_addr, 0);
1527         i40e_add_filter(vsi, (u8[6]) FC_FCOE_FLOGI_MAC, 0);
1528         i40e_add_filter(vsi, FIP_ALL_FCOE_MACS, 0);
1529         i40e_add_filter(vsi, FIP_ALL_ENODE_MACS, 0);
1530         spin_unlock_bh(&vsi->mac_filter_hash_lock);
1531
1532         /* use san mac */
1533         ether_addr_copy(netdev->dev_addr, hw->mac.san_addr);
1534         ether_addr_copy(netdev->perm_addr, hw->mac.san_addr);
1535         /* fcoe netdev ops */
1536         netdev->netdev_ops = &i40e_fcoe_netdev_ops;
1537 }
1538
1539 /**
1540  * i40e_fcoe_vsi_setup - allocate and set up FCoE VSI
1541  * @pf: the PF that VSI is associated with
1542  *
1543  **/
1544 void i40e_fcoe_vsi_setup(struct i40e_pf *pf)
1545 {
1546         struct i40e_vsi *vsi;
1547         u16 seid;
1548         int i;
1549
1550         if (!(pf->flags & I40E_FLAG_FCOE_ENABLED))
1551                 return;
1552
1553         for (i = 0; i < pf->num_alloc_vsi; i++) {
1554                 vsi = pf->vsi[i];
1555                 if (vsi && vsi->type == I40E_VSI_FCOE) {
1556                         dev_warn(&pf->pdev->dev,
1557                                  "FCoE VSI already created\n");
1558                         return;
1559                 }
1560         }
1561
1562         seid = pf->vsi[pf->lan_vsi]->seid;
1563         vsi = i40e_vsi_setup(pf, I40E_VSI_FCOE, seid, 0);
1564         if (vsi) {
1565                 dev_dbg(&pf->pdev->dev,
1566                         "Successfully created FCoE VSI seid %d id %d uplink_seid %d PF seid %d\n",
1567                         vsi->seid, vsi->id, vsi->uplink_seid, seid);
1568         } else {
1569                 dev_info(&pf->pdev->dev, "Failed to create FCoE VSI\n");
1570         }
1571 }