blob: bf0bbd42efb5d9d13b3289ee24c7ba6e852df6df [file] [log] [blame]
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001/*
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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070021#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>
Joe Eykholt78112e52009-11-03 11:49:22 -080027#include <scsi/fc/fc_fip.h>
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070028#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"
Hiral Pateld3c995f2013-02-25 16:18:36 -080034#include "fnic_fip.h"
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070035#include "cq_enet_desc.h"
36#include "cq_exch_desc.h"
37
Hiral Shah86001f22014-05-02 17:46:31 -070038static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
Hiral Pateld3c995f2013-02-25 16:18:36 -080039struct workqueue_struct *fnic_fip_queue;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070040struct workqueue_struct *fnic_event_queue;
41
Joe Eykholt78112e52009-11-03 11:49:22 -080042static void fnic_set_eth_mode(struct fnic *);
Hiral Pateld3c995f2013-02-25 16:18:36 -080043static void fnic_fcoe_send_vlan_req(struct fnic *fnic);
44static void fnic_fcoe_start_fcf_disc(struct fnic *fnic);
45static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *);
46static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag);
47static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb);
Joe Eykholt78112e52009-11-03 11:49:22 -080048
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070049void 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) {
Hiral Shahabb14142014-04-18 12:28:19 -070069 if (!fnic->link_status) {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070070 /* DOWN -> DOWN */
71 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Hiral Shahabb14142014-04-18 12:28:19 -070072 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 {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070076 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);
Hiral Shahabb14142014-04-18 12:28:19 -070080 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 );
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -070086 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
87 "link down\n");
Joe Eykholt78112e52009-11-03 11:49:22 -080088 fcoe_ctlr_link_down(&fnic->ctlr);
Hiral Pateld3c995f2013-02-25 16:18:36 -080089 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
90 /* start FCoE VLAN discovery */
Hiral Shahabb14142014-04-18 12:28:19 -070091 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 );
Hiral Pateld3c995f2013-02-25 16:18:36 -080098 fnic_fcoe_send_vlan_req(fnic);
99 return;
100 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700101 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
102 "link up\n");
Joe Eykholt78112e52009-11-03 11:49:22 -0800103 fcoe_ctlr_link_up(&fnic->ctlr);
Hiral Shah86001f22014-05-02 17:46:31 -0700104 } else {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700105 /* UP -> UP */
106 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Hiral Shahabb14142014-04-18 12:28:19 -0700107 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"));
Hiral Shah86001f22014-05-02 17:46:31 -0700111 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700112 }
113 } else if (fnic->link_status) {
114 /* DOWN -> UP */
115 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Hiral Pateld3c995f2013-02-25 16:18:36 -0800116 if (fnic->config.flags & VFCF_FIP_CAPABLE) {
117 /* start FCoE VLAN discovery */
Hiral Shahabb14142014-04-18 12:28:19 -0700118 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"));
Hiral Pateld3c995f2013-02-25 16:18:36 -0800122 fnic_fcoe_send_vlan_req(fnic);
123 return;
124 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700125 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "link up\n");
Hiral Shahabb14142014-04-18 12:28:19 -0700126 fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_LE,
127 "Link Status: DOWN_UP", strlen("Link Status: DOWN_UP"));
Joe Eykholt78112e52009-11-03 11:49:22 -0800128 fcoe_ctlr_link_up(&fnic->ctlr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700129 } 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");
Hiral Shahabb14142014-04-18 12:28:19 -0700134 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"));
Hiral Shah0ee7b872014-11-10 12:54:35 -0800138 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 }
Joe Eykholt78112e52009-11-03 11:49:22 -0800143 fcoe_ctlr_link_down(&fnic->ctlr);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700144 }
145
146}
147
148/*
149 * This function passes incoming fabric frames to libFC
150 */
151void 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;
Joe Eykholt78112e52009-11-03 11:49:22 -0800168
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;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700178 }
179 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
180
Vasu Dev52ff8782009-07-29 17:05:10 -0700181 fc_exch_recv(lp, fp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700182 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700183}
184
Hiral Pateld3c995f2013-02-25 16:18:36 -0800185void 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
204void 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 */
264static 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
Hiral Pateld3c995f2013-02-25 16:18:36 -0800306 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
333static void fnic_fcoe_send_vlan_req(struct fnic *fnic)
334{
335 struct fcoe_ctlr *fip = &fnic->ctlr;
Hiral Patel67125b02013-09-12 17:45:42 -0700336 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Hiral Pateld3c995f2013-02-25 16:18:36 -0800337 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 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
346 "Sending VLAN request...\n");
347 skb = dev_alloc_skb(sizeof(struct fip_vlan));
348 if (!skb)
349 return;
350
351 fr_len = sizeof(*vlan);
352 eth_fr = (char *)skb->data;
353 vlan = (struct fip_vlan *)eth_fr;
354
355 memset(vlan, 0, sizeof(*vlan));
356 memcpy(vlan->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
357 memcpy(vlan->eth.h_dest, fcoe_all_fcfs, ETH_ALEN);
358 vlan->eth.h_proto = htons(ETH_P_FIP);
359
360 vlan->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
361 vlan->fip.fip_op = htons(FIP_OP_VLAN);
362 vlan->fip.fip_subcode = FIP_SC_VL_REQ;
363 vlan->fip.fip_dl_len = htons(sizeof(vlan->desc) / FIP_BPW);
364
365 vlan->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
366 vlan->desc.mac.fd_desc.fip_dlen = sizeof(vlan->desc.mac) / FIP_BPW;
367 memcpy(&vlan->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
368
369 vlan->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
370 vlan->desc.wwnn.fd_desc.fip_dlen = sizeof(vlan->desc.wwnn) / FIP_BPW;
371 put_unaligned_be64(fip->lp->wwnn, &vlan->desc.wwnn.fd_wwn);
Hiral Patel67125b02013-09-12 17:45:42 -0700372 atomic64_inc(&fnic_stats->vlan_stats.vlan_disc_reqs);
Hiral Pateld3c995f2013-02-25 16:18:36 -0800373
374 skb_put(skb, sizeof(*vlan));
375 skb->protocol = htons(ETH_P_FIP);
376 skb_reset_mac_header(skb);
377 skb_reset_network_header(skb);
378 fip->send(fip, skb);
379
380 /* set a timer so that we can retry if there no response */
381 vlan_tov = jiffies + msecs_to_jiffies(FCOE_CTLR_FIPVLAN_TOV);
382 mod_timer(&fnic->fip_timer, round_jiffies(vlan_tov));
383}
384
385static void fnic_fcoe_process_vlan_resp(struct fnic *fnic, struct sk_buff *skb)
386{
387 struct fcoe_ctlr *fip = &fnic->ctlr;
388 struct fip_header *fiph;
389 struct fip_desc *desc;
Hiral Patel67125b02013-09-12 17:45:42 -0700390 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Hiral Pateld3c995f2013-02-25 16:18:36 -0800391 u16 vid;
392 size_t rlen;
393 size_t dlen;
394 struct fcoe_vlan *vlan;
395 u64 sol_time;
396 unsigned long flags;
397
398 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
399 "Received VLAN response...\n");
400
401 fiph = (struct fip_header *) skb->data;
402
403 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
404 "Received VLAN response... OP 0x%x SUB_OP 0x%x\n",
405 ntohs(fiph->fip_op), fiph->fip_subcode);
406
407 rlen = ntohs(fiph->fip_dl_len) * 4;
408 fnic_fcoe_reset_vlans(fnic);
409 spin_lock_irqsave(&fnic->vlans_lock, flags);
410 desc = (struct fip_desc *)(fiph + 1);
411 while (rlen > 0) {
412 dlen = desc->fip_dlen * FIP_BPW;
413 switch (desc->fip_dtype) {
414 case FIP_DT_VLAN:
415 vid = ntohs(((struct fip_vlan_desc *)desc)->fd_vlan);
416 shost_printk(KERN_INFO, fnic->lport->host,
417 "process_vlan_resp: FIP VLAN %d\n", vid);
418 vlan = kmalloc(sizeof(*vlan),
419 GFP_ATOMIC);
420 if (!vlan) {
421 /* retry from timer */
422 spin_unlock_irqrestore(&fnic->vlans_lock,
423 flags);
424 goto out;
425 }
426 memset(vlan, 0, sizeof(struct fcoe_vlan));
427 vlan->vid = vid & 0x0fff;
428 vlan->state = FIP_VLAN_AVAIL;
429 list_add_tail(&vlan->list, &fnic->vlans);
430 break;
431 }
432 desc = (struct fip_desc *)((char *)desc + dlen);
433 rlen -= dlen;
434 }
435
436 /* any VLAN descriptors present ? */
437 if (list_empty(&fnic->vlans)) {
438 /* retry from timer */
Hiral Patel67125b02013-09-12 17:45:42 -0700439 atomic64_inc(&fnic_stats->vlan_stats.resp_withno_vlanID);
Hiral Pateld3c995f2013-02-25 16:18:36 -0800440 FNIC_FCS_DBG(KERN_INFO, fnic->lport->host,
441 "No VLAN descriptors in FIP VLAN response\n");
442 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
443 goto out;
444 }
445
446 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
447 fnic->set_vlan(fnic, vlan->vid);
448 vlan->state = FIP_VLAN_SENT; /* sent now */
449 vlan->sol_count++;
450 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
451
452 /* start the solicitation */
453 fcoe_ctlr_link_up(fip);
454
455 sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
456 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
457out:
458 return;
459}
460
461static void fnic_fcoe_start_fcf_disc(struct fnic *fnic)
462{
463 unsigned long flags;
464 struct fcoe_vlan *vlan;
465 u64 sol_time;
466
467 spin_lock_irqsave(&fnic->vlans_lock, flags);
468 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
469 fnic->set_vlan(fnic, vlan->vid);
470 vlan->state = FIP_VLAN_SENT; /* sent now */
471 vlan->sol_count = 1;
472 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
473
474 /* start the solicitation */
475 fcoe_ctlr_link_up(&fnic->ctlr);
476
477 sol_time = jiffies + msecs_to_jiffies(FCOE_CTLR_START_DELAY);
478 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
479}
480
481static int fnic_fcoe_vlan_check(struct fnic *fnic, u16 flag)
482{
483 unsigned long flags;
484 struct fcoe_vlan *fvlan;
485
486 spin_lock_irqsave(&fnic->vlans_lock, flags);
487 if (list_empty(&fnic->vlans)) {
488 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
489 return -EINVAL;
490 }
491
492 fvlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
493 if (fvlan->state == FIP_VLAN_USED) {
494 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
495 return 0;
496 }
497
498 if (fvlan->state == FIP_VLAN_SENT) {
499 fvlan->state = FIP_VLAN_USED;
500 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
501 return 0;
502 }
503 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
504 return -EINVAL;
505}
506
507static void fnic_event_enq(struct fnic *fnic, enum fnic_evt ev)
508{
509 struct fnic_event *fevt;
510 unsigned long flags;
511
512 fevt = kmalloc(sizeof(*fevt), GFP_ATOMIC);
513 if (!fevt)
514 return;
515
516 fevt->fnic = fnic;
517 fevt->event = ev;
518
519 spin_lock_irqsave(&fnic->fnic_lock, flags);
520 list_add_tail(&fevt->list, &fnic->evlist);
521 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
522
523 schedule_work(&fnic->event_work);
524}
525
526static int fnic_fcoe_handle_fip_frame(struct fnic *fnic, struct sk_buff *skb)
527{
528 struct fip_header *fiph;
529 int ret = 1;
530 u16 op;
531 u8 sub;
532
533 if (!skb || !(skb->data))
534 return -1;
535
536 if (skb_linearize(skb))
537 goto drop;
538
539 fiph = (struct fip_header *)skb->data;
540 op = ntohs(fiph->fip_op);
541 sub = fiph->fip_subcode;
542
543 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
544 goto drop;
545
546 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
547 goto drop;
548
549 if (op == FIP_OP_DISC && sub == FIP_SC_ADV) {
550 if (fnic_fcoe_vlan_check(fnic, ntohs(fiph->fip_flags)))
551 goto drop;
552 /* pass it on to fcoe */
553 ret = 1;
554 } else if (op == FIP_OP_VLAN && sub == FIP_SC_VL_REP) {
555 /* set the vlan as used */
556 fnic_fcoe_process_vlan_resp(fnic, skb);
557 ret = 0;
558 } else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK) {
559 /* received CVL request, restart vlan disc */
560 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
561 /* pass it on to fcoe */
562 ret = 1;
563 }
564drop:
565 return ret;
566}
567
568void fnic_handle_fip_frame(struct work_struct *work)
569{
570 struct fnic *fnic = container_of(work, struct fnic, fip_frame_work);
Hiral Patel67125b02013-09-12 17:45:42 -0700571 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Hiral Pateld3c995f2013-02-25 16:18:36 -0800572 unsigned long flags;
573 struct sk_buff *skb;
574 struct ethhdr *eh;
575
576 while ((skb = skb_dequeue(&fnic->fip_frame_queue))) {
577 spin_lock_irqsave(&fnic->fnic_lock, flags);
578 if (fnic->stop_rx_link_events) {
579 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
580 dev_kfree_skb(skb);
581 return;
582 }
583 /*
584 * If we're in a transitional state, just re-queue and return.
585 * The queue will be serviced when we get to a stable state.
586 */
587 if (fnic->state != FNIC_IN_FC_MODE &&
588 fnic->state != FNIC_IN_ETH_MODE) {
589 skb_queue_head(&fnic->fip_frame_queue, skb);
590 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
591 return;
592 }
593 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
594 eh = (struct ethhdr *)skb->data;
595 if (eh->h_proto == htons(ETH_P_FIP)) {
596 skb_pull(skb, sizeof(*eh));
597 if (fnic_fcoe_handle_fip_frame(fnic, skb) <= 0) {
598 dev_kfree_skb(skb);
599 continue;
600 }
601 /*
602 * If there's FLOGI rejects - clear all
603 * fcf's & restart from scratch
604 */
605 if (is_fnic_fip_flogi_reject(&fnic->ctlr, skb)) {
Hiral Patel67125b02013-09-12 17:45:42 -0700606 atomic64_inc(
607 &fnic_stats->vlan_stats.flogi_rejects);
Hiral Pateld3c995f2013-02-25 16:18:36 -0800608 shost_printk(KERN_INFO, fnic->lport->host,
609 "Trigger a Link down - VLAN Disc\n");
610 fcoe_ctlr_link_down(&fnic->ctlr);
611 /* start FCoE VLAN discovery */
612 fnic_fcoe_send_vlan_req(fnic);
613 dev_kfree_skb(skb);
614 continue;
615 }
616 fcoe_ctlr_recv(&fnic->ctlr, skb);
617 continue;
618 }
619 }
620}
621
Joe Eykholt78112e52009-11-03 11:49:22 -0800622/**
623 * fnic_import_rq_eth_pkt() - handle received FCoE or FIP frame.
624 * @fnic: fnic instance.
625 * @skb: Ethernet Frame.
626 */
627static inline int fnic_import_rq_eth_pkt(struct fnic *fnic, struct sk_buff *skb)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700628{
629 struct fc_frame *fp;
630 struct ethhdr *eh;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700631 struct fcoe_hdr *fcoe_hdr;
632 struct fcoe_crc_eof *ft;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700633
Joe Eykholt78112e52009-11-03 11:49:22 -0800634 /*
635 * Undo VLAN encapsulation if present.
636 */
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700637 eh = (struct ethhdr *)skb->data;
Joe Eykholt78112e52009-11-03 11:49:22 -0800638 if (eh->h_proto == htons(ETH_P_8021Q)) {
639 memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
640 eh = (struct ethhdr *)skb_pull(skb, VLAN_HLEN);
641 skb_reset_mac_header(skb);
642 }
643 if (eh->h_proto == htons(ETH_P_FIP)) {
Hiral Pateld7fadce2013-02-25 16:18:37 -0800644 if (!(fnic->config.flags & VFCF_FIP_CAPABLE)) {
645 printk(KERN_ERR "Dropped FIP frame, as firmware "
646 "uses non-FIP mode, Enable FIP "
647 "using UCSM\n");
648 goto drop;
649 }
Hiral Shahabb14142014-04-18 12:28:19 -0700650 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
651 FNIC_FC_RECV|0x80, (char *)skb->data, skb->len)) != 0) {
652 printk(KERN_ERR "fnic ctlr frame trace error!!!");
653 }
Hiral Pateld3c995f2013-02-25 16:18:36 -0800654 skb_queue_tail(&fnic->fip_frame_queue, skb);
655 queue_work(fnic_fip_queue, &fnic->fip_frame_work);
Joe Eykholt78112e52009-11-03 11:49:22 -0800656 return 1; /* let caller know packet was used */
657 }
658 if (eh->h_proto != htons(ETH_P_FCOE))
659 goto drop;
660 skb_set_network_header(skb, sizeof(*eh));
661 skb_pull(skb, sizeof(*eh));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700662
663 fcoe_hdr = (struct fcoe_hdr *)skb->data;
664 if (FC_FCOE_DECAPS_VER(fcoe_hdr) != FC_FCOE_VER)
Joe Eykholt78112e52009-11-03 11:49:22 -0800665 goto drop;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700666
667 fp = (struct fc_frame *)skb;
668 fc_frame_init(fp);
669 fr_sof(fp) = fcoe_hdr->fcoe_sof;
670 skb_pull(skb, sizeof(struct fcoe_hdr));
Joe Eykholt78112e52009-11-03 11:49:22 -0800671 skb_reset_transport_header(skb);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700672
Joe Eykholt78112e52009-11-03 11:49:22 -0800673 ft = (struct fcoe_crc_eof *)(skb->data + skb->len - sizeof(*ft));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700674 fr_eof(fp) = ft->fcoe_eof;
Joe Eykholt78112e52009-11-03 11:49:22 -0800675 skb_trim(skb, skb->len - sizeof(*ft));
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700676 return 0;
Joe Eykholt78112e52009-11-03 11:49:22 -0800677drop:
678 dev_kfree_skb_irq(skb);
679 return -1;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700680}
681
Joe Eykholt78112e52009-11-03 11:49:22 -0800682/**
683 * fnic_update_mac_locked() - set data MAC address and filters.
684 * @fnic: fnic instance.
685 * @new: newly-assigned FCoE MAC address.
686 *
687 * Called with the fnic lock held.
688 */
689void fnic_update_mac_locked(struct fnic *fnic, u8 *new)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700690{
Joe Eykholt78112e52009-11-03 11:49:22 -0800691 u8 *ctl = fnic->ctlr.ctl_src_addr;
692 u8 *data = fnic->data_src_addr;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700693
Joe Eykholt78112e52009-11-03 11:49:22 -0800694 if (is_zero_ether_addr(new))
695 new = ctl;
Joe Perches6942df72013-09-02 03:32:33 +0000696 if (ether_addr_equal(data, new))
Joe Eykholt78112e52009-11-03 11:49:22 -0800697 return;
698 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host, "update_mac %pM\n", new);
Joe Perches6942df72013-09-02 03:32:33 +0000699 if (!is_zero_ether_addr(data) && !ether_addr_equal(data, ctl))
Joe Eykholt78112e52009-11-03 11:49:22 -0800700 vnic_dev_del_addr(fnic->vdev, data);
701 memcpy(data, new, ETH_ALEN);
Joe Perches6942df72013-09-02 03:32:33 +0000702 if (!ether_addr_equal(new, ctl))
Joe Eykholt78112e52009-11-03 11:49:22 -0800703 vnic_dev_add_addr(fnic->vdev, new);
704}
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700705
Joe Eykholt78112e52009-11-03 11:49:22 -0800706/**
707 * fnic_update_mac() - set data MAC address and filters.
708 * @lport: local port.
709 * @new: newly-assigned FCoE MAC address.
710 */
711void fnic_update_mac(struct fc_lport *lport, u8 *new)
712{
713 struct fnic *fnic = lport_priv(lport);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700714
Joe Eykholt78112e52009-11-03 11:49:22 -0800715 spin_lock_irq(&fnic->fnic_lock);
716 fnic_update_mac_locked(fnic, new);
717 spin_unlock_irq(&fnic->fnic_lock);
718}
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700719
Joe Eykholt78112e52009-11-03 11:49:22 -0800720/**
721 * fnic_set_port_id() - set the port_ID after successful FLOGI.
722 * @lport: local port.
723 * @port_id: assigned FC_ID.
724 * @fp: received frame containing the FLOGI accept or NULL.
725 *
726 * This is called from libfc when a new FC_ID has been assigned.
727 * This causes us to reset the firmware to FC_MODE and setup the new MAC
728 * address and FC_ID.
729 *
730 * It is also called with FC_ID 0 when we're logged off.
731 *
732 * If the FC_ID is due to point-to-point, fp may be NULL.
733 */
734void fnic_set_port_id(struct fc_lport *lport, u32 port_id, struct fc_frame *fp)
735{
736 struct fnic *fnic = lport_priv(lport);
737 u8 *mac;
738 int ret;
739
740 FNIC_FCS_DBG(KERN_DEBUG, lport->host, "set port_id %x fp %p\n",
741 port_id, fp);
742
743 /*
744 * If we're clearing the FC_ID, change to use the ctl_src_addr.
745 * Set ethernet mode to send FLOGI.
746 */
747 if (!port_id) {
748 fnic_update_mac(lport, fnic->ctlr.ctl_src_addr);
749 fnic_set_eth_mode(fnic);
750 return;
751 }
752
753 if (fp) {
754 mac = fr_cb(fp)->granted_mac;
755 if (is_zero_ether_addr(mac)) {
756 /* non-FIP - FLOGI already accepted - ignore return */
757 fcoe_ctlr_recv_flogi(&fnic->ctlr, lport, fp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700758 }
Joe Eykholt78112e52009-11-03 11:49:22 -0800759 fnic_update_mac(lport, mac);
760 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700761
Joe Eykholt78112e52009-11-03 11:49:22 -0800762 /* Change state to reflect transition to FC mode */
763 spin_lock_irq(&fnic->fnic_lock);
764 if (fnic->state == FNIC_IN_ETH_MODE || fnic->state == FNIC_IN_FC_MODE)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700765 fnic->state = FNIC_IN_ETH_TRANS_FC_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -0800766 else {
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700767 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
768 "Unexpected fnic state %s while"
769 " processing flogi resp\n",
770 fnic_state_to_str(fnic->state));
Joe Eykholt78112e52009-11-03 11:49:22 -0800771 spin_unlock_irq(&fnic->fnic_lock);
772 return;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700773 }
Joe Eykholt78112e52009-11-03 11:49:22 -0800774 spin_unlock_irq(&fnic->fnic_lock);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700775
776 /*
Joe Eykholt78112e52009-11-03 11:49:22 -0800777 * Send FLOGI registration to firmware to set up FC mode.
778 * The new address will be set up when registration completes.
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700779 */
Joe Eykholt78112e52009-11-03 11:49:22 -0800780 ret = fnic_flogi_reg_handler(fnic, port_id);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700781
782 if (ret < 0) {
Joe Eykholt78112e52009-11-03 11:49:22 -0800783 spin_lock_irq(&fnic->fnic_lock);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700784 if (fnic->state == FNIC_IN_ETH_TRANS_FC_MODE)
785 fnic->state = FNIC_IN_ETH_MODE;
Joe Eykholt78112e52009-11-03 11:49:22 -0800786 spin_unlock_irq(&fnic->fnic_lock);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700787 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700788}
789
790static void fnic_rq_cmpl_frame_recv(struct vnic_rq *rq, struct cq_desc
791 *cq_desc, struct vnic_rq_buf *buf,
792 int skipped __attribute__((unused)),
793 void *opaque)
794{
795 struct fnic *fnic = vnic_dev_priv(rq->vdev);
796 struct sk_buff *skb;
797 struct fc_frame *fp;
Hiral Patel67125b02013-09-12 17:45:42 -0700798 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700799 unsigned int eth_hdrs_stripped;
800 u8 type, color, eop, sop, ingress_port, vlan_stripped;
801 u8 fcoe = 0, fcoe_sof, fcoe_eof;
802 u8 fcoe_fc_crc_ok = 1, fcoe_enc_error = 0;
803 u8 tcp_udp_csum_ok, udp, tcp, ipv4_csum_ok;
804 u8 ipv6, ipv4, ipv4_fragment, rss_type, csum_not_calc;
805 u8 fcs_ok = 1, packet_error = 0;
806 u16 q_number, completed_index, bytes_written = 0, vlan, checksum;
807 u32 rss_hash;
808 u16 exchange_id, tmpl;
809 u8 sof = 0;
810 u8 eof = 0;
811 u32 fcp_bytes_written = 0;
812 unsigned long flags;
813
814 pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
815 PCI_DMA_FROMDEVICE);
816 skb = buf->os_buf;
Joe Eykholt78112e52009-11-03 11:49:22 -0800817 fp = (struct fc_frame *)skb;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700818 buf->os_buf = NULL;
819
820 cq_desc_dec(cq_desc, &type, &color, &q_number, &completed_index);
821 if (type == CQ_DESC_TYPE_RQ_FCP) {
822 cq_fcp_rq_desc_dec((struct cq_fcp_rq_desc *)cq_desc,
823 &type, &color, &q_number, &completed_index,
824 &eop, &sop, &fcoe_fc_crc_ok, &exchange_id,
825 &tmpl, &fcp_bytes_written, &sof, &eof,
826 &ingress_port, &packet_error,
827 &fcoe_enc_error, &fcs_ok, &vlan_stripped,
828 &vlan);
829 eth_hdrs_stripped = 1;
Joe Eykholt78112e52009-11-03 11:49:22 -0800830 skb_trim(skb, fcp_bytes_written);
831 fr_sof(fp) = sof;
832 fr_eof(fp) = eof;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700833
834 } else if (type == CQ_DESC_TYPE_RQ_ENET) {
835 cq_enet_rq_desc_dec((struct cq_enet_rq_desc *)cq_desc,
836 &type, &color, &q_number, &completed_index,
837 &ingress_port, &fcoe, &eop, &sop,
838 &rss_type, &csum_not_calc, &rss_hash,
839 &bytes_written, &packet_error,
840 &vlan_stripped, &vlan, &checksum,
841 &fcoe_sof, &fcoe_fc_crc_ok,
842 &fcoe_enc_error, &fcoe_eof,
843 &tcp_udp_csum_ok, &udp, &tcp,
844 &ipv4_csum_ok, &ipv6, &ipv4,
845 &ipv4_fragment, &fcs_ok);
846 eth_hdrs_stripped = 0;
Joe Eykholt78112e52009-11-03 11:49:22 -0800847 skb_trim(skb, bytes_written);
848 if (!fcs_ok) {
Hiral Patel67125b02013-09-12 17:45:42 -0700849 atomic64_inc(&fnic_stats->misc_stats.frame_errors);
Joe Eykholt78112e52009-11-03 11:49:22 -0800850 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
851 "fcs error. dropping packet.\n");
852 goto drop;
853 }
854 if (fnic_import_rq_eth_pkt(fnic, skb))
855 return;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700856
857 } else {
858 /* wrong CQ type*/
859 shost_printk(KERN_ERR, fnic->lport->host,
860 "fnic rq_cmpl wrong cq type x%x\n", type);
861 goto drop;
862 }
863
864 if (!fcs_ok || packet_error || !fcoe_fc_crc_ok || fcoe_enc_error) {
Hiral Patel67125b02013-09-12 17:45:42 -0700865 atomic64_inc(&fnic_stats->misc_stats.frame_errors);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700866 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
867 "fnic rq_cmpl fcoe x%x fcsok x%x"
868 " pkterr x%x fcoe_fc_crc_ok x%x, fcoe_enc_err"
869 " x%x\n",
870 fcoe, fcs_ok, packet_error,
871 fcoe_fc_crc_ok, fcoe_enc_error);
872 goto drop;
873 }
874
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700875 spin_lock_irqsave(&fnic->fnic_lock, flags);
876 if (fnic->stop_rx_link_events) {
877 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
878 goto drop;
879 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700880 fr_dev(fp) = fnic->lport;
881 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Hiral Shahabb14142014-04-18 12:28:19 -0700882 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_RECV,
883 (char *)skb->data, skb->len)) != 0) {
884 printk(KERN_ERR "fnic ctlr frame trace error!!!");
885 }
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700886
887 skb_queue_tail(&fnic->frame_queue, skb);
888 queue_work(fnic_event_queue, &fnic->frame_work);
889
890 return;
891drop:
892 dev_kfree_skb_irq(skb);
893}
894
895static int fnic_rq_cmpl_handler_cont(struct vnic_dev *vdev,
896 struct cq_desc *cq_desc, u8 type,
897 u16 q_number, u16 completed_index,
898 void *opaque)
899{
900 struct fnic *fnic = vnic_dev_priv(vdev);
901
902 vnic_rq_service(&fnic->rq[q_number], cq_desc, completed_index,
903 VNIC_RQ_RETURN_DESC, fnic_rq_cmpl_frame_recv,
904 NULL);
905 return 0;
906}
907
908int fnic_rq_cmpl_handler(struct fnic *fnic, int rq_work_to_do)
909{
910 unsigned int tot_rq_work_done = 0, cur_work_done;
911 unsigned int i;
912 int err;
913
914 for (i = 0; i < fnic->rq_count; i++) {
915 cur_work_done = vnic_cq_service(&fnic->cq[i], rq_work_to_do,
916 fnic_rq_cmpl_handler_cont,
917 NULL);
918 if (cur_work_done) {
919 err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
920 if (err)
921 shost_printk(KERN_ERR, fnic->lport->host,
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300922 "fnic_alloc_rq_frame can't alloc"
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700923 " frame\n");
924 }
925 tot_rq_work_done += cur_work_done;
926 }
927
928 return tot_rq_work_done;
929}
930
931/*
932 * This function is called once at init time to allocate and fill RQ
933 * buffers. Subsequently, it is called in the interrupt context after RQ
934 * buffer processing to replenish the buffers in the RQ
935 */
936int fnic_alloc_rq_frame(struct vnic_rq *rq)
937{
938 struct fnic *fnic = vnic_dev_priv(rq->vdev);
939 struct sk_buff *skb;
940 u16 len;
941 dma_addr_t pa;
942
943 len = FC_FRAME_HEADROOM + FC_MAX_FRAME + FC_FRAME_TAILROOM;
944 skb = dev_alloc_skb(len);
945 if (!skb) {
946 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
947 "Unable to allocate RQ sk_buff\n");
948 return -ENOMEM;
949 }
950 skb_reset_mac_header(skb);
951 skb_reset_transport_header(skb);
952 skb_reset_network_header(skb);
953 skb_put(skb, len);
954 pa = pci_map_single(fnic->pdev, skb->data, len, PCI_DMA_FROMDEVICE);
955 fnic_queue_rq_desc(rq, skb, pa, len);
956 return 0;
957}
958
959void fnic_free_rq_buf(struct vnic_rq *rq, struct vnic_rq_buf *buf)
960{
961 struct fc_frame *fp = buf->os_buf;
962 struct fnic *fnic = vnic_dev_priv(rq->vdev);
963
964 pci_unmap_single(fnic->pdev, buf->dma_addr, buf->len,
965 PCI_DMA_FROMDEVICE);
966
967 dev_kfree_skb(fp_skb(fp));
968 buf->os_buf = NULL;
969}
970
Joe Eykholt78112e52009-11-03 11:49:22 -0800971/**
972 * fnic_eth_send() - Send Ethernet frame.
973 * @fip: fcoe_ctlr instance.
974 * @skb: Ethernet Frame, FIP, without VLAN encapsulation.
975 */
976void fnic_eth_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -0700977{
Joe Eykholt78112e52009-11-03 11:49:22 -0800978 struct fnic *fnic = fnic_from_ctlr(fip);
979 struct vnic_wq *wq = &fnic->wq[0];
980 dma_addr_t pa;
981 struct ethhdr *eth_hdr;
982 struct vlan_ethhdr *vlan_hdr;
983 unsigned long flags;
984
985 if (!fnic->vlan_hw_insert) {
986 eth_hdr = (struct ethhdr *)skb_mac_header(skb);
987 vlan_hdr = (struct vlan_ethhdr *)skb_push(skb,
988 sizeof(*vlan_hdr) - sizeof(*eth_hdr));
989 memcpy(vlan_hdr, eth_hdr, 2 * ETH_ALEN);
990 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
991 vlan_hdr->h_vlan_encapsulated_proto = eth_hdr->h_proto;
992 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
Hiral Shahabb14142014-04-18 12:28:19 -0700993 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
994 FNIC_FC_SEND|0x80, (char *)eth_hdr, skb->len)) != 0) {
995 printk(KERN_ERR "fnic ctlr frame trace error!!!");
996 }
997 } else {
998 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no,
999 FNIC_FC_SEND|0x80, (char *)skb->data, skb->len)) != 0) {
1000 printk(KERN_ERR "fnic ctlr frame trace error!!!");
1001 }
Joe Eykholt78112e52009-11-03 11:49:22 -08001002 }
1003
1004 pa = pci_map_single(fnic->pdev, skb->data, skb->len, PCI_DMA_TODEVICE);
1005
1006 spin_lock_irqsave(&fnic->wq_lock[0], flags);
1007 if (!vnic_wq_desc_avail(wq)) {
1008 pci_unmap_single(fnic->pdev, pa, skb->len, PCI_DMA_TODEVICE);
1009 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1010 kfree_skb(skb);
1011 return;
1012 }
1013
1014 fnic_queue_wq_eth_desc(wq, skb, pa, skb->len,
Hiral Patelc0773b72012-12-10 01:21:30 -08001015 0 /* hw inserts cos value */,
1016 fnic->vlan_id, 1);
Joe Eykholt78112e52009-11-03 11:49:22 -08001017 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001018}
1019
Joe Eykholt78112e52009-11-03 11:49:22 -08001020/*
1021 * Send FC frame.
1022 */
1023static int fnic_send_frame(struct fnic *fnic, struct fc_frame *fp)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001024{
1025 struct vnic_wq *wq = &fnic->wq[0];
1026 struct sk_buff *skb;
1027 dma_addr_t pa;
1028 struct ethhdr *eth_hdr;
1029 struct vlan_ethhdr *vlan_hdr;
1030 struct fcoe_hdr *fcoe_hdr;
1031 struct fc_frame_header *fh;
1032 u32 tot_len, eth_hdr_len;
1033 int ret = 0;
1034 unsigned long flags;
1035
1036 fh = fc_frame_header_get(fp);
1037 skb = fp_skb(fp);
1038
Joe Eykholt78112e52009-11-03 11:49:22 -08001039 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1040 fcoe_ctlr_els_send(&fnic->ctlr, fnic->lport, skb))
1041 return 0;
1042
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001043 if (!fnic->vlan_hw_insert) {
1044 eth_hdr_len = sizeof(*vlan_hdr) + sizeof(*fcoe_hdr);
1045 vlan_hdr = (struct vlan_ethhdr *)skb_push(skb, eth_hdr_len);
1046 eth_hdr = (struct ethhdr *)vlan_hdr;
1047 vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
1048 vlan_hdr->h_vlan_encapsulated_proto = htons(ETH_P_FCOE);
1049 vlan_hdr->h_vlan_TCI = htons(fnic->vlan_id);
1050 fcoe_hdr = (struct fcoe_hdr *)(vlan_hdr + 1);
1051 } else {
1052 eth_hdr_len = sizeof(*eth_hdr) + sizeof(*fcoe_hdr);
1053 eth_hdr = (struct ethhdr *)skb_push(skb, eth_hdr_len);
1054 eth_hdr->h_proto = htons(ETH_P_FCOE);
1055 fcoe_hdr = (struct fcoe_hdr *)(eth_hdr + 1);
1056 }
1057
Joe Eykholt78112e52009-11-03 11:49:22 -08001058 if (fnic->ctlr.map_dest)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001059 fc_fcoe_set_mac(eth_hdr->h_dest, fh->fh_d_id);
Joe Eykholt78112e52009-11-03 11:49:22 -08001060 else
1061 memcpy(eth_hdr->h_dest, fnic->ctlr.dest_addr, ETH_ALEN);
1062 memcpy(eth_hdr->h_source, fnic->data_src_addr, ETH_ALEN);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001063
1064 tot_len = skb->len;
1065 BUG_ON(tot_len % 4);
1066
1067 memset(fcoe_hdr, 0, sizeof(*fcoe_hdr));
1068 fcoe_hdr->fcoe_sof = fr_sof(fp);
1069 if (FC_FCOE_VER)
1070 FC_FCOE_ENCAPS_VER(fcoe_hdr, FC_FCOE_VER);
1071
1072 pa = pci_map_single(fnic->pdev, eth_hdr, tot_len, PCI_DMA_TODEVICE);
1073
Hiral Shahabb14142014-04-18 12:28:19 -07001074 if ((fnic_fc_trace_set_data(fnic->lport->host->host_no, FNIC_FC_SEND,
1075 (char *)eth_hdr, tot_len)) != 0) {
1076 printk(KERN_ERR "fnic ctlr frame trace error!!!");
1077 }
1078
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001079 spin_lock_irqsave(&fnic->wq_lock[0], flags);
1080
1081 if (!vnic_wq_desc_avail(wq)) {
1082 pci_unmap_single(fnic->pdev, pa,
1083 tot_len, PCI_DMA_TODEVICE);
1084 ret = -1;
1085 goto fnic_send_frame_end;
1086 }
1087
1088 fnic_queue_wq_desc(wq, skb, pa, tot_len, fr_eof(fp),
Hiral Patelc0773b72012-12-10 01:21:30 -08001089 0 /* hw inserts cos value */,
1090 fnic->vlan_id, 1, 1, 1);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001091fnic_send_frame_end:
1092 spin_unlock_irqrestore(&fnic->wq_lock[0], flags);
1093
1094 if (ret)
1095 dev_kfree_skb_any(fp_skb(fp));
1096
1097 return ret;
1098}
1099
1100/*
1101 * fnic_send
1102 * Routine to send a raw frame
1103 */
1104int fnic_send(struct fc_lport *lp, struct fc_frame *fp)
1105{
1106 struct fnic *fnic = lport_priv(lp);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001107 unsigned long flags;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001108
1109 if (fnic->in_remove) {
1110 dev_kfree_skb(fp_skb(fp));
Joe Eykholt78112e52009-11-03 11:49:22 -08001111 return -1;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001112 }
1113
Joe Eykholt78112e52009-11-03 11:49:22 -08001114 /*
1115 * Queue frame if in a transitional state.
1116 * This occurs while registering the Port_ID / MAC address after FLOGI.
1117 */
1118 spin_lock_irqsave(&fnic->fnic_lock, flags);
1119 if (fnic->state != FNIC_IN_FC_MODE && fnic->state != FNIC_IN_ETH_MODE) {
1120 skb_queue_tail(&fnic->tx_queue, fp_skb(fp));
1121 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1122 return 0;
1123 }
1124 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001125
Joe Eykholt78112e52009-11-03 11:49:22 -08001126 return fnic_send_frame(fnic, fp);
1127}
1128
1129/**
1130 * fnic_flush_tx() - send queued frames.
1131 * @fnic: fnic device
1132 *
1133 * Send frames that were waiting to go out in FC or Ethernet mode.
1134 * Whenever changing modes we purge queued frames, so these frames should
1135 * be queued for the stable mode that we're in, either FC or Ethernet.
1136 *
1137 * Called without fnic_lock held.
1138 */
1139void fnic_flush_tx(struct fnic *fnic)
1140{
1141 struct sk_buff *skb;
1142 struct fc_frame *fp;
1143
Brian Uchinod9e9ab52010-04-09 14:22:49 -07001144 while ((skb = skb_dequeue(&fnic->tx_queue))) {
Joe Eykholt78112e52009-11-03 11:49:22 -08001145 fp = (struct fc_frame *)skb;
1146 fnic_send_frame(fnic, fp);
1147 }
1148}
1149
1150/**
1151 * fnic_set_eth_mode() - put fnic into ethernet mode.
1152 * @fnic: fnic device
1153 *
1154 * Called without fnic lock held.
1155 */
1156static void fnic_set_eth_mode(struct fnic *fnic)
1157{
1158 unsigned long flags;
1159 enum fnic_state old_state;
1160 int ret;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001161
1162 spin_lock_irqsave(&fnic->fnic_lock, flags);
1163again:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001164 old_state = fnic->state;
1165 switch (old_state) {
1166 case FNIC_IN_FC_MODE:
1167 case FNIC_IN_ETH_TRANS_FC_MODE:
1168 default:
1169 fnic->state = FNIC_IN_FC_TRANS_ETH_MODE;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001170 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1171
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001172 ret = fnic_fw_reset_handler(fnic);
1173
1174 spin_lock_irqsave(&fnic->fnic_lock, flags);
1175 if (fnic->state != FNIC_IN_FC_TRANS_ETH_MODE)
1176 goto again;
Joe Eykholt78112e52009-11-03 11:49:22 -08001177 if (ret)
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001178 fnic->state = old_state;
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001179 break;
1180
1181 case FNIC_IN_FC_TRANS_ETH_MODE:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001182 case FNIC_IN_ETH_MODE:
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001183 break;
1184 }
Joe Eykholt78112e52009-11-03 11:49:22 -08001185 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
Abhijeet Joglekar5df6d732009-04-17 18:33:26 -07001186}
1187
1188static void fnic_wq_complete_frame_send(struct vnic_wq *wq,
1189 struct cq_desc *cq_desc,
1190 struct vnic_wq_buf *buf, void *opaque)
1191{
1192 struct sk_buff *skb = buf->os_buf;
1193 struct fc_frame *fp = (struct fc_frame *)skb;
1194 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1195
1196 pci_unmap_single(fnic->pdev, buf->dma_addr,
1197 buf->len, PCI_DMA_TODEVICE);
1198 dev_kfree_skb_irq(fp_skb(fp));
1199 buf->os_buf = NULL;
1200}
1201
1202static int fnic_wq_cmpl_handler_cont(struct vnic_dev *vdev,
1203 struct cq_desc *cq_desc, u8 type,
1204 u16 q_number, u16 completed_index,
1205 void *opaque)
1206{
1207 struct fnic *fnic = vnic_dev_priv(vdev);
1208 unsigned long flags;
1209
1210 spin_lock_irqsave(&fnic->wq_lock[q_number], flags);
1211 vnic_wq_service(&fnic->wq[q_number], cq_desc, completed_index,
1212 fnic_wq_complete_frame_send, NULL);
1213 spin_unlock_irqrestore(&fnic->wq_lock[q_number], flags);
1214
1215 return 0;
1216}
1217
1218int fnic_wq_cmpl_handler(struct fnic *fnic, int work_to_do)
1219{
1220 unsigned int wq_work_done = 0;
1221 unsigned int i;
1222
1223 for (i = 0; i < fnic->raw_wq_count; i++) {
1224 wq_work_done += vnic_cq_service(&fnic->cq[fnic->rq_count+i],
1225 work_to_do,
1226 fnic_wq_cmpl_handler_cont,
1227 NULL);
1228 }
1229
1230 return wq_work_done;
1231}
1232
1233
1234void fnic_free_wq_buf(struct vnic_wq *wq, struct vnic_wq_buf *buf)
1235{
1236 struct fc_frame *fp = buf->os_buf;
1237 struct fnic *fnic = vnic_dev_priv(wq->vdev);
1238
1239 pci_unmap_single(fnic->pdev, buf->dma_addr,
1240 buf->len, PCI_DMA_TODEVICE);
1241
1242 dev_kfree_skb(fp_skb(fp));
1243 buf->os_buf = NULL;
1244}
Hiral Pateld3c995f2013-02-25 16:18:36 -08001245
1246void fnic_fcoe_reset_vlans(struct fnic *fnic)
1247{
1248 unsigned long flags;
1249 struct fcoe_vlan *vlan;
1250 struct fcoe_vlan *next;
1251
1252 /*
1253 * indicate a link down to fcoe so that all fcf's are free'd
1254 * might not be required since we did this before sending vlan
1255 * discovery request
1256 */
1257 spin_lock_irqsave(&fnic->vlans_lock, flags);
1258 if (!list_empty(&fnic->vlans)) {
1259 list_for_each_entry_safe(vlan, next, &fnic->vlans, list) {
1260 list_del(&vlan->list);
1261 kfree(vlan);
1262 }
1263 }
1264 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1265}
1266
1267void fnic_handle_fip_timer(struct fnic *fnic)
1268{
1269 unsigned long flags;
1270 struct fcoe_vlan *vlan;
Hiral Patel67125b02013-09-12 17:45:42 -07001271 struct fnic_stats *fnic_stats = &fnic->fnic_stats;
Hiral Pateld3c995f2013-02-25 16:18:36 -08001272 u64 sol_time;
1273
1274 spin_lock_irqsave(&fnic->fnic_lock, flags);
1275 if (fnic->stop_rx_link_events) {
1276 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1277 return;
1278 }
1279 spin_unlock_irqrestore(&fnic->fnic_lock, flags);
1280
1281 if (fnic->ctlr.mode == FIP_ST_NON_FIP)
1282 return;
1283
1284 spin_lock_irqsave(&fnic->vlans_lock, flags);
1285 if (list_empty(&fnic->vlans)) {
1286 /* no vlans available, try again */
1287 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1288 "Start VLAN Discovery\n");
1289 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1290 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1291 return;
1292 }
1293
1294 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan, list);
1295 shost_printk(KERN_DEBUG, fnic->lport->host,
1296 "fip_timer: vlan %d state %d sol_count %d\n",
1297 vlan->vid, vlan->state, vlan->sol_count);
1298 switch (vlan->state) {
1299 case FIP_VLAN_USED:
1300 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1301 "FIP VLAN is selected for FC transaction\n");
1302 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1303 break;
1304 case FIP_VLAN_FAILED:
1305 /* if all vlans are in failed state, restart vlan disc */
1306 FNIC_FCS_DBG(KERN_DEBUG, fnic->lport->host,
1307 "Start VLAN Discovery\n");
1308 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
1309 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1310 break;
1311 case FIP_VLAN_SENT:
1312 if (vlan->sol_count >= FCOE_CTLR_MAX_SOL) {
1313 /*
1314 * no response on this vlan, remove from the list.
1315 * Try the next vlan
1316 */
1317 shost_printk(KERN_INFO, fnic->lport->host,
1318 "Dequeue this VLAN ID %d from list\n",
1319 vlan->vid);
1320 list_del(&vlan->list);
1321 kfree(vlan);
1322 vlan = NULL;
1323 if (list_empty(&fnic->vlans)) {
1324 /* we exhausted all vlans, restart vlan disc */
1325 spin_unlock_irqrestore(&fnic->vlans_lock,
1326 flags);
1327 shost_printk(KERN_INFO, fnic->lport->host,
1328 "fip_timer: vlan list empty, "
1329 "trigger vlan disc\n");
1330 fnic_event_enq(fnic, FNIC_EVT_START_VLAN_DISC);
1331 return;
1332 }
1333 /* check the next vlan */
1334 vlan = list_first_entry(&fnic->vlans, struct fcoe_vlan,
1335 list);
1336 fnic->set_vlan(fnic, vlan->vid);
1337 vlan->state = FIP_VLAN_SENT; /* sent now */
1338 }
1339 spin_unlock_irqrestore(&fnic->vlans_lock, flags);
Hiral Patel67125b02013-09-12 17:45:42 -07001340 atomic64_inc(&fnic_stats->vlan_stats.sol_expiry_count);
Hiral Pateld3c995f2013-02-25 16:18:36 -08001341 vlan->sol_count++;
1342 sol_time = jiffies + msecs_to_jiffies
1343 (FCOE_CTLR_START_DELAY);
1344 mod_timer(&fnic->fip_timer, round_jiffies(sol_time));
1345 break;
1346 }
1347}