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