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