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