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