spi-topcliff-pch: add recovery processing in case wait-event timeout
[zen-stable.git] / drivers / scsi / bnx2fc / bnx2fc_fcoe.c
blob8c6156a10d90233a45bee5f27f343a33c7311929
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.
6 * Copyright (c) 2008 - 2011 Broadcom Corporation
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.
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
15 #include "bnx2fc.h"
17 static struct list_head adapter_list;
18 static struct list_head if_list;
19 static u32 adapter_count;
20 static DEFINE_MUTEX(bnx2fc_dev_lock);
21 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
23 #define DRV_MODULE_NAME "bnx2fc"
24 #define DRV_MODULE_VERSION BNX2FC_VERSION
25 #define DRV_MODULE_RELDATE "Oct 21, 2011"
28 static char version[] __devinitdata =
29 "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
30 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
33 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
34 MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
35 MODULE_LICENSE("GPL");
36 MODULE_VERSION(DRV_MODULE_VERSION);
38 #define BNX2FC_MAX_QUEUE_DEPTH 256
39 #define BNX2FC_MIN_QUEUE_DEPTH 32
40 #define FCOE_WORD_TO_BYTE 4
42 static struct scsi_transport_template *bnx2fc_transport_template;
43 static struct scsi_transport_template *bnx2fc_vport_xport_template;
45 struct workqueue_struct *bnx2fc_wq;
47 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
48 * Here the io threads are per cpu but the l2 thread is just one
50 struct fcoe_percpu_s bnx2fc_global;
51 DEFINE_SPINLOCK(bnx2fc_global_lock);
53 static struct cnic_ulp_ops bnx2fc_cnic_cb;
54 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
55 static struct scsi_host_template bnx2fc_shost_template;
56 static struct fc_function_template bnx2fc_transport_function;
57 static struct fc_function_template bnx2fc_vport_xport_function;
58 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
59 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
60 static int bnx2fc_destroy(struct net_device *net_device);
61 static int bnx2fc_enable(struct net_device *netdev);
62 static int bnx2fc_disable(struct net_device *netdev);
64 static void bnx2fc_recv_frame(struct sk_buff *skb);
66 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
67 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
68 static int bnx2fc_lport_config(struct fc_lport *lport);
69 static int bnx2fc_em_config(struct fc_lport *lport);
70 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
71 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
72 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
73 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
74 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
75 struct device *parent, int npiv);
76 static void bnx2fc_destroy_work(struct work_struct *work);
78 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
79 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
80 *phys_dev);
81 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
82 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
84 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
85 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
87 static void bnx2fc_port_shutdown(struct fc_lport *lport);
88 static void bnx2fc_stop(struct bnx2fc_interface *interface);
89 static int __init bnx2fc_mod_init(void);
90 static void __exit bnx2fc_mod_exit(void);
92 unsigned int bnx2fc_debug_level;
93 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
95 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
96 unsigned long action, void *hcpu);
97 /* notification function for CPU hotplug events */
98 static struct notifier_block bnx2fc_cpu_notifier = {
99 .notifier_call = bnx2fc_cpu_callback,
102 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
104 return ((struct bnx2fc_interface *)
105 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
109 * bnx2fc_get_lesb() - Fill the FCoE Link Error Status Block
110 * @lport: the local port
111 * @fc_lesb: the link error status block
113 static void bnx2fc_get_lesb(struct fc_lport *lport,
114 struct fc_els_lesb *fc_lesb)
116 struct net_device *netdev = bnx2fc_netdev(lport);
118 __fcoe_get_lesb(lport, fc_lesb, netdev);
121 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
123 struct fcoe_percpu_s *bg;
124 struct fcoe_rcv_info *fr;
125 struct sk_buff_head *list;
126 struct sk_buff *skb, *next;
127 struct sk_buff *head;
129 bg = &bnx2fc_global;
130 spin_lock_bh(&bg->fcoe_rx_list.lock);
131 list = &bg->fcoe_rx_list;
132 head = list->next;
133 for (skb = head; skb != (struct sk_buff *)list;
134 skb = next) {
135 next = skb->next;
136 fr = fcoe_dev_from_skb(skb);
137 if (fr->fr_dev == lp) {
138 __skb_unlink(skb, list);
139 kfree_skb(skb);
142 spin_unlock_bh(&bg->fcoe_rx_list.lock);
145 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
147 int rc;
148 spin_lock(&bnx2fc_global_lock);
149 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
150 spin_unlock(&bnx2fc_global_lock);
152 return rc;
155 static void bnx2fc_abort_io(struct fc_lport *lport)
158 * This function is no-op for bnx2fc, but we do
159 * not want to leave it as NULL either, as libfc
160 * can call the default function which is
161 * fc_fcp_abort_io.
165 static void bnx2fc_cleanup(struct fc_lport *lport)
167 struct fcoe_port *port = lport_priv(lport);
168 struct bnx2fc_interface *interface = port->priv;
169 struct bnx2fc_hba *hba = interface->hba;
170 struct bnx2fc_rport *tgt;
171 int i;
173 BNX2FC_MISC_DBG("Entered %s\n", __func__);
174 mutex_lock(&hba->hba_mutex);
175 spin_lock_bh(&hba->hba_lock);
176 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
177 tgt = hba->tgt_ofld_list[i];
178 if (tgt) {
179 /* Cleanup IOs belonging to requested vport */
180 if (tgt->port == port) {
181 spin_unlock_bh(&hba->hba_lock);
182 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
183 bnx2fc_flush_active_ios(tgt);
184 spin_lock_bh(&hba->hba_lock);
188 spin_unlock_bh(&hba->hba_lock);
189 mutex_unlock(&hba->hba_mutex);
192 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
193 struct fc_frame *fp)
195 struct fc_rport_priv *rdata = tgt->rdata;
196 struct fc_frame_header *fh;
197 int rc = 0;
199 fh = fc_frame_header_get(fp);
200 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
201 "r_ctl = 0x%x\n", rdata->ids.port_id,
202 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
203 if ((fh->fh_type == FC_TYPE_ELS) &&
204 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
206 switch (fc_frame_payload_op(fp)) {
207 case ELS_ADISC:
208 rc = bnx2fc_send_adisc(tgt, fp);
209 break;
210 case ELS_LOGO:
211 rc = bnx2fc_send_logo(tgt, fp);
212 break;
213 case ELS_RLS:
214 rc = bnx2fc_send_rls(tgt, fp);
215 break;
216 default:
217 break;
219 } else if ((fh->fh_type == FC_TYPE_BLS) &&
220 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
221 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
222 else {
223 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
224 "rctl 0x%x thru non-offload path\n",
225 fh->fh_type, fh->fh_r_ctl);
226 return -ENODEV;
228 if (rc)
229 return -ENOMEM;
230 else
231 return 0;
235 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
237 * @lport: the associated local port
238 * @fp: the fc_frame to be transmitted
240 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
242 struct ethhdr *eh;
243 struct fcoe_crc_eof *cp;
244 struct sk_buff *skb;
245 struct fc_frame_header *fh;
246 struct bnx2fc_interface *interface;
247 struct bnx2fc_hba *hba;
248 struct fcoe_port *port;
249 struct fcoe_hdr *hp;
250 struct bnx2fc_rport *tgt;
251 struct fcoe_dev_stats *stats;
252 u8 sof, eof;
253 u32 crc;
254 unsigned int hlen, tlen, elen;
255 int wlen, rc = 0;
257 port = (struct fcoe_port *)lport_priv(lport);
258 interface = port->priv;
259 hba = interface->hba;
261 fh = fc_frame_header_get(fp);
263 skb = fp_skb(fp);
264 if (!lport->link_up) {
265 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
266 kfree_skb(skb);
267 return 0;
270 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
271 if (!interface->ctlr.sel_fcf) {
272 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
273 kfree_skb(skb);
274 return -EINVAL;
276 if (fcoe_ctlr_els_send(&interface->ctlr, lport, skb))
277 return 0;
280 sof = fr_sof(fp);
281 eof = fr_eof(fp);
284 * Snoop the frame header to check if the frame is for
285 * an offloaded session
288 * tgt_ofld_list access is synchronized using
289 * both hba mutex and hba lock. Atleast hba mutex or
290 * hba lock needs to be held for read access.
293 spin_lock_bh(&hba->hba_lock);
294 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
295 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
296 /* This frame is for offloaded session */
297 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
298 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
299 spin_unlock_bh(&hba->hba_lock);
300 rc = bnx2fc_xmit_l2_frame(tgt, fp);
301 if (rc != -ENODEV) {
302 kfree_skb(skb);
303 return rc;
305 } else {
306 spin_unlock_bh(&hba->hba_lock);
309 elen = sizeof(struct ethhdr);
310 hlen = sizeof(struct fcoe_hdr);
311 tlen = sizeof(struct fcoe_crc_eof);
312 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
314 skb->ip_summed = CHECKSUM_NONE;
315 crc = fcoe_fc_crc(fp);
317 /* copy port crc and eof to the skb buff */
318 if (skb_is_nonlinear(skb)) {
319 skb_frag_t *frag;
320 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
321 kfree_skb(skb);
322 return -ENOMEM;
324 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
325 cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
326 + frag->page_offset;
327 } else {
328 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
331 memset(cp, 0, sizeof(*cp));
332 cp->fcoe_eof = eof;
333 cp->fcoe_crc32 = cpu_to_le32(~crc);
334 if (skb_is_nonlinear(skb)) {
335 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
336 cp = NULL;
339 /* adjust skb network/transport offsets to match mac/fcoe/port */
340 skb_push(skb, elen + hlen);
341 skb_reset_mac_header(skb);
342 skb_reset_network_header(skb);
343 skb->mac_len = elen;
344 skb->protocol = htons(ETH_P_FCOE);
345 skb->dev = interface->netdev;
347 /* fill up mac and fcoe headers */
348 eh = eth_hdr(skb);
349 eh->h_proto = htons(ETH_P_FCOE);
350 if (interface->ctlr.map_dest)
351 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
352 else
353 /* insert GW address */
354 memcpy(eh->h_dest, interface->ctlr.dest_addr, ETH_ALEN);
356 if (unlikely(interface->ctlr.flogi_oxid != FC_XID_UNKNOWN))
357 memcpy(eh->h_source, interface->ctlr.ctl_src_addr, ETH_ALEN);
358 else
359 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
361 hp = (struct fcoe_hdr *)(eh + 1);
362 memset(hp, 0, sizeof(*hp));
363 if (FC_FCOE_VER)
364 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
365 hp->fcoe_sof = sof;
367 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
368 if (lport->seq_offload && fr_max_payload(fp)) {
369 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
370 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
371 } else {
372 skb_shinfo(skb)->gso_type = 0;
373 skb_shinfo(skb)->gso_size = 0;
376 /*update tx stats */
377 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
378 stats->TxFrames++;
379 stats->TxWords += wlen;
380 put_cpu();
382 /* send down to lld */
383 fr_dev(fp) = lport;
384 if (port->fcoe_pending_queue.qlen)
385 fcoe_check_wait_queue(lport, skb);
386 else if (fcoe_start_io(skb))
387 fcoe_check_wait_queue(lport, skb);
389 return 0;
393 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
395 * @skb: the receive socket buffer
396 * @dev: associated net device
397 * @ptype: context
398 * @olddev: last device
400 * This function receives the packet and builds FC frame and passes it up
402 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
403 struct packet_type *ptype, struct net_device *olddev)
405 struct fc_lport *lport;
406 struct bnx2fc_interface *interface;
407 struct fc_frame_header *fh;
408 struct fcoe_rcv_info *fr;
409 struct fcoe_percpu_s *bg;
410 unsigned short oxid;
412 interface = container_of(ptype, struct bnx2fc_interface,
413 fcoe_packet_type);
414 lport = interface->ctlr.lp;
416 if (unlikely(lport == NULL)) {
417 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
418 goto err;
421 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
422 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
423 goto err;
427 * Check for minimum frame length, and make sure required FCoE
428 * and FC headers are pulled into the linear data area.
430 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
431 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
432 goto err;
434 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
435 fh = (struct fc_frame_header *) skb_transport_header(skb);
437 oxid = ntohs(fh->fh_ox_id);
439 fr = fcoe_dev_from_skb(skb);
440 fr->fr_dev = lport;
442 bg = &bnx2fc_global;
443 spin_lock_bh(&bg->fcoe_rx_list.lock);
445 __skb_queue_tail(&bg->fcoe_rx_list, skb);
446 if (bg->fcoe_rx_list.qlen == 1)
447 wake_up_process(bg->thread);
449 spin_unlock_bh(&bg->fcoe_rx_list.lock);
451 return 0;
452 err:
453 kfree_skb(skb);
454 return -1;
457 static int bnx2fc_l2_rcv_thread(void *arg)
459 struct fcoe_percpu_s *bg = arg;
460 struct sk_buff *skb;
462 set_user_nice(current, -20);
463 set_current_state(TASK_INTERRUPTIBLE);
464 while (!kthread_should_stop()) {
465 schedule();
466 spin_lock_bh(&bg->fcoe_rx_list.lock);
467 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
468 spin_unlock_bh(&bg->fcoe_rx_list.lock);
469 bnx2fc_recv_frame(skb);
470 spin_lock_bh(&bg->fcoe_rx_list.lock);
472 __set_current_state(TASK_INTERRUPTIBLE);
473 spin_unlock_bh(&bg->fcoe_rx_list.lock);
475 __set_current_state(TASK_RUNNING);
476 return 0;
480 static void bnx2fc_recv_frame(struct sk_buff *skb)
482 u32 fr_len;
483 struct fc_lport *lport;
484 struct fcoe_rcv_info *fr;
485 struct fcoe_dev_stats *stats;
486 struct fc_frame_header *fh;
487 struct fcoe_crc_eof crc_eof;
488 struct fc_frame *fp;
489 struct fc_lport *vn_port;
490 struct fcoe_port *port;
491 u8 *mac = NULL;
492 u8 *dest_mac = NULL;
493 struct fcoe_hdr *hp;
495 fr = fcoe_dev_from_skb(skb);
496 lport = fr->fr_dev;
497 if (unlikely(lport == NULL)) {
498 printk(KERN_ERR PFX "Invalid lport struct\n");
499 kfree_skb(skb);
500 return;
503 if (skb_is_nonlinear(skb))
504 skb_linearize(skb);
505 mac = eth_hdr(skb)->h_source;
506 dest_mac = eth_hdr(skb)->h_dest;
508 /* Pull the header */
509 hp = (struct fcoe_hdr *) skb_network_header(skb);
510 fh = (struct fc_frame_header *) skb_transport_header(skb);
511 skb_pull(skb, sizeof(struct fcoe_hdr));
512 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
514 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
515 stats->RxFrames++;
516 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
518 fp = (struct fc_frame *)skb;
519 fc_frame_init(fp);
520 fr_dev(fp) = lport;
521 fr_sof(fp) = hp->fcoe_sof;
522 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
523 put_cpu();
524 kfree_skb(skb);
525 return;
527 fr_eof(fp) = crc_eof.fcoe_eof;
528 fr_crc(fp) = crc_eof.fcoe_crc32;
529 if (pskb_trim(skb, fr_len)) {
530 put_cpu();
531 kfree_skb(skb);
532 return;
535 fh = fc_frame_header_get(fp);
537 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
538 if (vn_port) {
539 port = lport_priv(vn_port);
540 if (compare_ether_addr(port->data_src_addr, dest_mac)
541 != 0) {
542 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
543 put_cpu();
544 kfree_skb(skb);
545 return;
548 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
549 fh->fh_type == FC_TYPE_FCP) {
550 /* Drop FCP data. We dont this in L2 path */
551 put_cpu();
552 kfree_skb(skb);
553 return;
555 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
556 fh->fh_type == FC_TYPE_ELS) {
557 switch (fc_frame_payload_op(fp)) {
558 case ELS_LOGO:
559 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
560 /* drop non-FIP LOGO */
561 put_cpu();
562 kfree_skb(skb);
563 return;
565 break;
569 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
570 /* Drop incoming ABTS */
571 put_cpu();
572 kfree_skb(skb);
573 return;
576 if (le32_to_cpu(fr_crc(fp)) !=
577 ~crc32(~0, skb->data, fr_len)) {
578 if (stats->InvalidCRCCount < 5)
579 printk(KERN_WARNING PFX "dropping frame with "
580 "CRC error\n");
581 stats->InvalidCRCCount++;
582 put_cpu();
583 kfree_skb(skb);
584 return;
586 put_cpu();
587 fc_exch_recv(lport, fp);
591 * bnx2fc_percpu_io_thread - thread per cpu for ios
593 * @arg: ptr to bnx2fc_percpu_info structure
595 int bnx2fc_percpu_io_thread(void *arg)
597 struct bnx2fc_percpu_s *p = arg;
598 struct bnx2fc_work *work, *tmp;
599 LIST_HEAD(work_list);
601 set_user_nice(current, -20);
602 set_current_state(TASK_INTERRUPTIBLE);
603 while (!kthread_should_stop()) {
604 schedule();
605 spin_lock_bh(&p->fp_work_lock);
606 while (!list_empty(&p->work_list)) {
607 list_splice_init(&p->work_list, &work_list);
608 spin_unlock_bh(&p->fp_work_lock);
610 list_for_each_entry_safe(work, tmp, &work_list, list) {
611 list_del_init(&work->list);
612 bnx2fc_process_cq_compl(work->tgt, work->wqe);
613 kfree(work);
616 spin_lock_bh(&p->fp_work_lock);
618 __set_current_state(TASK_INTERRUPTIBLE);
619 spin_unlock_bh(&p->fp_work_lock);
621 __set_current_state(TASK_RUNNING);
623 return 0;
626 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
628 struct fc_host_statistics *bnx2fc_stats;
629 struct fc_lport *lport = shost_priv(shost);
630 struct fcoe_port *port = lport_priv(lport);
631 struct bnx2fc_interface *interface = port->priv;
632 struct bnx2fc_hba *hba = interface->hba;
633 struct fcoe_statistics_params *fw_stats;
634 int rc = 0;
636 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
637 if (!fw_stats)
638 return NULL;
640 bnx2fc_stats = fc_get_host_stats(shost);
642 init_completion(&hba->stat_req_done);
643 if (bnx2fc_send_stat_req(hba))
644 return bnx2fc_stats;
645 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
646 if (!rc) {
647 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
648 return bnx2fc_stats;
650 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
651 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
652 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
653 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
654 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
656 bnx2fc_stats->dumped_frames = 0;
657 bnx2fc_stats->lip_count = 0;
658 bnx2fc_stats->nos_count = 0;
659 bnx2fc_stats->loss_of_sync_count = 0;
660 bnx2fc_stats->loss_of_signal_count = 0;
661 bnx2fc_stats->prim_seq_protocol_err_count = 0;
663 return bnx2fc_stats;
666 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
668 struct fcoe_port *port = lport_priv(lport);
669 struct bnx2fc_interface *interface = port->priv;
670 struct Scsi_Host *shost = lport->host;
671 int rc = 0;
673 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
674 shost->max_lun = BNX2FC_MAX_LUN;
675 shost->max_id = BNX2FC_MAX_FCP_TGT;
676 shost->max_channel = 0;
677 if (lport->vport)
678 shost->transportt = bnx2fc_vport_xport_template;
679 else
680 shost->transportt = bnx2fc_transport_template;
682 /* Add the new host to SCSI-ml */
683 rc = scsi_add_host(lport->host, dev);
684 if (rc) {
685 printk(KERN_ERR PFX "Error on scsi_add_host\n");
686 return rc;
688 if (!lport->vport)
689 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
690 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
691 BNX2FC_NAME, BNX2FC_VERSION,
692 interface->netdev->name);
694 return 0;
697 static void bnx2fc_link_speed_update(struct fc_lport *lport)
699 struct fcoe_port *port = lport_priv(lport);
700 struct bnx2fc_interface *interface = port->priv;
701 struct net_device *netdev = interface->netdev;
702 struct ethtool_cmd ecmd;
704 if (!__ethtool_get_settings(netdev, &ecmd)) {
705 lport->link_supported_speeds &=
706 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
707 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
708 SUPPORTED_1000baseT_Full))
709 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
710 if (ecmd.supported & SUPPORTED_10000baseT_Full)
711 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
713 switch (ethtool_cmd_speed(&ecmd)) {
714 case SPEED_1000:
715 lport->link_speed = FC_PORTSPEED_1GBIT;
716 break;
717 case SPEED_2500:
718 lport->link_speed = FC_PORTSPEED_2GBIT;
719 break;
720 case SPEED_10000:
721 lport->link_speed = FC_PORTSPEED_10GBIT;
722 break;
726 static int bnx2fc_link_ok(struct fc_lport *lport)
728 struct fcoe_port *port = lport_priv(lport);
729 struct bnx2fc_interface *interface = port->priv;
730 struct bnx2fc_hba *hba = interface->hba;
731 struct net_device *dev = hba->phys_dev;
732 int rc = 0;
734 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
735 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
736 else {
737 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
738 rc = -1;
740 return rc;
744 * bnx2fc_get_link_state - get network link state
746 * @hba: adapter instance pointer
748 * updates adapter structure flag based on netdev state
750 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
752 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
753 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
754 else
755 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
758 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
760 struct bnx2fc_hba *hba;
761 struct bnx2fc_interface *interface;
762 struct fcoe_port *port;
763 u64 wwnn, wwpn;
765 port = lport_priv(lport);
766 interface = port->priv;
767 hba = interface->hba;
769 /* require support for get_pauseparam ethtool op. */
770 if (!hba->phys_dev->ethtool_ops ||
771 !hba->phys_dev->ethtool_ops->get_pauseparam)
772 return -EOPNOTSUPP;
774 if (fc_set_mfs(lport, BNX2FC_MFS))
775 return -EINVAL;
777 skb_queue_head_init(&port->fcoe_pending_queue);
778 port->fcoe_pending_queue_active = 0;
779 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
781 bnx2fc_link_speed_update(lport);
783 if (!lport->vport) {
784 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
785 wwnn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
786 1, 0);
787 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
788 fc_set_wwnn(lport, wwnn);
790 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
791 wwpn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
792 2, 0);
794 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
795 fc_set_wwpn(lport, wwpn);
798 return 0;
801 static void bnx2fc_destroy_timer(unsigned long data)
803 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
805 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
806 "Destroy compl not received!!\n");
807 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
808 wake_up_interruptible(&hba->destroy_wait);
812 * bnx2fc_indicate_netevent - Generic netdev event handler
814 * @context: adapter structure pointer
815 * @event: event type
816 * @vlan_id: vlan id - associated vlan id with this event
818 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
819 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
821 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
822 u16 vlan_id)
824 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
825 struct fc_lport *lport;
826 struct fc_lport *vport;
827 struct bnx2fc_interface *interface, *tmp;
828 int wait_for_upload = 0;
829 u32 link_possible = 1;
831 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
832 return;
834 switch (event) {
835 case NETDEV_UP:
836 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
837 printk(KERN_ERR "indicate_netevent: "\
838 "hba is not UP!!\n");
839 break;
841 case NETDEV_DOWN:
842 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
843 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
844 link_possible = 0;
845 break;
847 case NETDEV_GOING_DOWN:
848 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
849 link_possible = 0;
850 break;
852 case NETDEV_CHANGE:
853 break;
855 case NETDEV_UNREGISTER:
856 if (!vlan_id)
857 return;
858 mutex_lock(&bnx2fc_dev_lock);
859 list_for_each_entry_safe(interface, tmp, &if_list, list) {
860 if (interface->hba == hba &&
861 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
862 __bnx2fc_destroy(interface);
864 mutex_unlock(&bnx2fc_dev_lock);
865 return;
867 default:
868 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
869 return;
872 mutex_lock(&bnx2fc_dev_lock);
873 list_for_each_entry(interface, &if_list, list) {
875 if (interface->hba != hba)
876 continue;
878 lport = interface->ctlr.lp;
879 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
880 interface->netdev->name, event);
882 bnx2fc_link_speed_update(lport);
884 if (link_possible && !bnx2fc_link_ok(lport)) {
885 /* Reset max recv frame size to default */
886 fc_set_mfs(lport, BNX2FC_MFS);
888 * ctlr link up will only be handled during
889 * enable to avoid sending discovery solicitation
890 * on a stale vlan
892 if (interface->enabled)
893 fcoe_ctlr_link_up(&interface->ctlr);
894 } else if (fcoe_ctlr_link_down(&interface->ctlr)) {
895 mutex_lock(&lport->lp_mutex);
896 list_for_each_entry(vport, &lport->vports, list)
897 fc_host_port_type(vport->host) =
898 FC_PORTTYPE_UNKNOWN;
899 mutex_unlock(&lport->lp_mutex);
900 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
901 per_cpu_ptr(lport->dev_stats,
902 get_cpu())->LinkFailureCount++;
903 put_cpu();
904 fcoe_clean_pending_queue(lport);
905 wait_for_upload = 1;
908 mutex_unlock(&bnx2fc_dev_lock);
910 if (wait_for_upload) {
911 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
912 init_waitqueue_head(&hba->shutdown_wait);
913 BNX2FC_MISC_DBG("indicate_netevent "
914 "num_ofld_sess = %d\n",
915 hba->num_ofld_sess);
916 hba->wait_for_link_down = 1;
917 wait_event_interruptible(hba->shutdown_wait,
918 (hba->num_ofld_sess == 0));
919 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
920 hba->num_ofld_sess);
921 hba->wait_for_link_down = 0;
923 if (signal_pending(current))
924 flush_signals(current);
928 static int bnx2fc_libfc_config(struct fc_lport *lport)
931 /* Set the function pointers set by bnx2fc driver */
932 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
933 sizeof(struct libfc_function_template));
934 fc_elsct_init(lport);
935 fc_exch_init(lport);
936 fc_rport_init(lport);
937 fc_disc_init(lport);
938 return 0;
941 static int bnx2fc_em_config(struct fc_lport *lport)
943 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
944 FCOE_MAX_XID, NULL)) {
945 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
946 return -ENOMEM;
949 return 0;
952 static int bnx2fc_lport_config(struct fc_lport *lport)
954 lport->link_up = 0;
955 lport->qfull = 0;
956 lport->max_retry_count = 3;
957 lport->max_rport_retry_count = 3;
958 lport->e_d_tov = 2 * 1000;
959 lport->r_a_tov = 10 * 1000;
961 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
962 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
963 lport->does_npiv = 1;
965 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
966 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
968 /* alloc stats structure */
969 if (fc_lport_init_stats(lport))
970 return -ENOMEM;
972 /* Finish fc_lport configuration */
973 fc_lport_config(lport);
975 return 0;
979 * bnx2fc_fip_recv - handle a received FIP frame.
981 * @skb: the received skb
982 * @dev: associated &net_device
983 * @ptype: the &packet_type structure which was used to register this handler.
984 * @orig_dev: original receive &net_device, in case @ dev is a bond.
986 * Returns: 0 for success
988 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
989 struct packet_type *ptype,
990 struct net_device *orig_dev)
992 struct bnx2fc_interface *interface;
993 interface = container_of(ptype, struct bnx2fc_interface,
994 fip_packet_type);
995 fcoe_ctlr_recv(&interface->ctlr, skb);
996 return 0;
1000 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1002 * @fip: FCoE controller.
1003 * @old: Unicast MAC address to delete if the MAC is non-zero.
1004 * @new: Unicast MAC address to add.
1006 * Remove any previously-set unicast MAC filter.
1007 * Add secondary FCoE MAC address filter for our OUI.
1009 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1011 struct fcoe_port *port = lport_priv(lport);
1013 memcpy(port->data_src_addr, addr, ETH_ALEN);
1017 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1019 * @lport: libfc port
1021 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1023 struct fcoe_port *port;
1025 port = (struct fcoe_port *)lport_priv(lport);
1026 return port->data_src_addr;
1030 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1032 * @fip: FCoE controller.
1033 * @skb: FIP Packet.
1035 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1037 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1038 dev_queue_xmit(skb);
1041 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1043 struct Scsi_Host *shost = vport_to_shost(vport);
1044 struct fc_lport *n_port = shost_priv(shost);
1045 struct fcoe_port *port = lport_priv(n_port);
1046 struct bnx2fc_interface *interface = port->priv;
1047 struct net_device *netdev = interface->netdev;
1048 struct fc_lport *vn_port;
1049 int rc;
1050 char buf[32];
1052 rc = fcoe_validate_vport_create(vport);
1053 if (rc) {
1054 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1055 printk(KERN_ERR PFX "Failed to create vport, "
1056 "WWPN (0x%s) already exists\n",
1057 buf);
1058 return rc;
1061 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1062 printk(KERN_ERR PFX "vn ports cannot be created on"
1063 "this interface\n");
1064 return -EIO;
1066 rtnl_lock();
1067 mutex_lock(&bnx2fc_dev_lock);
1068 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1069 mutex_unlock(&bnx2fc_dev_lock);
1070 rtnl_unlock();
1072 if (IS_ERR(vn_port)) {
1073 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1074 netdev->name);
1075 return -EIO;
1078 if (disabled) {
1079 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1080 } else {
1081 vn_port->boot_time = jiffies;
1082 fc_lport_init(vn_port);
1083 fc_fabric_login(vn_port);
1084 fc_vport_setlink(vn_port);
1086 return 0;
1089 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1091 struct bnx2fc_lport *blport, *tmp;
1093 spin_lock_bh(&hba->hba_lock);
1094 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1095 if (blport->lport == lport) {
1096 list_del(&blport->list);
1097 kfree(blport);
1100 spin_unlock_bh(&hba->hba_lock);
1103 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1105 struct Scsi_Host *shost = vport_to_shost(vport);
1106 struct fc_lport *n_port = shost_priv(shost);
1107 struct fc_lport *vn_port = vport->dd_data;
1108 struct fcoe_port *port = lport_priv(vn_port);
1109 struct bnx2fc_interface *interface = port->priv;
1110 struct fc_lport *v_port;
1111 bool found = false;
1113 mutex_lock(&n_port->lp_mutex);
1114 list_for_each_entry(v_port, &n_port->vports, list)
1115 if (v_port->vport == vport) {
1116 found = true;
1117 break;
1120 if (!found) {
1121 mutex_unlock(&n_port->lp_mutex);
1122 return -ENOENT;
1124 list_del(&vn_port->list);
1125 mutex_unlock(&n_port->lp_mutex);
1126 bnx2fc_free_vport(interface->hba, port->lport);
1127 bnx2fc_port_shutdown(port->lport);
1128 bnx2fc_interface_put(interface);
1129 queue_work(bnx2fc_wq, &port->destroy_work);
1130 return 0;
1133 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1135 struct fc_lport *lport = vport->dd_data;
1137 if (disable) {
1138 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1139 fc_fabric_logoff(lport);
1140 } else {
1141 lport->boot_time = jiffies;
1142 fc_fabric_login(lport);
1143 fc_vport_setlink(lport);
1145 return 0;
1149 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1151 struct net_device *netdev = interface->netdev;
1152 struct net_device *physdev = interface->hba->phys_dev;
1153 struct netdev_hw_addr *ha;
1154 int sel_san_mac = 0;
1156 /* setup Source MAC Address */
1157 rcu_read_lock();
1158 for_each_dev_addr(physdev, ha) {
1159 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1160 ha->type);
1161 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1162 ha->addr[1], ha->addr[2], ha->addr[3],
1163 ha->addr[4], ha->addr[5]);
1165 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1166 (is_valid_ether_addr(ha->addr))) {
1167 memcpy(interface->ctlr.ctl_src_addr, ha->addr,
1168 ETH_ALEN);
1169 sel_san_mac = 1;
1170 BNX2FC_MISC_DBG("Found SAN MAC\n");
1173 rcu_read_unlock();
1175 if (!sel_san_mac)
1176 return -ENODEV;
1178 interface->fip_packet_type.func = bnx2fc_fip_recv;
1179 interface->fip_packet_type.type = htons(ETH_P_FIP);
1180 interface->fip_packet_type.dev = netdev;
1181 dev_add_pack(&interface->fip_packet_type);
1183 interface->fcoe_packet_type.func = bnx2fc_rcv;
1184 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1185 interface->fcoe_packet_type.dev = netdev;
1186 dev_add_pack(&interface->fcoe_packet_type);
1188 return 0;
1191 static int bnx2fc_attach_transport(void)
1193 bnx2fc_transport_template =
1194 fc_attach_transport(&bnx2fc_transport_function);
1196 if (bnx2fc_transport_template == NULL) {
1197 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1198 return -ENODEV;
1201 bnx2fc_vport_xport_template =
1202 fc_attach_transport(&bnx2fc_vport_xport_function);
1203 if (bnx2fc_vport_xport_template == NULL) {
1204 printk(KERN_ERR PFX
1205 "Failed to attach FC transport for vport\n");
1206 fc_release_transport(bnx2fc_transport_template);
1207 bnx2fc_transport_template = NULL;
1208 return -ENODEV;
1210 return 0;
1212 static void bnx2fc_release_transport(void)
1214 fc_release_transport(bnx2fc_transport_template);
1215 fc_release_transport(bnx2fc_vport_xport_template);
1216 bnx2fc_transport_template = NULL;
1217 bnx2fc_vport_xport_template = NULL;
1220 static void bnx2fc_interface_release(struct kref *kref)
1222 struct bnx2fc_interface *interface;
1223 struct net_device *netdev;
1225 interface = container_of(kref, struct bnx2fc_interface, kref);
1226 BNX2FC_MISC_DBG("Interface is being released\n");
1228 netdev = interface->netdev;
1230 /* tear-down FIP controller */
1231 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1232 fcoe_ctlr_destroy(&interface->ctlr);
1234 kfree(interface);
1236 dev_put(netdev);
1237 module_put(THIS_MODULE);
1240 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1242 kref_get(&interface->kref);
1245 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1247 kref_put(&interface->kref, bnx2fc_interface_release);
1249 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1251 /* Free the command manager */
1252 if (hba->cmd_mgr) {
1253 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1254 hba->cmd_mgr = NULL;
1256 kfree(hba->tgt_ofld_list);
1257 bnx2fc_unbind_pcidev(hba);
1258 kfree(hba);
1262 * bnx2fc_hba_create - create a new bnx2fc hba
1264 * @cnic: pointer to cnic device
1266 * Creates a new FCoE hba on the given device.
1269 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1271 struct bnx2fc_hba *hba;
1272 int rc;
1274 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1275 if (!hba) {
1276 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1277 return NULL;
1279 spin_lock_init(&hba->hba_lock);
1280 mutex_init(&hba->hba_mutex);
1282 hba->cnic = cnic;
1283 rc = bnx2fc_bind_pcidev(hba);
1284 if (rc) {
1285 printk(KERN_ERR PFX "create_adapter: bind error\n");
1286 goto bind_err;
1288 hba->phys_dev = cnic->netdev;
1289 hba->next_conn_id = 0;
1291 hba->tgt_ofld_list =
1292 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1293 GFP_KERNEL);
1294 if (!hba->tgt_ofld_list) {
1295 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1296 goto tgtofld_err;
1299 hba->num_ofld_sess = 0;
1301 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1302 BNX2FC_MAX_XID);
1303 if (!hba->cmd_mgr) {
1304 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1305 goto cmgr_err;
1308 init_waitqueue_head(&hba->shutdown_wait);
1309 init_waitqueue_head(&hba->destroy_wait);
1310 INIT_LIST_HEAD(&hba->vports);
1312 return hba;
1314 cmgr_err:
1315 kfree(hba->tgt_ofld_list);
1316 tgtofld_err:
1317 bnx2fc_unbind_pcidev(hba);
1318 bind_err:
1319 kfree(hba);
1320 return NULL;
1323 struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1324 struct net_device *netdev,
1325 enum fip_state fip_mode)
1327 struct bnx2fc_interface *interface;
1328 int rc = 0;
1330 interface = kzalloc(sizeof(*interface), GFP_KERNEL);
1331 if (!interface) {
1332 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1333 return NULL;
1335 dev_hold(netdev);
1336 kref_init(&interface->kref);
1337 interface->hba = hba;
1338 interface->netdev = netdev;
1340 /* Initialize FIP */
1341 fcoe_ctlr_init(&interface->ctlr, fip_mode);
1342 interface->ctlr.send = bnx2fc_fip_send;
1343 interface->ctlr.update_mac = bnx2fc_update_src_mac;
1344 interface->ctlr.get_src_addr = bnx2fc_get_src_mac;
1345 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1347 rc = bnx2fc_interface_setup(interface);
1348 if (!rc)
1349 return interface;
1351 fcoe_ctlr_destroy(&interface->ctlr);
1352 dev_put(netdev);
1353 kfree(interface);
1354 return NULL;
1358 * bnx2fc_if_create - Create FCoE instance on a given interface
1360 * @interface: FCoE interface to create a local port on
1361 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1362 * @npiv: Indicates if the port is vport or not
1364 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1366 * Returns: Allocated fc_lport or an error pointer
1368 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1369 struct device *parent, int npiv)
1371 struct fc_lport *lport, *n_port;
1372 struct fcoe_port *port;
1373 struct Scsi_Host *shost;
1374 struct fc_vport *vport = dev_to_vport(parent);
1375 struct bnx2fc_lport *blport;
1376 struct bnx2fc_hba *hba;
1377 int rc = 0;
1379 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1380 if (!blport) {
1381 BNX2FC_HBA_DBG(interface->ctlr.lp, "Unable to alloc blport\n");
1382 return NULL;
1385 /* Allocate Scsi_Host structure */
1386 if (!npiv)
1387 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1388 else
1389 lport = libfc_vport_create(vport, sizeof(*port));
1391 if (!lport) {
1392 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1393 goto free_blport;
1395 shost = lport->host;
1396 port = lport_priv(lport);
1397 port->lport = lport;
1398 port->priv = interface;
1399 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1401 /* Configure fcoe_port */
1402 rc = bnx2fc_lport_config(lport);
1403 if (rc)
1404 goto lp_config_err;
1406 if (npiv) {
1407 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1408 vport->node_name, vport->port_name);
1409 fc_set_wwnn(lport, vport->node_name);
1410 fc_set_wwpn(lport, vport->port_name);
1412 /* Configure netdev and networking properties of the lport */
1413 rc = bnx2fc_net_config(lport, interface->netdev);
1414 if (rc) {
1415 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1416 goto lp_config_err;
1419 rc = bnx2fc_shost_config(lport, parent);
1420 if (rc) {
1421 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1422 interface->netdev->name);
1423 goto lp_config_err;
1426 /* Initialize the libfc library */
1427 rc = bnx2fc_libfc_config(lport);
1428 if (rc) {
1429 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1430 goto shost_err;
1432 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1434 /* Allocate exchange manager */
1435 if (!npiv)
1436 rc = bnx2fc_em_config(lport);
1437 else {
1438 shost = vport_to_shost(vport);
1439 n_port = shost_priv(shost);
1440 rc = fc_exch_mgr_list_clone(n_port, lport);
1443 if (rc) {
1444 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1445 goto shost_err;
1448 bnx2fc_interface_get(interface);
1450 hba = interface->hba;
1451 spin_lock_bh(&hba->hba_lock);
1452 blport->lport = lport;
1453 list_add_tail(&blport->list, &hba->vports);
1454 spin_unlock_bh(&hba->hba_lock);
1456 return lport;
1458 shost_err:
1459 scsi_remove_host(shost);
1460 lp_config_err:
1461 scsi_host_put(lport->host);
1462 free_blport:
1463 kfree(blport);
1464 return NULL;
1467 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1469 /* Dont listen for Ethernet packets anymore */
1470 __dev_remove_pack(&interface->fcoe_packet_type);
1471 __dev_remove_pack(&interface->fip_packet_type);
1472 synchronize_net();
1475 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1477 struct fc_lport *lport = interface->ctlr.lp;
1478 struct fcoe_port *port = lport_priv(lport);
1479 struct bnx2fc_hba *hba = interface->hba;
1481 /* Stop the transmit retry timer */
1482 del_timer_sync(&port->timer);
1484 /* Free existing transmit skbs */
1485 fcoe_clean_pending_queue(lport);
1487 bnx2fc_net_cleanup(interface);
1489 bnx2fc_free_vport(hba, lport);
1492 static void bnx2fc_if_destroy(struct fc_lport *lport)
1495 /* Free queued packets for the receive thread */
1496 bnx2fc_clean_rx_queue(lport);
1498 /* Detach from scsi-ml */
1499 fc_remove_host(lport->host);
1500 scsi_remove_host(lport->host);
1503 * Note that only the physical lport will have the exchange manager.
1504 * for vports, this function is NOP
1506 fc_exch_mgr_free(lport);
1508 /* Free memory used by statistical counters */
1509 fc_lport_free_stats(lport);
1511 /* Release Scsi_Host */
1512 scsi_host_put(lport->host);
1515 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1517 struct fc_lport *lport = interface->ctlr.lp;
1518 struct fcoe_port *port = lport_priv(lport);
1520 bnx2fc_interface_cleanup(interface);
1521 bnx2fc_stop(interface);
1522 list_del(&interface->list);
1523 bnx2fc_interface_put(interface);
1524 queue_work(bnx2fc_wq, &port->destroy_work);
1528 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1530 * @buffer: The name of the Ethernet interface to be destroyed
1531 * @kp: The associated kernel parameter
1533 * Called from sysfs.
1535 * Returns: 0 for success
1537 static int bnx2fc_destroy(struct net_device *netdev)
1539 struct bnx2fc_interface *interface = NULL;
1540 int rc = 0;
1542 rtnl_lock();
1543 mutex_lock(&bnx2fc_dev_lock);
1545 interface = bnx2fc_interface_lookup(netdev);
1546 if (!interface || !interface->ctlr.lp) {
1547 rc = -ENODEV;
1548 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1549 goto netdev_err;
1553 destroy_workqueue(interface->timer_work_queue);
1554 __bnx2fc_destroy(interface);
1556 netdev_err:
1557 mutex_unlock(&bnx2fc_dev_lock);
1558 rtnl_unlock();
1559 return rc;
1562 static void bnx2fc_destroy_work(struct work_struct *work)
1564 struct fcoe_port *port;
1565 struct fc_lport *lport;
1567 port = container_of(work, struct fcoe_port, destroy_work);
1568 lport = port->lport;
1570 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1572 bnx2fc_if_destroy(lport);
1575 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1577 bnx2fc_free_fw_resc(hba);
1578 bnx2fc_free_task_ctx(hba);
1582 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1583 * pci structure
1585 * @hba: Adapter instance
1587 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1589 if (bnx2fc_setup_task_ctx(hba))
1590 goto mem_err;
1592 if (bnx2fc_setup_fw_resc(hba))
1593 goto mem_err;
1595 return 0;
1596 mem_err:
1597 bnx2fc_unbind_adapter_devices(hba);
1598 return -ENOMEM;
1601 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1603 struct cnic_dev *cnic;
1605 if (!hba->cnic) {
1606 printk(KERN_ERR PFX "cnic is NULL\n");
1607 return -ENODEV;
1609 cnic = hba->cnic;
1610 hba->pcidev = cnic->pcidev;
1611 if (hba->pcidev)
1612 pci_dev_get(hba->pcidev);
1614 return 0;
1617 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1619 if (hba->pcidev)
1620 pci_dev_put(hba->pcidev);
1621 hba->pcidev = NULL;
1627 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1629 * @handle: transport handle pointing to adapter struture
1631 * This function maps adapter structure to pcidev structure and initiates
1632 * firmware handshake to enable/initialize on-chip FCoE components.
1633 * This bnx2fc - cnic interface api callback is used after following
1634 * conditions are met -
1635 * a) underlying network interface is up (marked by event NETDEV_UP
1636 * from netdev
1637 * b) bnx2fc adatper structure is registered.
1639 static void bnx2fc_ulp_start(void *handle)
1641 struct bnx2fc_hba *hba = handle;
1642 struct bnx2fc_interface *interface;
1643 struct fc_lport *lport;
1645 mutex_lock(&bnx2fc_dev_lock);
1647 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1648 bnx2fc_fw_init(hba);
1650 BNX2FC_MISC_DBG("bnx2fc started.\n");
1652 list_for_each_entry(interface, &if_list, list) {
1653 if (interface->hba == hba) {
1654 lport = interface->ctlr.lp;
1655 /* Kick off Fabric discovery*/
1656 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1657 lport->tt.frame_send = bnx2fc_xmit;
1658 bnx2fc_start_disc(interface);
1662 mutex_unlock(&bnx2fc_dev_lock);
1665 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1667 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1668 fc_fabric_logoff(lport);
1669 fc_lport_destroy(lport);
1672 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1674 struct fc_lport *lport;
1675 struct fc_lport *vport;
1677 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1678 return;
1680 lport = interface->ctlr.lp;
1681 bnx2fc_port_shutdown(lport);
1683 mutex_lock(&lport->lp_mutex);
1684 list_for_each_entry(vport, &lport->vports, list)
1685 fc_host_port_type(vport->host) =
1686 FC_PORTTYPE_UNKNOWN;
1687 mutex_unlock(&lport->lp_mutex);
1688 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1689 fcoe_ctlr_link_down(&interface->ctlr);
1690 fcoe_clean_pending_queue(lport);
1693 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1695 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1696 int rc = -1;
1697 int i = HZ;
1699 rc = bnx2fc_bind_adapter_devices(hba);
1700 if (rc) {
1701 printk(KERN_ALERT PFX
1702 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1703 goto err_out;
1706 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1707 if (rc) {
1708 printk(KERN_ALERT PFX
1709 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1710 goto err_unbind;
1714 * Wait until the adapter init message is complete, and adapter
1715 * state is UP.
1717 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1718 msleep(BNX2FC_INIT_POLL_TIME);
1720 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1721 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1722 "Ignoring...\n",
1723 hba->cnic->netdev->name);
1724 rc = -1;
1725 goto err_unbind;
1729 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1730 return 0;
1732 err_unbind:
1733 bnx2fc_unbind_adapter_devices(hba);
1734 err_out:
1735 return rc;
1738 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1740 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1741 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1742 init_timer(&hba->destroy_timer);
1743 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1744 jiffies;
1745 hba->destroy_timer.function = bnx2fc_destroy_timer;
1746 hba->destroy_timer.data = (unsigned long)hba;
1747 add_timer(&hba->destroy_timer);
1748 wait_event_interruptible(hba->destroy_wait,
1749 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1750 &hba->flags));
1751 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1752 /* This should never happen */
1753 if (signal_pending(current))
1754 flush_signals(current);
1756 del_timer_sync(&hba->destroy_timer);
1758 bnx2fc_unbind_adapter_devices(hba);
1763 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1765 * @handle: transport handle pointing to adapter structure
1767 * Driver checks if adapter is already in shutdown mode, if not start
1768 * the shutdown process.
1770 static void bnx2fc_ulp_stop(void *handle)
1772 struct bnx2fc_hba *hba = handle;
1773 struct bnx2fc_interface *interface;
1775 printk(KERN_ERR "ULP_STOP\n");
1777 mutex_lock(&bnx2fc_dev_lock);
1778 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1779 goto exit;
1780 list_for_each_entry(interface, &if_list, list) {
1781 if (interface->hba == hba)
1782 bnx2fc_stop(interface);
1784 BUG_ON(hba->num_ofld_sess != 0);
1786 mutex_lock(&hba->hba_mutex);
1787 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1788 clear_bit(ADAPTER_STATE_GOING_DOWN,
1789 &hba->adapter_state);
1791 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1792 mutex_unlock(&hba->hba_mutex);
1794 bnx2fc_fw_destroy(hba);
1795 exit:
1796 mutex_unlock(&bnx2fc_dev_lock);
1799 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1801 struct fc_lport *lport;
1802 int wait_cnt = 0;
1804 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1805 /* Kick off FIP/FLOGI */
1806 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1807 printk(KERN_ERR PFX "Init not done yet\n");
1808 return;
1811 lport = interface->ctlr.lp;
1812 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1814 if (!bnx2fc_link_ok(lport) && interface->enabled) {
1815 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1816 fcoe_ctlr_link_up(&interface->ctlr);
1817 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1818 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1821 /* wait for the FCF to be selected before issuing FLOGI */
1822 while (!interface->ctlr.sel_fcf) {
1823 msleep(250);
1824 /* give up after 3 secs */
1825 if (++wait_cnt > 12)
1826 break;
1829 /* Reset max receive frame size to default */
1830 if (fc_set_mfs(lport, BNX2FC_MFS))
1831 return;
1833 fc_lport_init(lport);
1834 fc_fabric_login(lport);
1839 * bnx2fc_ulp_init - Initialize an adapter instance
1841 * @dev : cnic device handle
1842 * Called from cnic_register_driver() context to initialize all
1843 * enumerated cnic devices. This routine allocates adapter structure
1844 * and other device specific resources.
1846 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1848 struct bnx2fc_hba *hba;
1849 int rc = 0;
1851 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1852 /* bnx2fc works only when bnx2x is loaded */
1853 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1854 (dev->max_fcoe_conn == 0)) {
1855 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1856 " flags: %lx fcoe_conn: %d\n",
1857 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1858 return;
1861 hba = bnx2fc_hba_create(dev);
1862 if (!hba) {
1863 printk(KERN_ERR PFX "hba initialization failed\n");
1864 return;
1867 /* Add HBA to the adapter list */
1868 mutex_lock(&bnx2fc_dev_lock);
1869 list_add_tail(&hba->list, &adapter_list);
1870 adapter_count++;
1871 mutex_unlock(&bnx2fc_dev_lock);
1873 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1874 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1875 (void *) hba);
1876 if (rc)
1877 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
1878 else
1879 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1883 static int bnx2fc_disable(struct net_device *netdev)
1885 struct bnx2fc_interface *interface;
1886 int rc = 0;
1888 rtnl_lock();
1889 mutex_lock(&bnx2fc_dev_lock);
1891 interface = bnx2fc_interface_lookup(netdev);
1892 if (!interface || !interface->ctlr.lp) {
1893 rc = -ENODEV;
1894 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
1895 } else {
1896 interface->enabled = false;
1897 fcoe_ctlr_link_down(&interface->ctlr);
1898 fcoe_clean_pending_queue(interface->ctlr.lp);
1901 mutex_unlock(&bnx2fc_dev_lock);
1902 rtnl_unlock();
1903 return rc;
1907 static int bnx2fc_enable(struct net_device *netdev)
1909 struct bnx2fc_interface *interface;
1910 int rc = 0;
1912 rtnl_lock();
1913 mutex_lock(&bnx2fc_dev_lock);
1915 interface = bnx2fc_interface_lookup(netdev);
1916 if (!interface || !interface->ctlr.lp) {
1917 rc = -ENODEV;
1918 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
1919 } else if (!bnx2fc_link_ok(interface->ctlr.lp)) {
1920 fcoe_ctlr_link_up(&interface->ctlr);
1921 interface->enabled = true;
1924 mutex_unlock(&bnx2fc_dev_lock);
1925 rtnl_unlock();
1926 return rc;
1930 * bnx2fc_create - Create bnx2fc FCoE interface
1932 * @buffer: The name of Ethernet interface to create on
1933 * @kp: The associated kernel param
1935 * Called from sysfs.
1937 * Returns: 0 for success
1939 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1941 struct bnx2fc_interface *interface;
1942 struct bnx2fc_hba *hba;
1943 struct net_device *phys_dev;
1944 struct fc_lport *lport;
1945 struct ethtool_drvinfo drvinfo;
1946 int rc = 0;
1947 int vlan_id;
1949 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1950 if (fip_mode != FIP_MODE_FABRIC) {
1951 printk(KERN_ERR "fip mode not FABRIC\n");
1952 return -EIO;
1955 rtnl_lock();
1957 mutex_lock(&bnx2fc_dev_lock);
1959 if (!try_module_get(THIS_MODULE)) {
1960 rc = -EINVAL;
1961 goto mod_err;
1964 /* obtain physical netdev */
1965 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1966 phys_dev = vlan_dev_real_dev(netdev);
1967 vlan_id = vlan_dev_vlan_id(netdev);
1968 } else {
1969 printk(KERN_ERR PFX "Not a vlan device\n");
1970 rc = -EINVAL;
1971 goto netdev_err;
1973 /* verify if the physical device is a netxtreme2 device */
1974 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1975 memset(&drvinfo, 0, sizeof(drvinfo));
1976 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1977 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
1978 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1979 rc = -EINVAL;
1980 goto netdev_err;
1982 } else {
1983 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1984 rc = -EINVAL;
1985 goto netdev_err;
1988 /* obtain interface and initialize rest of the structure */
1989 hba = bnx2fc_hba_lookup(phys_dev);
1990 if (!hba) {
1991 rc = -ENODEV;
1992 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1993 goto netdev_err;
1996 if (bnx2fc_interface_lookup(netdev)) {
1997 rc = -EEXIST;
1998 goto netdev_err;
2001 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2002 if (!interface) {
2003 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2004 goto ifput_err;
2007 interface->vlan_id = vlan_id;
2008 interface->vlan_enabled = 1;
2010 interface->timer_work_queue =
2011 create_singlethread_workqueue("bnx2fc_timer_wq");
2012 if (!interface->timer_work_queue) {
2013 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2014 rc = -EINVAL;
2015 goto ifput_err;
2018 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
2019 if (!lport) {
2020 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2021 netdev->name);
2022 rc = -EINVAL;
2023 goto if_create_err;
2026 /* Add interface to if_list */
2027 list_add_tail(&interface->list, &if_list);
2029 lport->boot_time = jiffies;
2031 /* Make this master N_port */
2032 interface->ctlr.lp = lport;
2034 if (!bnx2fc_link_ok(lport)) {
2035 fcoe_ctlr_link_up(&interface->ctlr);
2036 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2037 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2040 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2041 bnx2fc_start_disc(interface);
2042 interface->enabled = true;
2044 * Release from kref_init in bnx2fc_interface_setup, on success
2045 * lport should be holding a reference taken in bnx2fc_if_create
2047 bnx2fc_interface_put(interface);
2048 /* put netdev that was held while calling dev_get_by_name */
2049 mutex_unlock(&bnx2fc_dev_lock);
2050 rtnl_unlock();
2051 return 0;
2053 if_create_err:
2054 destroy_workqueue(interface->timer_work_queue);
2055 ifput_err:
2056 bnx2fc_net_cleanup(interface);
2057 bnx2fc_interface_put(interface);
2058 netdev_err:
2059 module_put(THIS_MODULE);
2060 mod_err:
2061 mutex_unlock(&bnx2fc_dev_lock);
2062 rtnl_unlock();
2063 return rc;
2067 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2069 * @cnic: Pointer to cnic device instance
2072 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2074 struct list_head *list;
2075 struct list_head *temp;
2076 struct bnx2fc_hba *hba;
2078 /* Called with bnx2fc_dev_lock held */
2079 list_for_each_safe(list, temp, &adapter_list) {
2080 hba = (struct bnx2fc_hba *)list;
2081 if (hba->cnic == cnic)
2082 return hba;
2084 return NULL;
2087 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2088 *netdev)
2090 struct bnx2fc_interface *interface;
2092 /* Called with bnx2fc_dev_lock held */
2093 list_for_each_entry(interface, &if_list, list) {
2094 if (interface->netdev == netdev)
2095 return interface;
2097 return NULL;
2100 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2101 *phys_dev)
2103 struct bnx2fc_hba *hba;
2105 /* Called with bnx2fc_dev_lock held */
2106 list_for_each_entry(hba, &adapter_list, list) {
2107 if (hba->phys_dev == phys_dev)
2108 return hba;
2110 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2111 return NULL;
2115 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2117 * @dev cnic device handle
2119 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2121 struct bnx2fc_hba *hba;
2122 struct bnx2fc_interface *interface, *tmp;
2124 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2126 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2127 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2128 dev->netdev->name, dev->flags);
2129 return;
2132 mutex_lock(&bnx2fc_dev_lock);
2133 hba = bnx2fc_find_hba_for_cnic(dev);
2134 if (!hba) {
2135 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2136 dev);
2137 mutex_unlock(&bnx2fc_dev_lock);
2138 return;
2141 list_del_init(&hba->list);
2142 adapter_count--;
2144 list_for_each_entry_safe(interface, tmp, &if_list, list)
2145 /* destroy not called yet, move to quiesced list */
2146 if (interface->hba == hba)
2147 __bnx2fc_destroy(interface);
2148 mutex_unlock(&bnx2fc_dev_lock);
2150 bnx2fc_ulp_stop(hba);
2151 /* unregister cnic device */
2152 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2153 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2154 bnx2fc_hba_destroy(hba);
2158 * bnx2fc_fcoe_reset - Resets the fcoe
2160 * @shost: shost the reset is from
2162 * Returns: always 0
2164 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2166 struct fc_lport *lport = shost_priv(shost);
2167 fc_lport_reset(lport);
2168 return 0;
2172 static bool bnx2fc_match(struct net_device *netdev)
2174 mutex_lock(&bnx2fc_dev_lock);
2175 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2176 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2178 if (bnx2fc_hba_lookup(phys_dev)) {
2179 mutex_unlock(&bnx2fc_dev_lock);
2180 return true;
2183 mutex_unlock(&bnx2fc_dev_lock);
2184 return false;
2188 static struct fcoe_transport bnx2fc_transport = {
2189 .name = {"bnx2fc"},
2190 .attached = false,
2191 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2192 .match = bnx2fc_match,
2193 .create = bnx2fc_create,
2194 .destroy = bnx2fc_destroy,
2195 .enable = bnx2fc_enable,
2196 .disable = bnx2fc_disable,
2200 * bnx2fc_percpu_thread_create - Create a receive thread for an
2201 * online CPU
2203 * @cpu: cpu index for the online cpu
2205 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2207 struct bnx2fc_percpu_s *p;
2208 struct task_struct *thread;
2210 p = &per_cpu(bnx2fc_percpu, cpu);
2212 thread = kthread_create(bnx2fc_percpu_io_thread,
2213 (void *)p,
2214 "bnx2fc_thread/%d", cpu);
2215 /* bind thread to the cpu */
2216 if (likely(!IS_ERR(thread))) {
2217 kthread_bind(thread, cpu);
2218 p->iothread = thread;
2219 wake_up_process(thread);
2223 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2225 struct bnx2fc_percpu_s *p;
2226 struct task_struct *thread;
2227 struct bnx2fc_work *work, *tmp;
2229 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2231 /* Prevent any new work from being queued for this CPU */
2232 p = &per_cpu(bnx2fc_percpu, cpu);
2233 spin_lock_bh(&p->fp_work_lock);
2234 thread = p->iothread;
2235 p->iothread = NULL;
2238 /* Free all work in the list */
2239 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2240 list_del_init(&work->list);
2241 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2242 kfree(work);
2245 spin_unlock_bh(&p->fp_work_lock);
2247 if (thread)
2248 kthread_stop(thread);
2252 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2254 * @nfb: The callback data block
2255 * @action: The event triggering the callback
2256 * @hcpu: The index of the CPU that the event is for
2258 * This creates or destroys per-CPU data for fcoe
2260 * Returns NOTIFY_OK always.
2262 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2263 unsigned long action, void *hcpu)
2265 unsigned cpu = (unsigned long)hcpu;
2267 switch (action) {
2268 case CPU_ONLINE:
2269 case CPU_ONLINE_FROZEN:
2270 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2271 bnx2fc_percpu_thread_create(cpu);
2272 break;
2273 case CPU_DEAD:
2274 case CPU_DEAD_FROZEN:
2275 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2276 bnx2fc_percpu_thread_destroy(cpu);
2277 break;
2278 default:
2279 break;
2281 return NOTIFY_OK;
2285 * bnx2fc_mod_init - module init entry point
2287 * Initialize driver wide global data structures, and register
2288 * with cnic module
2290 static int __init bnx2fc_mod_init(void)
2292 struct fcoe_percpu_s *bg;
2293 struct task_struct *l2_thread;
2294 int rc = 0;
2295 unsigned int cpu = 0;
2296 struct bnx2fc_percpu_s *p;
2298 printk(KERN_INFO PFX "%s", version);
2300 /* register as a fcoe transport */
2301 rc = fcoe_transport_attach(&bnx2fc_transport);
2302 if (rc) {
2303 printk(KERN_ERR "failed to register an fcoe transport, check "
2304 "if libfcoe is loaded\n");
2305 goto out;
2308 INIT_LIST_HEAD(&adapter_list);
2309 INIT_LIST_HEAD(&if_list);
2310 mutex_init(&bnx2fc_dev_lock);
2311 adapter_count = 0;
2313 /* Attach FC transport template */
2314 rc = bnx2fc_attach_transport();
2315 if (rc)
2316 goto detach_ft;
2318 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2319 if (!bnx2fc_wq) {
2320 rc = -ENOMEM;
2321 goto release_bt;
2324 bg = &bnx2fc_global;
2325 skb_queue_head_init(&bg->fcoe_rx_list);
2326 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2327 (void *)bg,
2328 "bnx2fc_l2_thread");
2329 if (IS_ERR(l2_thread)) {
2330 rc = PTR_ERR(l2_thread);
2331 goto free_wq;
2333 wake_up_process(l2_thread);
2334 spin_lock_bh(&bg->fcoe_rx_list.lock);
2335 bg->thread = l2_thread;
2336 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2338 for_each_possible_cpu(cpu) {
2339 p = &per_cpu(bnx2fc_percpu, cpu);
2340 INIT_LIST_HEAD(&p->work_list);
2341 spin_lock_init(&p->fp_work_lock);
2344 for_each_online_cpu(cpu) {
2345 bnx2fc_percpu_thread_create(cpu);
2348 /* Initialize per CPU interrupt thread */
2349 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2351 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2353 return 0;
2355 free_wq:
2356 destroy_workqueue(bnx2fc_wq);
2357 release_bt:
2358 bnx2fc_release_transport();
2359 detach_ft:
2360 fcoe_transport_detach(&bnx2fc_transport);
2361 out:
2362 return rc;
2365 static void __exit bnx2fc_mod_exit(void)
2367 LIST_HEAD(to_be_deleted);
2368 struct bnx2fc_hba *hba, *next;
2369 struct fcoe_percpu_s *bg;
2370 struct task_struct *l2_thread;
2371 struct sk_buff *skb;
2372 unsigned int cpu = 0;
2375 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2376 * it will have higher precedence than bnx2fc_dev_lock.
2377 * unregister_device() cannot be called with bnx2fc_dev_lock
2378 * held.
2380 mutex_lock(&bnx2fc_dev_lock);
2381 list_splice(&adapter_list, &to_be_deleted);
2382 INIT_LIST_HEAD(&adapter_list);
2383 adapter_count = 0;
2384 mutex_unlock(&bnx2fc_dev_lock);
2386 /* Unregister with cnic */
2387 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2388 list_del_init(&hba->list);
2389 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2390 hba);
2391 bnx2fc_ulp_stop(hba);
2392 /* unregister cnic device */
2393 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2394 &hba->reg_with_cnic))
2395 hba->cnic->unregister_device(hba->cnic,
2396 CNIC_ULP_FCOE);
2397 bnx2fc_hba_destroy(hba);
2399 cnic_unregister_driver(CNIC_ULP_FCOE);
2401 /* Destroy global thread */
2402 bg = &bnx2fc_global;
2403 spin_lock_bh(&bg->fcoe_rx_list.lock);
2404 l2_thread = bg->thread;
2405 bg->thread = NULL;
2406 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2407 kfree_skb(skb);
2409 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2411 if (l2_thread)
2412 kthread_stop(l2_thread);
2414 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2416 /* Destroy per cpu threads */
2417 for_each_online_cpu(cpu) {
2418 bnx2fc_percpu_thread_destroy(cpu);
2421 destroy_workqueue(bnx2fc_wq);
2423 * detach from scsi transport
2424 * must happen after all destroys are done
2426 bnx2fc_release_transport();
2428 /* detach from fcoe transport */
2429 fcoe_transport_detach(&bnx2fc_transport);
2432 module_init(bnx2fc_mod_init);
2433 module_exit(bnx2fc_mod_exit);
2435 static struct fc_function_template bnx2fc_transport_function = {
2436 .show_host_node_name = 1,
2437 .show_host_port_name = 1,
2438 .show_host_supported_classes = 1,
2439 .show_host_supported_fc4s = 1,
2440 .show_host_active_fc4s = 1,
2441 .show_host_maxframe_size = 1,
2443 .show_host_port_id = 1,
2444 .show_host_supported_speeds = 1,
2445 .get_host_speed = fc_get_host_speed,
2446 .show_host_speed = 1,
2447 .show_host_port_type = 1,
2448 .get_host_port_state = fc_get_host_port_state,
2449 .show_host_port_state = 1,
2450 .show_host_symbolic_name = 1,
2452 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2453 sizeof(struct bnx2fc_rport)),
2454 .show_rport_maxframe_size = 1,
2455 .show_rport_supported_classes = 1,
2457 .show_host_fabric_name = 1,
2458 .show_starget_node_name = 1,
2459 .show_starget_port_name = 1,
2460 .show_starget_port_id = 1,
2461 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2462 .show_rport_dev_loss_tmo = 1,
2463 .get_fc_host_stats = bnx2fc_get_host_stats,
2465 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2467 .terminate_rport_io = fc_rport_terminate_io,
2469 .vport_create = bnx2fc_vport_create,
2470 .vport_delete = bnx2fc_vport_destroy,
2471 .vport_disable = bnx2fc_vport_disable,
2472 .bsg_request = fc_lport_bsg_request,
2475 static struct fc_function_template bnx2fc_vport_xport_function = {
2476 .show_host_node_name = 1,
2477 .show_host_port_name = 1,
2478 .show_host_supported_classes = 1,
2479 .show_host_supported_fc4s = 1,
2480 .show_host_active_fc4s = 1,
2481 .show_host_maxframe_size = 1,
2483 .show_host_port_id = 1,
2484 .show_host_supported_speeds = 1,
2485 .get_host_speed = fc_get_host_speed,
2486 .show_host_speed = 1,
2487 .show_host_port_type = 1,
2488 .get_host_port_state = fc_get_host_port_state,
2489 .show_host_port_state = 1,
2490 .show_host_symbolic_name = 1,
2492 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2493 sizeof(struct bnx2fc_rport)),
2494 .show_rport_maxframe_size = 1,
2495 .show_rport_supported_classes = 1,
2497 .show_host_fabric_name = 1,
2498 .show_starget_node_name = 1,
2499 .show_starget_port_name = 1,
2500 .show_starget_port_id = 1,
2501 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2502 .show_rport_dev_loss_tmo = 1,
2503 .get_fc_host_stats = fc_get_host_stats,
2504 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2505 .terminate_rport_io = fc_rport_terminate_io,
2506 .bsg_request = fc_lport_bsg_request,
2510 * scsi_host_template structure used while registering with SCSI-ml
2512 static struct scsi_host_template bnx2fc_shost_template = {
2513 .module = THIS_MODULE,
2514 .name = "Broadcom Offload FCoE Initiator",
2515 .queuecommand = bnx2fc_queuecommand,
2516 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2517 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2518 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2519 .eh_host_reset_handler = fc_eh_host_reset,
2520 .slave_alloc = fc_slave_alloc,
2521 .change_queue_depth = fc_change_queue_depth,
2522 .change_queue_type = fc_change_queue_type,
2523 .this_id = -1,
2524 .cmd_per_lun = 3,
2525 .can_queue = BNX2FC_CAN_QUEUE,
2526 .use_clustering = ENABLE_CLUSTERING,
2527 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2528 .max_sectors = 512,
2531 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2532 .frame_send = bnx2fc_xmit,
2533 .elsct_send = bnx2fc_elsct_send,
2534 .fcp_abort_io = bnx2fc_abort_io,
2535 .fcp_cleanup = bnx2fc_cleanup,
2536 .get_lesb = bnx2fc_get_lesb,
2537 .rport_event_callback = bnx2fc_rport_event_handler,
2541 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2542 * structure carrying callback function pointers
2544 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2545 .owner = THIS_MODULE,
2546 .cnic_init = bnx2fc_ulp_init,
2547 .cnic_exit = bnx2fc_ulp_exit,
2548 .cnic_start = bnx2fc_ulp_start,
2549 .cnic_stop = bnx2fc_ulp_stop,
2550 .indicate_kcqes = bnx2fc_indicate_kcqe,
2551 .indicate_netevent = bnx2fc_indicate_netevent,