Linux 4.18.10
[linux/fpc-iii.git] / drivers / scsi / bnx2fc / bnx2fc_fcoe.c
blobf000458133789935fbb2b15550d91106ec5c65ca
1 /* bnx2fc_fcoe.c: QLogic 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-2013 Broadcom Corporation
7 * Copyright (c) 2014-2016 QLogic Corporation
8 * Copyright (c) 2016-2017 Cavium Inc.
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation.
14 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
17 #include "bnx2fc.h"
19 static struct list_head adapter_list;
20 static struct list_head if_list;
21 static u32 adapter_count;
22 static DEFINE_MUTEX(bnx2fc_dev_lock);
23 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
25 #define DRV_MODULE_NAME "bnx2fc"
26 #define DRV_MODULE_VERSION BNX2FC_VERSION
27 #define DRV_MODULE_RELDATE "October 15, 2015"
30 static char version[] =
31 "QLogic FCoE Driver " DRV_MODULE_NAME \
32 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
35 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
36 MODULE_DESCRIPTION("QLogic FCoE Driver");
37 MODULE_LICENSE("GPL");
38 MODULE_VERSION(DRV_MODULE_VERSION);
40 #define BNX2FC_MAX_QUEUE_DEPTH 256
41 #define BNX2FC_MIN_QUEUE_DEPTH 32
42 #define FCOE_WORD_TO_BYTE 4
44 static struct scsi_transport_template *bnx2fc_transport_template;
45 static struct scsi_transport_template *bnx2fc_vport_xport_template;
47 struct workqueue_struct *bnx2fc_wq;
49 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
50 * Here the io threads are per cpu but the l2 thread is just one
52 struct fcoe_percpu_s bnx2fc_global;
53 DEFINE_SPINLOCK(bnx2fc_global_lock);
55 static struct cnic_ulp_ops bnx2fc_cnic_cb;
56 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
57 static struct scsi_host_template bnx2fc_shost_template;
58 static struct fc_function_template bnx2fc_transport_function;
59 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
60 static struct fc_function_template bnx2fc_vport_xport_function;
61 static int bnx2fc_create(struct net_device *netdev, enum fip_mode fip_mode);
62 static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
63 static int bnx2fc_destroy(struct net_device *net_device);
64 static int bnx2fc_enable(struct net_device *netdev);
65 static int bnx2fc_disable(struct net_device *netdev);
67 /* fcoe_syfs control interface handlers */
68 static int bnx2fc_ctlr_alloc(struct net_device *netdev);
69 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
71 static void bnx2fc_recv_frame(struct sk_buff *skb);
73 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
74 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
75 static int bnx2fc_lport_config(struct fc_lport *lport);
76 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
77 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
78 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
79 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
80 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
81 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
82 struct device *parent, int npiv);
83 static void bnx2fc_destroy_work(struct work_struct *work);
85 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
86 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
87 *phys_dev);
88 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
89 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
91 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
92 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
94 static void bnx2fc_port_shutdown(struct fc_lport *lport);
95 static void bnx2fc_stop(struct bnx2fc_interface *interface);
96 static int __init bnx2fc_mod_init(void);
97 static void __exit bnx2fc_mod_exit(void);
99 unsigned int bnx2fc_debug_level;
100 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
101 MODULE_PARM_DESC(debug_logging,
102 "Option to enable extended logging,\n"
103 "\t\tDefault is 0 - no logging.\n"
104 "\t\t0x01 - SCSI cmd error, cleanup.\n"
105 "\t\t0x02 - Session setup, cleanup, etc.\n"
106 "\t\t0x04 - lport events, link, mtu, etc.\n"
107 "\t\t0x08 - ELS logs.\n"
108 "\t\t0x10 - fcoe L2 fame related logs.\n"
109 "\t\t0xff - LOG all messages.");
111 uint bnx2fc_devloss_tmo;
112 module_param_named(devloss_tmo, bnx2fc_devloss_tmo, uint, S_IRUGO);
113 MODULE_PARM_DESC(devloss_tmo, " Change devloss_tmo for the remote ports "
114 "attached via bnx2fc.");
116 uint bnx2fc_max_luns = BNX2FC_MAX_LUN;
117 module_param_named(max_luns, bnx2fc_max_luns, uint, S_IRUGO);
118 MODULE_PARM_DESC(max_luns, " Change the default max_lun per SCSI host. Default "
119 "0xffff.");
121 uint bnx2fc_queue_depth;
122 module_param_named(queue_depth, bnx2fc_queue_depth, uint, S_IRUGO);
123 MODULE_PARM_DESC(queue_depth, " Change the default queue depth of SCSI devices "
124 "attached via bnx2fc.");
126 uint bnx2fc_log_fka;
127 module_param_named(log_fka, bnx2fc_log_fka, uint, S_IRUGO|S_IWUSR);
128 MODULE_PARM_DESC(log_fka, " Print message to kernel log when fcoe is "
129 "initiating a FIP keep alive when debug logging is enabled.");
131 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
133 return ((struct bnx2fc_interface *)
134 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
137 static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
139 struct fcoe_ctlr_device *ctlr_dev =
140 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
141 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
142 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
144 fcf_dev->vlan_id = fcoe->vlan_id;
147 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
149 struct fcoe_percpu_s *bg;
150 struct fcoe_rcv_info *fr;
151 struct sk_buff_head *list;
152 struct sk_buff *skb, *next;
153 struct sk_buff *head;
155 bg = &bnx2fc_global;
156 spin_lock_bh(&bg->fcoe_rx_list.lock);
157 list = &bg->fcoe_rx_list;
158 head = list->next;
159 for (skb = head; skb != (struct sk_buff *)list;
160 skb = next) {
161 next = skb->next;
162 fr = fcoe_dev_from_skb(skb);
163 if (fr->fr_dev == lp) {
164 __skb_unlink(skb, list);
165 kfree_skb(skb);
168 spin_unlock_bh(&bg->fcoe_rx_list.lock);
171 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
173 int rc;
174 spin_lock(&bnx2fc_global_lock);
175 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
176 spin_unlock(&bnx2fc_global_lock);
178 return rc;
181 static void bnx2fc_abort_io(struct fc_lport *lport)
184 * This function is no-op for bnx2fc, but we do
185 * not want to leave it as NULL either, as libfc
186 * can call the default function which is
187 * fc_fcp_abort_io.
191 static void bnx2fc_cleanup(struct fc_lport *lport)
193 struct fcoe_port *port = lport_priv(lport);
194 struct bnx2fc_interface *interface = port->priv;
195 struct bnx2fc_hba *hba = interface->hba;
196 struct bnx2fc_rport *tgt;
197 int i;
199 BNX2FC_MISC_DBG("Entered %s\n", __func__);
200 mutex_lock(&hba->hba_mutex);
201 spin_lock_bh(&hba->hba_lock);
202 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
203 tgt = hba->tgt_ofld_list[i];
204 if (tgt) {
205 /* Cleanup IOs belonging to requested vport */
206 if (tgt->port == port) {
207 spin_unlock_bh(&hba->hba_lock);
208 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
209 bnx2fc_flush_active_ios(tgt);
210 spin_lock_bh(&hba->hba_lock);
214 spin_unlock_bh(&hba->hba_lock);
215 mutex_unlock(&hba->hba_mutex);
218 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
219 struct fc_frame *fp)
221 struct fc_rport_priv *rdata = tgt->rdata;
222 struct fc_frame_header *fh;
223 int rc = 0;
225 fh = fc_frame_header_get(fp);
226 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
227 "r_ctl = 0x%x\n", rdata->ids.port_id,
228 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
229 if ((fh->fh_type == FC_TYPE_ELS) &&
230 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
232 switch (fc_frame_payload_op(fp)) {
233 case ELS_ADISC:
234 rc = bnx2fc_send_adisc(tgt, fp);
235 break;
236 case ELS_LOGO:
237 rc = bnx2fc_send_logo(tgt, fp);
238 break;
239 case ELS_RLS:
240 rc = bnx2fc_send_rls(tgt, fp);
241 break;
242 default:
243 break;
245 } else if ((fh->fh_type == FC_TYPE_BLS) &&
246 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
247 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
248 else {
249 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
250 "rctl 0x%x thru non-offload path\n",
251 fh->fh_type, fh->fh_r_ctl);
252 return -ENODEV;
254 if (rc)
255 return -ENOMEM;
256 else
257 return 0;
261 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
263 * @lport: the associated local port
264 * @fp: the fc_frame to be transmitted
266 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
268 struct ethhdr *eh;
269 struct fcoe_crc_eof *cp;
270 struct sk_buff *skb;
271 struct fc_frame_header *fh;
272 struct bnx2fc_interface *interface;
273 struct fcoe_ctlr *ctlr;
274 struct bnx2fc_hba *hba;
275 struct fcoe_port *port;
276 struct fcoe_hdr *hp;
277 struct bnx2fc_rport *tgt;
278 struct fc_stats *stats;
279 u8 sof, eof;
280 u32 crc;
281 unsigned int hlen, tlen, elen;
282 int wlen, rc = 0;
284 port = (struct fcoe_port *)lport_priv(lport);
285 interface = port->priv;
286 ctlr = bnx2fc_to_ctlr(interface);
287 hba = interface->hba;
289 fh = fc_frame_header_get(fp);
291 skb = fp_skb(fp);
292 if (!lport->link_up) {
293 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
294 kfree_skb(skb);
295 return 0;
298 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
299 if (!ctlr->sel_fcf) {
300 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
301 kfree_skb(skb);
302 return -EINVAL;
304 if (fcoe_ctlr_els_send(ctlr, lport, skb))
305 return 0;
308 sof = fr_sof(fp);
309 eof = fr_eof(fp);
312 * Snoop the frame header to check if the frame is for
313 * an offloaded session
316 * tgt_ofld_list access is synchronized using
317 * both hba mutex and hba lock. Atleast hba mutex or
318 * hba lock needs to be held for read access.
321 spin_lock_bh(&hba->hba_lock);
322 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
323 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
324 /* This frame is for offloaded session */
325 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
326 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
327 spin_unlock_bh(&hba->hba_lock);
328 rc = bnx2fc_xmit_l2_frame(tgt, fp);
329 if (rc != -ENODEV) {
330 kfree_skb(skb);
331 return rc;
333 } else {
334 spin_unlock_bh(&hba->hba_lock);
337 elen = sizeof(struct ethhdr);
338 hlen = sizeof(struct fcoe_hdr);
339 tlen = sizeof(struct fcoe_crc_eof);
340 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
342 skb->ip_summed = CHECKSUM_NONE;
343 crc = fcoe_fc_crc(fp);
345 /* copy port crc and eof to the skb buff */
346 if (skb_is_nonlinear(skb)) {
347 skb_frag_t *frag;
348 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
349 kfree_skb(skb);
350 return -ENOMEM;
352 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
353 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
354 } else {
355 cp = skb_put(skb, tlen);
358 memset(cp, 0, sizeof(*cp));
359 cp->fcoe_eof = eof;
360 cp->fcoe_crc32 = cpu_to_le32(~crc);
361 if (skb_is_nonlinear(skb)) {
362 kunmap_atomic(cp);
363 cp = NULL;
366 /* adjust skb network/transport offsets to match mac/fcoe/port */
367 skb_push(skb, elen + hlen);
368 skb_reset_mac_header(skb);
369 skb_reset_network_header(skb);
370 skb->mac_len = elen;
371 skb->protocol = htons(ETH_P_FCOE);
372 skb->dev = interface->netdev;
374 /* fill up mac and fcoe headers */
375 eh = eth_hdr(skb);
376 eh->h_proto = htons(ETH_P_FCOE);
377 if (ctlr->map_dest)
378 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
379 else
380 /* insert GW address */
381 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
383 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
384 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
385 else
386 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
388 hp = (struct fcoe_hdr *)(eh + 1);
389 memset(hp, 0, sizeof(*hp));
390 if (FC_FCOE_VER)
391 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
392 hp->fcoe_sof = sof;
394 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
395 if (lport->seq_offload && fr_max_payload(fp)) {
396 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
397 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
398 } else {
399 skb_shinfo(skb)->gso_type = 0;
400 skb_shinfo(skb)->gso_size = 0;
403 /*update tx stats */
404 stats = per_cpu_ptr(lport->stats, get_cpu());
405 stats->TxFrames++;
406 stats->TxWords += wlen;
407 put_cpu();
409 /* send down to lld */
410 fr_dev(fp) = lport;
411 if (port->fcoe_pending_queue.qlen)
412 fcoe_check_wait_queue(lport, skb);
413 else if (fcoe_start_io(skb))
414 fcoe_check_wait_queue(lport, skb);
416 return 0;
420 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
422 * @skb: the receive socket buffer
423 * @dev: associated net device
424 * @ptype: context
425 * @olddev: last device
427 * This function receives the packet and builds FC frame and passes it up
429 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
430 struct packet_type *ptype, struct net_device *olddev)
432 struct fc_lport *lport;
433 struct bnx2fc_interface *interface;
434 struct fcoe_ctlr *ctlr;
435 struct fc_frame_header *fh;
436 struct fcoe_rcv_info *fr;
437 struct fcoe_percpu_s *bg;
438 struct sk_buff *tmp_skb;
439 unsigned short oxid;
441 interface = container_of(ptype, struct bnx2fc_interface,
442 fcoe_packet_type);
443 ctlr = bnx2fc_to_ctlr(interface);
444 lport = ctlr->lp;
446 if (unlikely(lport == NULL)) {
447 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
448 goto err;
451 tmp_skb = skb_share_check(skb, GFP_ATOMIC);
452 if (!tmp_skb)
453 goto err;
455 skb = tmp_skb;
457 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
458 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
459 goto err;
463 * Check for minimum frame length, and make sure required FCoE
464 * and FC headers are pulled into the linear data area.
466 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
467 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
468 goto err;
470 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
471 fh = (struct fc_frame_header *) skb_transport_header(skb);
473 oxid = ntohs(fh->fh_ox_id);
475 fr = fcoe_dev_from_skb(skb);
476 fr->fr_dev = lport;
478 bg = &bnx2fc_global;
479 spin_lock(&bg->fcoe_rx_list.lock);
481 __skb_queue_tail(&bg->fcoe_rx_list, skb);
482 if (bg->fcoe_rx_list.qlen == 1)
483 wake_up_process(bg->kthread);
485 spin_unlock(&bg->fcoe_rx_list.lock);
487 return 0;
488 err:
489 kfree_skb(skb);
490 return -1;
493 static int bnx2fc_l2_rcv_thread(void *arg)
495 struct fcoe_percpu_s *bg = arg;
496 struct sk_buff *skb;
498 set_user_nice(current, MIN_NICE);
499 set_current_state(TASK_INTERRUPTIBLE);
500 while (!kthread_should_stop()) {
501 schedule();
502 spin_lock_bh(&bg->fcoe_rx_list.lock);
503 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
504 spin_unlock_bh(&bg->fcoe_rx_list.lock);
505 bnx2fc_recv_frame(skb);
506 spin_lock_bh(&bg->fcoe_rx_list.lock);
508 __set_current_state(TASK_INTERRUPTIBLE);
509 spin_unlock_bh(&bg->fcoe_rx_list.lock);
511 __set_current_state(TASK_RUNNING);
512 return 0;
516 static void bnx2fc_recv_frame(struct sk_buff *skb)
518 u32 fr_len;
519 struct fc_lport *lport;
520 struct fcoe_rcv_info *fr;
521 struct fc_stats *stats;
522 struct fc_frame_header *fh;
523 struct fcoe_crc_eof crc_eof;
524 struct fc_frame *fp;
525 struct fc_lport *vn_port;
526 struct fcoe_port *port, *phys_port;
527 u8 *mac = NULL;
528 u8 *dest_mac = NULL;
529 struct fcoe_hdr *hp;
530 struct bnx2fc_interface *interface;
531 struct fcoe_ctlr *ctlr;
533 fr = fcoe_dev_from_skb(skb);
534 lport = fr->fr_dev;
535 if (unlikely(lport == NULL)) {
536 printk(KERN_ERR PFX "Invalid lport struct\n");
537 kfree_skb(skb);
538 return;
541 if (skb_is_nonlinear(skb))
542 skb_linearize(skb);
543 mac = eth_hdr(skb)->h_source;
544 dest_mac = eth_hdr(skb)->h_dest;
546 /* Pull the header */
547 hp = (struct fcoe_hdr *) skb_network_header(skb);
548 fh = (struct fc_frame_header *) skb_transport_header(skb);
549 skb_pull(skb, sizeof(struct fcoe_hdr));
550 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
552 fp = (struct fc_frame *)skb;
553 fc_frame_init(fp);
554 fr_dev(fp) = lport;
555 fr_sof(fp) = hp->fcoe_sof;
556 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
557 kfree_skb(skb);
558 return;
560 fr_eof(fp) = crc_eof.fcoe_eof;
561 fr_crc(fp) = crc_eof.fcoe_crc32;
562 if (pskb_trim(skb, fr_len)) {
563 kfree_skb(skb);
564 return;
567 phys_port = lport_priv(lport);
568 interface = phys_port->priv;
569 ctlr = bnx2fc_to_ctlr(interface);
571 fh = fc_frame_header_get(fp);
573 if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
574 BNX2FC_HBA_DBG(lport, "FC frame d_id mismatch with MAC %pM.\n",
575 dest_mac);
576 kfree_skb(skb);
577 return;
580 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
581 if (vn_port) {
582 port = lport_priv(vn_port);
583 if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
584 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
585 kfree_skb(skb);
586 return;
589 if (ctlr->state) {
590 if (!ether_addr_equal(mac, ctlr->dest_addr)) {
591 BNX2FC_HBA_DBG(lport, "Wrong source address: mac:%pM dest_addr:%pM.\n",
592 mac, ctlr->dest_addr);
593 kfree_skb(skb);
594 return;
597 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
598 fh->fh_type == FC_TYPE_FCP) {
599 /* Drop FCP data. We dont this in L2 path */
600 kfree_skb(skb);
601 return;
603 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
604 fh->fh_type == FC_TYPE_ELS) {
605 switch (fc_frame_payload_op(fp)) {
606 case ELS_LOGO:
607 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
608 /* drop non-FIP LOGO */
609 kfree_skb(skb);
610 return;
612 break;
616 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
617 /* Drop incoming ABTS */
618 kfree_skb(skb);
619 return;
623 * If the destination ID from the frame header does not match what we
624 * have on record for lport and the search for a NPIV port came up
625 * empty then this is not addressed to our port so simply drop it.
627 if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
628 BNX2FC_HBA_DBG(lport, "Dropping frame due to destination mismatch: lport->port_id=%x fh->d_id=%x.\n",
629 lport->port_id, ntoh24(fh->fh_d_id));
630 kfree_skb(skb);
631 return;
634 stats = per_cpu_ptr(lport->stats, smp_processor_id());
635 stats->RxFrames++;
636 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
638 if (le32_to_cpu(fr_crc(fp)) !=
639 ~crc32(~0, skb->data, fr_len)) {
640 if (stats->InvalidCRCCount < 5)
641 printk(KERN_WARNING PFX "dropping frame with "
642 "CRC error\n");
643 stats->InvalidCRCCount++;
644 kfree_skb(skb);
645 return;
647 fc_exch_recv(lport, fp);
651 * bnx2fc_percpu_io_thread - thread per cpu for ios
653 * @arg: ptr to bnx2fc_percpu_info structure
655 static int bnx2fc_percpu_io_thread(void *arg)
657 struct bnx2fc_percpu_s *p = arg;
658 struct bnx2fc_work *work, *tmp;
659 LIST_HEAD(work_list);
661 set_user_nice(current, MIN_NICE);
662 set_current_state(TASK_INTERRUPTIBLE);
663 while (!kthread_should_stop()) {
664 schedule();
665 spin_lock_bh(&p->fp_work_lock);
666 while (!list_empty(&p->work_list)) {
667 list_splice_init(&p->work_list, &work_list);
668 spin_unlock_bh(&p->fp_work_lock);
670 list_for_each_entry_safe(work, tmp, &work_list, list) {
671 list_del_init(&work->list);
672 bnx2fc_process_cq_compl(work->tgt, work->wqe);
673 kfree(work);
676 spin_lock_bh(&p->fp_work_lock);
678 __set_current_state(TASK_INTERRUPTIBLE);
679 spin_unlock_bh(&p->fp_work_lock);
681 __set_current_state(TASK_RUNNING);
683 return 0;
686 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
688 struct fc_host_statistics *bnx2fc_stats;
689 struct fc_lport *lport = shost_priv(shost);
690 struct fcoe_port *port = lport_priv(lport);
691 struct bnx2fc_interface *interface = port->priv;
692 struct bnx2fc_hba *hba = interface->hba;
693 struct fcoe_statistics_params *fw_stats;
694 int rc = 0;
696 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
697 if (!fw_stats)
698 return NULL;
700 mutex_lock(&hba->hba_stats_mutex);
702 bnx2fc_stats = fc_get_host_stats(shost);
704 init_completion(&hba->stat_req_done);
705 if (bnx2fc_send_stat_req(hba))
706 goto unlock_stats_mutex;
707 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
708 if (!rc) {
709 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
710 goto unlock_stats_mutex;
712 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
713 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
714 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
715 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
716 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
717 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
718 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
719 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
720 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
721 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
723 bnx2fc_stats->dumped_frames = 0;
724 bnx2fc_stats->lip_count = 0;
725 bnx2fc_stats->nos_count = 0;
726 bnx2fc_stats->loss_of_sync_count = 0;
727 bnx2fc_stats->loss_of_signal_count = 0;
728 bnx2fc_stats->prim_seq_protocol_err_count = 0;
730 memcpy(&hba->prev_stats, hba->stats_buffer,
731 sizeof(struct fcoe_statistics_params));
733 unlock_stats_mutex:
734 mutex_unlock(&hba->hba_stats_mutex);
735 return bnx2fc_stats;
738 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
740 struct fcoe_port *port = lport_priv(lport);
741 struct bnx2fc_interface *interface = port->priv;
742 struct bnx2fc_hba *hba = interface->hba;
743 struct Scsi_Host *shost = lport->host;
744 int rc = 0;
746 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
747 shost->max_lun = bnx2fc_max_luns;
748 shost->max_id = BNX2FC_MAX_FCP_TGT;
749 shost->max_channel = 0;
750 if (lport->vport)
751 shost->transportt = bnx2fc_vport_xport_template;
752 else
753 shost->transportt = bnx2fc_transport_template;
755 /* Add the new host to SCSI-ml */
756 rc = scsi_add_host(lport->host, dev);
757 if (rc) {
758 printk(KERN_ERR PFX "Error on scsi_add_host\n");
759 return rc;
761 if (!lport->vport)
762 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
763 snprintf(fc_host_symbolic_name(lport->host), 256,
764 "%s (QLogic %s) v%s over %s",
765 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
766 interface->netdev->name);
768 return 0;
771 static int bnx2fc_link_ok(struct fc_lport *lport)
773 struct fcoe_port *port = lport_priv(lport);
774 struct bnx2fc_interface *interface = port->priv;
775 struct bnx2fc_hba *hba = interface->hba;
776 struct net_device *dev = hba->phys_dev;
777 int rc = 0;
779 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
780 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
781 else {
782 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
783 rc = -1;
785 return rc;
789 * bnx2fc_get_link_state - get network link state
791 * @hba: adapter instance pointer
793 * updates adapter structure flag based on netdev state
795 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
797 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
798 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
799 else
800 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
803 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
805 struct bnx2fc_hba *hba;
806 struct bnx2fc_interface *interface;
807 struct fcoe_ctlr *ctlr;
808 struct fcoe_port *port;
809 u64 wwnn, wwpn;
811 port = lport_priv(lport);
812 interface = port->priv;
813 ctlr = bnx2fc_to_ctlr(interface);
814 hba = interface->hba;
816 /* require support for get_pauseparam ethtool op. */
817 if (!hba->phys_dev->ethtool_ops ||
818 !hba->phys_dev->ethtool_ops->get_pauseparam)
819 return -EOPNOTSUPP;
821 if (fc_set_mfs(lport, BNX2FC_MFS))
822 return -EINVAL;
824 skb_queue_head_init(&port->fcoe_pending_queue);
825 port->fcoe_pending_queue_active = 0;
826 timer_setup(&port->timer, fcoe_queue_timer, 0);
828 fcoe_link_speed_update(lport);
830 if (!lport->vport) {
831 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
832 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
833 1, 0);
834 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
835 fc_set_wwnn(lport, wwnn);
837 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
838 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
839 2, 0);
841 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
842 fc_set_wwpn(lport, wwpn);
845 return 0;
848 static void bnx2fc_destroy_timer(struct timer_list *t)
850 struct bnx2fc_hba *hba = from_timer(hba, t, destroy_timer);
852 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
853 "Destroy compl not received!!\n");
854 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
855 wake_up_interruptible(&hba->destroy_wait);
859 * bnx2fc_indicate_netevent - Generic netdev event handler
861 * @context: adapter structure pointer
862 * @event: event type
863 * @vlan_id: vlan id - associated vlan id with this event
865 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
866 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
868 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
869 u16 vlan_id)
871 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
872 struct fcoe_ctlr_device *cdev;
873 struct fc_lport *lport;
874 struct fc_lport *vport;
875 struct bnx2fc_interface *interface, *tmp;
876 struct fcoe_ctlr *ctlr;
877 int wait_for_upload = 0;
878 u32 link_possible = 1;
880 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
881 return;
883 switch (event) {
884 case NETDEV_UP:
885 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
886 printk(KERN_ERR "indicate_netevent: "\
887 "hba is not UP!!\n");
888 break;
890 case NETDEV_DOWN:
891 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
892 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
893 link_possible = 0;
894 break;
896 case NETDEV_GOING_DOWN:
897 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
898 link_possible = 0;
899 break;
901 case NETDEV_CHANGE:
902 break;
904 case NETDEV_UNREGISTER:
905 if (!vlan_id)
906 return;
907 mutex_lock(&bnx2fc_dev_lock);
908 list_for_each_entry_safe(interface, tmp, &if_list, list) {
909 if (interface->hba == hba &&
910 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
911 __bnx2fc_destroy(interface);
913 mutex_unlock(&bnx2fc_dev_lock);
915 /* Ensure ALL destroy work has been completed before return */
916 flush_workqueue(bnx2fc_wq);
917 return;
919 default:
920 return;
923 mutex_lock(&bnx2fc_dev_lock);
924 list_for_each_entry(interface, &if_list, list) {
926 if (interface->hba != hba)
927 continue;
929 ctlr = bnx2fc_to_ctlr(interface);
930 lport = ctlr->lp;
931 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
932 interface->netdev->name, event);
934 fcoe_link_speed_update(lport);
936 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
938 if (link_possible && !bnx2fc_link_ok(lport)) {
939 switch (cdev->enabled) {
940 case FCOE_CTLR_DISABLED:
941 pr_info("Link up while interface is disabled.\n");
942 break;
943 case FCOE_CTLR_ENABLED:
944 case FCOE_CTLR_UNUSED:
945 /* Reset max recv frame size to default */
946 fc_set_mfs(lport, BNX2FC_MFS);
948 * ctlr link up will only be handled during
949 * enable to avoid sending discovery
950 * solicitation on a stale vlan
952 if (interface->enabled)
953 fcoe_ctlr_link_up(ctlr);
955 } else if (fcoe_ctlr_link_down(ctlr)) {
956 switch (cdev->enabled) {
957 case FCOE_CTLR_DISABLED:
958 pr_info("Link down while interface is disabled.\n");
959 break;
960 case FCOE_CTLR_ENABLED:
961 case FCOE_CTLR_UNUSED:
962 mutex_lock(&lport->lp_mutex);
963 list_for_each_entry(vport, &lport->vports, list)
964 fc_host_port_type(vport->host) =
965 FC_PORTTYPE_UNKNOWN;
966 mutex_unlock(&lport->lp_mutex);
967 fc_host_port_type(lport->host) =
968 FC_PORTTYPE_UNKNOWN;
969 per_cpu_ptr(lport->stats,
970 get_cpu())->LinkFailureCount++;
971 put_cpu();
972 fcoe_clean_pending_queue(lport);
973 wait_for_upload = 1;
977 mutex_unlock(&bnx2fc_dev_lock);
979 if (wait_for_upload) {
980 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
981 init_waitqueue_head(&hba->shutdown_wait);
982 BNX2FC_MISC_DBG("indicate_netevent "
983 "num_ofld_sess = %d\n",
984 hba->num_ofld_sess);
985 hba->wait_for_link_down = 1;
986 wait_event_interruptible(hba->shutdown_wait,
987 (hba->num_ofld_sess == 0));
988 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
989 hba->num_ofld_sess);
990 hba->wait_for_link_down = 0;
992 if (signal_pending(current))
993 flush_signals(current);
997 static int bnx2fc_libfc_config(struct fc_lport *lport)
1000 /* Set the function pointers set by bnx2fc driver */
1001 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
1002 sizeof(struct libfc_function_template));
1003 fc_elsct_init(lport);
1004 fc_exch_init(lport);
1005 fc_disc_init(lport);
1006 fc_disc_config(lport, lport);
1007 return 0;
1010 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
1012 int fcoe_min_xid, fcoe_max_xid;
1014 fcoe_min_xid = hba->max_xid + 1;
1015 if (nr_cpu_ids <= 2)
1016 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
1017 else
1018 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
1019 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
1020 fcoe_max_xid, NULL)) {
1021 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
1022 return -ENOMEM;
1025 return 0;
1028 static int bnx2fc_lport_config(struct fc_lport *lport)
1030 lport->link_up = 0;
1031 lport->qfull = 0;
1032 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
1033 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
1034 lport->e_d_tov = 2 * 1000;
1035 lport->r_a_tov = 10 * 1000;
1037 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1038 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
1039 lport->does_npiv = 1;
1041 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
1042 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
1044 /* alloc stats structure */
1045 if (fc_lport_init_stats(lport))
1046 return -ENOMEM;
1048 /* Finish fc_lport configuration */
1049 fc_lport_config(lport);
1051 return 0;
1055 * bnx2fc_fip_recv - handle a received FIP frame.
1057 * @skb: the received skb
1058 * @dev: associated &net_device
1059 * @ptype: the &packet_type structure which was used to register this handler.
1060 * @orig_dev: original receive &net_device, in case @ dev is a bond.
1062 * Returns: 0 for success
1064 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1065 struct packet_type *ptype,
1066 struct net_device *orig_dev)
1068 struct bnx2fc_interface *interface;
1069 struct fcoe_ctlr *ctlr;
1070 interface = container_of(ptype, struct bnx2fc_interface,
1071 fip_packet_type);
1072 ctlr = bnx2fc_to_ctlr(interface);
1073 fcoe_ctlr_recv(ctlr, skb);
1074 return 0;
1078 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1080 * @fip: FCoE controller.
1081 * @old: Unicast MAC address to delete if the MAC is non-zero.
1082 * @new: Unicast MAC address to add.
1084 * Remove any previously-set unicast MAC filter.
1085 * Add secondary FCoE MAC address filter for our OUI.
1087 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1089 struct fcoe_port *port = lport_priv(lport);
1091 memcpy(port->data_src_addr, addr, ETH_ALEN);
1095 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1097 * @lport: libfc port
1099 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1101 struct fcoe_port *port;
1103 port = (struct fcoe_port *)lport_priv(lport);
1104 return port->data_src_addr;
1108 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1110 * @fip: FCoE controller.
1111 * @skb: FIP Packet.
1113 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1115 struct fip_header *fiph;
1116 struct ethhdr *eth_hdr;
1117 u16 op;
1118 u8 sub;
1120 fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2);
1121 eth_hdr = (struct ethhdr *)skb_mac_header(skb);
1122 op = ntohs(fiph->fip_op);
1123 sub = fiph->fip_subcode;
1125 if (op == FIP_OP_CTRL && sub == FIP_SC_SOL && bnx2fc_log_fka)
1126 BNX2FC_MISC_DBG("Sending FKA from %pM to %pM.\n",
1127 eth_hdr->h_source, eth_hdr->h_dest);
1129 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1130 dev_queue_xmit(skb);
1133 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1135 struct Scsi_Host *shost = vport_to_shost(vport);
1136 struct fc_lport *n_port = shost_priv(shost);
1137 struct fcoe_port *port = lport_priv(n_port);
1138 struct bnx2fc_interface *interface = port->priv;
1139 struct net_device *netdev = interface->netdev;
1140 struct fc_lport *vn_port;
1141 int rc;
1142 char buf[32];
1144 rc = fcoe_validate_vport_create(vport);
1145 if (rc) {
1146 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1147 printk(KERN_ERR PFX "Failed to create vport, "
1148 "WWPN (0x%s) already exists\n",
1149 buf);
1150 return rc;
1153 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1154 printk(KERN_ERR PFX "vn ports cannot be created on"
1155 "this interface\n");
1156 return -EIO;
1158 rtnl_lock();
1159 mutex_lock(&bnx2fc_dev_lock);
1160 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1161 mutex_unlock(&bnx2fc_dev_lock);
1162 rtnl_unlock();
1164 if (!vn_port) {
1165 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1166 netdev->name);
1167 return -EIO;
1170 if (bnx2fc_devloss_tmo)
1171 fc_host_dev_loss_tmo(vn_port->host) = bnx2fc_devloss_tmo;
1173 if (disabled) {
1174 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1175 } else {
1176 vn_port->boot_time = jiffies;
1177 fc_lport_init(vn_port);
1178 fc_fabric_login(vn_port);
1179 fc_vport_setlink(vn_port);
1181 return 0;
1184 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1186 struct bnx2fc_lport *blport, *tmp;
1188 spin_lock_bh(&hba->hba_lock);
1189 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1190 if (blport->lport == lport) {
1191 list_del(&blport->list);
1192 kfree(blport);
1195 spin_unlock_bh(&hba->hba_lock);
1198 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1200 struct Scsi_Host *shost = vport_to_shost(vport);
1201 struct fc_lport *n_port = shost_priv(shost);
1202 struct fc_lport *vn_port = vport->dd_data;
1203 struct fcoe_port *port = lport_priv(vn_port);
1204 struct bnx2fc_interface *interface = port->priv;
1205 struct fc_lport *v_port;
1206 bool found = false;
1208 mutex_lock(&n_port->lp_mutex);
1209 list_for_each_entry(v_port, &n_port->vports, list)
1210 if (v_port->vport == vport) {
1211 found = true;
1212 break;
1215 if (!found) {
1216 mutex_unlock(&n_port->lp_mutex);
1217 return -ENOENT;
1219 list_del(&vn_port->list);
1220 mutex_unlock(&n_port->lp_mutex);
1221 bnx2fc_free_vport(interface->hba, port->lport);
1222 bnx2fc_port_shutdown(port->lport);
1223 bnx2fc_interface_put(interface);
1224 queue_work(bnx2fc_wq, &port->destroy_work);
1225 return 0;
1228 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1230 struct fc_lport *lport = vport->dd_data;
1232 if (disable) {
1233 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1234 fc_fabric_logoff(lport);
1235 } else {
1236 lport->boot_time = jiffies;
1237 fc_fabric_login(lport);
1238 fc_vport_setlink(lport);
1240 return 0;
1244 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1246 struct net_device *netdev = interface->netdev;
1247 struct net_device *physdev = interface->hba->phys_dev;
1248 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1249 struct netdev_hw_addr *ha;
1250 int sel_san_mac = 0;
1252 /* setup Source MAC Address */
1253 rcu_read_lock();
1254 for_each_dev_addr(physdev, ha) {
1255 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1256 ha->type);
1257 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1258 ha->addr[1], ha->addr[2], ha->addr[3],
1259 ha->addr[4], ha->addr[5]);
1261 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1262 (is_valid_ether_addr(ha->addr))) {
1263 memcpy(ctlr->ctl_src_addr, ha->addr,
1264 ETH_ALEN);
1265 sel_san_mac = 1;
1266 BNX2FC_MISC_DBG("Found SAN MAC\n");
1269 rcu_read_unlock();
1271 if (!sel_san_mac)
1272 return -ENODEV;
1274 interface->fip_packet_type.func = bnx2fc_fip_recv;
1275 interface->fip_packet_type.type = htons(ETH_P_FIP);
1276 interface->fip_packet_type.dev = netdev;
1277 dev_add_pack(&interface->fip_packet_type);
1279 interface->fcoe_packet_type.func = bnx2fc_rcv;
1280 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1281 interface->fcoe_packet_type.dev = netdev;
1282 dev_add_pack(&interface->fcoe_packet_type);
1284 return 0;
1287 static int bnx2fc_attach_transport(void)
1289 bnx2fc_transport_template =
1290 fc_attach_transport(&bnx2fc_transport_function);
1292 if (bnx2fc_transport_template == NULL) {
1293 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1294 return -ENODEV;
1297 bnx2fc_vport_xport_template =
1298 fc_attach_transport(&bnx2fc_vport_xport_function);
1299 if (bnx2fc_vport_xport_template == NULL) {
1300 printk(KERN_ERR PFX
1301 "Failed to attach FC transport for vport\n");
1302 fc_release_transport(bnx2fc_transport_template);
1303 bnx2fc_transport_template = NULL;
1304 return -ENODEV;
1306 return 0;
1308 static void bnx2fc_release_transport(void)
1310 fc_release_transport(bnx2fc_transport_template);
1311 fc_release_transport(bnx2fc_vport_xport_template);
1312 bnx2fc_transport_template = NULL;
1313 bnx2fc_vport_xport_template = NULL;
1316 static void bnx2fc_interface_release(struct kref *kref)
1318 struct fcoe_ctlr_device *ctlr_dev;
1319 struct bnx2fc_interface *interface;
1320 struct fcoe_ctlr *ctlr;
1321 struct net_device *netdev;
1323 interface = container_of(kref, struct bnx2fc_interface, kref);
1324 BNX2FC_MISC_DBG("Interface is being released\n");
1326 ctlr = bnx2fc_to_ctlr(interface);
1327 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
1328 netdev = interface->netdev;
1330 /* tear-down FIP controller */
1331 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1332 fcoe_ctlr_destroy(ctlr);
1334 fcoe_ctlr_device_delete(ctlr_dev);
1336 dev_put(netdev);
1337 module_put(THIS_MODULE);
1340 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1342 kref_get(&interface->kref);
1345 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1347 kref_put(&interface->kref, bnx2fc_interface_release);
1349 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1351 /* Free the command manager */
1352 if (hba->cmd_mgr) {
1353 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1354 hba->cmd_mgr = NULL;
1356 kfree(hba->tgt_ofld_list);
1357 bnx2fc_unbind_pcidev(hba);
1358 kfree(hba);
1362 * bnx2fc_hba_create - create a new bnx2fc hba
1364 * @cnic: pointer to cnic device
1366 * Creates a new FCoE hba on the given device.
1369 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1371 struct bnx2fc_hba *hba;
1372 struct fcoe_capabilities *fcoe_cap;
1373 int rc;
1375 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1376 if (!hba) {
1377 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1378 return NULL;
1380 spin_lock_init(&hba->hba_lock);
1381 mutex_init(&hba->hba_mutex);
1382 mutex_init(&hba->hba_stats_mutex);
1384 hba->cnic = cnic;
1386 hba->max_tasks = cnic->max_fcoe_exchanges;
1387 hba->elstm_xids = (hba->max_tasks / 2);
1388 hba->max_outstanding_cmds = hba->elstm_xids;
1389 hba->max_xid = (hba->max_tasks - 1);
1391 rc = bnx2fc_bind_pcidev(hba);
1392 if (rc) {
1393 printk(KERN_ERR PFX "create_adapter: bind error\n");
1394 goto bind_err;
1396 hba->phys_dev = cnic->netdev;
1397 hba->next_conn_id = 0;
1399 hba->tgt_ofld_list =
1400 kcalloc(BNX2FC_NUM_MAX_SESS, sizeof(struct bnx2fc_rport *),
1401 GFP_KERNEL);
1402 if (!hba->tgt_ofld_list) {
1403 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1404 goto tgtofld_err;
1407 hba->num_ofld_sess = 0;
1409 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
1410 if (!hba->cmd_mgr) {
1411 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1412 goto cmgr_err;
1414 fcoe_cap = &hba->fcoe_cap;
1416 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1417 FCOE_IOS_PER_CONNECTION_SHIFT;
1418 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1419 FCOE_LOGINS_PER_PORT_SHIFT;
1420 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
1421 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1422 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1423 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1424 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1425 FCOE_TARGETS_SUPPORTED_SHIFT;
1426 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
1427 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1428 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
1430 init_waitqueue_head(&hba->shutdown_wait);
1431 init_waitqueue_head(&hba->destroy_wait);
1432 INIT_LIST_HEAD(&hba->vports);
1434 return hba;
1436 cmgr_err:
1437 kfree(hba->tgt_ofld_list);
1438 tgtofld_err:
1439 bnx2fc_unbind_pcidev(hba);
1440 bind_err:
1441 kfree(hba);
1442 return NULL;
1445 static struct bnx2fc_interface *
1446 bnx2fc_interface_create(struct bnx2fc_hba *hba,
1447 struct net_device *netdev,
1448 enum fip_state fip_mode)
1450 struct fcoe_ctlr_device *ctlr_dev;
1451 struct bnx2fc_interface *interface;
1452 struct fcoe_ctlr *ctlr;
1453 int size;
1454 int rc = 0;
1456 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
1457 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1458 size);
1459 if (!ctlr_dev) {
1460 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1461 return NULL;
1463 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
1464 ctlr->cdev = ctlr_dev;
1465 interface = fcoe_ctlr_priv(ctlr);
1466 dev_hold(netdev);
1467 kref_init(&interface->kref);
1468 interface->hba = hba;
1469 interface->netdev = netdev;
1471 /* Initialize FIP */
1472 fcoe_ctlr_init(ctlr, fip_mode);
1473 ctlr->send = bnx2fc_fip_send;
1474 ctlr->update_mac = bnx2fc_update_src_mac;
1475 ctlr->get_src_addr = bnx2fc_get_src_mac;
1476 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1478 rc = bnx2fc_interface_setup(interface);
1479 if (!rc)
1480 return interface;
1482 fcoe_ctlr_destroy(ctlr);
1483 dev_put(netdev);
1484 fcoe_ctlr_device_delete(ctlr_dev);
1485 return NULL;
1489 * bnx2fc_if_create - Create FCoE instance on a given interface
1491 * @interface: FCoE interface to create a local port on
1492 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1493 * @npiv: Indicates if the port is vport or not
1495 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1497 * Returns: Allocated fc_lport or an error pointer
1499 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1500 struct device *parent, int npiv)
1502 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1503 struct fc_lport *lport, *n_port;
1504 struct fcoe_port *port;
1505 struct Scsi_Host *shost;
1506 struct fc_vport *vport = dev_to_vport(parent);
1507 struct bnx2fc_lport *blport;
1508 struct bnx2fc_hba *hba = interface->hba;
1509 int rc = 0;
1511 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1512 if (!blport) {
1513 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
1514 return NULL;
1517 /* Allocate Scsi_Host structure */
1518 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
1519 if (!npiv)
1520 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1521 else
1522 lport = libfc_vport_create(vport, sizeof(*port));
1524 if (!lport) {
1525 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1526 goto free_blport;
1528 shost = lport->host;
1529 port = lport_priv(lport);
1530 port->lport = lport;
1531 port->priv = interface;
1532 port->get_netdev = bnx2fc_netdev;
1533 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1535 /* Configure fcoe_port */
1536 rc = bnx2fc_lport_config(lport);
1537 if (rc)
1538 goto lp_config_err;
1540 if (npiv) {
1541 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1542 vport->node_name, vport->port_name);
1543 fc_set_wwnn(lport, vport->node_name);
1544 fc_set_wwpn(lport, vport->port_name);
1546 /* Configure netdev and networking properties of the lport */
1547 rc = bnx2fc_net_config(lport, interface->netdev);
1548 if (rc) {
1549 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1550 goto lp_config_err;
1553 rc = bnx2fc_shost_config(lport, parent);
1554 if (rc) {
1555 printk(KERN_ERR PFX "Couldn't configure shost for %s\n",
1556 interface->netdev->name);
1557 goto lp_config_err;
1560 /* Initialize the libfc library */
1561 rc = bnx2fc_libfc_config(lport);
1562 if (rc) {
1563 printk(KERN_ERR PFX "Couldn't configure libfc\n");
1564 goto shost_err;
1566 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1568 if (bnx2fc_devloss_tmo)
1569 fc_host_dev_loss_tmo(shost) = bnx2fc_devloss_tmo;
1571 /* Allocate exchange manager */
1572 if (!npiv)
1573 rc = bnx2fc_em_config(lport, hba);
1574 else {
1575 shost = vport_to_shost(vport);
1576 n_port = shost_priv(shost);
1577 rc = fc_exch_mgr_list_clone(n_port, lport);
1580 if (rc) {
1581 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1582 goto shost_err;
1585 bnx2fc_interface_get(interface);
1587 spin_lock_bh(&hba->hba_lock);
1588 blport->lport = lport;
1589 list_add_tail(&blport->list, &hba->vports);
1590 spin_unlock_bh(&hba->hba_lock);
1592 return lport;
1594 shost_err:
1595 scsi_remove_host(shost);
1596 lp_config_err:
1597 scsi_host_put(lport->host);
1598 free_blport:
1599 kfree(blport);
1600 return NULL;
1603 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
1605 /* Dont listen for Ethernet packets anymore */
1606 __dev_remove_pack(&interface->fcoe_packet_type);
1607 __dev_remove_pack(&interface->fip_packet_type);
1608 synchronize_net();
1611 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1613 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1614 struct fc_lport *lport = ctlr->lp;
1615 struct fcoe_port *port = lport_priv(lport);
1616 struct bnx2fc_hba *hba = interface->hba;
1618 /* Stop the transmit retry timer */
1619 del_timer_sync(&port->timer);
1621 /* Free existing transmit skbs */
1622 fcoe_clean_pending_queue(lport);
1624 bnx2fc_net_cleanup(interface);
1626 bnx2fc_free_vport(hba, lport);
1629 static void bnx2fc_if_destroy(struct fc_lport *lport)
1632 /* Free queued packets for the receive thread */
1633 bnx2fc_clean_rx_queue(lport);
1635 /* Detach from scsi-ml */
1636 fc_remove_host(lport->host);
1637 scsi_remove_host(lport->host);
1640 * Note that only the physical lport will have the exchange manager.
1641 * for vports, this function is NOP
1643 fc_exch_mgr_free(lport);
1645 /* Free memory used by statistical counters */
1646 fc_lport_free_stats(lport);
1648 /* Release Scsi_Host */
1649 scsi_host_put(lport->host);
1652 static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
1654 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1655 struct fc_lport *lport = ctlr->lp;
1656 struct fcoe_port *port = lport_priv(lport);
1658 bnx2fc_interface_cleanup(interface);
1659 bnx2fc_stop(interface);
1660 list_del(&interface->list);
1661 bnx2fc_interface_put(interface);
1662 queue_work(bnx2fc_wq, &port->destroy_work);
1666 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1668 * @buffer: The name of the Ethernet interface to be destroyed
1669 * @kp: The associated kernel parameter
1671 * Called from sysfs.
1673 * Returns: 0 for success
1675 static int bnx2fc_destroy(struct net_device *netdev)
1677 struct bnx2fc_interface *interface = NULL;
1678 struct workqueue_struct *timer_work_queue;
1679 struct fcoe_ctlr *ctlr;
1680 int rc = 0;
1682 rtnl_lock();
1683 mutex_lock(&bnx2fc_dev_lock);
1685 interface = bnx2fc_interface_lookup(netdev);
1686 ctlr = bnx2fc_to_ctlr(interface);
1687 if (!interface || !ctlr->lp) {
1688 rc = -ENODEV;
1689 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1690 goto netdev_err;
1693 timer_work_queue = interface->timer_work_queue;
1694 __bnx2fc_destroy(interface);
1695 destroy_workqueue(timer_work_queue);
1697 netdev_err:
1698 mutex_unlock(&bnx2fc_dev_lock);
1699 rtnl_unlock();
1700 return rc;
1703 static void bnx2fc_destroy_work(struct work_struct *work)
1705 struct fcoe_port *port;
1706 struct fc_lport *lport;
1708 port = container_of(work, struct fcoe_port, destroy_work);
1709 lport = port->lport;
1711 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1713 bnx2fc_if_destroy(lport);
1716 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1718 bnx2fc_free_fw_resc(hba);
1719 bnx2fc_free_task_ctx(hba);
1723 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1724 * pci structure
1726 * @hba: Adapter instance
1728 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1730 if (bnx2fc_setup_task_ctx(hba))
1731 goto mem_err;
1733 if (bnx2fc_setup_fw_resc(hba))
1734 goto mem_err;
1736 return 0;
1737 mem_err:
1738 bnx2fc_unbind_adapter_devices(hba);
1739 return -ENOMEM;
1742 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1744 struct cnic_dev *cnic;
1745 struct pci_dev *pdev;
1747 if (!hba->cnic) {
1748 printk(KERN_ERR PFX "cnic is NULL\n");
1749 return -ENODEV;
1751 cnic = hba->cnic;
1752 pdev = hba->pcidev = cnic->pcidev;
1753 if (!hba->pcidev)
1754 return -ENODEV;
1756 switch (pdev->device) {
1757 case PCI_DEVICE_ID_NX2_57710:
1758 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1759 break;
1760 case PCI_DEVICE_ID_NX2_57711:
1761 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1762 break;
1763 case PCI_DEVICE_ID_NX2_57712:
1764 case PCI_DEVICE_ID_NX2_57712_MF:
1765 case PCI_DEVICE_ID_NX2_57712_VF:
1766 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1767 break;
1768 case PCI_DEVICE_ID_NX2_57800:
1769 case PCI_DEVICE_ID_NX2_57800_MF:
1770 case PCI_DEVICE_ID_NX2_57800_VF:
1771 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1772 break;
1773 case PCI_DEVICE_ID_NX2_57810:
1774 case PCI_DEVICE_ID_NX2_57810_MF:
1775 case PCI_DEVICE_ID_NX2_57810_VF:
1776 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1777 break;
1778 case PCI_DEVICE_ID_NX2_57840:
1779 case PCI_DEVICE_ID_NX2_57840_MF:
1780 case PCI_DEVICE_ID_NX2_57840_VF:
1781 case PCI_DEVICE_ID_NX2_57840_2_20:
1782 case PCI_DEVICE_ID_NX2_57840_4_10:
1783 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1784 break;
1785 default:
1786 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1787 break;
1789 pci_dev_get(hba->pcidev);
1790 return 0;
1793 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1795 if (hba->pcidev) {
1796 hba->chip_num[0] = '\0';
1797 pci_dev_put(hba->pcidev);
1799 hba->pcidev = NULL;
1803 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1805 * @handle: transport handle pointing to adapter struture
1807 static int bnx2fc_ulp_get_stats(void *handle)
1809 struct bnx2fc_hba *hba = handle;
1810 struct cnic_dev *cnic;
1811 struct fcoe_stats_info *stats_addr;
1813 if (!hba)
1814 return -EINVAL;
1816 cnic = hba->cnic;
1817 stats_addr = &cnic->stats_addr->fcoe_stat;
1818 if (!stats_addr)
1819 return -EINVAL;
1821 strncpy(stats_addr->version, BNX2FC_VERSION,
1822 sizeof(stats_addr->version));
1823 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1824 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1826 return 0;
1831 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1833 * @handle: transport handle pointing to adapter structure
1835 * This function maps adapter structure to pcidev structure and initiates
1836 * firmware handshake to enable/initialize on-chip FCoE components.
1837 * This bnx2fc - cnic interface api callback is used after following
1838 * conditions are met -
1839 * a) underlying network interface is up (marked by event NETDEV_UP
1840 * from netdev
1841 * b) bnx2fc adatper structure is registered.
1843 static void bnx2fc_ulp_start(void *handle)
1845 struct bnx2fc_hba *hba = handle;
1846 struct bnx2fc_interface *interface;
1847 struct fcoe_ctlr *ctlr;
1848 struct fc_lport *lport;
1850 mutex_lock(&bnx2fc_dev_lock);
1852 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1853 bnx2fc_fw_init(hba);
1855 BNX2FC_MISC_DBG("bnx2fc started.\n");
1857 list_for_each_entry(interface, &if_list, list) {
1858 if (interface->hba == hba) {
1859 ctlr = bnx2fc_to_ctlr(interface);
1860 lport = ctlr->lp;
1861 /* Kick off Fabric discovery*/
1862 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1863 lport->tt.frame_send = bnx2fc_xmit;
1864 bnx2fc_start_disc(interface);
1868 mutex_unlock(&bnx2fc_dev_lock);
1871 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1873 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1874 fc_fabric_logoff(lport);
1875 fc_lport_destroy(lport);
1878 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1880 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1881 struct fc_lport *lport;
1882 struct fc_lport *vport;
1884 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1885 return;
1887 lport = ctlr->lp;
1888 bnx2fc_port_shutdown(lport);
1890 mutex_lock(&lport->lp_mutex);
1891 list_for_each_entry(vport, &lport->vports, list)
1892 fc_host_port_type(vport->host) =
1893 FC_PORTTYPE_UNKNOWN;
1894 mutex_unlock(&lport->lp_mutex);
1895 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1896 fcoe_ctlr_link_down(ctlr);
1897 fcoe_clean_pending_queue(lport);
1900 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1902 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1903 int rc = -1;
1904 int i = HZ;
1906 rc = bnx2fc_bind_adapter_devices(hba);
1907 if (rc) {
1908 printk(KERN_ALERT PFX
1909 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1910 goto err_out;
1913 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1914 if (rc) {
1915 printk(KERN_ALERT PFX
1916 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1917 goto err_unbind;
1921 * Wait until the adapter init message is complete, and adapter
1922 * state is UP.
1924 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1925 msleep(BNX2FC_INIT_POLL_TIME);
1927 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1928 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1929 "Ignoring...\n",
1930 hba->cnic->netdev->name);
1931 rc = -1;
1932 goto err_unbind;
1936 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1937 return 0;
1939 err_unbind:
1940 bnx2fc_unbind_adapter_devices(hba);
1941 err_out:
1942 return rc;
1945 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1947 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1948 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1949 timer_setup(&hba->destroy_timer, bnx2fc_destroy_timer,
1951 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1952 jiffies;
1953 add_timer(&hba->destroy_timer);
1954 wait_event_interruptible(hba->destroy_wait,
1955 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1956 &hba->flags));
1957 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
1958 /* This should never happen */
1959 if (signal_pending(current))
1960 flush_signals(current);
1962 del_timer_sync(&hba->destroy_timer);
1964 bnx2fc_unbind_adapter_devices(hba);
1969 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1971 * @handle: transport handle pointing to adapter structure
1973 * Driver checks if adapter is already in shutdown mode, if not start
1974 * the shutdown process.
1976 static void bnx2fc_ulp_stop(void *handle)
1978 struct bnx2fc_hba *hba = handle;
1979 struct bnx2fc_interface *interface;
1981 printk(KERN_ERR "ULP_STOP\n");
1983 mutex_lock(&bnx2fc_dev_lock);
1984 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1985 goto exit;
1986 list_for_each_entry(interface, &if_list, list) {
1987 if (interface->hba == hba)
1988 bnx2fc_stop(interface);
1990 BUG_ON(hba->num_ofld_sess != 0);
1992 mutex_lock(&hba->hba_mutex);
1993 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1994 clear_bit(ADAPTER_STATE_GOING_DOWN,
1995 &hba->adapter_state);
1997 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1998 mutex_unlock(&hba->hba_mutex);
2000 bnx2fc_fw_destroy(hba);
2001 exit:
2002 mutex_unlock(&bnx2fc_dev_lock);
2005 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
2007 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
2008 struct fc_lport *lport;
2009 int wait_cnt = 0;
2011 BNX2FC_MISC_DBG("Entered %s\n", __func__);
2012 /* Kick off FIP/FLOGI */
2013 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
2014 printk(KERN_ERR PFX "Init not done yet\n");
2015 return;
2018 lport = ctlr->lp;
2019 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
2021 if (!bnx2fc_link_ok(lport) && interface->enabled) {
2022 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
2023 fcoe_ctlr_link_up(ctlr);
2024 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2025 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2028 /* wait for the FCF to be selected before issuing FLOGI */
2029 while (!ctlr->sel_fcf) {
2030 msleep(250);
2031 /* give up after 3 secs */
2032 if (++wait_cnt > 12)
2033 break;
2036 /* Reset max receive frame size to default */
2037 if (fc_set_mfs(lport, BNX2FC_MFS))
2038 return;
2040 fc_lport_init(lport);
2041 fc_fabric_login(lport);
2046 * bnx2fc_ulp_init - Initialize an adapter instance
2048 * @dev : cnic device handle
2049 * Called from cnic_register_driver() context to initialize all
2050 * enumerated cnic devices. This routine allocates adapter structure
2051 * and other device specific resources.
2053 static void bnx2fc_ulp_init(struct cnic_dev *dev)
2055 struct bnx2fc_hba *hba;
2056 int rc = 0;
2058 BNX2FC_MISC_DBG("Entered %s\n", __func__);
2059 /* bnx2fc works only when bnx2x is loaded */
2060 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
2061 (dev->max_fcoe_conn == 0)) {
2062 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
2063 " flags: %lx fcoe_conn: %d\n",
2064 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
2065 return;
2068 hba = bnx2fc_hba_create(dev);
2069 if (!hba) {
2070 printk(KERN_ERR PFX "hba initialization failed\n");
2071 return;
2074 pr_info(PFX "FCoE initialized for %s.\n", dev->netdev->name);
2076 /* Add HBA to the adapter list */
2077 mutex_lock(&bnx2fc_dev_lock);
2078 list_add_tail(&hba->list, &adapter_list);
2079 adapter_count++;
2080 mutex_unlock(&bnx2fc_dev_lock);
2082 dev->fcoe_cap = &hba->fcoe_cap;
2083 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2084 rc = dev->register_device(dev, CNIC_ULP_FCOE,
2085 (void *) hba);
2086 if (rc)
2087 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
2088 else
2089 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2092 /* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2093 static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2095 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2097 if (interface->enabled == true) {
2098 if (!ctlr->lp) {
2099 pr_err(PFX "__bnx2fc_disable: lport not found\n");
2100 return -ENODEV;
2101 } else {
2102 interface->enabled = false;
2103 fcoe_ctlr_link_down(ctlr);
2104 fcoe_clean_pending_queue(ctlr->lp);
2107 return 0;
2111 * Deperecated: Use bnx2fc_enabled()
2113 static int bnx2fc_disable(struct net_device *netdev)
2115 struct bnx2fc_interface *interface;
2116 struct fcoe_ctlr *ctlr;
2117 int rc = 0;
2119 rtnl_lock();
2120 mutex_lock(&bnx2fc_dev_lock);
2122 interface = bnx2fc_interface_lookup(netdev);
2123 ctlr = bnx2fc_to_ctlr(interface);
2125 if (!interface) {
2126 rc = -ENODEV;
2127 pr_err(PFX "bnx2fc_disable: interface not found\n");
2128 } else {
2129 rc = __bnx2fc_disable(ctlr);
2131 mutex_unlock(&bnx2fc_dev_lock);
2132 rtnl_unlock();
2133 return rc;
2136 static uint bnx2fc_npiv_create_vports(struct fc_lport *lport,
2137 struct cnic_fc_npiv_tbl *npiv_tbl)
2139 struct fc_vport_identifiers vpid;
2140 uint i, created = 0;
2141 u64 wwnn = 0;
2142 char wwpn_str[32];
2143 char wwnn_str[32];
2145 if (npiv_tbl->count > MAX_NPIV_ENTRIES) {
2146 BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n");
2147 goto done;
2150 /* Sanity check the first entry to make sure it's not 0 */
2151 if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 &&
2152 wwn_to_u64(npiv_tbl->wwpn[0]) == 0) {
2153 BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n");
2154 goto done;
2157 vpid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2158 vpid.vport_type = FC_PORTTYPE_NPIV;
2159 vpid.disable = false;
2161 for (i = 0; i < npiv_tbl->count; i++) {
2162 wwnn = wwn_to_u64(npiv_tbl->wwnn[i]);
2163 if (wwnn == 0) {
2165 * If we get a 0 element from for the WWNN then assume
2166 * the WWNN should be the same as the physical port.
2168 wwnn = lport->wwnn;
2170 vpid.node_name = wwnn;
2171 vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]);
2172 scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name),
2173 "NPIV[%u]:%016llx-%016llx",
2174 created, vpid.port_name, vpid.node_name);
2175 fcoe_wwn_to_str(vpid.node_name, wwnn_str, sizeof(wwnn_str));
2176 fcoe_wwn_to_str(vpid.port_name, wwpn_str, sizeof(wwpn_str));
2177 BNX2FC_HBA_DBG(lport, "Creating vport %s:%s.\n", wwnn_str,
2178 wwpn_str);
2179 if (fc_vport_create(lport->host, 0, &vpid))
2180 created++;
2181 else
2182 BNX2FC_HBA_DBG(lport, "Failed to create vport\n");
2184 done:
2185 return created;
2188 static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2190 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2191 struct bnx2fc_hba *hba;
2192 struct cnic_fc_npiv_tbl *npiv_tbl;
2193 struct fc_lport *lport;
2195 if (interface->enabled == false) {
2196 if (!ctlr->lp) {
2197 pr_err(PFX "__bnx2fc_enable: lport not found\n");
2198 return -ENODEV;
2199 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2200 fcoe_ctlr_link_up(ctlr);
2201 interface->enabled = true;
2205 /* Create static NPIV ports if any are contained in NVRAM */
2206 hba = interface->hba;
2207 lport = ctlr->lp;
2209 if (!hba)
2210 goto done;
2212 if (!hba->cnic)
2213 goto done;
2215 if (!lport)
2216 goto done;
2218 if (!lport->host)
2219 goto done;
2221 if (!hba->cnic->get_fc_npiv_tbl)
2222 goto done;
2224 npiv_tbl = kzalloc(sizeof(struct cnic_fc_npiv_tbl), GFP_KERNEL);
2225 if (!npiv_tbl)
2226 goto done;
2228 if (hba->cnic->get_fc_npiv_tbl(hba->cnic, npiv_tbl))
2229 goto done_free;
2231 bnx2fc_npiv_create_vports(lport, npiv_tbl);
2232 done_free:
2233 kfree(npiv_tbl);
2234 done:
2235 return 0;
2239 * Deprecated: Use bnx2fc_enabled()
2241 static int bnx2fc_enable(struct net_device *netdev)
2243 struct bnx2fc_interface *interface;
2244 struct fcoe_ctlr *ctlr;
2245 int rc = 0;
2247 rtnl_lock();
2248 mutex_lock(&bnx2fc_dev_lock);
2250 interface = bnx2fc_interface_lookup(netdev);
2251 ctlr = bnx2fc_to_ctlr(interface);
2252 if (!interface) {
2253 rc = -ENODEV;
2254 pr_err(PFX "bnx2fc_enable: interface not found\n");
2255 } else {
2256 rc = __bnx2fc_enable(ctlr);
2259 mutex_unlock(&bnx2fc_dev_lock);
2260 rtnl_unlock();
2261 return rc;
2265 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2266 * @cdev: The FCoE Controller that is being enabled or disabled
2268 * fcoe_sysfs will ensure that the state of 'enabled' has
2269 * changed, so no checking is necessary here. This routine simply
2270 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2271 * When those routines are removed the functionality can be merged
2272 * here.
2274 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2276 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2278 switch (cdev->enabled) {
2279 case FCOE_CTLR_ENABLED:
2280 return __bnx2fc_enable(ctlr);
2281 case FCOE_CTLR_DISABLED:
2282 return __bnx2fc_disable(ctlr);
2283 case FCOE_CTLR_UNUSED:
2284 default:
2285 return -ENOTSUPP;
2289 enum bnx2fc_create_link_state {
2290 BNX2FC_CREATE_LINK_DOWN,
2291 BNX2FC_CREATE_LINK_UP,
2295 * _bnx2fc_create() - Create bnx2fc FCoE interface
2296 * @netdev : The net_device object the Ethernet interface to create on
2297 * @fip_mode: The FIP mode for this creation
2298 * @link_state: The ctlr link state on creation
2300 * Called from either the libfcoe 'create' module parameter
2301 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2303 * libfcoe's 'create' module parameter is deprecated so some
2304 * consolidation of code can be done when that interface is
2305 * removed.
2307 * Returns: 0 for success
2309 static int _bnx2fc_create(struct net_device *netdev,
2310 enum fip_mode fip_mode,
2311 enum bnx2fc_create_link_state link_state)
2313 struct fcoe_ctlr_device *cdev;
2314 struct fcoe_ctlr *ctlr;
2315 struct bnx2fc_interface *interface;
2316 struct bnx2fc_hba *hba;
2317 struct net_device *phys_dev = netdev;
2318 struct fc_lport *lport;
2319 struct ethtool_drvinfo drvinfo;
2320 int rc = 0;
2321 int vlan_id = 0;
2323 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2324 if (fip_mode != FIP_MODE_FABRIC) {
2325 printk(KERN_ERR "fip mode not FABRIC\n");
2326 return -EIO;
2329 rtnl_lock();
2331 mutex_lock(&bnx2fc_dev_lock);
2333 if (!try_module_get(THIS_MODULE)) {
2334 rc = -EINVAL;
2335 goto mod_err;
2338 /* obtain physical netdev */
2339 if (is_vlan_dev(netdev))
2340 phys_dev = vlan_dev_real_dev(netdev);
2342 /* verify if the physical device is a netxtreme2 device */
2343 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2344 memset(&drvinfo, 0, sizeof(drvinfo));
2345 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
2346 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
2347 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2348 rc = -EINVAL;
2349 goto netdev_err;
2351 } else {
2352 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2353 rc = -EINVAL;
2354 goto netdev_err;
2357 /* obtain interface and initialize rest of the structure */
2358 hba = bnx2fc_hba_lookup(phys_dev);
2359 if (!hba) {
2360 rc = -ENODEV;
2361 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2362 goto netdev_err;
2365 if (bnx2fc_interface_lookup(netdev)) {
2366 rc = -EEXIST;
2367 goto netdev_err;
2370 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2371 if (!interface) {
2372 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
2373 rc = -ENOMEM;
2374 goto ifput_err;
2377 if (is_vlan_dev(netdev)) {
2378 vlan_id = vlan_dev_vlan_id(netdev);
2379 interface->vlan_enabled = 1;
2382 ctlr = bnx2fc_to_ctlr(interface);
2383 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2384 interface->vlan_id = vlan_id;
2385 interface->tm_timeout = BNX2FC_TM_TIMEOUT;
2387 interface->timer_work_queue =
2388 create_singlethread_workqueue("bnx2fc_timer_wq");
2389 if (!interface->timer_work_queue) {
2390 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2391 rc = -EINVAL;
2392 goto ifput_err;
2395 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
2396 if (!lport) {
2397 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2398 netdev->name);
2399 rc = -EINVAL;
2400 goto if_create_err;
2403 /* Add interface to if_list */
2404 list_add_tail(&interface->list, &if_list);
2406 lport->boot_time = jiffies;
2408 /* Make this master N_port */
2409 ctlr->lp = lport;
2411 if (link_state == BNX2FC_CREATE_LINK_UP)
2412 cdev->enabled = FCOE_CTLR_ENABLED;
2413 else
2414 cdev->enabled = FCOE_CTLR_DISABLED;
2416 if (link_state == BNX2FC_CREATE_LINK_UP &&
2417 !bnx2fc_link_ok(lport)) {
2418 fcoe_ctlr_link_up(ctlr);
2419 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2420 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2423 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2424 bnx2fc_start_disc(interface);
2426 if (link_state == BNX2FC_CREATE_LINK_UP)
2427 interface->enabled = true;
2430 * Release from kref_init in bnx2fc_interface_setup, on success
2431 * lport should be holding a reference taken in bnx2fc_if_create
2433 bnx2fc_interface_put(interface);
2434 /* put netdev that was held while calling dev_get_by_name */
2435 mutex_unlock(&bnx2fc_dev_lock);
2436 rtnl_unlock();
2437 return 0;
2439 if_create_err:
2440 destroy_workqueue(interface->timer_work_queue);
2441 ifput_err:
2442 bnx2fc_net_cleanup(interface);
2443 bnx2fc_interface_put(interface);
2444 goto mod_err;
2445 netdev_err:
2446 module_put(THIS_MODULE);
2447 mod_err:
2448 mutex_unlock(&bnx2fc_dev_lock);
2449 rtnl_unlock();
2450 return rc;
2454 * bnx2fc_create() - Create a bnx2fc interface
2455 * @netdev : The net_device object the Ethernet interface to create on
2456 * @fip_mode: The FIP mode for this creation
2458 * Called from fcoe transport
2460 * Returns: 0 for success
2462 static int bnx2fc_create(struct net_device *netdev, enum fip_mode fip_mode)
2464 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2468 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2469 * @netdev: The net_device to be used by the allocated FCoE Controller
2471 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2472 * in a link_down state. The allows the user an opportunity to configure
2473 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2475 * Creating in with this routine starts the FCoE Controller in Fabric
2476 * mode. The user can change to VN2VN or another mode before enabling.
2478 static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2480 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2481 BNX2FC_CREATE_LINK_DOWN);
2485 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2487 * @cnic: Pointer to cnic device instance
2490 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2492 struct bnx2fc_hba *hba;
2494 /* Called with bnx2fc_dev_lock held */
2495 list_for_each_entry(hba, &adapter_list, list) {
2496 if (hba->cnic == cnic)
2497 return hba;
2499 return NULL;
2502 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2503 *netdev)
2505 struct bnx2fc_interface *interface;
2507 /* Called with bnx2fc_dev_lock held */
2508 list_for_each_entry(interface, &if_list, list) {
2509 if (interface->netdev == netdev)
2510 return interface;
2512 return NULL;
2515 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2516 *phys_dev)
2518 struct bnx2fc_hba *hba;
2520 /* Called with bnx2fc_dev_lock held */
2521 list_for_each_entry(hba, &adapter_list, list) {
2522 if (hba->phys_dev == phys_dev)
2523 return hba;
2525 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2526 return NULL;
2530 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2532 * @dev cnic device handle
2534 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2536 struct bnx2fc_hba *hba;
2537 struct bnx2fc_interface *interface, *tmp;
2539 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2541 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2542 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2543 dev->netdev->name, dev->flags);
2544 return;
2547 mutex_lock(&bnx2fc_dev_lock);
2548 hba = bnx2fc_find_hba_for_cnic(dev);
2549 if (!hba) {
2550 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2551 dev);
2552 mutex_unlock(&bnx2fc_dev_lock);
2553 return;
2556 list_del_init(&hba->list);
2557 adapter_count--;
2559 list_for_each_entry_safe(interface, tmp, &if_list, list)
2560 /* destroy not called yet, move to quiesced list */
2561 if (interface->hba == hba)
2562 __bnx2fc_destroy(interface);
2563 mutex_unlock(&bnx2fc_dev_lock);
2565 /* Ensure ALL destroy work has been completed before return */
2566 flush_workqueue(bnx2fc_wq);
2568 bnx2fc_ulp_stop(hba);
2569 /* unregister cnic device */
2570 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2571 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2572 bnx2fc_hba_destroy(hba);
2575 static void bnx2fc_rport_terminate_io(struct fc_rport *rport)
2577 /* This is a no-op */
2581 * bnx2fc_fcoe_reset - Resets the fcoe
2583 * @shost: shost the reset is from
2585 * Returns: always 0
2587 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2589 struct fc_lport *lport = shost_priv(shost);
2590 fc_lport_reset(lport);
2591 return 0;
2595 static bool bnx2fc_match(struct net_device *netdev)
2597 struct net_device *phys_dev = netdev;
2599 mutex_lock(&bnx2fc_dev_lock);
2600 if (is_vlan_dev(netdev))
2601 phys_dev = vlan_dev_real_dev(netdev);
2603 if (bnx2fc_hba_lookup(phys_dev)) {
2604 mutex_unlock(&bnx2fc_dev_lock);
2605 return true;
2608 mutex_unlock(&bnx2fc_dev_lock);
2609 return false;
2613 static struct fcoe_transport bnx2fc_transport = {
2614 .name = {"bnx2fc"},
2615 .attached = false,
2616 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2617 .alloc = bnx2fc_ctlr_alloc,
2618 .match = bnx2fc_match,
2619 .create = bnx2fc_create,
2620 .destroy = bnx2fc_destroy,
2621 .enable = bnx2fc_enable,
2622 .disable = bnx2fc_disable,
2626 * bnx2fc_cpu_online - Create a receive thread for an online CPU
2628 * @cpu: cpu index for the online cpu
2630 static int bnx2fc_cpu_online(unsigned int cpu)
2632 struct bnx2fc_percpu_s *p;
2633 struct task_struct *thread;
2635 p = &per_cpu(bnx2fc_percpu, cpu);
2637 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2638 (void *)p, cpu_to_node(cpu),
2639 "bnx2fc_thread/%d", cpu);
2640 if (IS_ERR(thread))
2641 return PTR_ERR(thread);
2643 /* bind thread to the cpu */
2644 kthread_bind(thread, cpu);
2645 p->iothread = thread;
2646 wake_up_process(thread);
2647 return 0;
2650 static int bnx2fc_cpu_offline(unsigned int cpu)
2652 struct bnx2fc_percpu_s *p;
2653 struct task_struct *thread;
2654 struct bnx2fc_work *work, *tmp;
2656 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2658 /* Prevent any new work from being queued for this CPU */
2659 p = &per_cpu(bnx2fc_percpu, cpu);
2660 spin_lock_bh(&p->fp_work_lock);
2661 thread = p->iothread;
2662 p->iothread = NULL;
2664 /* Free all work in the list */
2665 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2666 list_del_init(&work->list);
2667 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2668 kfree(work);
2671 spin_unlock_bh(&p->fp_work_lock);
2673 if (thread)
2674 kthread_stop(thread);
2675 return 0;
2678 static int bnx2fc_slave_configure(struct scsi_device *sdev)
2680 if (!bnx2fc_queue_depth)
2681 return 0;
2683 scsi_change_queue_depth(sdev, bnx2fc_queue_depth);
2684 return 0;
2687 static enum cpuhp_state bnx2fc_online_state;
2690 * bnx2fc_mod_init - module init entry point
2692 * Initialize driver wide global data structures, and register
2693 * with cnic module
2695 static int __init bnx2fc_mod_init(void)
2697 struct fcoe_percpu_s *bg;
2698 struct task_struct *l2_thread;
2699 int rc = 0;
2700 unsigned int cpu = 0;
2701 struct bnx2fc_percpu_s *p;
2703 printk(KERN_INFO PFX "%s", version);
2705 /* register as a fcoe transport */
2706 rc = fcoe_transport_attach(&bnx2fc_transport);
2707 if (rc) {
2708 printk(KERN_ERR "failed to register an fcoe transport, check "
2709 "if libfcoe is loaded\n");
2710 goto out;
2713 INIT_LIST_HEAD(&adapter_list);
2714 INIT_LIST_HEAD(&if_list);
2715 mutex_init(&bnx2fc_dev_lock);
2716 adapter_count = 0;
2718 /* Attach FC transport template */
2719 rc = bnx2fc_attach_transport();
2720 if (rc)
2721 goto detach_ft;
2723 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2724 if (!bnx2fc_wq) {
2725 rc = -ENOMEM;
2726 goto release_bt;
2729 bg = &bnx2fc_global;
2730 skb_queue_head_init(&bg->fcoe_rx_list);
2731 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2732 (void *)bg,
2733 "bnx2fc_l2_thread");
2734 if (IS_ERR(l2_thread)) {
2735 rc = PTR_ERR(l2_thread);
2736 goto free_wq;
2738 wake_up_process(l2_thread);
2739 spin_lock_bh(&bg->fcoe_rx_list.lock);
2740 bg->kthread = l2_thread;
2741 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2743 for_each_possible_cpu(cpu) {
2744 p = &per_cpu(bnx2fc_percpu, cpu);
2745 INIT_LIST_HEAD(&p->work_list);
2746 spin_lock_init(&p->fp_work_lock);
2749 rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/bnx2fc:online",
2750 bnx2fc_cpu_online, bnx2fc_cpu_offline);
2751 if (rc < 0)
2752 goto stop_thread;
2753 bnx2fc_online_state = rc;
2755 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2756 return 0;
2758 stop_thread:
2759 kthread_stop(l2_thread);
2760 free_wq:
2761 destroy_workqueue(bnx2fc_wq);
2762 release_bt:
2763 bnx2fc_release_transport();
2764 detach_ft:
2765 fcoe_transport_detach(&bnx2fc_transport);
2766 out:
2767 return rc;
2770 static void __exit bnx2fc_mod_exit(void)
2772 LIST_HEAD(to_be_deleted);
2773 struct bnx2fc_hba *hba, *next;
2774 struct fcoe_percpu_s *bg;
2775 struct task_struct *l2_thread;
2776 struct sk_buff *skb;
2779 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2780 * it will have higher precedence than bnx2fc_dev_lock.
2781 * unregister_device() cannot be called with bnx2fc_dev_lock
2782 * held.
2784 mutex_lock(&bnx2fc_dev_lock);
2785 list_splice_init(&adapter_list, &to_be_deleted);
2786 adapter_count = 0;
2787 mutex_unlock(&bnx2fc_dev_lock);
2789 /* Unregister with cnic */
2790 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2791 list_del_init(&hba->list);
2792 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2793 hba);
2794 bnx2fc_ulp_stop(hba);
2795 /* unregister cnic device */
2796 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2797 &hba->reg_with_cnic))
2798 hba->cnic->unregister_device(hba->cnic,
2799 CNIC_ULP_FCOE);
2800 bnx2fc_hba_destroy(hba);
2802 cnic_unregister_driver(CNIC_ULP_FCOE);
2804 /* Destroy global thread */
2805 bg = &bnx2fc_global;
2806 spin_lock_bh(&bg->fcoe_rx_list.lock);
2807 l2_thread = bg->kthread;
2808 bg->kthread = NULL;
2809 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2810 kfree_skb(skb);
2812 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2814 if (l2_thread)
2815 kthread_stop(l2_thread);
2817 cpuhp_remove_state(bnx2fc_online_state);
2819 destroy_workqueue(bnx2fc_wq);
2821 * detach from scsi transport
2822 * must happen after all destroys are done
2824 bnx2fc_release_transport();
2826 /* detach from fcoe transport */
2827 fcoe_transport_detach(&bnx2fc_transport);
2830 module_init(bnx2fc_mod_init);
2831 module_exit(bnx2fc_mod_exit);
2833 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
2834 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
2835 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2836 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2837 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2838 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2839 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2840 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
2842 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2843 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2846 static struct fc_function_template bnx2fc_transport_function = {
2847 .show_host_node_name = 1,
2848 .show_host_port_name = 1,
2849 .show_host_supported_classes = 1,
2850 .show_host_supported_fc4s = 1,
2851 .show_host_active_fc4s = 1,
2852 .show_host_maxframe_size = 1,
2854 .show_host_port_id = 1,
2855 .show_host_supported_speeds = 1,
2856 .get_host_speed = fc_get_host_speed,
2857 .show_host_speed = 1,
2858 .show_host_port_type = 1,
2859 .get_host_port_state = fc_get_host_port_state,
2860 .show_host_port_state = 1,
2861 .show_host_symbolic_name = 1,
2863 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2864 sizeof(struct bnx2fc_rport)),
2865 .show_rport_maxframe_size = 1,
2866 .show_rport_supported_classes = 1,
2868 .show_host_fabric_name = 1,
2869 .show_starget_node_name = 1,
2870 .show_starget_port_name = 1,
2871 .show_starget_port_id = 1,
2872 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2873 .show_rport_dev_loss_tmo = 1,
2874 .get_fc_host_stats = bnx2fc_get_host_stats,
2876 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2878 .terminate_rport_io = bnx2fc_rport_terminate_io,
2880 .vport_create = bnx2fc_vport_create,
2881 .vport_delete = bnx2fc_vport_destroy,
2882 .vport_disable = bnx2fc_vport_disable,
2883 .bsg_request = fc_lport_bsg_request,
2886 static struct fc_function_template bnx2fc_vport_xport_function = {
2887 .show_host_node_name = 1,
2888 .show_host_port_name = 1,
2889 .show_host_supported_classes = 1,
2890 .show_host_supported_fc4s = 1,
2891 .show_host_active_fc4s = 1,
2892 .show_host_maxframe_size = 1,
2894 .show_host_port_id = 1,
2895 .show_host_supported_speeds = 1,
2896 .get_host_speed = fc_get_host_speed,
2897 .show_host_speed = 1,
2898 .show_host_port_type = 1,
2899 .get_host_port_state = fc_get_host_port_state,
2900 .show_host_port_state = 1,
2901 .show_host_symbolic_name = 1,
2903 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2904 sizeof(struct bnx2fc_rport)),
2905 .show_rport_maxframe_size = 1,
2906 .show_rport_supported_classes = 1,
2908 .show_host_fabric_name = 1,
2909 .show_starget_node_name = 1,
2910 .show_starget_port_name = 1,
2911 .show_starget_port_id = 1,
2912 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2913 .show_rport_dev_loss_tmo = 1,
2914 .get_fc_host_stats = fc_get_host_stats,
2915 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2916 .terminate_rport_io = fc_rport_terminate_io,
2917 .bsg_request = fc_lport_bsg_request,
2921 * Additional scsi_host attributes.
2923 static ssize_t
2924 bnx2fc_tm_timeout_show(struct device *dev, struct device_attribute *attr,
2925 char *buf)
2927 struct Scsi_Host *shost = class_to_shost(dev);
2928 struct fc_lport *lport = shost_priv(shost);
2929 struct fcoe_port *port = lport_priv(lport);
2930 struct bnx2fc_interface *interface = port->priv;
2932 sprintf(buf, "%u\n", interface->tm_timeout);
2933 return strlen(buf);
2936 static ssize_t
2937 bnx2fc_tm_timeout_store(struct device *dev,
2938 struct device_attribute *attr, const char *buf, size_t count)
2940 struct Scsi_Host *shost = class_to_shost(dev);
2941 struct fc_lport *lport = shost_priv(shost);
2942 struct fcoe_port *port = lport_priv(lport);
2943 struct bnx2fc_interface *interface = port->priv;
2944 int rval, val;
2946 rval = kstrtouint(buf, 10, &val);
2947 if (rval)
2948 return rval;
2949 if (val > 255)
2950 return -ERANGE;
2952 interface->tm_timeout = (u8)val;
2953 return strlen(buf);
2956 static DEVICE_ATTR(tm_timeout, S_IRUGO|S_IWUSR, bnx2fc_tm_timeout_show,
2957 bnx2fc_tm_timeout_store);
2959 static struct device_attribute *bnx2fc_host_attrs[] = {
2960 &dev_attr_tm_timeout,
2961 NULL,
2965 * scsi_host_template structure used while registering with SCSI-ml
2967 static struct scsi_host_template bnx2fc_shost_template = {
2968 .module = THIS_MODULE,
2969 .name = "QLogic Offload FCoE Initiator",
2970 .queuecommand = bnx2fc_queuecommand,
2971 .eh_timed_out = fc_eh_timed_out,
2972 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2973 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2974 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2975 .eh_host_reset_handler = fc_eh_host_reset,
2976 .slave_alloc = fc_slave_alloc,
2977 .change_queue_depth = scsi_change_queue_depth,
2978 .this_id = -1,
2979 .cmd_per_lun = 3,
2980 .use_clustering = ENABLE_CLUSTERING,
2981 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2982 .max_sectors = 1024,
2983 .track_queue_depth = 1,
2984 .slave_configure = bnx2fc_slave_configure,
2985 .shost_attrs = bnx2fc_host_attrs,
2988 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2989 .frame_send = bnx2fc_xmit,
2990 .elsct_send = bnx2fc_elsct_send,
2991 .fcp_abort_io = bnx2fc_abort_io,
2992 .fcp_cleanup = bnx2fc_cleanup,
2993 .get_lesb = fcoe_get_lesb,
2994 .rport_event_callback = bnx2fc_rport_event_handler,
2998 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2999 * structure carrying callback function pointers
3001 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
3002 .owner = THIS_MODULE,
3003 .cnic_init = bnx2fc_ulp_init,
3004 .cnic_exit = bnx2fc_ulp_exit,
3005 .cnic_start = bnx2fc_ulp_start,
3006 .cnic_stop = bnx2fc_ulp_stop,
3007 .indicate_kcqes = bnx2fc_indicate_kcqe,
3008 .indicate_netevent = bnx2fc_indicate_netevent,
3009 .cnic_get_stats = bnx2fc_ulp_get_stats,