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