Merge tag 'gpio-v3.16-2' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw...
[cascardo/linux.git] / drivers / scsi / fnic / fnic_fcs.c
1 /*
2  * Copyright 2008 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  *
5  * This program is free software; you may redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; version 2 of the License.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
10  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
11  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
12  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
13  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
14  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
15  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
16  * SOFTWARE.
17  */
18 #include <linux/errno.h>
19 #include <linux/pci.h>
20 #include <linux/slab.h>
21 #include <linux/skbuff.h>
22 #include <linux/interrupt.h>
23 #include <linux/spinlock.h>
24 #include <linux/if_ether.h>
25 #include <linux/if_vlan.h>
26 #include <linux/workqueue.h>
27 #include <scsi/fc/fc_fip.h>
28 #include <scsi/fc/fc_els.h>
29 #include <scsi/fc/fc_fcoe.h>
30 #include <scsi/fc_frame.h>
31 #include <scsi/libfc.h>
32 #include "fnic_io.h"
33 #include "fnic.h"
34 #include "fnic_fip.h"
35 #include "cq_enet_desc.h"
36 #include "cq_exch_desc.h"
37
38 static u8 fcoe_all_fcfs[ETH_ALEN];
39 struct workqueue_struct *fnic_fip_queue;
40 struct workqueue_struct *fnic_event_queue;
41
42 static void fnic_set_eth_mode(struct fnic *);
43 static void fnic_fcoe_send_vlan_req(struct fnic *fnic);
44 static void fnic_fcoe_start_fcf_disc(struct fnic *fnic);
45 static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *);
46 static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag);
47 static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb);
48
49 void fnic_handle_link(struct work_struct *work)
50 {
51         struct fnic *fnic = container_of(work, struct fnic, link_work);
52         unsigned long flags;
53         int old_link_status;
54         u32 old_link_down_cnt;
55
56         spin_lock_irqsave(&fnic->fnic_lock, flags);
57
58         if (fnic->stop_rx_link_events) {
59                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
60                 return;
61         }
62
63         old_link_down_cnt = fnic->link_down_cnt;
64         old_link_status = fnic->link_status;
65         fnic->link_status = vnic_dev_link_status(fnic->vdev);
66         fnic->link_down_cnt = vnic_dev_link_down_cnt(fnic->vdev);
67
68         if (old_link_status == fnic->link_status) {
69                 if (!fnic->link_status) {
70                         /* DOWN -> DOWN */
71                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
72                         fnic_fc_trace_set_data(fnic->lport->host->host_no,
73                                 FNIC_FC_LE, "Link Status: DOWN->DOWN",
74                                 strlen("Link Status: DOWN->DOWN"));
75                 } else {
76                         if (old_link_down_cnt != fnic->link_down_cnt) {
77                                 /* UP -> DOWN -> UP */
78                                 fnic->lport->host_stats.link_failure_count++;
79                                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
80                                 fnic_fc_trace_set_data(
81                                         fnic->lport->host->host_no,
82                                         FNIC_FC_LE,
83                                         "Link Status:UP_DOWN_UP",
84                                         strlen("Link_Status:UP_DOWN_UP")
85                                         );
86                                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
87                                              "link down\n");
88                                 fcoe_ctlr_link_down(&fnic->ctlr);
89                                 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
90                                         /* start FCoE VLAN discovery */
91                                         fnic_fc_trace_set_data(
92                                                 fnic->lport->host->host_no,
93                                                 FNIC_FC_LE,
94                                                 "Link Status: UP_DOWN_UP_VLAN",
95                                                 strlen(
96                                                 "Link Status: UP_DOWN_UP_VLAN")
97                                                 );
98                                         fnic_fcoe_send_vlan_req(fnic);
99                                         return;
100                                 }
101                                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
102                                              "link up\n");
103                                 fcoe_ctlr_link_up(&fnic->ctlr);
104                         } else
105                                 /* UP -> UP */
106                                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
107                                 fnic_fc_trace_set_data(
108                                         fnic->lport->host->host_no, FNIC_FC_LE,
109                                         "Link Status: UP_UP",
110                                         strlen("Link Status: UP_UP"));
111                 }
112         } else if (fnic->link_status) {
113                 /* DOWN -> UP */
114                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
115                 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
116                         /* start FCoE VLAN discovery */
117                                 fnic_fc_trace_set_data(
118                                 fnic->lport->host->host_no,
119                                 FNIC_FC_LE, "Link Status: DOWN_UP_VLAN",
120                                 strlen("Link Status: DOWN_UP_VLAN"));
121                         fnic_fcoe_send_vlan_req(fnic);
122                         return;
123                 }
124                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "link up\n");
125                 fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_LE,
126                         "Link Status: DOWN_UP", strlen("Link Status: DOWN_UP"));
127                 fcoe_ctlr_link_up(&fnic->ctlr);
128         } else {
129                 /* UP -> DOWN */
130                 fnic->lport->host_stats.link_failure_count++;
131                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
132                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "link down\n");
133                 fnic_fc_trace_set_data(
134                         fnic->lport->host->host_no, FNIC_FC_LE,
135                         "Link Status: UP_DOWN",
136                         strlen("Link Status: UP_DOWN"));
137                 fcoe_ctlr_link_down(&fnic->ctlr);
138         }
139
140 }
141
142 /*
143  * This function passes incoming fabric frames to libFC
144  */
145 void fnic_handle_frame(struct work_struct *work)
146 {
147         struct fnic *fnic = container_of(work, struct fnic, frame_work);
148         struct fc_lport *lp = fnic->lport;
149         unsigned long flags;
150         struct sk_buff *skb;
151         struct fc_frame *fp;
152
153         while ((skb = skb_dequeue(&fnic->frame_queue))) {
154
155                 spin_lock_irqsave(&fnic->fnic_lock, flags);
156                 if (fnic->stop_rx_link_events) {
157                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
158                         dev_kfree_skb(skb);
159                         return;
160                 }
161                 fp = (struct fc_frame *)skb;
162
163                 /*
164                  * If we're in a transitional state, just re-queue and return.
165                  * The queue will be serviced when we get to a stable state.
166                  */
167                 if (fnic->state != FNIC_IN_FC_MODE &&
168                     fnic->state != FNIC_IN_ETH_MODE) {
169                         skb_queue_head(&fnic->frame_queue, skb);
170                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
171                         return;
172                 }
173                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
174
175                 fc_exch_recv(lp, fp);
176         }
177 }
178
179 void fnic_fcoe_evlist_free(struct fnic *fnic)
180 {
181         struct fnic_event *fevt = NULL;
182         struct fnic_event *next = NULL;
183         unsigned long flags;
184
185         spin_lock_irqsave(&fnic->fnic_lock, flags);
186         if (list_empty(&fnic->evlist)) {
187                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
188                 return;
189         }
190
191         list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
192                 list_del(&fevt->list);
193                 kfree(fevt);
194         }
195         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
196 }
197
198 void fnic_handle_event(struct work_struct *work)
199 {
200         struct fnic *fnic = container_of(work, struct fnic, event_work);
201         struct fnic_event *fevt = NULL;
202         struct fnic_event *next = NULL;
203         unsigned long flags;
204
205         spin_lock_irqsave(&fnic->fnic_lock, flags);
206         if (list_empty(&fnic->evlist)) {
207                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
208                 return;
209         }
210
211         list_for_each_entry_safe(fevt, next, &fnic->evlist, list) {
212                 if (fnic->stop_rx_link_events) {
213                         list_del(&fevt->list);
214                         kfree(fevt);
215                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
216                         return;
217                 }
218                 /*
219                  * If we're in a transitional state, just re-queue and return.
220                  * The queue will be serviced when we get to a stable state.
221                  */
222                 if (fnic->state != FNIC_IN_FC_MODE &&
223                     fnic->state != FNIC_IN_ETH_MODE) {
224                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
225                         return;
226                 }
227
228                 list_del(&fevt->list);
229                 switch (fevt->event) {
230                 case FNIC_EVT_START_VLAN_DISC:
231                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
232                         fnic_fcoe_send_vlan_req(fnic);
233                         spin_lock_irqsave(&fnic->fnic_lock, flags);
234                         break;
235                 case FNIC_EVT_START_FCF_DISC:
236                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
237                                   "Start FCF Discovery\n");
238                         fnic_fcoe_start_fcf_disc(fnic);
239                         break;
240                 default:
241                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
242                                   "Unknown event 0x%x\n", fevt->event);
243                         break;
244                 }
245                 kfree(fevt);
246         }
247         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
248 }
249
250 /**
251  * Check if the Received FIP FLOGI frame is rejected
252  * @fip: The FCoE controller that received the frame
253  * @skb: The received FIP frame
254  *
255  * Returns non-zero if the frame is rejected with unsupported cmd with
256  * insufficient resource els explanation.
257  */
258 static inline int is_fnic_fip_flogi_reject(struct fcoe_ctlr *fip,
259                                          struct sk_buff *skb)
260 {
261         struct fc_lport *lport = fip->lp;
262         struct fip_header *fiph;
263         struct fc_frame_header *fh = NULL;
264         struct fip_desc *desc;
265         struct fip_encaps *els;
266         enum fip_desc_type els_dtype = 0;
267         u16 op;
268         u8 els_op;
269         u8 sub;
270
271         size_t els_len = 0;
272         size_t rlen;
273         size_t dlen = 0;
274
275         if (skb_linearize(skb))
276                 return 0;
277
278         if (skb->len < sizeof(*fiph))
279                 return 0;
280
281         fiph = (struct fip_header *)skb->data;
282         op = ntohs(fiph->fip_op);
283         sub = fiph->fip_subcode;
284
285         if (op != FIP_OP_LS)
286                 return 0;
287
288         if (sub != FIP_SC_REP)
289                 return 0;
290
291         rlen = ntohs(fiph->fip_dl_len) * 4;
292         if (rlen + sizeof(*fiph) > skb->len)
293                 return 0;
294
295         desc = (struct fip_desc *)(fiph + 1);
296         dlen = desc->fip_dlen * FIP_BPW;
297
298         if (desc->fip_dtype == FIP_DT_FLOGI) {
299
300                 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
301                         return 0;
302
303                 els_len = dlen - sizeof(*els);
304                 els = (struct fip_encaps *)desc;
305                 fh = (struct fc_frame_header *)(els + 1);
306                 els_dtype = desc->fip_dtype;
307
308                 if (!fh)
309                         return 0;
310
311                 /*
312                  * ELS command code, reason and explanation should be = Reject,
313                  * unsupported command and insufficient resource
314                  */
315                 els_op = *(u8 *)(fh + 1);
316                 if (els_op == ELS_LS_RJT) {
317                         shost_printk(KERN_INFO, lport->host,
318                                   "Flogi Request Rejected by Switch\n");
319                         return 1;
320                 }
321                 shost_printk(KERN_INFO, lport->host,
322                                 "Flogi Request Accepted by Switch\n");
323         }
324         return 0;
325 }
326
327 static void fnic_fcoe_send_vlan_req(struct fnic *fnic)
328 {
329         struct fcoe_ctlr *fip = &fnic->ctlr;
330         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
331         struct sk_buff *skb;
332         char *eth_fr;
333         int fr_len;
334         struct fip_vlan *vlan;
335         u64 vlan_tov;
336
337         fnic_fcoe_reset_vlans(fnic);
338         fnic->set_vlan(fnic, 0);
339         FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
340                   "Sending VLAN request...\n");
341         skb = dev_alloc_skb(sizeof(struct fip_vlan));
342         if (!skb)
343                 return;
344
345         fr_len = sizeof(*vlan);
346         eth_fr = (char *)skb->data;
347         vlan = (struct fip_vlan *)eth_fr;
348
349         memset(vlan, 0, sizeof(*vlan));
350         memcpy(vlan->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
351         memcpy(vlan->eth.h_dest, fcoe_all_fcfs, ETH_ALEN);
352         vlan->eth.h_proto = htons(ETH_P_FIP);
353
354         vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
355         vlan->fip.fip_op = htons(FIP_OP_VLAN);
356         vlan->fip.fip_subcode = FIP_SC_VL_REQ;
357         vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
358
359         vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
360         vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
361         memcpy(&vlan->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
362
363         vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
364         vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
365         put_unaligned_be64(fip->lp->wwnn, &vlan->desc.wwnn.fd_wwn);
366         atomic64_inc(&fnic_stats->vlan_stats.vlan_disc_reqs);
367
368         skb_put(skb, sizeof(*vlan));
369         skb->protocol = htons(ETH_P_FIP);
370         skb_reset_mac_header(skb);
371         skb_reset_network_header(skb);
372         fip->send(fip, skb);
373
374         /* set a timer so that we can retry if there no response */
375         vlan_tov = jiffies + msecs_to_jiffies(FCOE_CTLR_FIPVLAN_TOV);
376         mod_timer(&fnic->fip_timer, round_jiffies(vlan_tov));
377 }
378
379 static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *skb)
380 {
381         struct fcoe_ctlr *fip = &fnic->ctlr;
382         struct fip_header *fiph;
383         struct fip_desc *desc;
384         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
385         u16 vid;
386         size_t rlen;
387         size_t dlen;
388         struct fcoe_vlan *vlan;
389         u64 sol_time;
390         unsigned long flags;
391
392         FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
393                   "Received VLAN response...\n");
394
395         fiph = (struct fip_header *) skb->data;
396
397         FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
398                   "Received VLAN response... OP 0x%x SUB_OP 0x%x\n",
399                   ntohs(fiph->fip_op), fiph->fip_subcode);
400
401         rlen = ntohs(fiph->fip_dl_len) * 4;
402         fnic_fcoe_reset_vlans(fnic);
403         spin_lock_irqsave(&fnic->vlans_lock, flags);
404         desc = (struct fip_desc *)(fiph + 1);
405         while (rlen > 0) {
406                 dlen = desc->fip_dlen * FIP_BPW;
407                 switch (desc->fip_dtype) {
408                 case FIP_DT_VLAN:
409                         vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
410                         shost_printk(KERN_INFO, fnic->lport->host,
411                                   "process_vlan_resp: FIP VLAN %d\n", vid);
412                         vlan = kmalloc(sizeof(*vlan),
413                                                         GFP_ATOMIC);
414                         if (!vlan) {
415                                 /* retry from timer */
416                                 spin_unlock_irqrestore(&fnic->vlans_lock,
417                                                         flags);
418                                 goto out;
419                         }
420                         memset(vlan, 0, sizeof(struct fcoe_vlan));
421                         vlan->vid = vid & 0x0fff;
422                         vlan->state = FIP_VLAN_AVAIL;
423                         list_add_tail(&vlan->list, &fnic->vlans);
424                         break;
425                 }
426                 desc = (struct fip_desc *)((char *)desc + dlen);
427                 rlen -= dlen;
428         }
429
430         /* any VLAN descriptors present ? */
431         if (list_empty(&fnic->vlans)) {
432                 /* retry from timer */
433                 atomic64_inc(&fnic_stats->vlan_stats.resp_withno_vlanID);
434                 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
435                           "No VLAN descriptors in FIP VLAN response\n");
436                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
437                 goto out;
438         }
439
440         vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
441         fnic->set_vlan(fnic, vlan->vid);
442         vlan->state = FIP_VLAN_SENT; /* sent now */
443         vlan->sol_count++;
444         spin_unlock_irqrestore(&fnic->vlans_lock, flags);
445
446         /* start the solicitation */
447         fcoe_ctlr_link_up(fip);
448
449         sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
450         mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
451 out:
452         return;
453 }
454
455 static void fnic_fcoe_start_fcf_disc(struct fnic *fnic)
456 {
457         unsigned long flags;
458         struct fcoe_vlan *vlan;
459         u64 sol_time;
460
461         spin_lock_irqsave(&fnic->vlans_lock, flags);
462         vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
463         fnic->set_vlan(fnic, vlan->vid);
464         vlan->state = FIP_VLAN_SENT; /* sent now */
465         vlan->sol_count = 1;
466         spin_unlock_irqrestore(&fnic->vlans_lock, flags);
467
468         /* start the solicitation */
469         fcoe_ctlr_link_up(&fnic->ctlr);
470
471         sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
472         mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
473 }
474
475 static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag)
476 {
477         unsigned long flags;
478         struct fcoe_vlan *fvlan;
479
480         spin_lock_irqsave(&fnic->vlans_lock, flags);
481         if (list_empty(&fnic->vlans)) {
482                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
483                 return -EINVAL;
484         }
485
486         fvlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
487         if (fvlan->state == FIP_VLAN_USED) {
488                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
489                 return 0;
490         }
491
492         if (fvlan->state == FIP_VLAN_SENT) {
493                 fvlan->state = FIP_VLAN_USED;
494                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
495                 return 0;
496         }
497         spin_unlock_irqrestore(&fnic->vlans_lock, flags);
498         return -EINVAL;
499 }
500
501 static void fnic_event_enq(struct fnic *fnic, enum fnic_evt ev)
502 {
503         struct fnic_event *fevt;
504         unsigned long flags;
505
506         fevt = kmalloc(sizeof(*fevt), GFP_ATOMIC);
507         if (!fevt)
508                 return;
509
510         fevt->fnic = fnic;
511         fevt->event = ev;
512
513         spin_lock_irqsave(&fnic->fnic_lock, flags);
514         list_add_tail(&fevt->list, &fnic->evlist);
515         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
516
517         schedule_work(&fnic->event_work);
518 }
519
520 static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb)
521 {
522         struct fip_header *fiph;
523         int ret = 1;
524         u16 op;
525         u8 sub;
526
527         if (!skb || !(skb->data))
528                 return -1;
529
530         if (skb_linearize(skb))
531                 goto drop;
532
533         fiph = (struct fip_header *)skb->data;
534         op = ntohs(fiph->fip_op);
535         sub = fiph->fip_subcode;
536
537         if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
538                 goto drop;
539
540         if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
541                 goto drop;
542
543         if (op == FIP_OP_DISC && sub == FIP_SC_ADV) {
544                 if (fnic_fcoe_vlan_check(fnic, ntohs(fiph->fip_flags)))
545                         goto drop;
546                 /* pass it on to fcoe */
547                 ret = 1;
548         } else if (op == FIP_OP_VLAN && sub == FIP_SC_VL_REP) {
549                 /* set the vlan as used */
550                 fnic_fcoe_process_vlan_resp(fnic, skb);
551                 ret = 0;
552         } else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
553                 /* received CVL request, restart vlan disc */
554                 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
555                 /* pass it on to fcoe */
556                 ret = 1;
557         }
558 drop:
559         return ret;
560 }
561
562 void fnic_handle_fip_frame(struct work_struct *work)
563 {
564         struct fnic *fnic = container_of(work, struct fnic, fip_frame_work);
565         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
566         unsigned long flags;
567         struct sk_buff *skb;
568         struct ethhdr *eh;
569
570         while ((skb = skb_dequeue(&fnic->fip_frame_queue))) {
571                 spin_lock_irqsave(&fnic->fnic_lock, flags);
572                 if (fnic->stop_rx_link_events) {
573                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
574                         dev_kfree_skb(skb);
575                         return;
576                 }
577                 /*
578                  * If we're in a transitional state, just re-queue and return.
579                  * The queue will be serviced when we get to a stable state.
580                  */
581                 if (fnic->state != FNIC_IN_FC_MODE &&
582                     fnic->state != FNIC_IN_ETH_MODE) {
583                         skb_queue_head(&fnic->fip_frame_queue, skb);
584                         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
585                         return;
586                 }
587                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
588                 eh = (struct ethhdr *)skb->data;
589                 if (eh->h_proto == htons(ETH_P_FIP)) {
590                         skb_pull(skb, sizeof(*eh));
591                         if (fnic_fcoe_handle_fip_frame(fnic, skb) <= 0) {
592                                 dev_kfree_skb(skb);
593                                 continue;
594                         }
595                         /*
596                          * If there's FLOGI rejects - clear all
597                          * fcf's & restart from scratch
598                          */
599                         if (is_fnic_fip_flogi_reject(&fnic->ctlr, skb)) {
600                                 atomic64_inc(
601                                         &fnic_stats->vlan_stats.flogi_rejects);
602                                 shost_printk(KERN_INFO, fnic->lport->host,
603                                           "Trigger a Link down - VLAN Disc\n");
604                                 fcoe_ctlr_link_down(&fnic->ctlr);
605                                 /* start FCoE VLAN discovery */
606                                 fnic_fcoe_send_vlan_req(fnic);
607                                 dev_kfree_skb(skb);
608                                 continue;
609                         }
610                         fcoe_ctlr_recv(&fnic->ctlr, skb);
611                         continue;
612                 }
613         }
614 }
615
616 /**
617  * fnic_import_rq_eth_pkt() - handle received FCoE or FIP frame.
618  * @fnic:       fnic instance.
619  * @skb:        Ethernet Frame.
620  */
621 static inline int fnic_import_rq_eth_pkt(struct fnic *fnic, struct sk_buff *skb)
622 {
623         struct fc_frame *fp;
624         struct ethhdr *eh;
625         struct fcoe_hdr *fcoe_hdr;
626         struct fcoe_crc_eof *ft;
627
628         /*
629          * Undo VLAN encapsulation if present.
630          */
631         eh = (struct ethhdr *)skb->data;
632         if (eh->h_proto == htons(ETH_P_8021Q)) {
633                 memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
634                 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
635                 skb_reset_mac_header(skb);
636         }
637         if (eh->h_proto == htons(ETH_P_FIP)) {
638                 if (!(fnic->config.flags & VFCF_FIP_CAPABLE)) {
639                         printk(KERN_ERR "Dropped FIP frame, as firmware "
640                                         "uses non-FIP mode, Enable FIP "
641                                         "using UCSM\n");
642                         goto drop;
643                 }
644                 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
645                         FNIC_FC_RECV|0x80, (char *)skb->data, skb->len)) != 0) {
646                         printk(KERN_ERR "fnic ctlr frame trace error!!!");
647                 }
648                 skb_queue_tail(&fnic->fip_frame_queue, skb);
649                 queue_work(fnic_fip_queue, &fnic->fip_frame_work);
650                 return 1;               /* let caller know packet was used */
651         }
652         if (eh->h_proto != htons(ETH_P_FCOE))
653                 goto drop;
654         skb_set_network_header(skb, sizeof(*eh));
655         skb_pull(skb, sizeof(*eh));
656
657         fcoe_hdr = (struct fcoe_hdr *)skb->data;
658         if (FC_FCOE_DECAPS_VER(fcoe_hdr) != FC_FCOE_VER)
659                 goto drop;
660
661         fp = (struct fc_frame *)skb;
662         fc_frame_init(fp);
663         fr_sof(fp) = fcoe_hdr->fcoe_sof;
664         skb_pull(skb, sizeof(struct fcoe_hdr));
665         skb_reset_transport_header(skb);
666
667         ft = (struct fcoe_crc_eof *)(skb->data + skb->len - sizeof(*ft));
668         fr_eof(fp) = ft->fcoe_eof;
669         skb_trim(skb, skb->len - sizeof(*ft));
670         return 0;
671 drop:
672         dev_kfree_skb_irq(skb);
673         return -1;
674 }
675
676 /**
677  * fnic_update_mac_locked() - set data MAC address and filters.
678  * @fnic:       fnic instance.
679  * @new:        newly-assigned FCoE MAC address.
680  *
681  * Called with the fnic lock held.
682  */
683 void fnic_update_mac_locked(struct fnic *fnic, u8 *new)
684 {
685         u8 *ctl = fnic->ctlr.ctl_src_addr;
686         u8 *data = fnic->data_src_addr;
687
688         if (is_zero_ether_addr(new))
689                 new = ctl;
690         if (ether_addr_equal(data, new))
691                 return;
692         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "update_mac %pM\n", new);
693         if (!is_zero_ether_addr(data) && !ether_addr_equal(data, ctl))
694                 vnic_dev_del_addr(fnic->vdev, data);
695         memcpy(data, new, ETH_ALEN);
696         if (!ether_addr_equal(new, ctl))
697                 vnic_dev_add_addr(fnic->vdev, new);
698 }
699
700 /**
701  * fnic_update_mac() - set data MAC address and filters.
702  * @lport:      local port.
703  * @new:        newly-assigned FCoE MAC address.
704  */
705 void fnic_update_mac(struct fc_lport *lport, u8 *new)
706 {
707         struct fnic *fnic = lport_priv(lport);
708
709         spin_lock_irq(&fnic->fnic_lock);
710         fnic_update_mac_locked(fnic, new);
711         spin_unlock_irq(&fnic->fnic_lock);
712 }
713
714 /**
715  * fnic_set_port_id() - set the port_ID after successful FLOGI.
716  * @lport:      local port.
717  * @port_id:    assigned FC_ID.
718  * @fp:         received frame containing the FLOGI accept or NULL.
719  *
720  * This is called from libfc when a new FC_ID has been assigned.
721  * This causes us to reset the firmware to FC_MODE and setup the new MAC
722  * address and FC_ID.
723  *
724  * It is also called with FC_ID 0 when we're logged off.
725  *
726  * If the FC_ID is due to point-to-point, fp may be NULL.
727  */
728 void fnic_set_port_id(struct fc_lport *lport, u32 port_id, struct fc_frame *fp)
729 {
730         struct fnic *fnic = lport_priv(lport);
731         u8 *mac;
732         int ret;
733
734         FNIC_FCS_DBG(KERN_DEBUG, lport->host, "set port_id %x fp %p\n",
735                      port_id, fp);
736
737         /*
738          * If we're clearing the FC_ID, change to use the ctl_src_addr.
739          * Set ethernet mode to send FLOGI.
740          */
741         if (!port_id) {
742                 fnic_update_mac(lport, fnic->ctlr.ctl_src_addr);
743                 fnic_set_eth_mode(fnic);
744                 return;
745         }
746
747         if (fp) {
748                 mac = fr_cb(fp)->granted_mac;
749                 if (is_zero_ether_addr(mac)) {
750                         /* non-FIP - FLOGI already accepted - ignore return */
751                         fcoe_ctlr_recv_flogi(&fnic->ctlr, lport, fp);
752                 }
753                 fnic_update_mac(lport, mac);
754         }
755
756         /* Change state to reflect transition to FC mode */
757         spin_lock_irq(&fnic->fnic_lock);
758         if (fnic->state == FNIC_IN_ETH_MODE || fnic->state == FNIC_IN_FC_MODE)
759                 fnic->state = FNIC_IN_ETH_TRANS_FC_MODE;
760         else {
761                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
762                              "Unexpected fnic state %s while"
763                              " processing flogi resp\n",
764                              fnic_state_to_str(fnic->state));
765                 spin_unlock_irq(&fnic->fnic_lock);
766                 return;
767         }
768         spin_unlock_irq(&fnic->fnic_lock);
769
770         /*
771          * Send FLOGI registration to firmware to set up FC mode.
772          * The new address will be set up when registration completes.
773          */
774         ret = fnic_flogi_reg_handler(fnic, port_id);
775
776         if (ret < 0) {
777                 spin_lock_irq(&fnic->fnic_lock);
778                 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE)
779                         fnic->state = FNIC_IN_ETH_MODE;
780                 spin_unlock_irq(&fnic->fnic_lock);
781         }
782 }
783
784 static void fnic_rq_cmpl_frame_recv(struct vnic_rq *rq, struct cq_desc
785                                     *cq_desc, struct vnic_rq_buf *buf,
786                                     int skipped __attribute__((unused)),
787                                     void *opaque)
788 {
789         struct fnic *fnic = vnic_dev_priv(rq->vdev);
790         struct sk_buff *skb;
791         struct fc_frame *fp;
792         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
793         unsigned int eth_hdrs_stripped;
794         u8 type, color, eop, sop, ingress_port, vlan_stripped;
795         u8 fcoe = 0, fcoe_sof, fcoe_eof;
796         u8 fcoe_fc_crc_ok = 1, fcoe_enc_error = 0;
797         u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
798         u8 ipv6, ipv4, ipv4_fragment, rss_type, csum_not_calc;
799         u8 fcs_ok = 1, packet_error = 0;
800         u16 q_number, completed_index, bytes_written = 0, vlan, checksum;
801         u32 rss_hash;
802         u16 exchange_id, tmpl;
803         u8 sof = 0;
804         u8 eof = 0;
805         u32 fcp_bytes_written = 0;
806         unsigned long flags;
807
808         pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
809                          PCI_DMA_FROMDEVICE);
810         skb = buf->os_buf;
811         fp = (struct fc_frame *)skb;
812         buf->os_buf = NULL;
813
814         cq_desc_dec(cq_desc, &type, &color, &q_number, &completed_index);
815         if (type == CQ_DESC_TYPE_RQ_FCP) {
816                 cq_fcp_rq_desc_dec((struct cq_fcp_rq_desc *)cq_desc,
817                                    &type, &color, &q_number, &completed_index,
818                                    &eop, &sop, &fcoe_fc_crc_ok, &exchange_id,
819                                    &tmpl, &fcp_bytes_written, &sof, &eof,
820                                    &ingress_port, &packet_error,
821                                    &fcoe_enc_error, &fcs_ok, &vlan_stripped,
822                                    &vlan);
823                 eth_hdrs_stripped = 1;
824                 skb_trim(skb, fcp_bytes_written);
825                 fr_sof(fp) = sof;
826                 fr_eof(fp) = eof;
827
828         } else if (type == CQ_DESC_TYPE_RQ_ENET) {
829                 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
830                                     &type, &color, &q_number, &completed_index,
831                                     &ingress_port, &fcoe, &eop, &sop,
832                                     &rss_type, &csum_not_calc, &rss_hash,
833                                     &bytes_written, &packet_error,
834                                     &vlan_stripped, &vlan, &checksum,
835                                     &fcoe_sof, &fcoe_fc_crc_ok,
836                                     &fcoe_enc_error, &fcoe_eof,
837                                     &tcp_udp_csum_ok, &udp, &tcp,
838                                     &ipv4_csum_ok, &ipv6, &ipv4,
839                                     &ipv4_fragment, &fcs_ok);
840                 eth_hdrs_stripped = 0;
841                 skb_trim(skb, bytes_written);
842                 if (!fcs_ok) {
843                         atomic64_inc(&fnic_stats->misc_stats.frame_errors);
844                         FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
845                                      "fcs error.  dropping packet.\n");
846                         goto drop;
847                 }
848                 if (fnic_import_rq_eth_pkt(fnic, skb))
849                         return;
850
851         } else {
852                 /* wrong CQ type*/
853                 shost_printk(KERN_ERR, fnic->lport->host,
854                              "fnic rq_cmpl wrong cq type x%x\n", type);
855                 goto drop;
856         }
857
858         if (!fcs_ok || packet_error || !fcoe_fc_crc_ok || fcoe_enc_error) {
859                 atomic64_inc(&fnic_stats->misc_stats.frame_errors);
860                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
861                              "fnic rq_cmpl fcoe x%x fcsok x%x"
862                              " pkterr x%x fcoe_fc_crc_ok x%x, fcoe_enc_err"
863                              " x%x\n",
864                              fcoe, fcs_ok, packet_error,
865                              fcoe_fc_crc_ok, fcoe_enc_error);
866                 goto drop;
867         }
868
869         spin_lock_irqsave(&fnic->fnic_lock, flags);
870         if (fnic->stop_rx_link_events) {
871                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
872                 goto drop;
873         }
874         fr_dev(fp) = fnic->lport;
875         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
876         if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_RECV,
877                                         (char *)skb->data, skb->len)) != 0) {
878                 printk(KERN_ERR "fnic ctlr frame trace error!!!");
879         }
880
881         skb_queue_tail(&fnic->frame_queue, skb);
882         queue_work(fnic_event_queue, &fnic->frame_work);
883
884         return;
885 drop:
886         dev_kfree_skb_irq(skb);
887 }
888
889 static int fnic_rq_cmpl_handler_cont(struct vnic_dev *vdev,
890                                      struct cq_desc *cq_desc, u8 type,
891                                      u16 q_number, u16 completed_index,
892                                      void *opaque)
893 {
894         struct fnic *fnic = vnic_dev_priv(vdev);
895
896         vnic_rq_service(&fnic->rq[q_number], cq_desc, completed_index,
897                         VNIC_RQ_RETURN_DESC, fnic_rq_cmpl_frame_recv,
898                         NULL);
899         return 0;
900 }
901
902 int fnic_rq_cmpl_handler(struct fnic *fnic, int rq_work_to_do)
903 {
904         unsigned int tot_rq_work_done = 0, cur_work_done;
905         unsigned int i;
906         int err;
907
908         for (i = 0; i < fnic->rq_count; i++) {
909                 cur_work_done = vnic_cq_service(&fnic->cq[i], rq_work_to_do,
910                                                 fnic_rq_cmpl_handler_cont,
911                                                 NULL);
912                 if (cur_work_done) {
913                         err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
914                         if (err)
915                                 shost_printk(KERN_ERR, fnic->lport->host,
916                                              "fnic_alloc_rq_frame can't alloc"
917                                              " frame\n");
918                 }
919                 tot_rq_work_done += cur_work_done;
920         }
921
922         return tot_rq_work_done;
923 }
924
925 /*
926  * This function is called once at init time to allocate and fill RQ
927  * buffers. Subsequently, it is called in the interrupt context after RQ
928  * buffer processing to replenish the buffers in the RQ
929  */
930 int fnic_alloc_rq_frame(struct vnic_rq *rq)
931 {
932         struct fnic *fnic = vnic_dev_priv(rq->vdev);
933         struct sk_buff *skb;
934         u16 len;
935         dma_addr_t pa;
936
937         len = FC_FRAME_HEADROOM + FC_MAX_FRAME + FC_FRAME_TAILROOM;
938         skb = dev_alloc_skb(len);
939         if (!skb) {
940                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
941                              "Unable to allocate RQ sk_buff\n");
942                 return -ENOMEM;
943         }
944         skb_reset_mac_header(skb);
945         skb_reset_transport_header(skb);
946         skb_reset_network_header(skb);
947         skb_put(skb, len);
948         pa = pci_map_single(fnic->pdev, skb->data, len, PCI_DMA_FROMDEVICE);
949         fnic_queue_rq_desc(rq, skb, pa, len);
950         return 0;
951 }
952
953 void fnic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
954 {
955         struct fc_frame *fp = buf->os_buf;
956         struct fnic *fnic = vnic_dev_priv(rq->vdev);
957
958         pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
959                          PCI_DMA_FROMDEVICE);
960
961         dev_kfree_skb(fp_skb(fp));
962         buf->os_buf = NULL;
963 }
964
965 /**
966  * fnic_eth_send() - Send Ethernet frame.
967  * @fip:        fcoe_ctlr instance.
968  * @skb:        Ethernet Frame, FIP, without VLAN encapsulation.
969  */
970 void fnic_eth_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
971 {
972         struct fnic *fnic = fnic_from_ctlr(fip);
973         struct vnic_wq *wq = &fnic->wq[0];
974         dma_addr_t pa;
975         struct ethhdr *eth_hdr;
976         struct vlan_ethhdr *vlan_hdr;
977         unsigned long flags;
978
979         if (!fnic->vlan_hw_insert) {
980                 eth_hdr = (struct ethhdr *)skb_mac_header(skb);
981                 vlan_hdr = (struct vlan_ethhdr *)skb_push(skb,
982                                 sizeof(*vlan_hdr) - sizeof(*eth_hdr));
983                 memcpy(vlan_hdr, eth_hdr, 2 * ETH_ALEN);
984                 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
985                 vlan_hdr->h_vlan_encapsulated_proto = eth_hdr->h_proto;
986                 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
987                 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
988                         FNIC_FC_SEND|0x80, (char *)eth_hdr, skb->len)) != 0) {
989                         printk(KERN_ERR "fnic ctlr frame trace error!!!");
990                 }
991         } else {
992                 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
993                         FNIC_FC_SEND|0x80, (char *)skb->data, skb->len)) != 0) {
994                         printk(KERN_ERR "fnic ctlr frame trace error!!!");
995                 }
996         }
997
998         pa = pci_map_single(fnic->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
999
1000         spin_lock_irqsave(&fnic->wq_lock[0], flags);
1001         if (!vnic_wq_desc_avail(wq)) {
1002                 pci_unmap_single(fnic->pdev, pa, skb->len, PCI_DMA_TODEVICE);
1003                 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1004                 kfree_skb(skb);
1005                 return;
1006         }
1007
1008         fnic_queue_wq_eth_desc(wq, skb, pa, skb->len,
1009                                0 /* hw inserts cos value */,
1010                                fnic->vlan_id, 1);
1011         spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1012 }
1013
1014 /*
1015  * Send FC frame.
1016  */
1017 static int fnic_send_frame(struct fnic *fnic, struct fc_frame *fp)
1018 {
1019         struct vnic_wq *wq = &fnic->wq[0];
1020         struct sk_buff *skb;
1021         dma_addr_t pa;
1022         struct ethhdr *eth_hdr;
1023         struct vlan_ethhdr *vlan_hdr;
1024         struct fcoe_hdr *fcoe_hdr;
1025         struct fc_frame_header *fh;
1026         u32 tot_len, eth_hdr_len;
1027         int ret = 0;
1028         unsigned long flags;
1029
1030         fh = fc_frame_header_get(fp);
1031         skb = fp_skb(fp);
1032
1033         if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1034             fcoe_ctlr_els_send(&fnic->ctlr, fnic->lport, skb))
1035                 return 0;
1036
1037         if (!fnic->vlan_hw_insert) {
1038                 eth_hdr_len = sizeof(*vlan_hdr) + sizeof(*fcoe_hdr);
1039                 vlan_hdr = (struct vlan_ethhdr *)skb_push(skb, eth_hdr_len);
1040                 eth_hdr = (struct ethhdr *)vlan_hdr;
1041                 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1042                 vlan_hdr->h_vlan_encapsulated_proto = htons(ETH_P_FCOE);
1043                 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1044                 fcoe_hdr = (struct fcoe_hdr *)(vlan_hdr + 1);
1045         } else {
1046                 eth_hdr_len = sizeof(*eth_hdr) + sizeof(*fcoe_hdr);
1047                 eth_hdr = (struct ethhdr *)skb_push(skb, eth_hdr_len);
1048                 eth_hdr->h_proto = htons(ETH_P_FCOE);
1049                 fcoe_hdr = (struct fcoe_hdr *)(eth_hdr + 1);
1050         }
1051
1052         if (fnic->ctlr.map_dest)
1053                 fc_fcoe_set_mac(eth_hdr->h_dest, fh->fh_d_id);
1054         else
1055                 memcpy(eth_hdr->h_dest, fnic->ctlr.dest_addr, ETH_ALEN);
1056         memcpy(eth_hdr->h_source, fnic->data_src_addr, ETH_ALEN);
1057
1058         tot_len = skb->len;
1059         BUG_ON(tot_len % 4);
1060
1061         memset(fcoe_hdr, 0, sizeof(*fcoe_hdr));
1062         fcoe_hdr->fcoe_sof = fr_sof(fp);
1063         if (FC_FCOE_VER)
1064                 FC_FCOE_ENCAPS_VER(fcoe_hdr, FC_FCOE_VER);
1065
1066         pa = pci_map_single(fnic->pdev, eth_hdr, tot_len, PCI_DMA_TODEVICE);
1067
1068         if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_SEND,
1069                                 (char *)eth_hdr, tot_len)) != 0) {
1070                 printk(KERN_ERR "fnic ctlr frame trace error!!!");
1071         }
1072
1073         spin_lock_irqsave(&fnic->wq_lock[0], flags);
1074
1075         if (!vnic_wq_desc_avail(wq)) {
1076                 pci_unmap_single(fnic->pdev, pa,
1077                                  tot_len, PCI_DMA_TODEVICE);
1078                 ret = -1;
1079                 goto fnic_send_frame_end;
1080         }
1081
1082         fnic_queue_wq_desc(wq, skb, pa, tot_len, fr_eof(fp),
1083                            0 /* hw inserts cos value */,
1084                            fnic->vlan_id, 1, 1, 1);
1085 fnic_send_frame_end:
1086         spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1087
1088         if (ret)
1089                 dev_kfree_skb_any(fp_skb(fp));
1090
1091         return ret;
1092 }
1093
1094 /*
1095  * fnic_send
1096  * Routine to send a raw frame
1097  */
1098 int fnic_send(struct fc_lport *lp, struct fc_frame *fp)
1099 {
1100         struct fnic *fnic = lport_priv(lp);
1101         unsigned long flags;
1102
1103         if (fnic->in_remove) {
1104                 dev_kfree_skb(fp_skb(fp));
1105                 return -1;
1106         }
1107
1108         /*
1109          * Queue frame if in a transitional state.
1110          * This occurs while registering the Port_ID / MAC address after FLOGI.
1111          */
1112         spin_lock_irqsave(&fnic->fnic_lock, flags);
1113         if (fnic->state != FNIC_IN_FC_MODE && fnic->state != FNIC_IN_ETH_MODE) {
1114                 skb_queue_tail(&fnic->tx_queue, fp_skb(fp));
1115                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1116                 return 0;
1117         }
1118         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1119
1120         return fnic_send_frame(fnic, fp);
1121 }
1122
1123 /**
1124  * fnic_flush_tx() - send queued frames.
1125  * @fnic: fnic device
1126  *
1127  * Send frames that were waiting to go out in FC or Ethernet mode.
1128  * Whenever changing modes we purge queued frames, so these frames should
1129  * be queued for the stable mode that we're in, either FC or Ethernet.
1130  *
1131  * Called without fnic_lock held.
1132  */
1133 void fnic_flush_tx(struct fnic *fnic)
1134 {
1135         struct sk_buff *skb;
1136         struct fc_frame *fp;
1137
1138         while ((skb = skb_dequeue(&fnic->tx_queue))) {
1139                 fp = (struct fc_frame *)skb;
1140                 fnic_send_frame(fnic, fp);
1141         }
1142 }
1143
1144 /**
1145  * fnic_set_eth_mode() - put fnic into ethernet mode.
1146  * @fnic: fnic device
1147  *
1148  * Called without fnic lock held.
1149  */
1150 static void fnic_set_eth_mode(struct fnic *fnic)
1151 {
1152         unsigned long flags;
1153         enum fnic_state old_state;
1154         int ret;
1155
1156         spin_lock_irqsave(&fnic->fnic_lock, flags);
1157 again:
1158         old_state = fnic->state;
1159         switch (old_state) {
1160         case FNIC_IN_FC_MODE:
1161         case FNIC_IN_ETH_TRANS_FC_MODE:
1162         default:
1163                 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
1164                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1165
1166                 ret = fnic_fw_reset_handler(fnic);
1167
1168                 spin_lock_irqsave(&fnic->fnic_lock, flags);
1169                 if (fnic->state != FNIC_IN_FC_TRANS_ETH_MODE)
1170                         goto again;
1171                 if (ret)
1172                         fnic->state = old_state;
1173                 break;
1174
1175         case FNIC_IN_FC_TRANS_ETH_MODE:
1176         case FNIC_IN_ETH_MODE:
1177                 break;
1178         }
1179         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1180 }
1181
1182 static void fnic_wq_complete_frame_send(struct vnic_wq *wq,
1183                                         struct cq_desc *cq_desc,
1184                                         struct vnic_wq_buf *buf, void *opaque)
1185 {
1186         struct sk_buff *skb = buf->os_buf;
1187         struct fc_frame *fp = (struct fc_frame *)skb;
1188         struct fnic *fnic = vnic_dev_priv(wq->vdev);
1189
1190         pci_unmap_single(fnic->pdev, buf->dma_addr,
1191                          buf->len, PCI_DMA_TODEVICE);
1192         dev_kfree_skb_irq(fp_skb(fp));
1193         buf->os_buf = NULL;
1194 }
1195
1196 static int fnic_wq_cmpl_handler_cont(struct vnic_dev *vdev,
1197                                      struct cq_desc *cq_desc, u8 type,
1198                                      u16 q_number, u16 completed_index,
1199                                      void *opaque)
1200 {
1201         struct fnic *fnic = vnic_dev_priv(vdev);
1202         unsigned long flags;
1203
1204         spin_lock_irqsave(&fnic->wq_lock[q_number], flags);
1205         vnic_wq_service(&fnic->wq[q_number], cq_desc, completed_index,
1206                         fnic_wq_complete_frame_send, NULL);
1207         spin_unlock_irqrestore(&fnic->wq_lock[q_number], flags);
1208
1209         return 0;
1210 }
1211
1212 int fnic_wq_cmpl_handler(struct fnic *fnic, int work_to_do)
1213 {
1214         unsigned int wq_work_done = 0;
1215         unsigned int i;
1216
1217         for (i = 0; i < fnic->raw_wq_count; i++) {
1218                 wq_work_done  += vnic_cq_service(&fnic->cq[fnic->rq_count+i],
1219                                                  work_to_do,
1220                                                  fnic_wq_cmpl_handler_cont,
1221                                                  NULL);
1222         }
1223
1224         return wq_work_done;
1225 }
1226
1227
1228 void fnic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
1229 {
1230         struct fc_frame *fp = buf->os_buf;
1231         struct fnic *fnic = vnic_dev_priv(wq->vdev);
1232
1233         pci_unmap_single(fnic->pdev, buf->dma_addr,
1234                          buf->len, PCI_DMA_TODEVICE);
1235
1236         dev_kfree_skb(fp_skb(fp));
1237         buf->os_buf = NULL;
1238 }
1239
1240 void fnic_fcoe_reset_vlans(struct fnic *fnic)
1241 {
1242         unsigned long flags;
1243         struct fcoe_vlan *vlan;
1244         struct fcoe_vlan *next;
1245
1246         /*
1247          * indicate a link down to fcoe so that all fcf's are free'd
1248          * might not be required since we did this before sending vlan
1249          * discovery request
1250          */
1251         spin_lock_irqsave(&fnic->vlans_lock, flags);
1252         if (!list_empty(&fnic->vlans)) {
1253                 list_for_each_entry_safe(vlan, next, &fnic->vlans, list) {
1254                         list_del(&vlan->list);
1255                         kfree(vlan);
1256                 }
1257         }
1258         spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1259 }
1260
1261 void fnic_handle_fip_timer(struct fnic *fnic)
1262 {
1263         unsigned long flags;
1264         struct fcoe_vlan *vlan;
1265         struct fnic_stats *fnic_stats = &fnic->fnic_stats;
1266         u64 sol_time;
1267
1268         spin_lock_irqsave(&fnic->fnic_lock, flags);
1269         if (fnic->stop_rx_link_events) {
1270                 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1271                 return;
1272         }
1273         spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1274
1275         if (fnic->ctlr.mode == FIP_ST_NON_FIP)
1276                 return;
1277
1278         spin_lock_irqsave(&fnic->vlans_lock, flags);
1279         if (list_empty(&fnic->vlans)) {
1280                 /* no vlans available, try again */
1281                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1282                           "Start VLAN Discovery\n");
1283                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1284                 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1285                 return;
1286         }
1287
1288         vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
1289         shost_printk(KERN_DEBUG, fnic->lport->host,
1290                   "fip_timer: vlan %d state %d sol_count %d\n",
1291                   vlan->vid, vlan->state, vlan->sol_count);
1292         switch (vlan->state) {
1293         case FIP_VLAN_USED:
1294                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1295                           "FIP VLAN is selected for FC transaction\n");
1296                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1297                 break;
1298         case FIP_VLAN_FAILED:
1299                 /* if all vlans are in failed state, restart vlan disc */
1300                 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1301                           "Start VLAN Discovery\n");
1302                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1303                 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1304                 break;
1305         case FIP_VLAN_SENT:
1306                 if (vlan->sol_count >= FCOE_CTLR_MAX_SOL) {
1307                         /*
1308                          * no response on this vlan, remove  from the list.
1309                          * Try the next vlan
1310                          */
1311                         shost_printk(KERN_INFO, fnic->lport->host,
1312                                   "Dequeue this VLAN ID %d from list\n",
1313                                   vlan->vid);
1314                         list_del(&vlan->list);
1315                         kfree(vlan);
1316                         vlan = NULL;
1317                         if (list_empty(&fnic->vlans)) {
1318                                 /* we exhausted all vlans, restart vlan disc */
1319                                 spin_unlock_irqrestore(&fnic->vlans_lock,
1320                                                         flags);
1321                                 shost_printk(KERN_INFO, fnic->lport->host,
1322                                           "fip_timer: vlan list empty, "
1323                                           "trigger vlan disc\n");
1324                                 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1325                                 return;
1326                         }
1327                         /* check the next vlan */
1328                         vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan,
1329                                                         list);
1330                         fnic->set_vlan(fnic, vlan->vid);
1331                         vlan->state = FIP_VLAN_SENT; /* sent now */
1332                 }
1333                 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1334                 atomic64_inc(&fnic_stats->vlan_stats.sol_expiry_count);
1335                 vlan->sol_count++;
1336                 sol_time = jiffies + msecs_to_jiffies
1337                                         (FCOE_CTLR_START_DELAY);
1338                 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
1339                 break;
1340         }
1341 }