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. 5 * 6 * Copyright (c) 2008-2013 Broadcom Corporation 7 * Copyright (c) 2014-2016 QLogic Corporation 8 * Copyright (c) 2016-2017 Cavium Inc. 9 * 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. 13 * 14 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com) 15 */ 16 17 #include "bnx2fc.h" 18 19 #include <linux/ethtool.h> 20 #include <net/netdev_lock.h> 21 22 static struct list_head adapter_list; 23 static struct list_head if_list; 24 static u32 adapter_count; 25 static DEFINE_MUTEX(bnx2fc_dev_lock); 26 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu); 27 28 #define DRV_MODULE_NAME "bnx2fc" 29 #define DRV_MODULE_VERSION BNX2FC_VERSION 30 #define DRV_MODULE_RELDATE "October 15, 2015" 31 32 33 static char version[] = 34 "QLogic FCoE Driver " DRV_MODULE_NAME \ 35 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n"; 36 37 38 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>"); 39 MODULE_DESCRIPTION("QLogic FCoE Driver"); 40 MODULE_LICENSE("GPL"); 41 MODULE_VERSION(DRV_MODULE_VERSION); 42 43 #define BNX2FC_MAX_QUEUE_DEPTH 256 44 #define BNX2FC_MIN_QUEUE_DEPTH 32 45 #define FCOE_WORD_TO_BYTE 4 46 47 static struct scsi_transport_template *bnx2fc_transport_template; 48 static struct scsi_transport_template *bnx2fc_vport_xport_template; 49 50 struct workqueue_struct *bnx2fc_wq; 51 52 /* bnx2fc structure needs only one instance of the fcoe_percpu_s structure. 53 * Here the io threads are per cpu but the l2 thread is just one 54 */ 55 struct fcoe_percpu_s bnx2fc_global; 56 static DEFINE_SPINLOCK(bnx2fc_global_lock); 57 58 static struct cnic_ulp_ops bnx2fc_cnic_cb; 59 static struct libfc_function_template bnx2fc_libfc_fcn_templ; 60 static struct scsi_host_template bnx2fc_shost_template; 61 static struct fc_function_template bnx2fc_transport_function; 62 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ; 63 static struct fc_function_template bnx2fc_vport_xport_function; 64 static int bnx2fc_create(struct net_device *netdev, enum fip_mode fip_mode); 65 static void __bnx2fc_destroy(struct bnx2fc_interface *interface); 66 static int bnx2fc_destroy(struct net_device *net_device); 67 static int bnx2fc_enable(struct net_device *netdev); 68 static int bnx2fc_disable(struct net_device *netdev); 69 70 /* fcoe_syfs control interface handlers */ 71 static int bnx2fc_ctlr_alloc(struct net_device *netdev); 72 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev); 73 74 static void bnx2fc_recv_frame(struct sk_buff *skb); 75 76 static void bnx2fc_start_disc(struct bnx2fc_interface *interface); 77 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev); 78 static int bnx2fc_lport_config(struct fc_lport *lport); 79 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba); 80 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba); 81 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba); 82 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba); 83 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba); 84 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface, 85 struct device *parent, int npiv); 86 static void bnx2fc_port_destroy(struct fcoe_port *port); 87 88 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev); 89 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device 90 *phys_dev); 91 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface); 92 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic); 93 94 static int bnx2fc_fw_init(struct bnx2fc_hba *hba); 95 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba); 96 97 static void bnx2fc_port_shutdown(struct fc_lport *lport); 98 static void bnx2fc_stop(struct bnx2fc_interface *interface); 99 static int __init bnx2fc_mod_init(void); 100 static void __exit bnx2fc_mod_exit(void); 101 102 unsigned int bnx2fc_debug_level; 103 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR); 104 MODULE_PARM_DESC(debug_logging, 105 "Option to enable extended logging,\n" 106 "\t\tDefault is 0 - no logging.\n" 107 "\t\t0x01 - SCSI cmd error, cleanup.\n" 108 "\t\t0x02 - Session setup, cleanup, etc.\n" 109 "\t\t0x04 - lport events, link, mtu, etc.\n" 110 "\t\t0x08 - ELS logs.\n" 111 "\t\t0x10 - fcoe L2 fame related logs.\n" 112 "\t\t0xff - LOG all messages."); 113 114 static uint bnx2fc_devloss_tmo; 115 module_param_named(devloss_tmo, bnx2fc_devloss_tmo, uint, S_IRUGO); 116 MODULE_PARM_DESC(devloss_tmo, " Change devloss_tmo for the remote ports " 117 "attached via bnx2fc."); 118 119 static uint bnx2fc_max_luns = BNX2FC_MAX_LUN; 120 module_param_named(max_luns, bnx2fc_max_luns, uint, S_IRUGO); 121 MODULE_PARM_DESC(max_luns, " Change the default max_lun per SCSI host. Default " 122 "0xffff."); 123 124 static uint bnx2fc_queue_depth; 125 module_param_named(queue_depth, bnx2fc_queue_depth, uint, S_IRUGO); 126 MODULE_PARM_DESC(queue_depth, " Change the default queue depth of SCSI devices " 127 "attached via bnx2fc."); 128 129 static uint bnx2fc_log_fka; 130 module_param_named(log_fka, bnx2fc_log_fka, uint, S_IRUGO|S_IWUSR); 131 MODULE_PARM_DESC(log_fka, " Print message to kernel log when fcoe is " 132 "initiating a FIP keep alive when debug logging is enabled."); 133 134 static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport) 135 { 136 return ((struct bnx2fc_interface *) 137 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev; 138 } 139 140 static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev) 141 { 142 struct fcoe_ctlr_device *ctlr_dev = 143 fcoe_fcf_dev_to_ctlr_dev(fcf_dev); 144 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev); 145 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr); 146 147 fcf_dev->vlan_id = fcoe->vlan_id; 148 } 149 150 static void bnx2fc_clean_rx_queue(struct fc_lport *lp) 151 { 152 struct fcoe_percpu_s *bg; 153 struct fcoe_rcv_info *fr; 154 struct sk_buff_head *list; 155 struct sk_buff *skb, *next; 156 157 bg = &bnx2fc_global; 158 spin_lock_bh(&bg->fcoe_rx_list.lock); 159 list = &bg->fcoe_rx_list; 160 skb_queue_walk_safe(list, skb, next) { 161 fr = fcoe_dev_from_skb(skb); 162 if (fr->fr_dev == lp) { 163 __skb_unlink(skb, list); 164 kfree_skb(skb); 165 } 166 } 167 spin_unlock_bh(&bg->fcoe_rx_list.lock); 168 } 169 170 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen) 171 { 172 int rc; 173 spin_lock(&bnx2fc_global_lock); 174 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global); 175 spin_unlock(&bnx2fc_global_lock); 176 177 return rc; 178 } 179 180 static void bnx2fc_abort_io(struct fc_lport *lport) 181 { 182 /* 183 * This function is no-op for bnx2fc, but we do 184 * not want to leave it as NULL either, as libfc 185 * can call the default function which is 186 * fc_fcp_abort_io. 187 */ 188 } 189 190 static void bnx2fc_cleanup(struct fc_lport *lport) 191 { 192 struct fcoe_port *port = lport_priv(lport); 193 struct bnx2fc_interface *interface = port->priv; 194 struct bnx2fc_hba *hba = interface->hba; 195 struct bnx2fc_rport *tgt; 196 int i; 197 198 BNX2FC_MISC_DBG("Entered %s\n", __func__); 199 mutex_lock(&hba->hba_mutex); 200 spin_lock_bh(&hba->hba_lock); 201 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) { 202 tgt = hba->tgt_ofld_list[i]; 203 if (tgt) { 204 /* Cleanup IOs belonging to requested vport */ 205 if (tgt->port == port) { 206 spin_unlock_bh(&hba->hba_lock); 207 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n"); 208 bnx2fc_flush_active_ios(tgt); 209 spin_lock_bh(&hba->hba_lock); 210 } 211 } 212 } 213 spin_unlock_bh(&hba->hba_lock); 214 mutex_unlock(&hba->hba_mutex); 215 } 216 217 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt, 218 struct fc_frame *fp) 219 { 220 struct fc_rport_priv *rdata = tgt->rdata; 221 struct fc_frame_header *fh; 222 int rc = 0; 223 224 fh = fc_frame_header_get(fp); 225 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, " 226 "r_ctl = 0x%x\n", rdata->ids.port_id, 227 ntohs(fh->fh_ox_id), fh->fh_r_ctl); 228 if ((fh->fh_type == FC_TYPE_ELS) && 229 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) { 230 231 switch (fc_frame_payload_op(fp)) { 232 case ELS_ADISC: 233 rc = bnx2fc_send_adisc(tgt, fp); 234 break; 235 case ELS_LOGO: 236 rc = bnx2fc_send_logo(tgt, fp); 237 break; 238 case ELS_RLS: 239 rc = bnx2fc_send_rls(tgt, fp); 240 break; 241 default: 242 break; 243 } 244 } else if ((fh->fh_type == FC_TYPE_BLS) && 245 (fh->fh_r_ctl == FC_RCTL_BA_ABTS)) 246 BNX2FC_TGT_DBG(tgt, "ABTS frame\n"); 247 else { 248 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x " 249 "rctl 0x%x thru non-offload path\n", 250 fh->fh_type, fh->fh_r_ctl); 251 return -ENODEV; 252 } 253 if (rc) 254 return -ENOMEM; 255 else 256 return 0; 257 } 258 259 /** 260 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function 261 * 262 * @lport: the associated local port 263 * @fp: the fc_frame to be transmitted 264 */ 265 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp) 266 { 267 struct ethhdr *eh; 268 struct fcoe_crc_eof *cp; 269 struct sk_buff *skb; 270 struct fc_frame_header *fh; 271 struct bnx2fc_interface *interface; 272 struct fcoe_ctlr *ctlr; 273 struct bnx2fc_hba *hba; 274 struct fcoe_port *port; 275 struct fcoe_hdr *hp; 276 struct bnx2fc_rport *tgt; 277 u8 sof, eof; 278 u32 crc; 279 unsigned int hlen, tlen, elen; 280 int wlen, rc = 0; 281 282 port = (struct fcoe_port *)lport_priv(lport); 283 interface = port->priv; 284 ctlr = bnx2fc_to_ctlr(interface); 285 hba = interface->hba; 286 287 fh = fc_frame_header_get(fp); 288 289 skb = fp_skb(fp); 290 if (!lport->link_up) { 291 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n"); 292 kfree_skb(skb); 293 return 0; 294 } 295 296 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) { 297 if (!ctlr->sel_fcf) { 298 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n"); 299 kfree_skb(skb); 300 return -EINVAL; 301 } 302 if (fcoe_ctlr_els_send(ctlr, lport, skb)) 303 return 0; 304 } 305 306 sof = fr_sof(fp); 307 eof = fr_eof(fp); 308 309 /* 310 * Snoop the frame header to check if the frame is for 311 * an offloaded session 312 */ 313 /* 314 * tgt_ofld_list access is synchronized using 315 * both hba mutex and hba lock. Atleast hba mutex or 316 * hba lock needs to be held for read access. 317 */ 318 319 spin_lock_bh(&hba->hba_lock); 320 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id)); 321 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) { 322 /* This frame is for offloaded session */ 323 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session " 324 "port_id = 0x%x\n", ntoh24(fh->fh_d_id)); 325 spin_unlock_bh(&hba->hba_lock); 326 rc = bnx2fc_xmit_l2_frame(tgt, fp); 327 if (rc != -ENODEV) { 328 kfree_skb(skb); 329 return rc; 330 } 331 } else { 332 spin_unlock_bh(&hba->hba_lock); 333 } 334 335 elen = sizeof(struct ethhdr); 336 hlen = sizeof(struct fcoe_hdr); 337 tlen = sizeof(struct fcoe_crc_eof); 338 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE; 339 340 skb->ip_summed = CHECKSUM_NONE; 341 crc = fcoe_fc_crc(fp); 342 343 /* copy port crc and eof to the skb buff */ 344 if (skb_is_nonlinear(skb)) { 345 skb_frag_t *frag; 346 if (bnx2fc_get_paged_crc_eof(skb, tlen)) { 347 kfree_skb(skb); 348 return -ENOMEM; 349 } 350 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1]; 351 cp = kmap_atomic(skb_frag_page(frag)) + skb_frag_off(frag); 352 } else { 353 cp = skb_put(skb, tlen); 354 } 355 356 memset(cp, 0, sizeof(*cp)); 357 cp->fcoe_eof = eof; 358 cp->fcoe_crc32 = cpu_to_le32(~crc); 359 if (skb_is_nonlinear(skb)) { 360 kunmap_atomic(cp); 361 cp = NULL; 362 } 363 364 /* adjust skb network/transport offsets to match mac/fcoe/port */ 365 skb_push(skb, elen + hlen); 366 skb_reset_mac_header(skb); 367 skb_reset_network_header(skb); 368 skb->mac_len = elen; 369 skb->protocol = htons(ETH_P_FCOE); 370 skb->dev = interface->netdev; 371 372 /* fill up mac and fcoe headers */ 373 eh = eth_hdr(skb); 374 eh->h_proto = htons(ETH_P_FCOE); 375 if (ctlr->map_dest) 376 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id); 377 else 378 /* insert GW address */ 379 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN); 380 381 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN)) 382 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN); 383 else 384 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN); 385 386 hp = (struct fcoe_hdr *)(eh + 1); 387 memset(hp, 0, sizeof(*hp)); 388 if (FC_FCOE_VER) 389 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER); 390 hp->fcoe_sof = sof; 391 392 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */ 393 if (lport->seq_offload && fr_max_payload(fp)) { 394 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE; 395 skb_shinfo(skb)->gso_size = fr_max_payload(fp); 396 } else { 397 skb_shinfo(skb)->gso_type = 0; 398 skb_shinfo(skb)->gso_size = 0; 399 } 400 401 /*update tx stats */ 402 this_cpu_inc(lport->stats->TxFrames); 403 this_cpu_add(lport->stats->TxWords, wlen); 404 405 /* send down to lld */ 406 fr_dev(fp) = lport; 407 if (port->fcoe_pending_queue.qlen) 408 fcoe_check_wait_queue(lport, skb); 409 else if (fcoe_start_io(skb)) 410 fcoe_check_wait_queue(lport, skb); 411 412 return 0; 413 } 414 415 /** 416 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ 417 * 418 * @skb: the receive socket buffer 419 * @dev: associated net device 420 * @ptype: context 421 * @olddev: last device 422 * 423 * This function receives the packet and builds FC frame and passes it up 424 */ 425 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev, 426 struct packet_type *ptype, struct net_device *olddev) 427 { 428 struct fc_lport *lport; 429 struct bnx2fc_interface *interface; 430 struct fcoe_ctlr *ctlr; 431 struct fcoe_rcv_info *fr; 432 struct fcoe_percpu_s *bg; 433 434 interface = container_of(ptype, struct bnx2fc_interface, 435 fcoe_packet_type); 436 ctlr = bnx2fc_to_ctlr(interface); 437 lport = ctlr->lp; 438 439 if (unlikely(lport == NULL)) { 440 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n"); 441 goto err; 442 } 443 444 skb = skb_share_check(skb, GFP_ATOMIC); 445 if (!skb) 446 return -1; 447 448 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) { 449 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n"); 450 goto err; 451 } 452 453 /* 454 * Check for minimum frame length, and make sure required FCoE 455 * and FC headers are pulled into the linear data area. 456 */ 457 if (unlikely((skb->len < FCOE_MIN_FRAME) || 458 !pskb_may_pull(skb, FCOE_HEADER_LEN))) 459 goto err; 460 461 skb_set_transport_header(skb, sizeof(struct fcoe_hdr)); 462 463 fr = fcoe_dev_from_skb(skb); 464 fr->fr_dev = lport; 465 466 bg = &bnx2fc_global; 467 spin_lock(&bg->fcoe_rx_list.lock); 468 469 __skb_queue_tail(&bg->fcoe_rx_list, skb); 470 if (bg->fcoe_rx_list.qlen == 1) 471 wake_up_process(bg->kthread); 472 473 spin_unlock(&bg->fcoe_rx_list.lock); 474 475 return 0; 476 err: 477 kfree_skb(skb); 478 return -1; 479 } 480 481 static int bnx2fc_l2_rcv_thread(void *arg) 482 { 483 struct fcoe_percpu_s *bg = arg; 484 struct sk_buff *skb; 485 486 set_user_nice(current, MIN_NICE); 487 set_current_state(TASK_INTERRUPTIBLE); 488 while (!kthread_should_stop()) { 489 schedule(); 490 spin_lock_bh(&bg->fcoe_rx_list.lock); 491 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) { 492 spin_unlock_bh(&bg->fcoe_rx_list.lock); 493 bnx2fc_recv_frame(skb); 494 spin_lock_bh(&bg->fcoe_rx_list.lock); 495 } 496 __set_current_state(TASK_INTERRUPTIBLE); 497 spin_unlock_bh(&bg->fcoe_rx_list.lock); 498 } 499 __set_current_state(TASK_RUNNING); 500 return 0; 501 } 502 503 504 static void bnx2fc_recv_frame(struct sk_buff *skb) 505 { 506 u64 crc_err; 507 u32 fr_len, fr_crc; 508 struct fc_lport *lport; 509 struct fcoe_rcv_info *fr; 510 struct fc_frame_header *fh; 511 struct fcoe_crc_eof crc_eof; 512 struct fc_frame *fp; 513 struct fc_lport *vn_port; 514 struct fcoe_port *port, *phys_port; 515 u8 *mac = NULL; 516 u8 *dest_mac = NULL; 517 struct fcoe_hdr *hp; 518 struct bnx2fc_interface *interface; 519 struct fcoe_ctlr *ctlr; 520 521 fr = fcoe_dev_from_skb(skb); 522 lport = fr->fr_dev; 523 if (unlikely(lport == NULL)) { 524 printk(KERN_ERR PFX "Invalid lport struct\n"); 525 kfree_skb(skb); 526 return; 527 } 528 529 if (skb_is_nonlinear(skb)) 530 skb_linearize(skb); 531 mac = eth_hdr(skb)->h_source; 532 dest_mac = eth_hdr(skb)->h_dest; 533 534 /* Pull the header */ 535 hp = (struct fcoe_hdr *) skb_network_header(skb); 536 fh = (struct fc_frame_header *) skb_transport_header(skb); 537 skb_pull(skb, sizeof(struct fcoe_hdr)); 538 fr_len = skb->len - sizeof(struct fcoe_crc_eof); 539 540 this_cpu_inc(lport->stats->RxFrames); 541 this_cpu_add(lport->stats->RxWords, fr_len / FCOE_WORD_TO_BYTE); 542 543 fp = (struct fc_frame *)skb; 544 fc_frame_init(fp); 545 fr_dev(fp) = lport; 546 fr_sof(fp) = hp->fcoe_sof; 547 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) { 548 kfree_skb(skb); 549 return; 550 } 551 fr_eof(fp) = crc_eof.fcoe_eof; 552 fr_crc(fp) = crc_eof.fcoe_crc32; 553 if (pskb_trim(skb, fr_len)) { 554 kfree_skb(skb); 555 return; 556 } 557 558 phys_port = lport_priv(lport); 559 interface = phys_port->priv; 560 ctlr = bnx2fc_to_ctlr(interface); 561 562 fh = fc_frame_header_get(fp); 563 564 if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) { 565 BNX2FC_HBA_DBG(lport, "FC frame d_id mismatch with MAC %pM.\n", 566 dest_mac); 567 kfree_skb(skb); 568 return; 569 } 570 571 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id)); 572 if (vn_port) { 573 port = lport_priv(vn_port); 574 if (!ether_addr_equal(port->data_src_addr, dest_mac)) { 575 BNX2FC_HBA_DBG(lport, "fpma mismatch\n"); 576 kfree_skb(skb); 577 return; 578 } 579 } 580 if (ctlr->state) { 581 if (!ether_addr_equal(mac, ctlr->dest_addr)) { 582 BNX2FC_HBA_DBG(lport, "Wrong source address: mac:%pM dest_addr:%pM.\n", 583 mac, ctlr->dest_addr); 584 kfree_skb(skb); 585 return; 586 } 587 } 588 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && 589 fh->fh_type == FC_TYPE_FCP) { 590 /* Drop FCP data. We dont this in L2 path */ 591 kfree_skb(skb); 592 return; 593 } 594 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ && 595 fh->fh_type == FC_TYPE_ELS) { 596 switch (fc_frame_payload_op(fp)) { 597 case ELS_LOGO: 598 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) { 599 /* drop non-FIP LOGO */ 600 kfree_skb(skb); 601 return; 602 } 603 break; 604 } 605 } 606 607 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) { 608 /* Drop incoming ABTS */ 609 kfree_skb(skb); 610 return; 611 } 612 613 /* 614 * If the destination ID from the frame header does not match what we 615 * have on record for lport and the search for a NPIV port came up 616 * empty then this is not addressed to our port so simply drop it. 617 */ 618 if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) { 619 BNX2FC_HBA_DBG(lport, "Dropping frame due to destination mismatch: lport->port_id=%x fh->d_id=%x.\n", 620 lport->port_id, ntoh24(fh->fh_d_id)); 621 kfree_skb(skb); 622 return; 623 } 624 625 fr_crc = le32_to_cpu(fr_crc(fp)); 626 627 if (unlikely(fr_crc != ~crc32(~0, skb->data, fr_len))) { 628 crc_err = this_cpu_inc_return(lport->stats->InvalidCRCCount); 629 if (crc_err < 5) 630 printk(KERN_WARNING PFX "dropping frame with " 631 "CRC error\n"); 632 kfree_skb(skb); 633 return; 634 } 635 fc_exch_recv(lport, fp); 636 } 637 638 /** 639 * bnx2fc_percpu_io_thread - thread per cpu for ios 640 * 641 * @arg: ptr to bnx2fc_percpu_info structure 642 */ 643 static int bnx2fc_percpu_io_thread(void *arg) 644 { 645 struct bnx2fc_percpu_s *p = arg; 646 struct bnx2fc_work *work, *tmp; 647 LIST_HEAD(work_list); 648 649 set_user_nice(current, MIN_NICE); 650 set_current_state(TASK_INTERRUPTIBLE); 651 while (!kthread_should_stop()) { 652 schedule(); 653 spin_lock_bh(&p->fp_work_lock); 654 while (!list_empty(&p->work_list)) { 655 list_splice_init(&p->work_list, &work_list); 656 spin_unlock_bh(&p->fp_work_lock); 657 658 list_for_each_entry_safe(work, tmp, &work_list, list) { 659 list_del_init(&work->list); 660 bnx2fc_process_cq_compl(work->tgt, work->wqe, 661 work->rq_data, 662 work->num_rq, 663 work->task); 664 kfree(work); 665 } 666 667 spin_lock_bh(&p->fp_work_lock); 668 } 669 __set_current_state(TASK_INTERRUPTIBLE); 670 spin_unlock_bh(&p->fp_work_lock); 671 } 672 __set_current_state(TASK_RUNNING); 673 674 return 0; 675 } 676 677 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost) 678 { 679 struct fc_host_statistics *bnx2fc_stats; 680 struct fc_lport *lport = shost_priv(shost); 681 struct fcoe_port *port = lport_priv(lport); 682 struct bnx2fc_interface *interface = port->priv; 683 struct bnx2fc_hba *hba = interface->hba; 684 struct fcoe_statistics_params *fw_stats; 685 int rc = 0; 686 687 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer; 688 if (!fw_stats) 689 return NULL; 690 691 mutex_lock(&hba->hba_stats_mutex); 692 693 bnx2fc_stats = fc_get_host_stats(shost); 694 695 init_completion(&hba->stat_req_done); 696 if (bnx2fc_send_stat_req(hba)) 697 goto unlock_stats_mutex; 698 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ)); 699 if (!rc) { 700 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n"); 701 goto unlock_stats_mutex; 702 } 703 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt); 704 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt; 705 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt); 706 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt; 707 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt); 708 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4); 709 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt); 710 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt; 711 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt); 712 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4); 713 714 bnx2fc_stats->dumped_frames = 0; 715 bnx2fc_stats->lip_count = 0; 716 bnx2fc_stats->nos_count = 0; 717 bnx2fc_stats->loss_of_sync_count = 0; 718 bnx2fc_stats->loss_of_signal_count = 0; 719 bnx2fc_stats->prim_seq_protocol_err_count = 0; 720 721 memcpy(&hba->prev_stats, hba->stats_buffer, 722 sizeof(struct fcoe_statistics_params)); 723 724 unlock_stats_mutex: 725 mutex_unlock(&hba->hba_stats_mutex); 726 return bnx2fc_stats; 727 } 728 729 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev) 730 { 731 struct fcoe_port *port = lport_priv(lport); 732 struct bnx2fc_interface *interface = port->priv; 733 struct bnx2fc_hba *hba = interface->hba; 734 struct Scsi_Host *shost = lport->host; 735 int rc = 0; 736 737 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN; 738 shost->max_lun = bnx2fc_max_luns; 739 shost->max_id = BNX2FC_MAX_FCP_TGT; 740 shost->max_channel = 0; 741 if (lport->vport) 742 shost->transportt = bnx2fc_vport_xport_template; 743 else 744 shost->transportt = bnx2fc_transport_template; 745 746 /* Add the new host to SCSI-ml */ 747 rc = scsi_add_host(lport->host, dev); 748 if (rc) { 749 printk(KERN_ERR PFX "Error on scsi_add_host\n"); 750 return rc; 751 } 752 if (!lport->vport) 753 fc_host_max_npiv_vports(lport->host) = USHRT_MAX; 754 snprintf(fc_host_symbolic_name(lport->host), 256, 755 "%s (QLogic %s) v%s over %s", 756 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION, 757 interface->netdev->name); 758 759 return 0; 760 } 761 762 static int bnx2fc_link_ok(struct fc_lport *lport) 763 { 764 struct fcoe_port *port = lport_priv(lport); 765 struct bnx2fc_interface *interface = port->priv; 766 struct bnx2fc_hba *hba = interface->hba; 767 struct net_device *dev = hba->phys_dev; 768 int rc = 0; 769 770 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) 771 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state); 772 else { 773 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state); 774 rc = -1; 775 } 776 return rc; 777 } 778 779 /** 780 * bnx2fc_get_link_state - get network link state 781 * 782 * @hba: adapter instance pointer 783 * 784 * updates adapter structure flag based on netdev state 785 */ 786 void bnx2fc_get_link_state(struct bnx2fc_hba *hba) 787 { 788 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state)) 789 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state); 790 else 791 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state); 792 } 793 794 static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev) 795 { 796 struct bnx2fc_hba *hba; 797 struct bnx2fc_interface *interface; 798 struct fcoe_ctlr *ctlr; 799 struct fcoe_port *port; 800 u64 wwnn, wwpn; 801 802 port = lport_priv(lport); 803 interface = port->priv; 804 ctlr = bnx2fc_to_ctlr(interface); 805 hba = interface->hba; 806 807 /* require support for get_pauseparam ethtool op. */ 808 if (!hba->phys_dev->ethtool_ops || 809 !hba->phys_dev->ethtool_ops->get_pauseparam) 810 return -EOPNOTSUPP; 811 812 if (fc_set_mfs(lport, BNX2FC_MFS)) 813 return -EINVAL; 814 815 skb_queue_head_init(&port->fcoe_pending_queue); 816 port->fcoe_pending_queue_active = 0; 817 timer_setup(&port->timer, fcoe_queue_timer, 0); 818 819 netdev_lock_ops(netdev); 820 fcoe_link_speed_update(lport); 821 netdev_unlock_ops(netdev); 822 823 if (!lport->vport) { 824 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN)) 825 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 826 1, 0); 827 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn); 828 fc_set_wwnn(lport, wwnn); 829 830 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN)) 831 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 832 2, 0); 833 834 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn); 835 fc_set_wwpn(lport, wwpn); 836 } 837 838 return 0; 839 } 840 841 static void bnx2fc_destroy_timer(struct timer_list *t) 842 { 843 struct bnx2fc_hba *hba = timer_container_of(hba, t, destroy_timer); 844 845 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - " 846 "Destroy compl not received!!\n"); 847 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags); 848 wake_up_interruptible(&hba->destroy_wait); 849 } 850 851 /** 852 * bnx2fc_indicate_netevent - Generic netdev event handler 853 * 854 * @context: adapter structure pointer 855 * @event: event type 856 * @vlan_id: vlan id - associated vlan id with this event 857 * 858 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and 859 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans. 860 */ 861 static void bnx2fc_indicate_netevent(void *context, unsigned long event, 862 u16 vlan_id) 863 { 864 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context; 865 struct fcoe_ctlr_device *cdev; 866 struct fc_lport *lport; 867 struct fc_lport *vport; 868 struct bnx2fc_interface *interface, *tmp; 869 struct fcoe_ctlr *ctlr; 870 int wait_for_upload = 0; 871 u32 link_possible = 1; 872 873 if (vlan_id != 0 && event != NETDEV_UNREGISTER) 874 return; 875 876 switch (event) { 877 case NETDEV_UP: 878 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) 879 printk(KERN_ERR "indicate_netevent: "\ 880 "hba is not UP!!\n"); 881 break; 882 883 case NETDEV_DOWN: 884 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state); 885 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state); 886 link_possible = 0; 887 break; 888 889 case NETDEV_GOING_DOWN: 890 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state); 891 link_possible = 0; 892 break; 893 894 case NETDEV_CHANGE: 895 break; 896 897 case NETDEV_UNREGISTER: 898 if (!vlan_id) 899 return; 900 mutex_lock(&bnx2fc_dev_lock); 901 list_for_each_entry_safe(interface, tmp, &if_list, list) { 902 if (interface->hba == hba && 903 interface->vlan_id == (vlan_id & VLAN_VID_MASK)) 904 __bnx2fc_destroy(interface); 905 } 906 mutex_unlock(&bnx2fc_dev_lock); 907 return; 908 909 default: 910 return; 911 } 912 913 mutex_lock(&bnx2fc_dev_lock); 914 list_for_each_entry(interface, &if_list, list) { 915 916 if (interface->hba != hba) 917 continue; 918 919 ctlr = bnx2fc_to_ctlr(interface); 920 lport = ctlr->lp; 921 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n", 922 interface->netdev->name, event); 923 924 fcoe_link_speed_update(lport); 925 926 cdev = fcoe_ctlr_to_ctlr_dev(ctlr); 927 928 if (link_possible && !bnx2fc_link_ok(lport)) { 929 switch (cdev->enabled) { 930 case FCOE_CTLR_DISABLED: 931 pr_info("Link up while interface is disabled.\n"); 932 break; 933 case FCOE_CTLR_ENABLED: 934 case FCOE_CTLR_UNUSED: 935 /* Reset max recv frame size to default */ 936 fc_set_mfs(lport, BNX2FC_MFS); 937 /* 938 * ctlr link up will only be handled during 939 * enable to avoid sending discovery 940 * solicitation on a stale vlan 941 */ 942 if (interface->enabled) 943 fcoe_ctlr_link_up(ctlr); 944 } 945 } else if (fcoe_ctlr_link_down(ctlr)) { 946 switch (cdev->enabled) { 947 case FCOE_CTLR_DISABLED: 948 pr_info("Link down while interface is disabled.\n"); 949 break; 950 case FCOE_CTLR_ENABLED: 951 case FCOE_CTLR_UNUSED: 952 mutex_lock(&lport->lp_mutex); 953 list_for_each_entry(vport, &lport->vports, list) 954 fc_host_port_type(vport->host) = 955 FC_PORTTYPE_UNKNOWN; 956 mutex_unlock(&lport->lp_mutex); 957 fc_host_port_type(lport->host) = 958 FC_PORTTYPE_UNKNOWN; 959 this_cpu_inc(lport->stats->LinkFailureCount); 960 fcoe_clean_pending_queue(lport); 961 wait_for_upload = 1; 962 } 963 } 964 } 965 mutex_unlock(&bnx2fc_dev_lock); 966 967 if (wait_for_upload) { 968 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state); 969 init_waitqueue_head(&hba->shutdown_wait); 970 BNX2FC_MISC_DBG("indicate_netevent " 971 "num_ofld_sess = %d\n", 972 hba->num_ofld_sess); 973 hba->wait_for_link_down = 1; 974 wait_event_interruptible(hba->shutdown_wait, 975 (hba->num_ofld_sess == 0)); 976 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n", 977 hba->num_ofld_sess); 978 hba->wait_for_link_down = 0; 979 980 if (signal_pending(current)) 981 flush_signals(current); 982 } 983 } 984 985 static int bnx2fc_libfc_config(struct fc_lport *lport) 986 { 987 988 /* Set the function pointers set by bnx2fc driver */ 989 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ, 990 sizeof(struct libfc_function_template)); 991 fc_elsct_init(lport); 992 fc_exch_init(lport); 993 fc_disc_init(lport); 994 fc_disc_config(lport, lport); 995 return 0; 996 } 997 998 static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba) 999 { 1000 int fcoe_min_xid, fcoe_max_xid; 1001 1002 fcoe_min_xid = hba->max_xid + 1; 1003 if (nr_cpu_ids <= 2) 1004 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET; 1005 else 1006 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET; 1007 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid, 1008 fcoe_max_xid, NULL)) { 1009 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n"); 1010 return -ENOMEM; 1011 } 1012 1013 return 0; 1014 } 1015 1016 static int bnx2fc_lport_config(struct fc_lport *lport) 1017 { 1018 lport->link_up = 0; 1019 lport->qfull = 0; 1020 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT; 1021 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT; 1022 lport->e_d_tov = 2 * 1000; 1023 lport->r_a_tov = 10 * 1000; 1024 1025 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS | 1026 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL); 1027 lport->does_npiv = 1; 1028 1029 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen)); 1030 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA; 1031 1032 /* alloc stats structure */ 1033 if (fc_lport_init_stats(lport)) 1034 return -ENOMEM; 1035 1036 /* Finish fc_lport configuration */ 1037 fc_lport_config(lport); 1038 1039 return 0; 1040 } 1041 1042 /** 1043 * bnx2fc_fip_recv - handle a received FIP frame. 1044 * 1045 * @skb: the received skb 1046 * @dev: associated &net_device 1047 * @ptype: the &packet_type structure which was used to register this handler. 1048 * @orig_dev: original receive &net_device, in case @ dev is a bond. 1049 * 1050 * Returns: 0 for success 1051 */ 1052 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev, 1053 struct packet_type *ptype, 1054 struct net_device *orig_dev) 1055 { 1056 struct bnx2fc_interface *interface; 1057 struct fcoe_ctlr *ctlr; 1058 interface = container_of(ptype, struct bnx2fc_interface, 1059 fip_packet_type); 1060 ctlr = bnx2fc_to_ctlr(interface); 1061 fcoe_ctlr_recv(ctlr, skb); 1062 return 0; 1063 } 1064 1065 /** 1066 * bnx2fc_update_src_mac - Update Ethernet MAC filters. 1067 * 1068 * @lport: The local port 1069 * @addr: Location of data to copy 1070 * 1071 * Remove any previously-set unicast MAC filter. 1072 * Add secondary FCoE MAC address filter for our OUI. 1073 */ 1074 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr) 1075 { 1076 struct fcoe_port *port = lport_priv(lport); 1077 1078 memcpy(port->data_src_addr, addr, ETH_ALEN); 1079 } 1080 1081 /** 1082 * bnx2fc_get_src_mac - return the ethernet source address for an lport 1083 * 1084 * @lport: libfc port 1085 */ 1086 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport) 1087 { 1088 struct fcoe_port *port; 1089 1090 port = (struct fcoe_port *)lport_priv(lport); 1091 return port->data_src_addr; 1092 } 1093 1094 /** 1095 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame. 1096 * 1097 * @fip: FCoE controller. 1098 * @skb: FIP Packet. 1099 */ 1100 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb) 1101 { 1102 struct fip_header *fiph; 1103 struct ethhdr *eth_hdr; 1104 u16 op; 1105 u8 sub; 1106 1107 fiph = (struct fip_header *) ((void *)skb->data + 2 * ETH_ALEN + 2); 1108 eth_hdr = (struct ethhdr *)skb_mac_header(skb); 1109 op = ntohs(fiph->fip_op); 1110 sub = fiph->fip_subcode; 1111 1112 if (op == FIP_OP_CTRL && sub == FIP_SC_SOL && bnx2fc_log_fka) 1113 BNX2FC_MISC_DBG("Sending FKA from %pM to %pM.\n", 1114 eth_hdr->h_source, eth_hdr->h_dest); 1115 1116 skb->dev = bnx2fc_from_ctlr(fip)->netdev; 1117 dev_queue_xmit(skb); 1118 } 1119 1120 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled) 1121 { 1122 struct Scsi_Host *shost = vport_to_shost(vport); 1123 struct fc_lport *n_port = shost_priv(shost); 1124 struct fcoe_port *port = lport_priv(n_port); 1125 struct bnx2fc_interface *interface = port->priv; 1126 struct net_device *netdev = interface->netdev; 1127 struct fc_lport *vn_port; 1128 int rc; 1129 char buf[32]; 1130 1131 rc = fcoe_validate_vport_create(vport); 1132 if (rc) { 1133 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf)); 1134 printk(KERN_ERR PFX "Failed to create vport, " 1135 "WWPN (0x%s) already exists\n", 1136 buf); 1137 return rc; 1138 } 1139 1140 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) { 1141 printk(KERN_ERR PFX "vn ports cannot be created on" 1142 "this interface\n"); 1143 return -EIO; 1144 } 1145 rtnl_lock(); 1146 mutex_lock(&bnx2fc_dev_lock); 1147 vn_port = bnx2fc_if_create(interface, &vport->dev, 1); 1148 mutex_unlock(&bnx2fc_dev_lock); 1149 rtnl_unlock(); 1150 1151 if (!vn_port) { 1152 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n", 1153 netdev->name); 1154 return -EIO; 1155 } 1156 1157 if (bnx2fc_devloss_tmo) 1158 fc_host_dev_loss_tmo(vn_port->host) = bnx2fc_devloss_tmo; 1159 1160 if (disabled) { 1161 fc_vport_set_state(vport, FC_VPORT_DISABLED); 1162 } else { 1163 vn_port->boot_time = jiffies; 1164 fc_lport_init(vn_port); 1165 fc_fabric_login(vn_port); 1166 fc_vport_setlink(vn_port); 1167 } 1168 return 0; 1169 } 1170 1171 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport) 1172 { 1173 struct bnx2fc_lport *blport, *tmp; 1174 1175 spin_lock_bh(&hba->hba_lock); 1176 list_for_each_entry_safe(blport, tmp, &hba->vports, list) { 1177 if (blport->lport == lport) { 1178 list_del(&blport->list); 1179 kfree(blport); 1180 } 1181 } 1182 spin_unlock_bh(&hba->hba_lock); 1183 } 1184 1185 static int bnx2fc_vport_destroy(struct fc_vport *vport) 1186 { 1187 struct Scsi_Host *shost = vport_to_shost(vport); 1188 struct fc_lport *n_port = shost_priv(shost); 1189 struct fc_lport *vn_port = vport->dd_data; 1190 struct fcoe_port *port = lport_priv(vn_port); 1191 struct bnx2fc_interface *interface = port->priv; 1192 struct fc_lport *v_port; 1193 bool found = false; 1194 1195 mutex_lock(&n_port->lp_mutex); 1196 list_for_each_entry(v_port, &n_port->vports, list) 1197 if (v_port->vport == vport) { 1198 found = true; 1199 break; 1200 } 1201 1202 if (!found) { 1203 mutex_unlock(&n_port->lp_mutex); 1204 return -ENOENT; 1205 } 1206 list_del(&vn_port->list); 1207 mutex_unlock(&n_port->lp_mutex); 1208 bnx2fc_free_vport(interface->hba, port->lport); 1209 bnx2fc_port_shutdown(port->lport); 1210 bnx2fc_port_destroy(port); 1211 bnx2fc_interface_put(interface); 1212 return 0; 1213 } 1214 1215 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable) 1216 { 1217 struct fc_lport *lport = vport->dd_data; 1218 1219 if (disable) { 1220 fc_vport_set_state(vport, FC_VPORT_DISABLED); 1221 fc_fabric_logoff(lport); 1222 } else { 1223 lport->boot_time = jiffies; 1224 fc_fabric_login(lport); 1225 fc_vport_setlink(lport); 1226 } 1227 return 0; 1228 } 1229 1230 1231 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface) 1232 { 1233 struct net_device *netdev = interface->netdev; 1234 struct net_device *physdev = interface->hba->phys_dev; 1235 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface); 1236 struct netdev_hw_addr *ha; 1237 int sel_san_mac = 0; 1238 1239 /* setup Source MAC Address */ 1240 rcu_read_lock(); 1241 for_each_dev_addr(physdev, ha) { 1242 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ", 1243 ha->type); 1244 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0], 1245 ha->addr[1], ha->addr[2], ha->addr[3], 1246 ha->addr[4], ha->addr[5]); 1247 1248 if ((ha->type == NETDEV_HW_ADDR_T_SAN) && 1249 (is_valid_ether_addr(ha->addr))) { 1250 memcpy(ctlr->ctl_src_addr, ha->addr, 1251 ETH_ALEN); 1252 sel_san_mac = 1; 1253 BNX2FC_MISC_DBG("Found SAN MAC\n"); 1254 } 1255 } 1256 rcu_read_unlock(); 1257 1258 if (!sel_san_mac) 1259 return -ENODEV; 1260 1261 interface->fip_packet_type.func = bnx2fc_fip_recv; 1262 interface->fip_packet_type.type = htons(ETH_P_FIP); 1263 interface->fip_packet_type.dev = netdev; 1264 dev_add_pack(&interface->fip_packet_type); 1265 1266 interface->fcoe_packet_type.func = bnx2fc_rcv; 1267 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE); 1268 interface->fcoe_packet_type.dev = netdev; 1269 dev_add_pack(&interface->fcoe_packet_type); 1270 1271 return 0; 1272 } 1273 1274 static int bnx2fc_attach_transport(void) 1275 { 1276 bnx2fc_transport_template = 1277 fc_attach_transport(&bnx2fc_transport_function); 1278 1279 if (bnx2fc_transport_template == NULL) { 1280 printk(KERN_ERR PFX "Failed to attach FC transport\n"); 1281 return -ENODEV; 1282 } 1283 1284 bnx2fc_vport_xport_template = 1285 fc_attach_transport(&bnx2fc_vport_xport_function); 1286 if (bnx2fc_vport_xport_template == NULL) { 1287 printk(KERN_ERR PFX 1288 "Failed to attach FC transport for vport\n"); 1289 fc_release_transport(bnx2fc_transport_template); 1290 bnx2fc_transport_template = NULL; 1291 return -ENODEV; 1292 } 1293 return 0; 1294 } 1295 static void bnx2fc_release_transport(void) 1296 { 1297 fc_release_transport(bnx2fc_transport_template); 1298 fc_release_transport(bnx2fc_vport_xport_template); 1299 bnx2fc_transport_template = NULL; 1300 bnx2fc_vport_xport_template = NULL; 1301 } 1302 1303 static void bnx2fc_interface_release(struct kref *kref) 1304 { 1305 struct fcoe_ctlr_device *ctlr_dev; 1306 struct bnx2fc_interface *interface; 1307 struct fcoe_ctlr *ctlr; 1308 struct net_device *netdev; 1309 1310 interface = container_of(kref, struct bnx2fc_interface, kref); 1311 BNX2FC_MISC_DBG("Interface is being released\n"); 1312 1313 ctlr = bnx2fc_to_ctlr(interface); 1314 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr); 1315 netdev = interface->netdev; 1316 1317 /* tear-down FIP controller */ 1318 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags)) 1319 fcoe_ctlr_destroy(ctlr); 1320 1321 fcoe_ctlr_device_delete(ctlr_dev); 1322 1323 dev_put(netdev); 1324 module_put(THIS_MODULE); 1325 } 1326 1327 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface) 1328 { 1329 kref_get(&interface->kref); 1330 } 1331 1332 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface) 1333 { 1334 kref_put(&interface->kref, bnx2fc_interface_release); 1335 } 1336 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba) 1337 { 1338 /* Free the command manager */ 1339 if (hba->cmd_mgr) { 1340 bnx2fc_cmd_mgr_free(hba->cmd_mgr); 1341 hba->cmd_mgr = NULL; 1342 } 1343 kfree(hba->tgt_ofld_list); 1344 bnx2fc_unbind_pcidev(hba); 1345 kfree(hba); 1346 } 1347 1348 /** 1349 * bnx2fc_hba_create - create a new bnx2fc hba 1350 * 1351 * @cnic: pointer to cnic device 1352 * 1353 * Creates a new FCoE hba on the given device. 1354 * 1355 */ 1356 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic) 1357 { 1358 struct bnx2fc_hba *hba; 1359 struct fcoe_capabilities *fcoe_cap; 1360 int rc; 1361 1362 hba = kzalloc_obj(*hba); 1363 if (!hba) { 1364 printk(KERN_ERR PFX "Unable to allocate hba structure\n"); 1365 return NULL; 1366 } 1367 spin_lock_init(&hba->hba_lock); 1368 mutex_init(&hba->hba_mutex); 1369 mutex_init(&hba->hba_stats_mutex); 1370 1371 hba->cnic = cnic; 1372 1373 hba->max_tasks = cnic->max_fcoe_exchanges; 1374 hba->elstm_xids = (hba->max_tasks / 2); 1375 hba->max_outstanding_cmds = hba->elstm_xids; 1376 hba->max_xid = (hba->max_tasks - 1); 1377 1378 rc = bnx2fc_bind_pcidev(hba); 1379 if (rc) { 1380 printk(KERN_ERR PFX "create_adapter: bind error\n"); 1381 goto bind_err; 1382 } 1383 hba->phys_dev = cnic->netdev; 1384 hba->next_conn_id = 0; 1385 1386 hba->tgt_ofld_list = 1387 kzalloc_objs(struct bnx2fc_rport *, BNX2FC_NUM_MAX_SESS); 1388 if (!hba->tgt_ofld_list) { 1389 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n"); 1390 goto tgtofld_err; 1391 } 1392 1393 hba->num_ofld_sess = 0; 1394 1395 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba); 1396 if (!hba->cmd_mgr) { 1397 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n"); 1398 goto cmgr_err; 1399 } 1400 fcoe_cap = &hba->fcoe_cap; 1401 1402 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES << 1403 FCOE_IOS_PER_CONNECTION_SHIFT; 1404 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS << 1405 FCOE_LOGINS_PER_PORT_SHIFT; 1406 fcoe_cap->capability2 = hba->max_outstanding_cmds << 1407 FCOE_NUMBER_OF_EXCHANGES_SHIFT; 1408 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV << 1409 FCOE_NPIV_WWN_PER_PORT_SHIFT; 1410 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS << 1411 FCOE_TARGETS_SUPPORTED_SHIFT; 1412 fcoe_cap->capability3 |= hba->max_outstanding_cmds << 1413 FCOE_OUTSTANDING_COMMANDS_SHIFT; 1414 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL; 1415 1416 init_waitqueue_head(&hba->shutdown_wait); 1417 init_waitqueue_head(&hba->destroy_wait); 1418 INIT_LIST_HEAD(&hba->vports); 1419 1420 return hba; 1421 1422 cmgr_err: 1423 kfree(hba->tgt_ofld_list); 1424 tgtofld_err: 1425 bnx2fc_unbind_pcidev(hba); 1426 bind_err: 1427 kfree(hba); 1428 return NULL; 1429 } 1430 1431 static struct bnx2fc_interface * 1432 bnx2fc_interface_create(struct bnx2fc_hba *hba, 1433 struct net_device *netdev, 1434 enum fip_mode fip_mode) 1435 { 1436 struct fcoe_ctlr_device *ctlr_dev; 1437 struct bnx2fc_interface *interface; 1438 struct fcoe_ctlr *ctlr; 1439 int size; 1440 int rc = 0; 1441 1442 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr)); 1443 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ, 1444 size); 1445 if (!ctlr_dev) { 1446 printk(KERN_ERR PFX "Unable to allocate interface structure\n"); 1447 return NULL; 1448 } 1449 ctlr = fcoe_ctlr_device_priv(ctlr_dev); 1450 ctlr->cdev = ctlr_dev; 1451 interface = fcoe_ctlr_priv(ctlr); 1452 dev_hold(netdev); 1453 kref_init(&interface->kref); 1454 interface->hba = hba; 1455 interface->netdev = netdev; 1456 1457 /* Initialize FIP */ 1458 fcoe_ctlr_init(ctlr, fip_mode); 1459 ctlr->send = bnx2fc_fip_send; 1460 ctlr->update_mac = bnx2fc_update_src_mac; 1461 ctlr->get_src_addr = bnx2fc_get_src_mac; 1462 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags); 1463 1464 rc = bnx2fc_interface_setup(interface); 1465 if (!rc) 1466 return interface; 1467 1468 fcoe_ctlr_destroy(ctlr); 1469 dev_put(netdev); 1470 fcoe_ctlr_device_delete(ctlr_dev); 1471 return NULL; 1472 } 1473 1474 /** 1475 * bnx2fc_if_create - Create FCoE instance on a given interface 1476 * 1477 * @interface: FCoE interface to create a local port on 1478 * @parent: Device pointer to be the parent in sysfs for the SCSI host 1479 * @npiv: Indicates if the port is vport or not 1480 * 1481 * Creates a fc_lport instance and a Scsi_Host instance and configure them. 1482 * 1483 * Returns: Allocated fc_lport or an error pointer 1484 */ 1485 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface, 1486 struct device *parent, int npiv) 1487 { 1488 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface); 1489 struct fc_lport *lport, *n_port; 1490 struct fcoe_port *port; 1491 struct Scsi_Host *shost; 1492 struct fc_vport *vport = dev_to_vport(parent); 1493 struct bnx2fc_lport *blport; 1494 struct bnx2fc_hba *hba = interface->hba; 1495 int rc = 0; 1496 1497 blport = kzalloc_obj(struct bnx2fc_lport); 1498 if (!blport) { 1499 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n"); 1500 return NULL; 1501 } 1502 1503 /* Allocate Scsi_Host structure */ 1504 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds; 1505 if (!npiv) 1506 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port)); 1507 else 1508 lport = libfc_vport_create(vport, sizeof(*port)); 1509 1510 if (!lport) { 1511 printk(KERN_ERR PFX "could not allocate scsi host structure\n"); 1512 goto free_blport; 1513 } 1514 shost = lport->host; 1515 port = lport_priv(lport); 1516 port->lport = lport; 1517 port->priv = interface; 1518 port->get_netdev = bnx2fc_netdev; 1519 1520 /* Configure fcoe_port */ 1521 rc = bnx2fc_lport_config(lport); 1522 if (rc) 1523 goto lp_config_err; 1524 1525 if (npiv) { 1526 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n", 1527 vport->node_name, vport->port_name); 1528 fc_set_wwnn(lport, vport->node_name); 1529 fc_set_wwpn(lport, vport->port_name); 1530 } 1531 /* Configure netdev and networking properties of the lport */ 1532 rc = bnx2fc_net_config(lport, interface->netdev); 1533 if (rc) { 1534 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n"); 1535 goto lp_config_err; 1536 } 1537 1538 rc = bnx2fc_shost_config(lport, parent); 1539 if (rc) { 1540 printk(KERN_ERR PFX "Couldn't configure shost for %s\n", 1541 interface->netdev->name); 1542 goto lp_config_err; 1543 } 1544 1545 /* Initialize the libfc library */ 1546 rc = bnx2fc_libfc_config(lport); 1547 if (rc) { 1548 printk(KERN_ERR PFX "Couldn't configure libfc\n"); 1549 goto shost_err; 1550 } 1551 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN; 1552 1553 if (bnx2fc_devloss_tmo) 1554 fc_host_dev_loss_tmo(shost) = bnx2fc_devloss_tmo; 1555 1556 /* Allocate exchange manager */ 1557 if (!npiv) 1558 rc = bnx2fc_em_config(lport, hba); 1559 else { 1560 shost = vport_to_shost(vport); 1561 n_port = shost_priv(shost); 1562 rc = fc_exch_mgr_list_clone(n_port, lport); 1563 } 1564 1565 if (rc) { 1566 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n"); 1567 goto shost_err; 1568 } 1569 1570 bnx2fc_interface_get(interface); 1571 1572 spin_lock_bh(&hba->hba_lock); 1573 blport->lport = lport; 1574 list_add_tail(&blport->list, &hba->vports); 1575 spin_unlock_bh(&hba->hba_lock); 1576 1577 return lport; 1578 1579 shost_err: 1580 scsi_remove_host(shost); 1581 lp_config_err: 1582 scsi_host_put(lport->host); 1583 free_blport: 1584 kfree(blport); 1585 return NULL; 1586 } 1587 1588 static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface) 1589 { 1590 /* Dont listen for Ethernet packets anymore */ 1591 __dev_remove_pack(&interface->fcoe_packet_type); 1592 __dev_remove_pack(&interface->fip_packet_type); 1593 synchronize_net(); 1594 } 1595 1596 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface) 1597 { 1598 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface); 1599 struct fc_lport *lport = ctlr->lp; 1600 struct fcoe_port *port = lport_priv(lport); 1601 struct bnx2fc_hba *hba = interface->hba; 1602 1603 /* Stop the transmit retry timer */ 1604 timer_delete_sync(&port->timer); 1605 1606 /* Free existing transmit skbs */ 1607 fcoe_clean_pending_queue(lport); 1608 1609 bnx2fc_net_cleanup(interface); 1610 1611 bnx2fc_free_vport(hba, lport); 1612 } 1613 1614 static void bnx2fc_if_destroy(struct fc_lport *lport) 1615 { 1616 1617 /* Free queued packets for the receive thread */ 1618 bnx2fc_clean_rx_queue(lport); 1619 1620 /* Detach from scsi-ml */ 1621 fc_remove_host(lport->host); 1622 scsi_remove_host(lport->host); 1623 1624 /* 1625 * Note that only the physical lport will have the exchange manager. 1626 * for vports, this function is NOP 1627 */ 1628 fc_exch_mgr_free(lport); 1629 1630 /* Free memory used by statistical counters */ 1631 fc_lport_free_stats(lport); 1632 1633 /* Release Scsi_Host */ 1634 scsi_host_put(lport->host); 1635 } 1636 1637 static void __bnx2fc_destroy(struct bnx2fc_interface *interface) 1638 { 1639 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface); 1640 struct fc_lport *lport = ctlr->lp; 1641 struct fcoe_port *port = lport_priv(lport); 1642 1643 bnx2fc_interface_cleanup(interface); 1644 bnx2fc_stop(interface); 1645 list_del(&interface->list); 1646 bnx2fc_port_destroy(port); 1647 bnx2fc_interface_put(interface); 1648 } 1649 1650 /** 1651 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface 1652 * 1653 * @netdev: The net device that the FCoE interface is on 1654 * 1655 * Called from sysfs. 1656 * 1657 * Returns: 0 for success 1658 */ 1659 static int bnx2fc_destroy(struct net_device *netdev) 1660 { 1661 struct bnx2fc_interface *interface = NULL; 1662 struct workqueue_struct *timer_work_queue; 1663 struct fcoe_ctlr *ctlr; 1664 int rc = 0; 1665 1666 rtnl_lock(); 1667 mutex_lock(&bnx2fc_dev_lock); 1668 1669 interface = bnx2fc_interface_lookup(netdev); 1670 ctlr = bnx2fc_to_ctlr(interface); 1671 if (!interface || !ctlr->lp) { 1672 rc = -ENODEV; 1673 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n"); 1674 goto netdev_err; 1675 } 1676 1677 timer_work_queue = interface->timer_work_queue; 1678 __bnx2fc_destroy(interface); 1679 destroy_workqueue(timer_work_queue); 1680 1681 netdev_err: 1682 mutex_unlock(&bnx2fc_dev_lock); 1683 rtnl_unlock(); 1684 return rc; 1685 } 1686 1687 static void bnx2fc_port_destroy(struct fcoe_port *port) 1688 { 1689 struct fc_lport *lport; 1690 1691 lport = port->lport; 1692 BNX2FC_HBA_DBG(lport, "Entered %s, destroying lport %p\n", __func__, lport); 1693 1694 bnx2fc_if_destroy(lport); 1695 } 1696 1697 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba) 1698 { 1699 bnx2fc_free_fw_resc(hba); 1700 bnx2fc_free_task_ctx(hba); 1701 } 1702 1703 /** 1704 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated 1705 * pci structure 1706 * 1707 * @hba: Adapter instance 1708 */ 1709 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba) 1710 { 1711 if (bnx2fc_setup_task_ctx(hba)) 1712 goto mem_err; 1713 1714 if (bnx2fc_setup_fw_resc(hba)) 1715 goto mem_err; 1716 1717 return 0; 1718 mem_err: 1719 bnx2fc_unbind_adapter_devices(hba); 1720 return -ENOMEM; 1721 } 1722 1723 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba) 1724 { 1725 struct cnic_dev *cnic; 1726 struct pci_dev *pdev; 1727 1728 if (!hba->cnic) { 1729 printk(KERN_ERR PFX "cnic is NULL\n"); 1730 return -ENODEV; 1731 } 1732 cnic = hba->cnic; 1733 pdev = hba->pcidev = cnic->pcidev; 1734 if (!hba->pcidev) 1735 return -ENODEV; 1736 1737 switch (pdev->device) { 1738 case PCI_DEVICE_ID_NX2_57710: 1739 strscpy(hba->chip_num, "BCM57710", sizeof(hba->chip_num)); 1740 break; 1741 case PCI_DEVICE_ID_NX2_57711: 1742 strscpy(hba->chip_num, "BCM57711", sizeof(hba->chip_num)); 1743 break; 1744 case PCI_DEVICE_ID_NX2_57712: 1745 case PCI_DEVICE_ID_NX2_57712_MF: 1746 case PCI_DEVICE_ID_NX2_57712_VF: 1747 strscpy(hba->chip_num, "BCM57712", sizeof(hba->chip_num)); 1748 break; 1749 case PCI_DEVICE_ID_NX2_57800: 1750 case PCI_DEVICE_ID_NX2_57800_MF: 1751 case PCI_DEVICE_ID_NX2_57800_VF: 1752 strscpy(hba->chip_num, "BCM57800", sizeof(hba->chip_num)); 1753 break; 1754 case PCI_DEVICE_ID_NX2_57810: 1755 case PCI_DEVICE_ID_NX2_57810_MF: 1756 case PCI_DEVICE_ID_NX2_57810_VF: 1757 strscpy(hba->chip_num, "BCM57810", sizeof(hba->chip_num)); 1758 break; 1759 case PCI_DEVICE_ID_NX2_57840: 1760 case PCI_DEVICE_ID_NX2_57840_MF: 1761 case PCI_DEVICE_ID_NX2_57840_VF: 1762 case PCI_DEVICE_ID_NX2_57840_2_20: 1763 case PCI_DEVICE_ID_NX2_57840_4_10: 1764 strscpy(hba->chip_num, "BCM57840", sizeof(hba->chip_num)); 1765 break; 1766 default: 1767 pr_err(PFX "Unknown device id 0x%x\n", pdev->device); 1768 break; 1769 } 1770 pci_dev_get(hba->pcidev); 1771 return 0; 1772 } 1773 1774 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba) 1775 { 1776 if (hba->pcidev) { 1777 hba->chip_num[0] = '\0'; 1778 pci_dev_put(hba->pcidev); 1779 } 1780 hba->pcidev = NULL; 1781 } 1782 1783 /** 1784 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats 1785 * 1786 * @handle: transport handle pointing to adapter structure 1787 */ 1788 static int bnx2fc_ulp_get_stats(void *handle) 1789 { 1790 struct bnx2fc_hba *hba = handle; 1791 struct cnic_dev *cnic; 1792 struct fcoe_stats_info *stats_addr; 1793 1794 if (!hba) 1795 return -EINVAL; 1796 1797 cnic = hba->cnic; 1798 stats_addr = &cnic->stats_addr->fcoe_stat; 1799 if (!stats_addr) 1800 return -EINVAL; 1801 1802 strscpy(stats_addr->version, BNX2FC_VERSION, 1803 sizeof(stats_addr->version)); 1804 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX; 1805 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX; 1806 1807 return 0; 1808 } 1809 1810 1811 /** 1812 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance 1813 * 1814 * @handle: transport handle pointing to adapter structure 1815 * 1816 * This function maps adapter structure to pcidev structure and initiates 1817 * firmware handshake to enable/initialize on-chip FCoE components. 1818 * This bnx2fc - cnic interface api callback is used after following 1819 * conditions are met - 1820 * a) underlying network interface is up (marked by event NETDEV_UP 1821 * from netdev 1822 * b) bnx2fc adatper structure is registered. 1823 */ 1824 static void bnx2fc_ulp_start(void *handle) 1825 { 1826 struct bnx2fc_hba *hba = handle; 1827 struct bnx2fc_interface *interface; 1828 struct fcoe_ctlr *ctlr; 1829 struct fc_lport *lport; 1830 1831 mutex_lock(&bnx2fc_dev_lock); 1832 1833 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) 1834 bnx2fc_fw_init(hba); 1835 1836 BNX2FC_MISC_DBG("bnx2fc started.\n"); 1837 1838 list_for_each_entry(interface, &if_list, list) { 1839 if (interface->hba == hba) { 1840 ctlr = bnx2fc_to_ctlr(interface); 1841 lport = ctlr->lp; 1842 /* Kick off Fabric discovery*/ 1843 printk(KERN_ERR PFX "ulp_init: start discovery\n"); 1844 lport->tt.frame_send = bnx2fc_xmit; 1845 bnx2fc_start_disc(interface); 1846 } 1847 } 1848 1849 mutex_unlock(&bnx2fc_dev_lock); 1850 } 1851 1852 static void bnx2fc_port_shutdown(struct fc_lport *lport) 1853 { 1854 BNX2FC_MISC_DBG("Entered %s\n", __func__); 1855 fc_fabric_logoff(lport); 1856 fc_lport_destroy(lport); 1857 } 1858 1859 static void bnx2fc_stop(struct bnx2fc_interface *interface) 1860 { 1861 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface); 1862 struct fc_lport *lport; 1863 struct fc_lport *vport; 1864 1865 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) 1866 return; 1867 1868 lport = ctlr->lp; 1869 bnx2fc_port_shutdown(lport); 1870 1871 mutex_lock(&lport->lp_mutex); 1872 list_for_each_entry(vport, &lport->vports, list) 1873 fc_host_port_type(vport->host) = 1874 FC_PORTTYPE_UNKNOWN; 1875 mutex_unlock(&lport->lp_mutex); 1876 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN; 1877 fcoe_ctlr_link_down(ctlr); 1878 fcoe_clean_pending_queue(lport); 1879 } 1880 1881 static int bnx2fc_fw_init(struct bnx2fc_hba *hba) 1882 { 1883 #define BNX2FC_INIT_POLL_TIME (1000 / HZ) 1884 int rc = -1; 1885 int i = HZ; 1886 1887 rc = bnx2fc_bind_adapter_devices(hba); 1888 if (rc) { 1889 printk(KERN_ALERT PFX 1890 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc); 1891 goto err_out; 1892 } 1893 1894 rc = bnx2fc_send_fw_fcoe_init_msg(hba); 1895 if (rc) { 1896 printk(KERN_ALERT PFX 1897 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc); 1898 goto err_unbind; 1899 } 1900 1901 /* 1902 * Wait until the adapter init message is complete, and adapter 1903 * state is UP. 1904 */ 1905 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--) 1906 msleep(BNX2FC_INIT_POLL_TIME); 1907 1908 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) { 1909 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. " 1910 "Ignoring...\n", 1911 hba->cnic->netdev->name); 1912 rc = -1; 1913 goto err_unbind; 1914 } 1915 1916 1917 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags); 1918 return 0; 1919 1920 err_unbind: 1921 bnx2fc_unbind_adapter_devices(hba); 1922 err_out: 1923 return rc; 1924 } 1925 1926 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba) 1927 { 1928 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) { 1929 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) { 1930 timer_setup(&hba->destroy_timer, bnx2fc_destroy_timer, 1931 0); 1932 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT + 1933 jiffies; 1934 add_timer(&hba->destroy_timer); 1935 wait_event_interruptible(hba->destroy_wait, 1936 test_bit(BNX2FC_FLAG_DESTROY_CMPL, 1937 &hba->flags)); 1938 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags); 1939 /* This should never happen */ 1940 if (signal_pending(current)) 1941 flush_signals(current); 1942 1943 timer_delete_sync(&hba->destroy_timer); 1944 } 1945 bnx2fc_unbind_adapter_devices(hba); 1946 } 1947 } 1948 1949 /** 1950 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance 1951 * 1952 * @handle: transport handle pointing to adapter structure 1953 * 1954 * Driver checks if adapter is already in shutdown mode, if not start 1955 * the shutdown process. 1956 */ 1957 static void bnx2fc_ulp_stop(void *handle) 1958 { 1959 struct bnx2fc_hba *hba = handle; 1960 struct bnx2fc_interface *interface; 1961 1962 printk(KERN_ERR "ULP_STOP\n"); 1963 1964 mutex_lock(&bnx2fc_dev_lock); 1965 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) 1966 goto exit; 1967 list_for_each_entry(interface, &if_list, list) { 1968 if (interface->hba == hba) 1969 bnx2fc_stop(interface); 1970 } 1971 BUG_ON(hba->num_ofld_sess != 0); 1972 1973 mutex_lock(&hba->hba_mutex); 1974 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state); 1975 clear_bit(ADAPTER_STATE_GOING_DOWN, 1976 &hba->adapter_state); 1977 1978 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state); 1979 mutex_unlock(&hba->hba_mutex); 1980 1981 bnx2fc_fw_destroy(hba); 1982 exit: 1983 mutex_unlock(&bnx2fc_dev_lock); 1984 } 1985 1986 static void bnx2fc_start_disc(struct bnx2fc_interface *interface) 1987 { 1988 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface); 1989 struct fc_lport *lport; 1990 int wait_cnt = 0; 1991 1992 BNX2FC_MISC_DBG("Entered %s\n", __func__); 1993 /* Kick off FIP/FLOGI */ 1994 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) { 1995 printk(KERN_ERR PFX "Init not done yet\n"); 1996 return; 1997 } 1998 1999 lport = ctlr->lp; 2000 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n"); 2001 2002 if (!bnx2fc_link_ok(lport) && interface->enabled) { 2003 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n"); 2004 fcoe_ctlr_link_up(ctlr); 2005 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT; 2006 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state); 2007 } 2008 2009 /* wait for the FCF to be selected before issuing FLOGI */ 2010 while (!ctlr->sel_fcf) { 2011 msleep(250); 2012 /* give up after 3 secs */ 2013 if (++wait_cnt > 12) 2014 break; 2015 } 2016 2017 /* Reset max receive frame size to default */ 2018 if (fc_set_mfs(lport, BNX2FC_MFS)) 2019 return; 2020 2021 fc_lport_init(lport); 2022 fc_fabric_login(lport); 2023 } 2024 2025 2026 /** 2027 * bnx2fc_ulp_init - Initialize an adapter instance 2028 * 2029 * @dev : cnic device handle 2030 * Called from cnic_register_driver() context to initialize all 2031 * enumerated cnic devices. This routine allocates adapter structure 2032 * and other device specific resources. 2033 */ 2034 static void bnx2fc_ulp_init(struct cnic_dev *dev) 2035 { 2036 struct bnx2fc_hba *hba; 2037 int rc = 0; 2038 2039 BNX2FC_MISC_DBG("Entered %s\n", __func__); 2040 /* bnx2fc works only when bnx2x is loaded */ 2041 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) || 2042 (dev->max_fcoe_conn == 0)) { 2043 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s," 2044 " flags: %lx fcoe_conn: %d\n", 2045 dev->netdev->name, dev->flags, dev->max_fcoe_conn); 2046 return; 2047 } 2048 2049 hba = bnx2fc_hba_create(dev); 2050 if (!hba) { 2051 printk(KERN_ERR PFX "hba initialization failed\n"); 2052 return; 2053 } 2054 2055 pr_info(PFX "FCoE initialized for %s.\n", dev->netdev->name); 2056 2057 /* Add HBA to the adapter list */ 2058 mutex_lock(&bnx2fc_dev_lock); 2059 list_add_tail(&hba->list, &adapter_list); 2060 adapter_count++; 2061 mutex_unlock(&bnx2fc_dev_lock); 2062 2063 dev->fcoe_cap = &hba->fcoe_cap; 2064 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic); 2065 rc = dev->register_device(dev, CNIC_ULP_FCOE, 2066 (void *) hba); 2067 if (rc) 2068 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc); 2069 else 2070 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic); 2071 } 2072 2073 /* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */ 2074 static int __bnx2fc_disable(struct fcoe_ctlr *ctlr) 2075 { 2076 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr); 2077 2078 if (interface->enabled) { 2079 if (!ctlr->lp) { 2080 pr_err(PFX "__bnx2fc_disable: lport not found\n"); 2081 return -ENODEV; 2082 } else { 2083 interface->enabled = false; 2084 fcoe_ctlr_link_down(ctlr); 2085 fcoe_clean_pending_queue(ctlr->lp); 2086 } 2087 } 2088 return 0; 2089 } 2090 2091 /* 2092 * Deperecated: Use bnx2fc_enabled() 2093 */ 2094 static int bnx2fc_disable(struct net_device *netdev) 2095 { 2096 struct bnx2fc_interface *interface; 2097 struct fcoe_ctlr *ctlr; 2098 int rc = 0; 2099 2100 rtnl_lock(); 2101 mutex_lock(&bnx2fc_dev_lock); 2102 2103 interface = bnx2fc_interface_lookup(netdev); 2104 ctlr = bnx2fc_to_ctlr(interface); 2105 2106 if (!interface) { 2107 rc = -ENODEV; 2108 pr_err(PFX "bnx2fc_disable: interface not found\n"); 2109 } else { 2110 rc = __bnx2fc_disable(ctlr); 2111 } 2112 mutex_unlock(&bnx2fc_dev_lock); 2113 rtnl_unlock(); 2114 return rc; 2115 } 2116 2117 static uint bnx2fc_npiv_create_vports(struct fc_lport *lport, 2118 struct cnic_fc_npiv_tbl *npiv_tbl) 2119 { 2120 struct fc_vport_identifiers vpid; 2121 uint i, created = 0; 2122 u64 wwnn = 0; 2123 char wwpn_str[32]; 2124 char wwnn_str[32]; 2125 2126 if (npiv_tbl->count > MAX_NPIV_ENTRIES) { 2127 BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n"); 2128 goto done; 2129 } 2130 2131 /* Sanity check the first entry to make sure it's not 0 */ 2132 if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 && 2133 wwn_to_u64(npiv_tbl->wwpn[0]) == 0) { 2134 BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n"); 2135 goto done; 2136 } 2137 2138 vpid.roles = FC_PORT_ROLE_FCP_INITIATOR; 2139 vpid.vport_type = FC_PORTTYPE_NPIV; 2140 vpid.disable = false; 2141 2142 for (i = 0; i < npiv_tbl->count; i++) { 2143 wwnn = wwn_to_u64(npiv_tbl->wwnn[i]); 2144 if (wwnn == 0) { 2145 /* 2146 * If we get a 0 element from for the WWNN then assume 2147 * the WWNN should be the same as the physical port. 2148 */ 2149 wwnn = lport->wwnn; 2150 } 2151 vpid.node_name = wwnn; 2152 vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]); 2153 scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name), 2154 "NPIV[%u]:%016llx-%016llx", 2155 created, vpid.port_name, vpid.node_name); 2156 fcoe_wwn_to_str(vpid.node_name, wwnn_str, sizeof(wwnn_str)); 2157 fcoe_wwn_to_str(vpid.port_name, wwpn_str, sizeof(wwpn_str)); 2158 BNX2FC_HBA_DBG(lport, "Creating vport %s:%s.\n", wwnn_str, 2159 wwpn_str); 2160 if (fc_vport_create(lport->host, 0, &vpid)) 2161 created++; 2162 else 2163 BNX2FC_HBA_DBG(lport, "Failed to create vport\n"); 2164 } 2165 done: 2166 return created; 2167 } 2168 2169 static int __bnx2fc_enable(struct fcoe_ctlr *ctlr) 2170 { 2171 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr); 2172 struct bnx2fc_hba *hba; 2173 struct cnic_fc_npiv_tbl *npiv_tbl; 2174 struct fc_lport *lport; 2175 2176 if (!interface->enabled) { 2177 if (!ctlr->lp) { 2178 pr_err(PFX "__bnx2fc_enable: lport not found\n"); 2179 return -ENODEV; 2180 } else if (!bnx2fc_link_ok(ctlr->lp)) { 2181 fcoe_ctlr_link_up(ctlr); 2182 interface->enabled = true; 2183 } 2184 } 2185 2186 /* Create static NPIV ports if any are contained in NVRAM */ 2187 hba = interface->hba; 2188 lport = ctlr->lp; 2189 2190 if (!hba) 2191 goto done; 2192 2193 if (!hba->cnic) 2194 goto done; 2195 2196 if (!lport) 2197 goto done; 2198 2199 if (!lport->host) 2200 goto done; 2201 2202 if (!hba->cnic->get_fc_npiv_tbl) 2203 goto done; 2204 2205 npiv_tbl = kzalloc_obj(struct cnic_fc_npiv_tbl); 2206 if (!npiv_tbl) 2207 goto done; 2208 2209 if (hba->cnic->get_fc_npiv_tbl(hba->cnic, npiv_tbl)) 2210 goto done_free; 2211 2212 bnx2fc_npiv_create_vports(lport, npiv_tbl); 2213 done_free: 2214 kfree(npiv_tbl); 2215 done: 2216 return 0; 2217 } 2218 2219 /* 2220 * Deprecated: Use bnx2fc_enabled() 2221 */ 2222 static int bnx2fc_enable(struct net_device *netdev) 2223 { 2224 struct bnx2fc_interface *interface; 2225 struct fcoe_ctlr *ctlr; 2226 int rc = 0; 2227 2228 rtnl_lock(); 2229 mutex_lock(&bnx2fc_dev_lock); 2230 2231 interface = bnx2fc_interface_lookup(netdev); 2232 ctlr = bnx2fc_to_ctlr(interface); 2233 if (!interface) { 2234 rc = -ENODEV; 2235 pr_err(PFX "bnx2fc_enable: interface not found\n"); 2236 } else { 2237 rc = __bnx2fc_enable(ctlr); 2238 } 2239 2240 mutex_unlock(&bnx2fc_dev_lock); 2241 rtnl_unlock(); 2242 return rc; 2243 } 2244 2245 /** 2246 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller 2247 * @cdev: The FCoE Controller that is being enabled or disabled 2248 * 2249 * fcoe_sysfs will ensure that the state of 'enabled' has 2250 * changed, so no checking is necessary here. This routine simply 2251 * calls fcoe_enable or fcoe_disable, both of which are deprecated. 2252 * When those routines are removed the functionality can be merged 2253 * here. 2254 */ 2255 static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev) 2256 { 2257 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev); 2258 2259 switch (cdev->enabled) { 2260 case FCOE_CTLR_ENABLED: 2261 return __bnx2fc_enable(ctlr); 2262 case FCOE_CTLR_DISABLED: 2263 return __bnx2fc_disable(ctlr); 2264 case FCOE_CTLR_UNUSED: 2265 default: 2266 return -ENOTSUPP; 2267 } 2268 } 2269 2270 enum bnx2fc_create_link_state { 2271 BNX2FC_CREATE_LINK_DOWN, 2272 BNX2FC_CREATE_LINK_UP, 2273 }; 2274 2275 /** 2276 * _bnx2fc_create() - Create bnx2fc FCoE interface 2277 * @netdev : The net_device object the Ethernet interface to create on 2278 * @fip_mode: The FIP mode for this creation 2279 * @link_state: The ctlr link state on creation 2280 * 2281 * Called from either the libfcoe 'create' module parameter 2282 * via fcoe_create or from fcoe_syfs's ctlr_create file. 2283 * 2284 * libfcoe's 'create' module parameter is deprecated so some 2285 * consolidation of code can be done when that interface is 2286 * removed. 2287 * 2288 * Returns: 0 for success 2289 */ 2290 static int _bnx2fc_create(struct net_device *netdev, 2291 enum fip_mode fip_mode, 2292 enum bnx2fc_create_link_state link_state) 2293 { 2294 struct fcoe_ctlr_device *cdev; 2295 struct fcoe_ctlr *ctlr; 2296 struct bnx2fc_interface *interface; 2297 struct bnx2fc_hba *hba; 2298 struct net_device *phys_dev = netdev; 2299 struct fc_lport *lport; 2300 struct ethtool_drvinfo drvinfo; 2301 int rc = 0; 2302 int vlan_id = 0; 2303 2304 BNX2FC_MISC_DBG("Entered bnx2fc_create\n"); 2305 if (fip_mode != FIP_MODE_FABRIC) { 2306 printk(KERN_ERR "fip mode not FABRIC\n"); 2307 return -EIO; 2308 } 2309 2310 rtnl_lock(); 2311 2312 mutex_lock(&bnx2fc_dev_lock); 2313 2314 if (!try_module_get(THIS_MODULE)) { 2315 rc = -EINVAL; 2316 goto mod_err; 2317 } 2318 2319 /* obtain physical netdev */ 2320 if (is_vlan_dev(netdev)) 2321 phys_dev = vlan_dev_real_dev(netdev); 2322 2323 /* verify if the physical device is a netxtreme2 device */ 2324 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) { 2325 memset(&drvinfo, 0, sizeof(drvinfo)); 2326 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo); 2327 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) { 2328 printk(KERN_ERR PFX "Not a netxtreme2 device\n"); 2329 rc = -EINVAL; 2330 goto netdev_err; 2331 } 2332 } else { 2333 printk(KERN_ERR PFX "unable to obtain drv_info\n"); 2334 rc = -EINVAL; 2335 goto netdev_err; 2336 } 2337 2338 /* obtain interface and initialize rest of the structure */ 2339 hba = bnx2fc_hba_lookup(phys_dev); 2340 if (!hba) { 2341 rc = -ENODEV; 2342 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n"); 2343 goto netdev_err; 2344 } 2345 2346 if (bnx2fc_interface_lookup(netdev)) { 2347 rc = -EEXIST; 2348 goto netdev_err; 2349 } 2350 2351 interface = bnx2fc_interface_create(hba, netdev, fip_mode); 2352 if (!interface) { 2353 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n"); 2354 rc = -ENOMEM; 2355 goto netdev_err; 2356 } 2357 2358 if (is_vlan_dev(netdev)) { 2359 vlan_id = vlan_dev_vlan_id(netdev); 2360 interface->vlan_enabled = 1; 2361 } 2362 2363 ctlr = bnx2fc_to_ctlr(interface); 2364 cdev = fcoe_ctlr_to_ctlr_dev(ctlr); 2365 interface->vlan_id = vlan_id; 2366 interface->tm_timeout = BNX2FC_TM_TIMEOUT; 2367 2368 interface->timer_work_queue = alloc_ordered_workqueue( 2369 "%s", WQ_MEM_RECLAIM, "bnx2fc_timer_wq"); 2370 if (!interface->timer_work_queue) { 2371 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n"); 2372 rc = -EINVAL; 2373 goto ifput_err; 2374 } 2375 2376 lport = bnx2fc_if_create(interface, &cdev->dev, 0); 2377 if (!lport) { 2378 printk(KERN_ERR PFX "Failed to create interface (%s)\n", 2379 netdev->name); 2380 rc = -EINVAL; 2381 goto if_create_err; 2382 } 2383 2384 /* Add interface to if_list */ 2385 list_add_tail(&interface->list, &if_list); 2386 2387 lport->boot_time = jiffies; 2388 2389 /* Make this master N_port */ 2390 ctlr->lp = lport; 2391 2392 if (link_state == BNX2FC_CREATE_LINK_UP) 2393 cdev->enabled = FCOE_CTLR_ENABLED; 2394 else 2395 cdev->enabled = FCOE_CTLR_DISABLED; 2396 2397 if (link_state == BNX2FC_CREATE_LINK_UP && 2398 !bnx2fc_link_ok(lport)) { 2399 fcoe_ctlr_link_up(ctlr); 2400 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT; 2401 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state); 2402 } 2403 2404 BNX2FC_HBA_DBG(lport, "create: START DISC\n"); 2405 bnx2fc_start_disc(interface); 2406 2407 if (link_state == BNX2FC_CREATE_LINK_UP) 2408 interface->enabled = true; 2409 2410 /* 2411 * Release from kref_init in bnx2fc_interface_setup, on success 2412 * lport should be holding a reference taken in bnx2fc_if_create 2413 */ 2414 bnx2fc_interface_put(interface); 2415 /* put netdev that was held while calling dev_get_by_name */ 2416 mutex_unlock(&bnx2fc_dev_lock); 2417 rtnl_unlock(); 2418 return 0; 2419 2420 if_create_err: 2421 destroy_workqueue(interface->timer_work_queue); 2422 ifput_err: 2423 bnx2fc_net_cleanup(interface); 2424 bnx2fc_interface_put(interface); 2425 goto mod_err; 2426 netdev_err: 2427 module_put(THIS_MODULE); 2428 mod_err: 2429 mutex_unlock(&bnx2fc_dev_lock); 2430 rtnl_unlock(); 2431 return rc; 2432 } 2433 2434 /** 2435 * bnx2fc_create() - Create a bnx2fc interface 2436 * @netdev : The net_device object the Ethernet interface to create on 2437 * @fip_mode: The FIP mode for this creation 2438 * 2439 * Called from fcoe transport 2440 * 2441 * Returns: 0 for success 2442 */ 2443 static int bnx2fc_create(struct net_device *netdev, enum fip_mode fip_mode) 2444 { 2445 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP); 2446 } 2447 2448 /** 2449 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs 2450 * @netdev: The net_device to be used by the allocated FCoE Controller 2451 * 2452 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr 2453 * in a link_down state. The allows the user an opportunity to configure 2454 * the FCoE Controller from sysfs before enabling the FCoE Controller. 2455 * 2456 * Creating in with this routine starts the FCoE Controller in Fabric 2457 * mode. The user can change to VN2VN or another mode before enabling. 2458 */ 2459 static int bnx2fc_ctlr_alloc(struct net_device *netdev) 2460 { 2461 return _bnx2fc_create(netdev, FIP_MODE_FABRIC, 2462 BNX2FC_CREATE_LINK_DOWN); 2463 } 2464 2465 /** 2466 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance 2467 * 2468 * @cnic: Pointer to cnic device instance 2469 * 2470 **/ 2471 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic) 2472 { 2473 struct bnx2fc_hba *hba; 2474 2475 /* Called with bnx2fc_dev_lock held */ 2476 list_for_each_entry(hba, &adapter_list, list) { 2477 if (hba->cnic == cnic) 2478 return hba; 2479 } 2480 return NULL; 2481 } 2482 2483 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device 2484 *netdev) 2485 { 2486 struct bnx2fc_interface *interface; 2487 2488 /* Called with bnx2fc_dev_lock held */ 2489 list_for_each_entry(interface, &if_list, list) { 2490 if (interface->netdev == netdev) 2491 return interface; 2492 } 2493 return NULL; 2494 } 2495 2496 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device 2497 *phys_dev) 2498 { 2499 struct bnx2fc_hba *hba; 2500 2501 /* Called with bnx2fc_dev_lock held */ 2502 list_for_each_entry(hba, &adapter_list, list) { 2503 if (hba->phys_dev == phys_dev) 2504 return hba; 2505 } 2506 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n"); 2507 return NULL; 2508 } 2509 2510 /** 2511 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources 2512 * 2513 * @dev: cnic device handle 2514 */ 2515 static void bnx2fc_ulp_exit(struct cnic_dev *dev) 2516 { 2517 struct bnx2fc_hba *hba; 2518 struct bnx2fc_interface *interface, *tmp; 2519 2520 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n"); 2521 2522 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) { 2523 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n", 2524 dev->netdev->name, dev->flags); 2525 return; 2526 } 2527 2528 mutex_lock(&bnx2fc_dev_lock); 2529 hba = bnx2fc_find_hba_for_cnic(dev); 2530 if (!hba) { 2531 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n", 2532 dev); 2533 mutex_unlock(&bnx2fc_dev_lock); 2534 return; 2535 } 2536 2537 list_del_init(&hba->list); 2538 adapter_count--; 2539 2540 list_for_each_entry_safe(interface, tmp, &if_list, list) 2541 /* destroy not called yet, move to quiesced list */ 2542 if (interface->hba == hba) 2543 __bnx2fc_destroy(interface); 2544 mutex_unlock(&bnx2fc_dev_lock); 2545 2546 bnx2fc_ulp_stop(hba); 2547 /* unregister cnic device */ 2548 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic)) 2549 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE); 2550 bnx2fc_hba_destroy(hba); 2551 } 2552 2553 static void bnx2fc_rport_terminate_io(struct fc_rport *rport) 2554 { 2555 /* This is a no-op */ 2556 } 2557 2558 /** 2559 * bnx2fc_fcoe_reset - Resets the fcoe 2560 * 2561 * @shost: shost the reset is from 2562 * 2563 * Returns: always 0 2564 */ 2565 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost) 2566 { 2567 struct fc_lport *lport = shost_priv(shost); 2568 fc_lport_reset(lport); 2569 return 0; 2570 } 2571 2572 2573 static bool bnx2fc_match(struct net_device *netdev) 2574 { 2575 struct net_device *phys_dev = netdev; 2576 2577 mutex_lock(&bnx2fc_dev_lock); 2578 if (is_vlan_dev(netdev)) 2579 phys_dev = vlan_dev_real_dev(netdev); 2580 2581 if (bnx2fc_hba_lookup(phys_dev)) { 2582 mutex_unlock(&bnx2fc_dev_lock); 2583 return true; 2584 } 2585 2586 mutex_unlock(&bnx2fc_dev_lock); 2587 return false; 2588 } 2589 2590 2591 static struct fcoe_transport bnx2fc_transport = { 2592 .name = {"bnx2fc"}, 2593 .attached = false, 2594 .list = LIST_HEAD_INIT(bnx2fc_transport.list), 2595 .alloc = bnx2fc_ctlr_alloc, 2596 .match = bnx2fc_match, 2597 .create = bnx2fc_create, 2598 .destroy = bnx2fc_destroy, 2599 .enable = bnx2fc_enable, 2600 .disable = bnx2fc_disable, 2601 }; 2602 2603 /** 2604 * bnx2fc_cpu_online - Create a receive thread for an online CPU 2605 * 2606 * @cpu: cpu index for the online cpu 2607 */ 2608 static int bnx2fc_cpu_online(unsigned int cpu) 2609 { 2610 struct bnx2fc_percpu_s *p; 2611 struct task_struct *thread; 2612 2613 p = &per_cpu(bnx2fc_percpu, cpu); 2614 2615 thread = kthread_create_on_cpu(bnx2fc_percpu_io_thread, 2616 (void *)p, cpu, "bnx2fc_thread/%d"); 2617 if (IS_ERR(thread)) 2618 return PTR_ERR(thread); 2619 2620 p->iothread = thread; 2621 wake_up_process(thread); 2622 return 0; 2623 } 2624 2625 static int bnx2fc_cpu_offline(unsigned int cpu) 2626 { 2627 struct bnx2fc_percpu_s *p; 2628 struct task_struct *thread; 2629 struct bnx2fc_work *work, *tmp; 2630 2631 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu); 2632 2633 /* Prevent any new work from being queued for this CPU */ 2634 p = &per_cpu(bnx2fc_percpu, cpu); 2635 spin_lock_bh(&p->fp_work_lock); 2636 thread = p->iothread; 2637 p->iothread = NULL; 2638 2639 /* Free all work in the list */ 2640 list_for_each_entry_safe(work, tmp, &p->work_list, list) { 2641 list_del_init(&work->list); 2642 bnx2fc_process_cq_compl(work->tgt, work->wqe, work->rq_data, 2643 work->num_rq, work->task); 2644 kfree(work); 2645 } 2646 2647 spin_unlock_bh(&p->fp_work_lock); 2648 2649 if (thread) 2650 kthread_stop(thread); 2651 return 0; 2652 } 2653 2654 static int bnx2fc_sdev_configure(struct scsi_device *sdev, 2655 struct queue_limits *lim) 2656 { 2657 if (!bnx2fc_queue_depth) 2658 return 0; 2659 2660 scsi_change_queue_depth(sdev, bnx2fc_queue_depth); 2661 return 0; 2662 } 2663 2664 static enum cpuhp_state bnx2fc_online_state; 2665 2666 /** 2667 * bnx2fc_mod_init - module init entry point 2668 * 2669 * Initialize driver wide global data structures, and register 2670 * with cnic module 2671 **/ 2672 static int __init bnx2fc_mod_init(void) 2673 { 2674 struct fcoe_percpu_s *bg; 2675 struct task_struct *l2_thread; 2676 int rc = 0; 2677 unsigned int cpu = 0; 2678 struct bnx2fc_percpu_s *p; 2679 2680 printk(KERN_INFO PFX "%s", version); 2681 2682 /* register as a fcoe transport */ 2683 rc = fcoe_transport_attach(&bnx2fc_transport); 2684 if (rc) { 2685 printk(KERN_ERR "failed to register an fcoe transport, check " 2686 "if libfcoe is loaded\n"); 2687 goto out; 2688 } 2689 2690 INIT_LIST_HEAD(&adapter_list); 2691 INIT_LIST_HEAD(&if_list); 2692 mutex_init(&bnx2fc_dev_lock); 2693 adapter_count = 0; 2694 2695 /* Attach FC transport template */ 2696 rc = bnx2fc_attach_transport(); 2697 if (rc) 2698 goto detach_ft; 2699 2700 bnx2fc_wq = alloc_workqueue("bnx2fc", WQ_PERCPU, 0); 2701 if (!bnx2fc_wq) { 2702 rc = -ENOMEM; 2703 goto release_bt; 2704 } 2705 2706 bg = &bnx2fc_global; 2707 skb_queue_head_init(&bg->fcoe_rx_list); 2708 l2_thread = kthread_run(bnx2fc_l2_rcv_thread, 2709 (void *)bg, 2710 "bnx2fc_l2_thread"); 2711 if (IS_ERR(l2_thread)) { 2712 rc = PTR_ERR(l2_thread); 2713 goto free_wq; 2714 } 2715 spin_lock_bh(&bg->fcoe_rx_list.lock); 2716 bg->kthread = l2_thread; 2717 spin_unlock_bh(&bg->fcoe_rx_list.lock); 2718 2719 for_each_possible_cpu(cpu) { 2720 p = &per_cpu(bnx2fc_percpu, cpu); 2721 INIT_LIST_HEAD(&p->work_list); 2722 spin_lock_init(&p->fp_work_lock); 2723 } 2724 2725 rc = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "scsi/bnx2fc:online", 2726 bnx2fc_cpu_online, bnx2fc_cpu_offline); 2727 if (rc < 0) 2728 goto stop_thread; 2729 bnx2fc_online_state = rc; 2730 2731 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb); 2732 return 0; 2733 2734 stop_thread: 2735 kthread_stop(l2_thread); 2736 free_wq: 2737 destroy_workqueue(bnx2fc_wq); 2738 release_bt: 2739 bnx2fc_release_transport(); 2740 detach_ft: 2741 fcoe_transport_detach(&bnx2fc_transport); 2742 out: 2743 return rc; 2744 } 2745 2746 static void __exit bnx2fc_mod_exit(void) 2747 { 2748 LIST_HEAD(to_be_deleted); 2749 struct bnx2fc_hba *hba, *next; 2750 struct fcoe_percpu_s *bg; 2751 struct task_struct *l2_thread; 2752 struct sk_buff *skb; 2753 2754 /* 2755 * NOTE: Since cnic calls register_driver routine rtnl_lock, 2756 * it will have higher precedence than bnx2fc_dev_lock. 2757 * unregister_device() cannot be called with bnx2fc_dev_lock 2758 * held. 2759 */ 2760 mutex_lock(&bnx2fc_dev_lock); 2761 list_splice_init(&adapter_list, &to_be_deleted); 2762 adapter_count = 0; 2763 mutex_unlock(&bnx2fc_dev_lock); 2764 2765 /* Unregister with cnic */ 2766 list_for_each_entry_safe(hba, next, &to_be_deleted, list) { 2767 list_del_init(&hba->list); 2768 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n", 2769 hba); 2770 bnx2fc_ulp_stop(hba); 2771 /* unregister cnic device */ 2772 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, 2773 &hba->reg_with_cnic)) 2774 hba->cnic->unregister_device(hba->cnic, 2775 CNIC_ULP_FCOE); 2776 bnx2fc_hba_destroy(hba); 2777 } 2778 cnic_unregister_driver(CNIC_ULP_FCOE); 2779 2780 /* Destroy global thread */ 2781 bg = &bnx2fc_global; 2782 spin_lock_bh(&bg->fcoe_rx_list.lock); 2783 l2_thread = bg->kthread; 2784 bg->kthread = NULL; 2785 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) 2786 kfree_skb(skb); 2787 2788 spin_unlock_bh(&bg->fcoe_rx_list.lock); 2789 2790 if (l2_thread) 2791 kthread_stop(l2_thread); 2792 2793 cpuhp_remove_state(bnx2fc_online_state); 2794 2795 destroy_workqueue(bnx2fc_wq); 2796 /* 2797 * detach from scsi transport 2798 * must happen after all destroys are done 2799 */ 2800 bnx2fc_release_transport(); 2801 2802 /* detach from fcoe transport */ 2803 fcoe_transport_detach(&bnx2fc_transport); 2804 } 2805 2806 module_init(bnx2fc_mod_init); 2807 module_exit(bnx2fc_mod_exit); 2808 2809 static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = { 2810 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled, 2811 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb, 2812 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb, 2813 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb, 2814 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb, 2815 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb, 2816 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb, 2817 2818 .get_fcoe_fcf_selected = fcoe_fcf_get_selected, 2819 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id, 2820 }; 2821 2822 static struct fc_function_template bnx2fc_transport_function = { 2823 .show_host_node_name = 1, 2824 .show_host_port_name = 1, 2825 .show_host_supported_classes = 1, 2826 .show_host_supported_fc4s = 1, 2827 .show_host_active_fc4s = 1, 2828 .show_host_maxframe_size = 1, 2829 2830 .show_host_port_id = 1, 2831 .show_host_supported_speeds = 1, 2832 .get_host_speed = fc_get_host_speed, 2833 .show_host_speed = 1, 2834 .show_host_port_type = 1, 2835 .get_host_port_state = fc_get_host_port_state, 2836 .show_host_port_state = 1, 2837 .show_host_symbolic_name = 1, 2838 2839 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) + 2840 sizeof(struct bnx2fc_rport)), 2841 .show_rport_maxframe_size = 1, 2842 .show_rport_supported_classes = 1, 2843 2844 .show_host_fabric_name = 1, 2845 .show_starget_node_name = 1, 2846 .show_starget_port_name = 1, 2847 .show_starget_port_id = 1, 2848 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo, 2849 .show_rport_dev_loss_tmo = 1, 2850 .get_fc_host_stats = bnx2fc_get_host_stats, 2851 2852 .issue_fc_host_lip = bnx2fc_fcoe_reset, 2853 2854 .terminate_rport_io = bnx2fc_rport_terminate_io, 2855 2856 .vport_create = bnx2fc_vport_create, 2857 .vport_delete = bnx2fc_vport_destroy, 2858 .vport_disable = bnx2fc_vport_disable, 2859 .bsg_request = fc_lport_bsg_request, 2860 }; 2861 2862 static struct fc_function_template bnx2fc_vport_xport_function = { 2863 .show_host_node_name = 1, 2864 .show_host_port_name = 1, 2865 .show_host_supported_classes = 1, 2866 .show_host_supported_fc4s = 1, 2867 .show_host_active_fc4s = 1, 2868 .show_host_maxframe_size = 1, 2869 2870 .show_host_port_id = 1, 2871 .show_host_supported_speeds = 1, 2872 .get_host_speed = fc_get_host_speed, 2873 .show_host_speed = 1, 2874 .show_host_port_type = 1, 2875 .get_host_port_state = fc_get_host_port_state, 2876 .show_host_port_state = 1, 2877 .show_host_symbolic_name = 1, 2878 2879 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) + 2880 sizeof(struct bnx2fc_rport)), 2881 .show_rport_maxframe_size = 1, 2882 .show_rport_supported_classes = 1, 2883 2884 .show_host_fabric_name = 1, 2885 .show_starget_node_name = 1, 2886 .show_starget_port_name = 1, 2887 .show_starget_port_id = 1, 2888 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo, 2889 .show_rport_dev_loss_tmo = 1, 2890 .get_fc_host_stats = fc_get_host_stats, 2891 .issue_fc_host_lip = bnx2fc_fcoe_reset, 2892 .terminate_rport_io = fc_rport_terminate_io, 2893 .bsg_request = fc_lport_bsg_request, 2894 }; 2895 2896 /* 2897 * Additional scsi_host attributes. 2898 */ 2899 static ssize_t 2900 bnx2fc_tm_timeout_show(struct device *dev, struct device_attribute *attr, 2901 char *buf) 2902 { 2903 struct Scsi_Host *shost = class_to_shost(dev); 2904 struct fc_lport *lport = shost_priv(shost); 2905 struct fcoe_port *port = lport_priv(lport); 2906 struct bnx2fc_interface *interface = port->priv; 2907 2908 sprintf(buf, "%u\n", interface->tm_timeout); 2909 return strlen(buf); 2910 } 2911 2912 static ssize_t 2913 bnx2fc_tm_timeout_store(struct device *dev, 2914 struct device_attribute *attr, const char *buf, size_t count) 2915 { 2916 struct Scsi_Host *shost = class_to_shost(dev); 2917 struct fc_lport *lport = shost_priv(shost); 2918 struct fcoe_port *port = lport_priv(lport); 2919 struct bnx2fc_interface *interface = port->priv; 2920 int rval, val; 2921 2922 rval = kstrtouint(buf, 10, &val); 2923 if (rval) 2924 return rval; 2925 if (val > 255) 2926 return -ERANGE; 2927 2928 interface->tm_timeout = (u8)val; 2929 return strlen(buf); 2930 } 2931 2932 static DEVICE_ATTR(tm_timeout, S_IRUGO|S_IWUSR, bnx2fc_tm_timeout_show, 2933 bnx2fc_tm_timeout_store); 2934 2935 static struct attribute *bnx2fc_host_attrs[] = { 2936 &dev_attr_tm_timeout.attr, 2937 NULL, 2938 }; 2939 2940 ATTRIBUTE_GROUPS(bnx2fc_host); 2941 2942 /* 2943 * scsi_host_template structure used while registering with SCSI-ml 2944 */ 2945 static struct scsi_host_template bnx2fc_shost_template = { 2946 .module = THIS_MODULE, 2947 .name = "QLogic Offload FCoE Initiator", 2948 .queuecommand = bnx2fc_queuecommand, 2949 .eh_timed_out = fc_eh_timed_out, 2950 .eh_abort_handler = bnx2fc_eh_abort, /* abts */ 2951 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */ 2952 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */ 2953 .eh_host_reset_handler = fc_eh_host_reset, 2954 .sdev_init = fc_sdev_init, 2955 .change_queue_depth = scsi_change_queue_depth, 2956 .this_id = -1, 2957 .cmd_per_lun = 3, 2958 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD, 2959 .dma_boundary = 0x7fff, 2960 .max_sectors = 0x3fbf, 2961 .track_queue_depth = 1, 2962 .sdev_configure = bnx2fc_sdev_configure, 2963 .shost_groups = bnx2fc_host_groups, 2964 .cmd_size = sizeof(struct bnx2fc_priv), 2965 }; 2966 2967 static struct libfc_function_template bnx2fc_libfc_fcn_templ = { 2968 .frame_send = bnx2fc_xmit, 2969 .elsct_send = bnx2fc_elsct_send, 2970 .fcp_abort_io = bnx2fc_abort_io, 2971 .fcp_cleanup = bnx2fc_cleanup, 2972 .get_lesb = fcoe_get_lesb, 2973 .rport_event_callback = bnx2fc_rport_event_handler, 2974 }; 2975 2976 /* 2977 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface 2978 * structure carrying callback function pointers 2979 */ 2980 static struct cnic_ulp_ops bnx2fc_cnic_cb = { 2981 .owner = THIS_MODULE, 2982 .cnic_init = bnx2fc_ulp_init, 2983 .cnic_exit = bnx2fc_ulp_exit, 2984 .cnic_start = bnx2fc_ulp_start, 2985 .cnic_stop = bnx2fc_ulp_stop, 2986 .indicate_kcqes = bnx2fc_indicate_kcqe, 2987 .indicate_netevent = bnx2fc_indicate_netevent, 2988 .cnic_get_stats = bnx2fc_ulp_get_stats, 2989 }; 2990