]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/scsi/bnx2fc/bnx2fc_fcoe.c
[SCSI] qla4xxx: Update driver version to 5.02.00-k6
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / bnx2fc / bnx2fc_fcoe.c
CommitLineData
853e2bd2
BG
1/* bnx2fc_fcoe.c: Broadcom NetXtreme II Linux FCoE offload driver.
2 * This file contains the code that interacts with libfc, libfcoe,
3 * cnic modules to create FCoE instances, send/receive non-offloaded
4 * FIP/FCoE packets, listen to link events etc.
5 *
6 * Copyright (c) 2008 - 2010 Broadcom Corporation
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 *
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
13 */
14
15#include "bnx2fc.h"
16
17static struct list_head adapter_list;
18static u32 adapter_count;
19static DEFINE_MUTEX(bnx2fc_dev_lock);
20DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
21
22#define DRV_MODULE_NAME "bnx2fc"
23#define DRV_MODULE_VERSION BNX2FC_VERSION
d9f7f37b 24#define DRV_MODULE_RELDATE "Mar 17, 2011"
853e2bd2
BG
25
26
27static char version[] __devinitdata =
28 "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
29 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
30
31
32MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
33MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
34MODULE_LICENSE("GPL");
35MODULE_VERSION(DRV_MODULE_VERSION);
36
37#define BNX2FC_MAX_QUEUE_DEPTH 256
38#define BNX2FC_MIN_QUEUE_DEPTH 32
39#define FCOE_WORD_TO_BYTE 4
40
41static struct scsi_transport_template *bnx2fc_transport_template;
42static struct scsi_transport_template *bnx2fc_vport_xport_template;
43
44struct workqueue_struct *bnx2fc_wq;
45
46/* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
47 * Here the io threads are per cpu but the l2 thread is just one
48 */
49struct fcoe_percpu_s bnx2fc_global;
50DEFINE_SPINLOCK(bnx2fc_global_lock);
51
52static struct cnic_ulp_ops bnx2fc_cnic_cb;
53static struct libfc_function_template bnx2fc_libfc_fcn_templ;
54static struct scsi_host_template bnx2fc_shost_template;
55static struct fc_function_template bnx2fc_transport_function;
56static struct fc_function_template bnx2fc_vport_xport_function;
57static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
58static int bnx2fc_destroy(struct net_device *net_device);
59static int bnx2fc_enable(struct net_device *netdev);
60static int bnx2fc_disable(struct net_device *netdev);
61
62static void bnx2fc_recv_frame(struct sk_buff *skb);
63
64static void bnx2fc_start_disc(struct bnx2fc_hba *hba);
65static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
66static int bnx2fc_net_config(struct fc_lport *lp);
67static int bnx2fc_lport_config(struct fc_lport *lport);
68static int bnx2fc_em_config(struct fc_lport *lport);
69static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
70static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
71static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
72static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
73static struct fc_lport *bnx2fc_if_create(struct bnx2fc_hba *hba,
74 struct device *parent, int npiv);
75static void bnx2fc_destroy_work(struct work_struct *work);
76
77static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
78static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
79
80static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
81static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
82
83static void bnx2fc_port_shutdown(struct fc_lport *lport);
84static void bnx2fc_stop(struct bnx2fc_hba *hba);
85static int __init bnx2fc_mod_init(void);
86static void __exit bnx2fc_mod_exit(void);
87
88unsigned int bnx2fc_debug_level;
89module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
90
91static int bnx2fc_cpu_callback(struct notifier_block *nfb,
92 unsigned long action, void *hcpu);
93/* notification function for CPU hotplug events */
94static struct notifier_block bnx2fc_cpu_notifier = {
95 .notifier_call = bnx2fc_cpu_callback,
96};
97
98static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
99{
100 struct fcoe_percpu_s *bg;
101 struct fcoe_rcv_info *fr;
102 struct sk_buff_head *list;
103 struct sk_buff *skb, *next;
104 struct sk_buff *head;
105
106 bg = &bnx2fc_global;
107 spin_lock_bh(&bg->fcoe_rx_list.lock);
108 list = &bg->fcoe_rx_list;
109 head = list->next;
110 for (skb = head; skb != (struct sk_buff *)list;
111 skb = next) {
112 next = skb->next;
113 fr = fcoe_dev_from_skb(skb);
114 if (fr->fr_dev == lp) {
115 __skb_unlink(skb, list);
116 kfree_skb(skb);
117 }
118 }
119 spin_unlock_bh(&bg->fcoe_rx_list.lock);
120}
121
122int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
123{
124 int rc;
125 spin_lock(&bnx2fc_global_lock);
126 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
127 spin_unlock(&bnx2fc_global_lock);
128
129 return rc;
130}
131
132static void bnx2fc_abort_io(struct fc_lport *lport)
133{
134 /*
135 * This function is no-op for bnx2fc, but we do
136 * not want to leave it as NULL either, as libfc
137 * can call the default function which is
138 * fc_fcp_abort_io.
139 */
140}
141
142static void bnx2fc_cleanup(struct fc_lport *lport)
143{
144 struct fcoe_port *port = lport_priv(lport);
145 struct bnx2fc_hba *hba = port->priv;
146 struct bnx2fc_rport *tgt;
147 int i;
148
149 BNX2FC_MISC_DBG("Entered %s\n", __func__);
150 mutex_lock(&hba->hba_mutex);
151 spin_lock_bh(&hba->hba_lock);
152 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
153 tgt = hba->tgt_ofld_list[i];
154 if (tgt) {
155 /* Cleanup IOs belonging to requested vport */
156 if (tgt->port == port) {
157 spin_unlock_bh(&hba->hba_lock);
158 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
159 bnx2fc_flush_active_ios(tgt);
160 spin_lock_bh(&hba->hba_lock);
161 }
162 }
163 }
164 spin_unlock_bh(&hba->hba_lock);
165 mutex_unlock(&hba->hba_mutex);
166}
167
168static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
169 struct fc_frame *fp)
170{
171 struct fc_rport_priv *rdata = tgt->rdata;
172 struct fc_frame_header *fh;
173 int rc = 0;
174
175 fh = fc_frame_header_get(fp);
176 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
177 "r_ctl = 0x%x\n", rdata->ids.port_id,
178 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
179 if ((fh->fh_type == FC_TYPE_ELS) &&
180 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
181
182 switch (fc_frame_payload_op(fp)) {
183 case ELS_ADISC:
184 rc = bnx2fc_send_adisc(tgt, fp);
185 break;
186 case ELS_LOGO:
187 rc = bnx2fc_send_logo(tgt, fp);
188 break;
189 case ELS_RLS:
190 rc = bnx2fc_send_rls(tgt, fp);
191 break;
192 default:
193 break;
194 }
195 } else if ((fh->fh_type == FC_TYPE_BLS) &&
196 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
197 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
198 else {
199 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
200 "rctl 0x%x thru non-offload path\n",
201 fh->fh_type, fh->fh_r_ctl);
202 return -ENODEV;
203 }
204 if (rc)
205 return -ENOMEM;
206 else
207 return 0;
208}
209
210/**
211 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
212 *
213 * @lport: the associated local port
214 * @fp: the fc_frame to be transmitted
215 */
216static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
217{
218 struct ethhdr *eh;
219 struct fcoe_crc_eof *cp;
220 struct sk_buff *skb;
221 struct fc_frame_header *fh;
222 struct bnx2fc_hba *hba;
223 struct fcoe_port *port;
224 struct fcoe_hdr *hp;
225 struct bnx2fc_rport *tgt;
226 struct fcoe_dev_stats *stats;
227 u8 sof, eof;
228 u32 crc;
229 unsigned int hlen, tlen, elen;
230 int wlen, rc = 0;
231
232 port = (struct fcoe_port *)lport_priv(lport);
233 hba = port->priv;
234
235 fh = fc_frame_header_get(fp);
236
237 skb = fp_skb(fp);
238 if (!lport->link_up) {
239 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
240 kfree_skb(skb);
241 return 0;
242 }
243
244 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
245 if (!hba->ctlr.sel_fcf) {
246 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
247 kfree_skb(skb);
248 return -EINVAL;
249 }
250 if (fcoe_ctlr_els_send(&hba->ctlr, lport, skb))
251 return 0;
252 }
253
254 sof = fr_sof(fp);
255 eof = fr_eof(fp);
256
257 /*
258 * Snoop the frame header to check if the frame is for
259 * an offloaded session
260 */
261 /*
262 * tgt_ofld_list access is synchronized using
263 * both hba mutex and hba lock. Atleast hba mutex or
264 * hba lock needs to be held for read access.
265 */
266
267 spin_lock_bh(&hba->hba_lock);
268 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
269 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
270 /* This frame is for offloaded session */
271 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
272 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
273 spin_unlock_bh(&hba->hba_lock);
274 rc = bnx2fc_xmit_l2_frame(tgt, fp);
275 if (rc != -ENODEV) {
276 kfree_skb(skb);
277 return rc;
278 }
279 } else {
280 spin_unlock_bh(&hba->hba_lock);
281 }
282
283 elen = sizeof(struct ethhdr);
284 hlen = sizeof(struct fcoe_hdr);
285 tlen = sizeof(struct fcoe_crc_eof);
286 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
287
288 skb->ip_summed = CHECKSUM_NONE;
289 crc = fcoe_fc_crc(fp);
290
291 /* copy port crc and eof to the skb buff */
292 if (skb_is_nonlinear(skb)) {
293 skb_frag_t *frag;
294 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
295 kfree_skb(skb);
296 return -ENOMEM;
297 }
298 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
299 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
300 + frag->page_offset;
301 } else {
302 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
303 }
304
305 memset(cp, 0, sizeof(*cp));
306 cp->fcoe_eof = eof;
307 cp->fcoe_crc32 = cpu_to_le32(~crc);
308 if (skb_is_nonlinear(skb)) {
309 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
310 cp = NULL;
311 }
312
313 /* adjust skb network/transport offsets to match mac/fcoe/port */
314 skb_push(skb, elen + hlen);
315 skb_reset_mac_header(skb);
316 skb_reset_network_header(skb);
317 skb->mac_len = elen;
318 skb->protocol = htons(ETH_P_FCOE);
319 skb->dev = hba->netdev;
320
321 /* fill up mac and fcoe headers */
322 eh = eth_hdr(skb);
323 eh->h_proto = htons(ETH_P_FCOE);
324 if (hba->ctlr.map_dest)
325 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
326 else
327 /* insert GW address */
328 memcpy(eh->h_dest, hba->ctlr.dest_addr, ETH_ALEN);
329
330 if (unlikely(hba->ctlr.flogi_oxid != FC_XID_UNKNOWN))
331 memcpy(eh->h_source, hba->ctlr.ctl_src_addr, ETH_ALEN);
332 else
333 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
334
335 hp = (struct fcoe_hdr *)(eh + 1);
336 memset(hp, 0, sizeof(*hp));
337 if (FC_FCOE_VER)
338 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
339 hp->fcoe_sof = sof;
340
341 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
342 if (lport->seq_offload && fr_max_payload(fp)) {
343 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
344 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
345 } else {
346 skb_shinfo(skb)->gso_type = 0;
347 skb_shinfo(skb)->gso_size = 0;
348 }
349
350 /*update tx stats */
351 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
352 stats->TxFrames++;
353 stats->TxWords += wlen;
354 put_cpu();
355
356 /* send down to lld */
357 fr_dev(fp) = lport;
358 if (port->fcoe_pending_queue.qlen)
359 fcoe_check_wait_queue(lport, skb);
360 else if (fcoe_start_io(skb))
361 fcoe_check_wait_queue(lport, skb);
362
363 return 0;
364}
365
366/**
367 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
368 *
369 * @skb: the receive socket buffer
370 * @dev: associated net device
371 * @ptype: context
372 * @olddev: last device
373 *
374 * This function receives the packet and builds FC frame and passes it up
375 */
376static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
377 struct packet_type *ptype, struct net_device *olddev)
378{
379 struct fc_lport *lport;
380 struct bnx2fc_hba *hba;
381 struct fc_frame_header *fh;
382 struct fcoe_rcv_info *fr;
383 struct fcoe_percpu_s *bg;
384 unsigned short oxid;
385
386 hba = container_of(ptype, struct bnx2fc_hba, fcoe_packet_type);
387 lport = hba->ctlr.lp;
388
389 if (unlikely(lport == NULL)) {
390 printk(KERN_ALERT PFX "bnx2fc_rcv: lport is NULL\n");
391 goto err;
392 }
393
394 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
395 printk(KERN_ALERT PFX "bnx2fc_rcv: Wrong FC type frame\n");
396 goto err;
397 }
398
399 /*
400 * Check for minimum frame length, and make sure required FCoE
401 * and FC headers are pulled into the linear data area.
402 */
403 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
404 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
405 goto err;
406
407 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
408 fh = (struct fc_frame_header *) skb_transport_header(skb);
409
410 oxid = ntohs(fh->fh_ox_id);
411
412 fr = fcoe_dev_from_skb(skb);
413 fr->fr_dev = lport;
414 fr->ptype = ptype;
415
416 bg = &bnx2fc_global;
417 spin_lock_bh(&bg->fcoe_rx_list.lock);
418
419 __skb_queue_tail(&bg->fcoe_rx_list, skb);
420 if (bg->fcoe_rx_list.qlen == 1)
421 wake_up_process(bg->thread);
422
423 spin_unlock_bh(&bg->fcoe_rx_list.lock);
424
425 return 0;
426err:
427 kfree_skb(skb);
428 return -1;
429}
430
431static int bnx2fc_l2_rcv_thread(void *arg)
432{
433 struct fcoe_percpu_s *bg = arg;
434 struct sk_buff *skb;
435
436 set_user_nice(current, -20);
437 set_current_state(TASK_INTERRUPTIBLE);
438 while (!kthread_should_stop()) {
439 schedule();
440 set_current_state(TASK_RUNNING);
441 spin_lock_bh(&bg->fcoe_rx_list.lock);
442 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
443 spin_unlock_bh(&bg->fcoe_rx_list.lock);
444 bnx2fc_recv_frame(skb);
445 spin_lock_bh(&bg->fcoe_rx_list.lock);
446 }
447 spin_unlock_bh(&bg->fcoe_rx_list.lock);
448 set_current_state(TASK_INTERRUPTIBLE);
449 }
450 set_current_state(TASK_RUNNING);
451 return 0;
452}
453
454
455static void bnx2fc_recv_frame(struct sk_buff *skb)
456{
457 u32 fr_len;
458 struct fc_lport *lport;
459 struct fcoe_rcv_info *fr;
460 struct fcoe_dev_stats *stats;
461 struct fc_frame_header *fh;
462 struct fcoe_crc_eof crc_eof;
463 struct fc_frame *fp;
464 struct fc_lport *vn_port;
465 struct fcoe_port *port;
466 u8 *mac = NULL;
467 u8 *dest_mac = NULL;
468 struct fcoe_hdr *hp;
469
470 fr = fcoe_dev_from_skb(skb);
471 lport = fr->fr_dev;
472 if (unlikely(lport == NULL)) {
473 printk(KERN_ALERT PFX "Invalid lport struct\n");
474 kfree_skb(skb);
475 return;
476 }
477
478 if (skb_is_nonlinear(skb))
479 skb_linearize(skb);
480 mac = eth_hdr(skb)->h_source;
481 dest_mac = eth_hdr(skb)->h_dest;
482
483 /* Pull the header */
484 hp = (struct fcoe_hdr *) skb_network_header(skb);
485 fh = (struct fc_frame_header *) skb_transport_header(skb);
486 skb_pull(skb, sizeof(struct fcoe_hdr));
487 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
488
489 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
490 stats->RxFrames++;
491 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
492
493 fp = (struct fc_frame *)skb;
494 fc_frame_init(fp);
495 fr_dev(fp) = lport;
496 fr_sof(fp) = hp->fcoe_sof;
497 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
498 put_cpu();
499 kfree_skb(skb);
500 return;
501 }
502 fr_eof(fp) = crc_eof.fcoe_eof;
503 fr_crc(fp) = crc_eof.fcoe_crc32;
504 if (pskb_trim(skb, fr_len)) {
505 put_cpu();
506 kfree_skb(skb);
507 return;
508 }
509
510 fh = fc_frame_header_get(fp);
511
512 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
513 if (vn_port) {
514 port = lport_priv(vn_port);
515 if (compare_ether_addr(port->data_src_addr, dest_mac)
516 != 0) {
517 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
518 put_cpu();
519 kfree_skb(skb);
520 return;
521 }
522 }
523 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
524 fh->fh_type == FC_TYPE_FCP) {
525 /* Drop FCP data. We dont this in L2 path */
526 put_cpu();
527 kfree_skb(skb);
528 return;
529 }
530 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
531 fh->fh_type == FC_TYPE_ELS) {
532 switch (fc_frame_payload_op(fp)) {
533 case ELS_LOGO:
534 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
535 /* drop non-FIP LOGO */
536 put_cpu();
537 kfree_skb(skb);
538 return;
539 }
540 break;
541 }
542 }
543 if (le32_to_cpu(fr_crc(fp)) !=
544 ~crc32(~0, skb->data, fr_len)) {
545 if (stats->InvalidCRCCount < 5)
546 printk(KERN_WARNING PFX "dropping frame with "
547 "CRC error\n");
548 stats->InvalidCRCCount++;
549 put_cpu();
550 kfree_skb(skb);
551 return;
552 }
553 put_cpu();
554 fc_exch_recv(lport, fp);
555}
556
557/**
558 * bnx2fc_percpu_io_thread - thread per cpu for ios
559 *
560 * @arg: ptr to bnx2fc_percpu_info structure
561 */
562int bnx2fc_percpu_io_thread(void *arg)
563{
564 struct bnx2fc_percpu_s *p = arg;
565 struct bnx2fc_work *work, *tmp;
566 LIST_HEAD(work_list);
567
568 set_user_nice(current, -20);
569 set_current_state(TASK_INTERRUPTIBLE);
570 while (!kthread_should_stop()) {
571 schedule();
572 set_current_state(TASK_RUNNING);
573 spin_lock_bh(&p->fp_work_lock);
574 while (!list_empty(&p->work_list)) {
575 list_splice_init(&p->work_list, &work_list);
576 spin_unlock_bh(&p->fp_work_lock);
577
578 list_for_each_entry_safe(work, tmp, &work_list, list) {
579 list_del_init(&work->list);
580 bnx2fc_process_cq_compl(work->tgt, work->wqe);
581 kfree(work);
582 }
583
584 spin_lock_bh(&p->fp_work_lock);
585 }
586 spin_unlock_bh(&p->fp_work_lock);
587 set_current_state(TASK_INTERRUPTIBLE);
588 }
589 set_current_state(TASK_RUNNING);
590
591 return 0;
592}
593
594static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
595{
596 struct fc_host_statistics *bnx2fc_stats;
597 struct fc_lport *lport = shost_priv(shost);
598 struct fcoe_port *port = lport_priv(lport);
599 struct bnx2fc_hba *hba = port->priv;
600 struct fcoe_statistics_params *fw_stats;
601 int rc = 0;
602
603 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
604 if (!fw_stats)
605 return NULL;
606
607 bnx2fc_stats = fc_get_host_stats(shost);
608
609 init_completion(&hba->stat_req_done);
610 if (bnx2fc_send_stat_req(hba))
611 return bnx2fc_stats;
612 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
613 if (!rc) {
614 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
615 return bnx2fc_stats;
616 }
617 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat1.fc_crc_cnt;
618 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
619 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
620 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
621 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
622
623 bnx2fc_stats->dumped_frames = 0;
624 bnx2fc_stats->lip_count = 0;
625 bnx2fc_stats->nos_count = 0;
626 bnx2fc_stats->loss_of_sync_count = 0;
627 bnx2fc_stats->loss_of_signal_count = 0;
628 bnx2fc_stats->prim_seq_protocol_err_count = 0;
629
630 return bnx2fc_stats;
631}
632
633static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
634{
635 struct fcoe_port *port = lport_priv(lport);
636 struct bnx2fc_hba *hba = port->priv;
637 struct Scsi_Host *shost = lport->host;
638 int rc = 0;
639
640 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
641 shost->max_lun = BNX2FC_MAX_LUN;
642 shost->max_id = BNX2FC_MAX_FCP_TGT;
643 shost->max_channel = 0;
644 if (lport->vport)
645 shost->transportt = bnx2fc_vport_xport_template;
646 else
647 shost->transportt = bnx2fc_transport_template;
648
649 /* Add the new host to SCSI-ml */
650 rc = scsi_add_host(lport->host, dev);
651 if (rc) {
652 printk(KERN_ERR PFX "Error on scsi_add_host\n");
653 return rc;
654 }
655 if (!lport->vport)
656 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
657 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
658 BNX2FC_NAME, BNX2FC_VERSION,
659 hba->netdev->name);
660
661 return 0;
662}
663
853e2bd2
BG
664static void bnx2fc_link_speed_update(struct fc_lport *lport)
665{
666 struct fcoe_port *port = lport_priv(lport);
667 struct bnx2fc_hba *hba = port->priv;
668 struct net_device *netdev = hba->netdev;
669 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
670
671 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
672 lport->link_supported_speeds &=
673 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
674 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
675 SUPPORTED_1000baseT_Full))
676 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
677 if (ecmd.supported & SUPPORTED_10000baseT_Full)
678 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
679
680 if (ecmd.speed == SPEED_1000)
681 lport->link_speed = FC_PORTSPEED_1GBIT;
682 if (ecmd.speed == SPEED_10000)
683 lport->link_speed = FC_PORTSPEED_10GBIT;
684 }
685 return;
686}
687static int bnx2fc_link_ok(struct fc_lport *lport)
688{
689 struct fcoe_port *port = lport_priv(lport);
690 struct bnx2fc_hba *hba = port->priv;
691 struct net_device *dev = hba->phys_dev;
692 int rc = 0;
693
694 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
695 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
696 else {
697 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
698 rc = -1;
699 }
700 return rc;
701}
702
703/**
704 * bnx2fc_get_link_state - get network link state
705 *
706 * @hba: adapter instance pointer
707 *
708 * updates adapter structure flag based on netdev state
709 */
710void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
711{
712 if (test_bit(__LINK_STATE_NOCARRIER, &hba->netdev->state))
713 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
714 else
715 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
716}
717
718static int bnx2fc_net_config(struct fc_lport *lport)
719{
720 struct bnx2fc_hba *hba;
721 struct fcoe_port *port;
722 u64 wwnn, wwpn;
723
724 port = lport_priv(lport);
725 hba = port->priv;
726
727 /* require support for get_pauseparam ethtool op. */
728 if (!hba->phys_dev->ethtool_ops ||
729 !hba->phys_dev->ethtool_ops->get_pauseparam)
730 return -EOPNOTSUPP;
731
1294bfe6 732 if (fc_set_mfs(lport, BNX2FC_MFS))
853e2bd2
BG
733 return -EINVAL;
734
735 skb_queue_head_init(&port->fcoe_pending_queue);
736 port->fcoe_pending_queue_active = 0;
737 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
738
739 bnx2fc_link_speed_update(lport);
740
741 if (!lport->vport) {
742 wwnn = fcoe_wwn_from_mac(hba->ctlr.ctl_src_addr, 1, 0);
743 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
744 fc_set_wwnn(lport, wwnn);
745
746 wwpn = fcoe_wwn_from_mac(hba->ctlr.ctl_src_addr, 2, 0);
747 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
748 fc_set_wwpn(lport, wwpn);
749 }
750
751 return 0;
752}
753
754static void bnx2fc_destroy_timer(unsigned long data)
755{
756 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
757
758 BNX2FC_HBA_DBG(hba->ctlr.lp, "ERROR:bnx2fc_destroy_timer - "
759 "Destroy compl not received!!\n");
760 hba->flags |= BNX2FC_FLAG_DESTROY_CMPL;
761 wake_up_interruptible(&hba->destroy_wait);
762}
763
764/**
765 * bnx2fc_indicate_netevent - Generic netdev event handler
766 *
767 * @context: adapter structure pointer
768 * @event: event type
769 *
770 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
771 * NETDEV_CHANGE_MTU events
772 */
773static void bnx2fc_indicate_netevent(void *context, unsigned long event)
774{
775 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
776 struct fc_lport *lport = hba->ctlr.lp;
777 struct fc_lport *vport;
778 u32 link_possible = 1;
779
780 if (!test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
781 BNX2FC_MISC_DBG("driver not ready. event=%s %ld\n",
782 hba->netdev->name, event);
783 return;
784 }
785
786 /*
787 * ASSUMPTION:
788 * indicate_netevent cannot be called from cnic unless bnx2fc
789 * does register_device
790 */
791 BUG_ON(!lport);
792
793 BNX2FC_HBA_DBG(lport, "enter netevent handler - event=%s %ld\n",
794 hba->netdev->name, event);
795
796 switch (event) {
797 case NETDEV_UP:
798 BNX2FC_HBA_DBG(lport, "Port up, adapter_state = %ld\n",
799 hba->adapter_state);
800 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
801 printk(KERN_ERR "indicate_netevent: "\
802 "adapter is not UP!!\n");
853e2bd2
BG
803 break;
804
805 case NETDEV_DOWN:
806 BNX2FC_HBA_DBG(lport, "Port down\n");
807 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
808 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
809 link_possible = 0;
810 break;
811
812 case NETDEV_GOING_DOWN:
813 BNX2FC_HBA_DBG(lport, "Port going down\n");
814 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
815 link_possible = 0;
816 break;
817
818 case NETDEV_CHANGE:
819 BNX2FC_HBA_DBG(lport, "NETDEV_CHANGE\n");
820 break;
821
822 default:
823 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
824 return;
825 }
826
827 bnx2fc_link_speed_update(lport);
828
829 if (link_possible && !bnx2fc_link_ok(lport)) {
830 printk(KERN_ERR "indicate_netevent: call ctlr_link_up\n");
831 fcoe_ctlr_link_up(&hba->ctlr);
832 } else {
833 printk(KERN_ERR "indicate_netevent: call ctlr_link_down\n");
834 if (fcoe_ctlr_link_down(&hba->ctlr)) {
835 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
836 mutex_lock(&lport->lp_mutex);
837 list_for_each_entry(vport, &lport->vports, list)
838 fc_host_port_type(vport->host) =
839 FC_PORTTYPE_UNKNOWN;
840 mutex_unlock(&lport->lp_mutex);
841 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
842 per_cpu_ptr(lport->dev_stats,
843 get_cpu())->LinkFailureCount++;
844 put_cpu();
845 fcoe_clean_pending_queue(lport);
846
847 init_waitqueue_head(&hba->shutdown_wait);
848 BNX2FC_HBA_DBG(lport, "indicate_netevent "
849 "num_ofld_sess = %d\n",
850 hba->num_ofld_sess);
851 hba->wait_for_link_down = 1;
852 BNX2FC_HBA_DBG(lport, "waiting for uploads to "
853 "compl proc = %s\n",
854 current->comm);
855 wait_event_interruptible(hba->shutdown_wait,
856 (hba->num_ofld_sess == 0));
857 BNX2FC_HBA_DBG(lport, "wakeup - num_ofld_sess = %d\n",
858 hba->num_ofld_sess);
859 hba->wait_for_link_down = 0;
860
861 if (signal_pending(current))
862 flush_signals(current);
863 }
864 }
865}
866
867static int bnx2fc_libfc_config(struct fc_lport *lport)
868{
869
870 /* Set the function pointers set by bnx2fc driver */
871 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
872 sizeof(struct libfc_function_template));
873 fc_elsct_init(lport);
874 fc_exch_init(lport);
875 fc_rport_init(lport);
876 fc_disc_init(lport);
877 return 0;
878}
879
880static int bnx2fc_em_config(struct fc_lport *lport)
881{
882 struct fcoe_port *port = lport_priv(lport);
883 struct bnx2fc_hba *hba = port->priv;
884
885 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
886 FCOE_MAX_XID, NULL)) {
887 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
888 return -ENOMEM;
889 }
890
891 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
892 BNX2FC_MAX_XID);
893
894 if (!hba->cmd_mgr) {
895 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
896 fc_exch_mgr_free(lport);
897 return -ENOMEM;
898 }
899 return 0;
900}
901
902static int bnx2fc_lport_config(struct fc_lport *lport)
903{
904 lport->link_up = 0;
905 lport->qfull = 0;
906 lport->max_retry_count = 3;
907 lport->max_rport_retry_count = 3;
908 lport->e_d_tov = 2 * 1000;
909 lport->r_a_tov = 10 * 1000;
910
911 /* REVISIT: enable when supporting tape devices
912 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
913 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
914 */
915 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS);
916 lport->does_npiv = 1;
917
918 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
919 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
920
921 /* alloc stats structure */
922 if (fc_lport_init_stats(lport))
923 return -ENOMEM;
924
925 /* Finish fc_lport configuration */
926 fc_lport_config(lport);
927
928 return 0;
929}
930
931/**
932 * bnx2fc_fip_recv - handle a received FIP frame.
933 *
934 * @skb: the received skb
935 * @dev: associated &net_device
936 * @ptype: the &packet_type structure which was used to register this handler.
937 * @orig_dev: original receive &net_device, in case @ dev is a bond.
938 *
939 * Returns: 0 for success
940 */
941static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
942 struct packet_type *ptype,
943 struct net_device *orig_dev)
944{
945 struct bnx2fc_hba *hba;
946 hba = container_of(ptype, struct bnx2fc_hba, fip_packet_type);
947 fcoe_ctlr_recv(&hba->ctlr, skb);
948 return 0;
949}
950
951/**
952 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
953 *
954 * @fip: FCoE controller.
955 * @old: Unicast MAC address to delete if the MAC is non-zero.
956 * @new: Unicast MAC address to add.
957 *
958 * Remove any previously-set unicast MAC filter.
959 * Add secondary FCoE MAC address filter for our OUI.
960 */
961static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
962{
963 struct fcoe_port *port = lport_priv(lport);
964
965 memcpy(port->data_src_addr, addr, ETH_ALEN);
966}
967
968/**
969 * bnx2fc_get_src_mac - return the ethernet source address for an lport
970 *
971 * @lport: libfc port
972 */
973static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
974{
975 struct fcoe_port *port;
976
977 port = (struct fcoe_port *)lport_priv(lport);
978 return port->data_src_addr;
979}
980
981/**
982 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
983 *
984 * @fip: FCoE controller.
985 * @skb: FIP Packet.
986 */
987static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
988{
989 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
990 dev_queue_xmit(skb);
991}
992
993static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
994{
995 struct Scsi_Host *shost = vport_to_shost(vport);
996 struct fc_lport *n_port = shost_priv(shost);
997 struct fcoe_port *port = lport_priv(n_port);
998 struct bnx2fc_hba *hba = port->priv;
999 struct net_device *netdev = hba->netdev;
1000 struct fc_lport *vn_port;
1001
1002 if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
1003 printk(KERN_ERR PFX "vn ports cannot be created on"
1004 "this hba\n");
1005 return -EIO;
1006 }
1007 mutex_lock(&bnx2fc_dev_lock);
1008 vn_port = bnx2fc_if_create(hba, &vport->dev, 1);
1009 mutex_unlock(&bnx2fc_dev_lock);
1010
1011 if (IS_ERR(vn_port)) {
1012 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1013 netdev->name);
1014 return -EIO;
1015 }
1016
1017 if (disabled) {
1018 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1019 } else {
1020 vn_port->boot_time = jiffies;
1021 fc_lport_init(vn_port);
1022 fc_fabric_login(vn_port);
1023 fc_vport_setlink(vn_port);
1024 }
1025 return 0;
1026}
1027
1028static int bnx2fc_vport_destroy(struct fc_vport *vport)
1029{
1030 struct Scsi_Host *shost = vport_to_shost(vport);
1031 struct fc_lport *n_port = shost_priv(shost);
1032 struct fc_lport *vn_port = vport->dd_data;
1033 struct fcoe_port *port = lport_priv(vn_port);
1034
1035 mutex_lock(&n_port->lp_mutex);
1036 list_del(&vn_port->list);
1037 mutex_unlock(&n_port->lp_mutex);
1038 queue_work(bnx2fc_wq, &port->destroy_work);
1039 return 0;
1040}
1041
1042static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1043{
1044 struct fc_lport *lport = vport->dd_data;
1045
1046 if (disable) {
1047 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1048 fc_fabric_logoff(lport);
1049 } else {
1050 lport->boot_time = jiffies;
1051 fc_fabric_login(lport);
1052 fc_vport_setlink(lport);
1053 }
1054 return 0;
1055}
1056
1057
1058static int bnx2fc_netdev_setup(struct bnx2fc_hba *hba)
1059{
1060 struct net_device *netdev = hba->netdev;
1061 struct net_device *physdev = hba->phys_dev;
1062 struct netdev_hw_addr *ha;
1063 int sel_san_mac = 0;
1064
853e2bd2
BG
1065 /* setup Source MAC Address */
1066 rcu_read_lock();
1067 for_each_dev_addr(physdev, ha) {
1068 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1069 ha->type);
1070 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1071 ha->addr[1], ha->addr[2], ha->addr[3],
1072 ha->addr[4], ha->addr[5]);
1073
1074 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1075 (is_valid_ether_addr(ha->addr))) {
1076 memcpy(hba->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
1077 sel_san_mac = 1;
1078 BNX2FC_MISC_DBG("Found SAN MAC\n");
1079 }
1080 }
1081 rcu_read_unlock();
1082
1083 if (!sel_san_mac)
1084 return -ENODEV;
1085
1086 hba->fip_packet_type.func = bnx2fc_fip_recv;
1087 hba->fip_packet_type.type = htons(ETH_P_FIP);
1088 hba->fip_packet_type.dev = netdev;
1089 dev_add_pack(&hba->fip_packet_type);
1090
1091 hba->fcoe_packet_type.func = bnx2fc_rcv;
1092 hba->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1093 hba->fcoe_packet_type.dev = netdev;
1094 dev_add_pack(&hba->fcoe_packet_type);
1095
1096 return 0;
1097}
1098
1099static int bnx2fc_attach_transport(void)
1100{
1101 bnx2fc_transport_template =
1102 fc_attach_transport(&bnx2fc_transport_function);
1103
1104 if (bnx2fc_transport_template == NULL) {
1105 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1106 return -ENODEV;
1107 }
1108
1109 bnx2fc_vport_xport_template =
1110 fc_attach_transport(&bnx2fc_vport_xport_function);
1111 if (bnx2fc_vport_xport_template == NULL) {
1112 printk(KERN_ERR PFX
1113 "Failed to attach FC transport for vport\n");
1114 fc_release_transport(bnx2fc_transport_template);
1115 bnx2fc_transport_template = NULL;
1116 return -ENODEV;
1117 }
1118 return 0;
1119}
1120static void bnx2fc_release_transport(void)
1121{
1122 fc_release_transport(bnx2fc_transport_template);
1123 fc_release_transport(bnx2fc_vport_xport_template);
1124 bnx2fc_transport_template = NULL;
1125 bnx2fc_vport_xport_template = NULL;
1126}
1127
1128static void bnx2fc_interface_release(struct kref *kref)
1129{
1130 struct bnx2fc_hba *hba;
1131 struct net_device *netdev;
1132 struct net_device *phys_dev;
1133
1134 hba = container_of(kref, struct bnx2fc_hba, kref);
1135 BNX2FC_HBA_DBG(hba->ctlr.lp, "Interface is being released\n");
1136
1137 netdev = hba->netdev;
1138 phys_dev = hba->phys_dev;
1139
1140 /* tear-down FIP controller */
1141 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &hba->init_done))
1142 fcoe_ctlr_destroy(&hba->ctlr);
1143
1144 /* Free the command manager */
1145 if (hba->cmd_mgr) {
1146 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1147 hba->cmd_mgr = NULL;
1148 }
1149 dev_put(netdev);
1150 module_put(THIS_MODULE);
1151}
1152
1153static inline void bnx2fc_interface_get(struct bnx2fc_hba *hba)
1154{
1155 kref_get(&hba->kref);
1156}
1157
1158static inline void bnx2fc_interface_put(struct bnx2fc_hba *hba)
1159{
1160 kref_put(&hba->kref, bnx2fc_interface_release);
1161}
1162static void bnx2fc_interface_destroy(struct bnx2fc_hba *hba)
1163{
1164 bnx2fc_unbind_pcidev(hba);
1165 kfree(hba);
1166}
1167
1168/**
1169 * bnx2fc_interface_create - create a new fcoe instance
1170 *
1171 * @cnic: pointer to cnic device
1172 *
1173 * Creates a new FCoE instance on the given device which include allocating
1174 * hba structure, scsi_host and lport structures.
1175 */
1176static struct bnx2fc_hba *bnx2fc_interface_create(struct cnic_dev *cnic)
1177{
1178 struct bnx2fc_hba *hba;
1179 int rc;
1180
1181 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1182 if (!hba) {
1183 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1184 return NULL;
1185 }
1186 spin_lock_init(&hba->hba_lock);
1187 mutex_init(&hba->hba_mutex);
1188
1189 hba->cnic = cnic;
1190 rc = bnx2fc_bind_pcidev(hba);
1191 if (rc)
1192 goto bind_err;
1193 hba->phys_dev = cnic->netdev;
1194 /* will get overwritten after we do vlan discovery */
1195 hba->netdev = hba->phys_dev;
1196
1197 init_waitqueue_head(&hba->shutdown_wait);
1198 init_waitqueue_head(&hba->destroy_wait);
1199
1200 return hba;
1201bind_err:
1202 printk(KERN_ERR PFX "create_interface: bind error\n");
1203 kfree(hba);
1204 return NULL;
1205}
1206
1207static int bnx2fc_interface_setup(struct bnx2fc_hba *hba,
1208 enum fip_state fip_mode)
1209{
1210 int rc = 0;
1211 struct net_device *netdev = hba->netdev;
1212 struct fcoe_ctlr *fip = &hba->ctlr;
1213
1214 dev_hold(netdev);
1215 kref_init(&hba->kref);
1216
1217 hba->flags = 0;
1218
1219 /* Initialize FIP */
1220 memset(fip, 0, sizeof(*fip));
1221 fcoe_ctlr_init(fip, fip_mode);
1222 hba->ctlr.send = bnx2fc_fip_send;
1223 hba->ctlr.update_mac = bnx2fc_update_src_mac;
1224 hba->ctlr.get_src_addr = bnx2fc_get_src_mac;
1225 set_bit(BNX2FC_CTLR_INIT_DONE, &hba->init_done);
1226
1227 rc = bnx2fc_netdev_setup(hba);
1228 if (rc)
1229 goto setup_err;
1230
1231 hba->next_conn_id = 0;
1232
1233 memset(hba->tgt_ofld_list, 0, sizeof(hba->tgt_ofld_list));
1234 hba->num_ofld_sess = 0;
1235
1236 return 0;
1237
1238setup_err:
1239 fcoe_ctlr_destroy(&hba->ctlr);
1240 dev_put(netdev);
1241 bnx2fc_interface_put(hba);
1242 return rc;
1243}
1244
1245/**
1246 * bnx2fc_if_create - Create FCoE instance on a given interface
1247 *
1248 * @hba: FCoE interface to create a local port on
1249 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1250 * @npiv: Indicates if the port is vport or not
1251 *
1252 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1253 *
1254 * Returns: Allocated fc_lport or an error pointer
1255 */
1256static struct fc_lport *bnx2fc_if_create(struct bnx2fc_hba *hba,
1257 struct device *parent, int npiv)
1258{
1259 struct fc_lport *lport = NULL;
1260 struct fcoe_port *port;
1261 struct Scsi_Host *shost;
1262 struct fc_vport *vport = dev_to_vport(parent);
1263 int rc = 0;
1264
1265 /* Allocate Scsi_Host structure */
1266 if (!npiv) {
1267 lport = libfc_host_alloc(&bnx2fc_shost_template,
1268 sizeof(struct fcoe_port));
1269 } else {
1270 lport = libfc_vport_create(vport,
1271 sizeof(struct fcoe_port));
1272 }
1273
1274 if (!lport) {
1275 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1276 return NULL;
1277 }
1278 shost = lport->host;
1279 port = lport_priv(lport);
1280 port->lport = lport;
1281 port->priv = hba;
1282 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1283
1284 /* Configure fcoe_port */
1285 rc = bnx2fc_lport_config(lport);
1286 if (rc)
1287 goto lp_config_err;
1288
1289 if (npiv) {
1290 vport = dev_to_vport(parent);
1291 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1292 vport->node_name, vport->port_name);
1293 fc_set_wwnn(lport, vport->node_name);
1294 fc_set_wwpn(lport, vport->port_name);
1295 }
1296 /* Configure netdev and networking properties of the lport */
1297 rc = bnx2fc_net_config(lport);
1298 if (rc) {
1299 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1300 goto lp_config_err;
1301 }
1302
1303 rc = bnx2fc_shost_config(lport, parent);
1304 if (rc) {
1305 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1306 hba->netdev->name);
1307 goto lp_config_err;
1308 }
1309
1310 /* Initialize the libfc library */
1311 rc = bnx2fc_libfc_config(lport);
1312 if (rc) {
1313 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1314 goto shost_err;
1315 }
1316 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1317
1318 /* Allocate exchange manager */
1319 if (!npiv) {
1320 rc = bnx2fc_em_config(lport);
1321 if (rc) {
1322 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1323 goto shost_err;
1324 }
1325 }
1326
1327 bnx2fc_interface_get(hba);
1328 return lport;
1329
1330shost_err:
1331 scsi_remove_host(shost);
1332lp_config_err:
1333 scsi_host_put(lport->host);
1334 return NULL;
1335}
1336
1337static void bnx2fc_netdev_cleanup(struct bnx2fc_hba *hba)
1338{
1339 /* Dont listen for Ethernet packets anymore */
1340 __dev_remove_pack(&hba->fcoe_packet_type);
1341 __dev_remove_pack(&hba->fip_packet_type);
1342 synchronize_net();
1343}
1344
1345static void bnx2fc_if_destroy(struct fc_lport *lport)
1346{
1347 struct fcoe_port *port = lport_priv(lport);
1348 struct bnx2fc_hba *hba = port->priv;
1349
1350 BNX2FC_HBA_DBG(hba->ctlr.lp, "ENTERED bnx2fc_if_destroy\n");
1351 /* Stop the transmit retry timer */
1352 del_timer_sync(&port->timer);
1353
1354 /* Free existing transmit skbs */
1355 fcoe_clean_pending_queue(lport);
1356
1357 bnx2fc_interface_put(hba);
1358
1359 /* Free queued packets for the receive thread */
1360 bnx2fc_clean_rx_queue(lport);
1361
1362 /* Detach from scsi-ml */
1363 fc_remove_host(lport->host);
1364 scsi_remove_host(lport->host);
1365
1366 /*
1367 * Note that only the physical lport will have the exchange manager.
1368 * for vports, this function is NOP
1369 */
1370 fc_exch_mgr_free(lport);
1371
1372 /* Free memory used by statistical counters */
1373 fc_lport_free_stats(lport);
1374
1375 /* Release Scsi_Host */
1376 scsi_host_put(lport->host);
1377}
1378
1379/**
1380 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1381 *
1382 * @buffer: The name of the Ethernet interface to be destroyed
1383 * @kp: The associated kernel parameter
1384 *
1385 * Called from sysfs.
1386 *
1387 * Returns: 0 for success
1388 */
1389static int bnx2fc_destroy(struct net_device *netdev)
1390{
1391 struct bnx2fc_hba *hba = NULL;
1392 struct net_device *phys_dev;
1393 int rc = 0;
1394
6702ca1d 1395 rtnl_lock();
853e2bd2
BG
1396
1397 mutex_lock(&bnx2fc_dev_lock);
853e2bd2
BG
1398 /* obtain physical netdev */
1399 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1400 phys_dev = vlan_dev_real_dev(netdev);
1401 else {
1402 printk(KERN_ERR PFX "Not a vlan device\n");
1403 rc = -ENODEV;
1404 goto netdev_err;
1405 }
1406
1407 hba = bnx2fc_hba_lookup(phys_dev);
1408 if (!hba || !hba->ctlr.lp) {
1409 rc = -ENODEV;
1410 printk(KERN_ERR PFX "bnx2fc_destroy: hba or lport not found\n");
1411 goto netdev_err;
1412 }
1413
1414 if (!test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
1415 printk(KERN_ERR PFX "bnx2fc_destroy: Create not called\n");
1416 goto netdev_err;
1417 }
1418
1419 bnx2fc_netdev_cleanup(hba);
1420
1421 bnx2fc_stop(hba);
1422
1423 bnx2fc_if_destroy(hba->ctlr.lp);
1424
1425 destroy_workqueue(hba->timer_work_queue);
1426
1427 if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done))
1428 bnx2fc_fw_destroy(hba);
1429
1430 clear_bit(BNX2FC_CREATE_DONE, &hba->init_done);
1431netdev_err:
1432 mutex_unlock(&bnx2fc_dev_lock);
1433 rtnl_unlock();
1434 return rc;
1435}
1436
1437static void bnx2fc_destroy_work(struct work_struct *work)
1438{
1439 struct fcoe_port *port;
1440 struct fc_lport *lport;
1441
1442 port = container_of(work, struct fcoe_port, destroy_work);
1443 lport = port->lport;
1444
1445 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1446
1447 bnx2fc_port_shutdown(lport);
1448 rtnl_lock();
1449 mutex_lock(&bnx2fc_dev_lock);
1450 bnx2fc_if_destroy(lport);
1451 mutex_unlock(&bnx2fc_dev_lock);
1452 rtnl_unlock();
1453}
1454
1455static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1456{
1457 bnx2fc_free_fw_resc(hba);
1458 bnx2fc_free_task_ctx(hba);
1459}
1460
1461/**
1462 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1463 * pci structure
1464 *
1465 * @hba: Adapter instance
1466 */
1467static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1468{
1469 if (bnx2fc_setup_task_ctx(hba))
1470 goto mem_err;
1471
1472 if (bnx2fc_setup_fw_resc(hba))
1473 goto mem_err;
1474
1475 return 0;
1476mem_err:
1477 bnx2fc_unbind_adapter_devices(hba);
1478 return -ENOMEM;
1479}
1480
1481static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1482{
1483 struct cnic_dev *cnic;
1484
1485 if (!hba->cnic) {
1486 printk(KERN_ERR PFX "cnic is NULL\n");
1487 return -ENODEV;
1488 }
1489 cnic = hba->cnic;
1490 hba->pcidev = cnic->pcidev;
1491 if (hba->pcidev)
1492 pci_dev_get(hba->pcidev);
1493
1494 return 0;
1495}
1496
1497static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1498{
1499 if (hba->pcidev)
1500 pci_dev_put(hba->pcidev);
1501 hba->pcidev = NULL;
1502}
1503
1504
1505
1506/**
1507 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1508 *
1509 * @handle: transport handle pointing to adapter struture
1510 *
1511 * This function maps adapter structure to pcidev structure and initiates
1512 * firmware handshake to enable/initialize on-chip FCoE components.
1513 * This bnx2fc - cnic interface api callback is used after following
1514 * conditions are met -
1515 * a) underlying network interface is up (marked by event NETDEV_UP
1516 * from netdev
1517 * b) bnx2fc adatper structure is registered.
1518 */
1519static void bnx2fc_ulp_start(void *handle)
1520{
1521 struct bnx2fc_hba *hba = handle;
1522 struct fc_lport *lport = hba->ctlr.lp;
1523
1524 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1525 mutex_lock(&bnx2fc_dev_lock);
1526
1527 if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done))
1528 goto start_disc;
1529
1530 if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done))
1531 bnx2fc_fw_init(hba);
1532
1533start_disc:
1534 mutex_unlock(&bnx2fc_dev_lock);
1535
1536 BNX2FC_MISC_DBG("bnx2fc started.\n");
1537
1538 /* Kick off Fabric discovery*/
1539 if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
1540 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1541 lport->tt.frame_send = bnx2fc_xmit;
1542 bnx2fc_start_disc(hba);
1543 }
1544}
1545
1546static void bnx2fc_port_shutdown(struct fc_lport *lport)
1547{
1548 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1549 fc_fabric_logoff(lport);
1550 fc_lport_destroy(lport);
1551}
1552
1553static void bnx2fc_stop(struct bnx2fc_hba *hba)
1554{
1555 struct fc_lport *lport;
1556 struct fc_lport *vport;
1557
1558 BNX2FC_MISC_DBG("ENTERED %s - init_done = %ld\n", __func__,
1559 hba->init_done);
1560 if (test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done) &&
1561 test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
1562 lport = hba->ctlr.lp;
1563 bnx2fc_port_shutdown(lport);
1564 BNX2FC_HBA_DBG(lport, "bnx2fc_stop: waiting for %d "
1565 "offloaded sessions\n",
1566 hba->num_ofld_sess);
1567 wait_event_interruptible(hba->shutdown_wait,
1568 (hba->num_ofld_sess == 0));
1569 mutex_lock(&lport->lp_mutex);
1570 list_for_each_entry(vport, &lport->vports, list)
1571 fc_host_port_type(vport->host) = FC_PORTTYPE_UNKNOWN;
1572 mutex_unlock(&lport->lp_mutex);
1573 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1574 fcoe_ctlr_link_down(&hba->ctlr);
1575 fcoe_clean_pending_queue(lport);
1576
1577 mutex_lock(&hba->hba_mutex);
1578 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1579 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
1580
1581 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1582 mutex_unlock(&hba->hba_mutex);
1583 }
1584}
1585
1586static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1587{
1588#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1589 int rc = -1;
1590 int i = HZ;
1591
1592 rc = bnx2fc_bind_adapter_devices(hba);
1593 if (rc) {
1594 printk(KERN_ALERT PFX
1595 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1596 goto err_out;
1597 }
1598
1599 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1600 if (rc) {
1601 printk(KERN_ALERT PFX
1602 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1603 goto err_unbind;
1604 }
1605
1606 /*
1607 * Wait until the adapter init message is complete, and adapter
1608 * state is UP.
1609 */
1610 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1611 msleep(BNX2FC_INIT_POLL_TIME);
1612
1613 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1614 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1615 "Ignoring...\n",
1616 hba->cnic->netdev->name);
1617 rc = -1;
1618 goto err_unbind;
1619 }
1620
1621
1622 /* Mark HBA to indicate that the FW INIT is done */
1623 set_bit(BNX2FC_FW_INIT_DONE, &hba->init_done);
1624 return 0;
1625
1626err_unbind:
1627 bnx2fc_unbind_adapter_devices(hba);
1628err_out:
1629 return rc;
1630}
1631
1632static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1633{
1634 if (test_and_clear_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
1635 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1636 init_timer(&hba->destroy_timer);
1637 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1638 jiffies;
1639 hba->destroy_timer.function = bnx2fc_destroy_timer;
1640 hba->destroy_timer.data = (unsigned long)hba;
1641 add_timer(&hba->destroy_timer);
1642 wait_event_interruptible(hba->destroy_wait,
1643 (hba->flags &
1644 BNX2FC_FLAG_DESTROY_CMPL));
1645 /* This should never happen */
1646 if (signal_pending(current))
1647 flush_signals(current);
1648
1649 del_timer_sync(&hba->destroy_timer);
1650 }
1651 bnx2fc_unbind_adapter_devices(hba);
1652 }
1653}
1654
1655/**
1656 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1657 *
1658 * @handle: transport handle pointing to adapter structure
1659 *
1660 * Driver checks if adapter is already in shutdown mode, if not start
1661 * the shutdown process.
1662 */
1663static void bnx2fc_ulp_stop(void *handle)
1664{
1665 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)handle;
1666
1667 printk(KERN_ERR "ULP_STOP\n");
1668
1669 mutex_lock(&bnx2fc_dev_lock);
1670 bnx2fc_stop(hba);
1671 bnx2fc_fw_destroy(hba);
1672 mutex_unlock(&bnx2fc_dev_lock);
1673}
1674
1675static void bnx2fc_start_disc(struct bnx2fc_hba *hba)
1676{
1677 struct fc_lport *lport;
1678 int wait_cnt = 0;
1679
1680 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1681 /* Kick off FIP/FLOGI */
1682 if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
1683 printk(KERN_ERR PFX "Init not done yet\n");
1684 return;
1685 }
1686
1687 lport = hba->ctlr.lp;
1688 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1689
1690 if (!bnx2fc_link_ok(lport)) {
1691 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1692 fcoe_ctlr_link_up(&hba->ctlr);
1693 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1694 set_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1695 }
1696
1697 /* wait for the FCF to be selected before issuing FLOGI */
1698 while (!hba->ctlr.sel_fcf) {
1699 msleep(250);
1700 /* give up after 3 secs */
1701 if (++wait_cnt > 12)
1702 break;
1703 }
1704 fc_lport_init(lport);
1705 fc_fabric_login(lport);
1706}
1707
1708
1709/**
1710 * bnx2fc_ulp_init - Initialize an adapter instance
1711 *
1712 * @dev : cnic device handle
1713 * Called from cnic_register_driver() context to initialize all
1714 * enumerated cnic devices. This routine allocates adapter structure
1715 * and other device specific resources.
1716 */
1717static void bnx2fc_ulp_init(struct cnic_dev *dev)
1718{
1719 struct bnx2fc_hba *hba;
1720 int rc = 0;
1721
1722 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1723 /* bnx2fc works only when bnx2x is loaded */
1724 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1725 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1726 " flags: %lx\n",
1727 dev->netdev->name, dev->flags);
1728 return;
1729 }
1730
1731 /* Configure FCoE interface */
1732 hba = bnx2fc_interface_create(dev);
1733 if (!hba) {
1734 printk(KERN_ERR PFX "hba initialization failed\n");
1735 return;
1736 }
1737
1738 /* Add HBA to the adapter list */
1739 mutex_lock(&bnx2fc_dev_lock);
1740 list_add_tail(&hba->link, &adapter_list);
1741 adapter_count++;
1742 mutex_unlock(&bnx2fc_dev_lock);
1743
1744 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1745 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1746 (void *) hba);
1747 if (rc)
1748 printk(KERN_ALERT PFX "register_device failed, rc = %d\n", rc);
1749 else
1750 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1751}
1752
1753
1754static int bnx2fc_disable(struct net_device *netdev)
1755{
1756 struct bnx2fc_hba *hba;
1757 struct net_device *phys_dev;
1758 struct ethtool_drvinfo drvinfo;
1759 int rc = 0;
1760
6702ca1d 1761 rtnl_lock();
853e2bd2
BG
1762
1763 mutex_lock(&bnx2fc_dev_lock);
1764
853e2bd2
BG
1765 /* obtain physical netdev */
1766 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1767 phys_dev = vlan_dev_real_dev(netdev);
1768 else {
1769 printk(KERN_ERR PFX "Not a vlan device\n");
1770 rc = -ENODEV;
1771 goto nodev;
1772 }
1773
1774 /* verify if the physical device is a netxtreme2 device */
1775 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1776 memset(&drvinfo, 0, sizeof(drvinfo));
1777 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1778 if (strcmp(drvinfo.driver, "bnx2x")) {
1779 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1780 rc = -ENODEV;
1781 goto nodev;
1782 }
1783 } else {
1784 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1785 rc = -ENODEV;
1786 goto nodev;
1787 }
1788
1789 printk(KERN_ERR PFX "phys_dev is netxtreme2 device\n");
1790
1791 /* obtain hba and initialize rest of the structure */
1792 hba = bnx2fc_hba_lookup(phys_dev);
1793 if (!hba || !hba->ctlr.lp) {
1794 rc = -ENODEV;
1795 printk(KERN_ERR PFX "bnx2fc_disable: hba or lport not found\n");
1796 } else {
1797 fcoe_ctlr_link_down(&hba->ctlr);
1798 fcoe_clean_pending_queue(hba->ctlr.lp);
1799 }
1800
1801nodev:
1802 mutex_unlock(&bnx2fc_dev_lock);
1803 rtnl_unlock();
1804 return rc;
1805}
1806
1807
1808static int bnx2fc_enable(struct net_device *netdev)
1809{
1810 struct bnx2fc_hba *hba;
1811 struct net_device *phys_dev;
1812 struct ethtool_drvinfo drvinfo;
1813 int rc = 0;
1814
6702ca1d 1815 rtnl_lock();
853e2bd2
BG
1816
1817 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1818 mutex_lock(&bnx2fc_dev_lock);
1819
853e2bd2
BG
1820 /* obtain physical netdev */
1821 if (netdev->priv_flags & IFF_802_1Q_VLAN)
1822 phys_dev = vlan_dev_real_dev(netdev);
1823 else {
1824 printk(KERN_ERR PFX "Not a vlan device\n");
1825 rc = -ENODEV;
1826 goto nodev;
1827 }
1828 /* verify if the physical device is a netxtreme2 device */
1829 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1830 memset(&drvinfo, 0, sizeof(drvinfo));
1831 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1832 if (strcmp(drvinfo.driver, "bnx2x")) {
1833 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1834 rc = -ENODEV;
1835 goto nodev;
1836 }
1837 } else {
1838 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1839 rc = -ENODEV;
1840 goto nodev;
1841 }
1842
1843 /* obtain hba and initialize rest of the structure */
1844 hba = bnx2fc_hba_lookup(phys_dev);
1845 if (!hba || !hba->ctlr.lp) {
1846 rc = -ENODEV;
1847 printk(KERN_ERR PFX "bnx2fc_enable: hba or lport not found\n");
1848 } else if (!bnx2fc_link_ok(hba->ctlr.lp))
1849 fcoe_ctlr_link_up(&hba->ctlr);
1850
1851nodev:
1852 mutex_unlock(&bnx2fc_dev_lock);
1853 rtnl_unlock();
1854 return rc;
1855}
1856
1857/**
1858 * bnx2fc_create - Create bnx2fc FCoE interface
1859 *
1860 * @buffer: The name of Ethernet interface to create on
1861 * @kp: The associated kernel param
1862 *
1863 * Called from sysfs.
1864 *
1865 * Returns: 0 for success
1866 */
1867static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1868{
1869 struct bnx2fc_hba *hba;
1870 struct net_device *phys_dev;
1871 struct fc_lport *lport;
1872 struct ethtool_drvinfo drvinfo;
1873 int rc = 0;
1874 int vlan_id;
1875
1876 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1877 if (fip_mode != FIP_MODE_FABRIC) {
1878 printk(KERN_ERR "fip mode not FABRIC\n");
1879 return -EIO;
1880 }
1881
6702ca1d
NS
1882 rtnl_lock();
1883
853e2bd2
BG
1884 mutex_lock(&bnx2fc_dev_lock);
1885
853e2bd2
BG
1886 if (!try_module_get(THIS_MODULE)) {
1887 rc = -EINVAL;
1888 goto mod_err;
1889 }
1890
1891 /* obtain physical netdev */
1892 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1893 phys_dev = vlan_dev_real_dev(netdev);
1894 vlan_id = vlan_dev_vlan_id(netdev);
1895 } else {
1896 printk(KERN_ERR PFX "Not a vlan device\n");
1897 rc = -EINVAL;
1898 goto netdev_err;
1899 }
1900 /* verify if the physical device is a netxtreme2 device */
1901 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1902 memset(&drvinfo, 0, sizeof(drvinfo));
1903 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1904 if (strcmp(drvinfo.driver, "bnx2x")) {
1905 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1906 rc = -EINVAL;
1907 goto netdev_err;
1908 }
1909 } else {
1910 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1911 rc = -EINVAL;
1912 goto netdev_err;
1913 }
1914
1915 /* obtain hba and initialize rest of the structure */
1916 hba = bnx2fc_hba_lookup(phys_dev);
1917 if (!hba) {
1918 rc = -ENODEV;
1919 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1920 goto netdev_err;
1921 }
1922
1923 if (!test_bit(BNX2FC_FW_INIT_DONE, &hba->init_done)) {
1924 rc = bnx2fc_fw_init(hba);
1925 if (rc)
1926 goto netdev_err;
1927 }
1928
1929 if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
1930 rc = -EEXIST;
1931 goto netdev_err;
1932 }
1933
1934 /* update netdev with vlan netdev */
1935 hba->netdev = netdev;
1936 hba->vlan_id = vlan_id;
1937 hba->vlan_enabled = 1;
1938
1939 rc = bnx2fc_interface_setup(hba, fip_mode);
1940 if (rc) {
1941 printk(KERN_ERR PFX "bnx2fc_interface_setup failed\n");
1942 goto ifput_err;
1943 }
1944
1945 hba->timer_work_queue =
1946 create_singlethread_workqueue("bnx2fc_timer_wq");
1947 if (!hba->timer_work_queue) {
1948 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
1949 rc = -EINVAL;
1950 goto ifput_err;
1951 }
1952
1953 lport = bnx2fc_if_create(hba, &hba->pcidev->dev, 0);
1954 if (!lport) {
1955 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
1956 netdev->name);
1957 bnx2fc_netdev_cleanup(hba);
1958 rc = -EINVAL;
1959 goto if_create_err;
1960 }
1961
1962 lport->boot_time = jiffies;
1963
1964 /* Make this master N_port */
1965 hba->ctlr.lp = lport;
1966
1967 set_bit(BNX2FC_CREATE_DONE, &hba->init_done);
1968 printk(KERN_ERR PFX "create: START DISC\n");
1969 bnx2fc_start_disc(hba);
1970 /*
1971 * Release from kref_init in bnx2fc_interface_setup, on success
1972 * lport should be holding a reference taken in bnx2fc_if_create
1973 */
1974 bnx2fc_interface_put(hba);
1975 /* put netdev that was held while calling dev_get_by_name */
1976 mutex_unlock(&bnx2fc_dev_lock);
1977 rtnl_unlock();
1978 return 0;
1979
1980if_create_err:
1981 destroy_workqueue(hba->timer_work_queue);
1982ifput_err:
1983 bnx2fc_interface_put(hba);
1984netdev_err:
1985 module_put(THIS_MODULE);
1986mod_err:
1987 mutex_unlock(&bnx2fc_dev_lock);
1988 rtnl_unlock();
1989 return rc;
1990}
1991
1992/**
1993 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc adapter instance
1994 *
1995 * @cnic: Pointer to cnic device instance
1996 *
1997 **/
1998static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
1999{
2000 struct list_head *list;
2001 struct list_head *temp;
2002 struct bnx2fc_hba *hba;
2003
2004 /* Called with bnx2fc_dev_lock held */
2005 list_for_each_safe(list, temp, &adapter_list) {
2006 hba = (struct bnx2fc_hba *)list;
2007 if (hba->cnic == cnic)
2008 return hba;
2009 }
2010 return NULL;
2011}
2012
2013static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev)
2014{
2015 struct list_head *list;
2016 struct list_head *temp;
2017 struct bnx2fc_hba *hba;
2018
2019 /* Called with bnx2fc_dev_lock held */
2020 list_for_each_safe(list, temp, &adapter_list) {
2021 hba = (struct bnx2fc_hba *)list;
2022 if (hba->phys_dev == phys_dev)
2023 return hba;
2024 }
2025 printk(KERN_ERR PFX "hba_lookup: hba NULL\n");
2026 return NULL;
2027}
2028
2029/**
2030 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2031 *
2032 * @dev cnic device handle
2033 */
2034static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2035{
2036 struct bnx2fc_hba *hba;
2037
2038 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2039
2040 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2041 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2042 dev->netdev->name, dev->flags);
2043 return;
2044 }
2045
2046 mutex_lock(&bnx2fc_dev_lock);
2047 hba = bnx2fc_find_hba_for_cnic(dev);
2048 if (!hba) {
2049 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2050 dev);
2051 mutex_unlock(&bnx2fc_dev_lock);
2052 return;
2053 }
2054
2055 list_del_init(&hba->link);
2056 adapter_count--;
2057
2058 if (test_bit(BNX2FC_CREATE_DONE, &hba->init_done)) {
2059 /* destroy not called yet, move to quiesced list */
2060 bnx2fc_netdev_cleanup(hba);
2061 bnx2fc_if_destroy(hba->ctlr.lp);
2062 }
2063 mutex_unlock(&bnx2fc_dev_lock);
2064
2065 bnx2fc_ulp_stop(hba);
2066 /* unregister cnic device */
2067 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2068 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2069 bnx2fc_interface_destroy(hba);
2070}
2071
2072/**
2073 * bnx2fc_fcoe_reset - Resets the fcoe
2074 *
2075 * @shost: shost the reset is from
2076 *
2077 * Returns: always 0
2078 */
2079static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2080{
2081 struct fc_lport *lport = shost_priv(shost);
2082 fc_lport_reset(lport);
2083 return 0;
2084}
2085
2086
2087static bool bnx2fc_match(struct net_device *netdev)
2088{
2089 mutex_lock(&bnx2fc_dev_lock);
2090 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2091 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2092
2093 if (bnx2fc_hba_lookup(phys_dev)) {
2094 mutex_unlock(&bnx2fc_dev_lock);
2095 return true;
2096 }
2097 }
2098 mutex_unlock(&bnx2fc_dev_lock);
2099 return false;
2100}
2101
2102
2103static struct fcoe_transport bnx2fc_transport = {
2104 .name = {"bnx2fc"},
2105 .attached = false,
2106 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2107 .match = bnx2fc_match,
2108 .create = bnx2fc_create,
2109 .destroy = bnx2fc_destroy,
2110 .enable = bnx2fc_enable,
2111 .disable = bnx2fc_disable,
2112};
2113
2114/**
2115 * bnx2fc_percpu_thread_create - Create a receive thread for an
2116 * online CPU
2117 *
2118 * @cpu: cpu index for the online cpu
2119 */
2120static void bnx2fc_percpu_thread_create(unsigned int cpu)
2121{
2122 struct bnx2fc_percpu_s *p;
2123 struct task_struct *thread;
2124
2125 p = &per_cpu(bnx2fc_percpu, cpu);
2126
2127 thread = kthread_create(bnx2fc_percpu_io_thread,
2128 (void *)p,
2129 "bnx2fc_thread/%d", cpu);
2130 /* bind thread to the cpu */
2131 if (likely(!IS_ERR(p->iothread))) {
2132 kthread_bind(thread, cpu);
2133 p->iothread = thread;
2134 wake_up_process(thread);
2135 }
2136}
2137
2138static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2139{
2140 struct bnx2fc_percpu_s *p;
2141 struct task_struct *thread;
2142 struct bnx2fc_work *work, *tmp;
2143 LIST_HEAD(work_list);
2144
2145 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2146
2147 /* Prevent any new work from being queued for this CPU */
2148 p = &per_cpu(bnx2fc_percpu, cpu);
2149 spin_lock_bh(&p->fp_work_lock);
2150 thread = p->iothread;
2151 p->iothread = NULL;
2152
2153
2154 /* Free all work in the list */
2155 list_for_each_entry_safe(work, tmp, &work_list, list) {
2156 list_del_init(&work->list);
2157 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2158 kfree(work);
2159 }
2160
2161 spin_unlock_bh(&p->fp_work_lock);
2162
2163 if (thread)
2164 kthread_stop(thread);
2165}
2166
2167/**
2168 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2169 *
2170 * @nfb: The callback data block
2171 * @action: The event triggering the callback
2172 * @hcpu: The index of the CPU that the event is for
2173 *
2174 * This creates or destroys per-CPU data for fcoe
2175 *
2176 * Returns NOTIFY_OK always.
2177 */
2178static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2179 unsigned long action, void *hcpu)
2180{
2181 unsigned cpu = (unsigned long)hcpu;
2182
2183 switch (action) {
2184 case CPU_ONLINE:
2185 case CPU_ONLINE_FROZEN:
2186 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2187 bnx2fc_percpu_thread_create(cpu);
2188 break;
2189 case CPU_DEAD:
2190 case CPU_DEAD_FROZEN:
2191 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2192 bnx2fc_percpu_thread_destroy(cpu);
2193 break;
2194 default:
2195 break;
2196 }
2197 return NOTIFY_OK;
2198}
2199
2200/**
2201 * bnx2fc_mod_init - module init entry point
2202 *
2203 * Initialize driver wide global data structures, and register
2204 * with cnic module
2205 **/
2206static int __init bnx2fc_mod_init(void)
2207{
2208 struct fcoe_percpu_s *bg;
2209 struct task_struct *l2_thread;
2210 int rc = 0;
2211 unsigned int cpu = 0;
2212 struct bnx2fc_percpu_s *p;
2213
2214 printk(KERN_INFO PFX "%s", version);
2215
2216 /* register as a fcoe transport */
2217 rc = fcoe_transport_attach(&bnx2fc_transport);
2218 if (rc) {
2219 printk(KERN_ERR "failed to register an fcoe transport, check "
2220 "if libfcoe is loaded\n");
2221 goto out;
2222 }
2223
2224 INIT_LIST_HEAD(&adapter_list);
2225 mutex_init(&bnx2fc_dev_lock);
2226 adapter_count = 0;
2227
2228 /* Attach FC transport template */
2229 rc = bnx2fc_attach_transport();
2230 if (rc)
2231 goto detach_ft;
2232
2233 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2234 if (!bnx2fc_wq) {
2235 rc = -ENOMEM;
2236 goto release_bt;
2237 }
2238
2239 bg = &bnx2fc_global;
2240 skb_queue_head_init(&bg->fcoe_rx_list);
2241 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2242 (void *)bg,
2243 "bnx2fc_l2_thread");
2244 if (IS_ERR(l2_thread)) {
2245 rc = PTR_ERR(l2_thread);
2246 goto free_wq;
2247 }
2248 wake_up_process(l2_thread);
2249 spin_lock_bh(&bg->fcoe_rx_list.lock);
2250 bg->thread = l2_thread;
2251 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2252
2253 for_each_possible_cpu(cpu) {
2254 p = &per_cpu(bnx2fc_percpu, cpu);
2255 INIT_LIST_HEAD(&p->work_list);
2256 spin_lock_init(&p->fp_work_lock);
2257 }
2258
2259 for_each_online_cpu(cpu) {
2260 bnx2fc_percpu_thread_create(cpu);
2261 }
2262
2263 /* Initialize per CPU interrupt thread */
2264 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2265
2266 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2267
2268 return 0;
2269
2270free_wq:
2271 destroy_workqueue(bnx2fc_wq);
2272release_bt:
2273 bnx2fc_release_transport();
2274detach_ft:
2275 fcoe_transport_detach(&bnx2fc_transport);
2276out:
2277 return rc;
2278}
2279
2280static void __exit bnx2fc_mod_exit(void)
2281{
2282 LIST_HEAD(to_be_deleted);
2283 struct bnx2fc_hba *hba, *next;
2284 struct fcoe_percpu_s *bg;
2285 struct task_struct *l2_thread;
2286 struct sk_buff *skb;
2287 unsigned int cpu = 0;
2288
2289 /*
2290 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2291 * it will have higher precedence than bnx2fc_dev_lock.
2292 * unregister_device() cannot be called with bnx2fc_dev_lock
2293 * held.
2294 */
2295 mutex_lock(&bnx2fc_dev_lock);
2296 list_splice(&adapter_list, &to_be_deleted);
2297 INIT_LIST_HEAD(&adapter_list);
2298 adapter_count = 0;
2299 mutex_unlock(&bnx2fc_dev_lock);
2300
2301 /* Unregister with cnic */
2302 list_for_each_entry_safe(hba, next, &to_be_deleted, link) {
2303 list_del_init(&hba->link);
2304 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p, kref = %d\n",
2305 hba, atomic_read(&hba->kref.refcount));
2306 bnx2fc_ulp_stop(hba);
2307 /* unregister cnic device */
2308 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2309 &hba->reg_with_cnic))
2310 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2311 bnx2fc_interface_destroy(hba);
2312 }
2313 cnic_unregister_driver(CNIC_ULP_FCOE);
2314
2315 /* Destroy global thread */
2316 bg = &bnx2fc_global;
2317 spin_lock_bh(&bg->fcoe_rx_list.lock);
2318 l2_thread = bg->thread;
2319 bg->thread = NULL;
2320 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2321 kfree_skb(skb);
2322
2323 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2324
2325 if (l2_thread)
2326 kthread_stop(l2_thread);
2327
2328 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2329
2330 /* Destroy per cpu threads */
2331 for_each_online_cpu(cpu) {
2332 bnx2fc_percpu_thread_destroy(cpu);
2333 }
2334
2335 destroy_workqueue(bnx2fc_wq);
2336 /*
2337 * detach from scsi transport
2338 * must happen after all destroys are done
2339 */
2340 bnx2fc_release_transport();
2341
2342 /* detach from fcoe transport */
2343 fcoe_transport_detach(&bnx2fc_transport);
2344}
2345
2346module_init(bnx2fc_mod_init);
2347module_exit(bnx2fc_mod_exit);
2348
2349static struct fc_function_template bnx2fc_transport_function = {
2350 .show_host_node_name = 1,
2351 .show_host_port_name = 1,
2352 .show_host_supported_classes = 1,
2353 .show_host_supported_fc4s = 1,
2354 .show_host_active_fc4s = 1,
2355 .show_host_maxframe_size = 1,
2356
2357 .show_host_port_id = 1,
2358 .show_host_supported_speeds = 1,
2359 .get_host_speed = fc_get_host_speed,
2360 .show_host_speed = 1,
2361 .show_host_port_type = 1,
2362 .get_host_port_state = fc_get_host_port_state,
2363 .show_host_port_state = 1,
2364 .show_host_symbolic_name = 1,
2365
2366 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2367 sizeof(struct bnx2fc_rport)),
2368 .show_rport_maxframe_size = 1,
2369 .show_rport_supported_classes = 1,
2370
2371 .show_host_fabric_name = 1,
2372 .show_starget_node_name = 1,
2373 .show_starget_port_name = 1,
2374 .show_starget_port_id = 1,
2375 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2376 .show_rport_dev_loss_tmo = 1,
2377 .get_fc_host_stats = bnx2fc_get_host_stats,
2378
2379 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2380
2381 .terminate_rport_io = fc_rport_terminate_io,
2382
2383 .vport_create = bnx2fc_vport_create,
2384 .vport_delete = bnx2fc_vport_destroy,
2385 .vport_disable = bnx2fc_vport_disable,
2386};
2387
2388static struct fc_function_template bnx2fc_vport_xport_function = {
2389 .show_host_node_name = 1,
2390 .show_host_port_name = 1,
2391 .show_host_supported_classes = 1,
2392 .show_host_supported_fc4s = 1,
2393 .show_host_active_fc4s = 1,
2394 .show_host_maxframe_size = 1,
2395
2396 .show_host_port_id = 1,
2397 .show_host_supported_speeds = 1,
2398 .get_host_speed = fc_get_host_speed,
2399 .show_host_speed = 1,
2400 .show_host_port_type = 1,
2401 .get_host_port_state = fc_get_host_port_state,
2402 .show_host_port_state = 1,
2403 .show_host_symbolic_name = 1,
2404
2405 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2406 sizeof(struct bnx2fc_rport)),
2407 .show_rport_maxframe_size = 1,
2408 .show_rport_supported_classes = 1,
2409
2410 .show_host_fabric_name = 1,
2411 .show_starget_node_name = 1,
2412 .show_starget_port_name = 1,
2413 .show_starget_port_id = 1,
2414 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2415 .show_rport_dev_loss_tmo = 1,
2416 .get_fc_host_stats = fc_get_host_stats,
2417 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2418 .terminate_rport_io = fc_rport_terminate_io,
2419};
2420
2421/**
2422 * scsi_host_template structure used while registering with SCSI-ml
2423 */
2424static struct scsi_host_template bnx2fc_shost_template = {
2425 .module = THIS_MODULE,
2426 .name = "Broadcom Offload FCoE Initiator",
2427 .queuecommand = bnx2fc_queuecommand,
2428 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2429 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2430 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2431 .eh_host_reset_handler = fc_eh_host_reset,
2432 .slave_alloc = fc_slave_alloc,
2433 .change_queue_depth = fc_change_queue_depth,
2434 .change_queue_type = fc_change_queue_type,
2435 .this_id = -1,
2436 .cmd_per_lun = 3,
0ea5c275 2437 .can_queue = BNX2FC_CAN_QUEUE,
853e2bd2
BG
2438 .use_clustering = ENABLE_CLUSTERING,
2439 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2440 .max_sectors = 512,
2441};
2442
2443static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2444 .frame_send = bnx2fc_xmit,
2445 .elsct_send = bnx2fc_elsct_send,
2446 .fcp_abort_io = bnx2fc_abort_io,
2447 .fcp_cleanup = bnx2fc_cleanup,
2448 .rport_event_callback = bnx2fc_rport_event_handler,
2449};
2450
2451/**
2452 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2453 * structure carrying callback function pointers
2454 */
2455static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2456 .owner = THIS_MODULE,
2457 .cnic_init = bnx2fc_ulp_init,
2458 .cnic_exit = bnx2fc_ulp_exit,
2459 .cnic_start = bnx2fc_ulp_start,
2460 .cnic_stop = bnx2fc_ulp_stop,
2461 .indicate_kcqes = bnx2fc_indicate_kcqe,
2462 .indicate_netevent = bnx2fc_indicate_netevent,
2463};