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