blob: b0bc5ffee903ad8b497886c0c445700496630110 [file] [log] [blame]
Saurav Kashyap17d87c42014-07-03 08:18:28 -04001/* bnx2fc_fcoe.c: QLogic NetXtreme II Linux FCoE offload driver.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002 * 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 *
Bhanu Prakash Gollapudicf122192013-03-08 13:28:53 -08006 * Copyright (c) 2008 - 2013 Broadcom Corporation
Saurav Kashyap17d87c42014-07-03 08:18:28 -04007 * Copyright (c) 2014, QLogic Corporation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation.
12 *
13 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
14 */
15
16#include "bnx2fc.h"
17
18static struct list_head adapter_list;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070019static struct list_head if_list;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080020static u32 adapter_count;
21static DEFINE_MUTEX(bnx2fc_dev_lock);
22DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
23
24#define DRV_MODULE_NAME "bnx2fc"
25#define DRV_MODULE_VERSION BNX2FC_VERSION
Eddie Wai38e83bf2013-12-11 15:30:23 -080026#define DRV_MODULE_RELDATE "Dec 11, 2013"
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080027
28
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080029static char version[] =
Saurav Kashyap17d87c42014-07-03 08:18:28 -040030 "QLogic NetXtreme II FCoE Driver " DRV_MODULE_NAME \
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080031 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
32
33
34MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
Saurav Kashyap17d87c42014-07-03 08:18:28 -040035MODULE_DESCRIPTION("QLogic NetXtreme II BCM57710 FCoE Driver");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080036MODULE_LICENSE("GPL");
37MODULE_VERSION(DRV_MODULE_VERSION);
38
39#define BNX2FC_MAX_QUEUE_DEPTH 256
40#define BNX2FC_MIN_QUEUE_DEPTH 32
41#define FCOE_WORD_TO_BYTE 4
42
43static struct scsi_transport_template *bnx2fc_transport_template;
44static struct scsi_transport_template *bnx2fc_vport_xport_template;
45
46struct workqueue_struct *bnx2fc_wq;
47
48/* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
49 * Here the io threads are per cpu but the l2 thread is just one
50 */
51struct fcoe_percpu_s bnx2fc_global;
52DEFINE_SPINLOCK(bnx2fc_global_lock);
53
54static struct cnic_ulp_ops bnx2fc_cnic_cb;
55static struct libfc_function_template bnx2fc_libfc_fcn_templ;
56static struct scsi_host_template bnx2fc_shost_template;
57static struct fc_function_template bnx2fc_transport_function;
Robert Love8d55e502012-05-22 19:06:26 -070058static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080059static struct fc_function_template bnx2fc_vport_xport_function;
60static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -070061static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080062static int bnx2fc_destroy(struct net_device *net_device);
63static int bnx2fc_enable(struct net_device *netdev);
64static int bnx2fc_disable(struct net_device *netdev);
65
Robert Love6e89ea32012-11-27 06:53:40 +000066/* fcoe_syfs control interface handlers */
67static int bnx2fc_ctlr_alloc(struct net_device *netdev);
68static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev);
69
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080070static void bnx2fc_recv_frame(struct sk_buff *skb);
71
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070072static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080073static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080074static int bnx2fc_lport_config(struct fc_lport *lport);
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +000075static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080076static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
77static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
78static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
79static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070080static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080081 struct device *parent, int npiv);
82static void bnx2fc_destroy_work(struct work_struct *work);
83
84static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070085static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
86 *phys_dev);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -070087static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080088static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
89
90static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
91static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
92
93static void bnx2fc_port_shutdown(struct fc_lport *lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070094static void bnx2fc_stop(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080095static int __init bnx2fc_mod_init(void);
96static void __exit bnx2fc_mod_exit(void);
97
98unsigned int bnx2fc_debug_level;
99module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
100
101static int bnx2fc_cpu_callback(struct notifier_block *nfb,
102 unsigned long action, void *hcpu);
103/* notification function for CPU hotplug events */
104static struct notifier_block bnx2fc_cpu_notifier = {
105 .notifier_call = bnx2fc_cpu_callback,
106};
107
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -0700108static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
109{
110 return ((struct bnx2fc_interface *)
111 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
112}
113
Robert Love8d55e502012-05-22 19:06:26 -0700114static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
115{
116 struct fcoe_ctlr_device *ctlr_dev =
117 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
118 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
119 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
120
121 fcf_dev->vlan_id = fcoe->vlan_id;
122}
123
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800124static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
125{
126 struct fcoe_percpu_s *bg;
127 struct fcoe_rcv_info *fr;
128 struct sk_buff_head *list;
129 struct sk_buff *skb, *next;
130 struct sk_buff *head;
131
132 bg = &bnx2fc_global;
133 spin_lock_bh(&bg->fcoe_rx_list.lock);
134 list = &bg->fcoe_rx_list;
135 head = list->next;
136 for (skb = head; skb != (struct sk_buff *)list;
137 skb = next) {
138 next = skb->next;
139 fr = fcoe_dev_from_skb(skb);
140 if (fr->fr_dev == lp) {
141 __skb_unlink(skb, list);
142 kfree_skb(skb);
143 }
144 }
145 spin_unlock_bh(&bg->fcoe_rx_list.lock);
146}
147
148int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
149{
150 int rc;
151 spin_lock(&bnx2fc_global_lock);
152 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
153 spin_unlock(&bnx2fc_global_lock);
154
155 return rc;
156}
157
158static void bnx2fc_abort_io(struct fc_lport *lport)
159{
160 /*
161 * This function is no-op for bnx2fc, but we do
162 * not want to leave it as NULL either, as libfc
163 * can call the default function which is
164 * fc_fcp_abort_io.
165 */
166}
167
168static void bnx2fc_cleanup(struct fc_lport *lport)
169{
170 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700171 struct bnx2fc_interface *interface = port->priv;
172 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800173 struct bnx2fc_rport *tgt;
174 int i;
175
176 BNX2FC_MISC_DBG("Entered %s\n", __func__);
177 mutex_lock(&hba->hba_mutex);
178 spin_lock_bh(&hba->hba_lock);
179 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
180 tgt = hba->tgt_ofld_list[i];
181 if (tgt) {
182 /* Cleanup IOs belonging to requested vport */
183 if (tgt->port == port) {
184 spin_unlock_bh(&hba->hba_lock);
185 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
186 bnx2fc_flush_active_ios(tgt);
187 spin_lock_bh(&hba->hba_lock);
188 }
189 }
190 }
191 spin_unlock_bh(&hba->hba_lock);
192 mutex_unlock(&hba->hba_mutex);
193}
194
195static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
196 struct fc_frame *fp)
197{
198 struct fc_rport_priv *rdata = tgt->rdata;
199 struct fc_frame_header *fh;
200 int rc = 0;
201
202 fh = fc_frame_header_get(fp);
203 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
204 "r_ctl = 0x%x\n", rdata->ids.port_id,
205 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
206 if ((fh->fh_type == FC_TYPE_ELS) &&
207 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
208
209 switch (fc_frame_payload_op(fp)) {
210 case ELS_ADISC:
211 rc = bnx2fc_send_adisc(tgt, fp);
212 break;
213 case ELS_LOGO:
214 rc = bnx2fc_send_logo(tgt, fp);
215 break;
216 case ELS_RLS:
217 rc = bnx2fc_send_rls(tgt, fp);
218 break;
219 default:
220 break;
221 }
222 } else if ((fh->fh_type == FC_TYPE_BLS) &&
223 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
224 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
225 else {
226 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
227 "rctl 0x%x thru non-offload path\n",
228 fh->fh_type, fh->fh_r_ctl);
229 return -ENODEV;
230 }
231 if (rc)
232 return -ENOMEM;
233 else
234 return 0;
235}
236
237/**
238 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
239 *
240 * @lport: the associated local port
241 * @fp: the fc_frame to be transmitted
242 */
243static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
244{
245 struct ethhdr *eh;
246 struct fcoe_crc_eof *cp;
247 struct sk_buff *skb;
248 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700249 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700250 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700251 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800252 struct fcoe_port *port;
253 struct fcoe_hdr *hp;
254 struct bnx2fc_rport *tgt;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700255 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800256 u8 sof, eof;
257 u32 crc;
258 unsigned int hlen, tlen, elen;
259 int wlen, rc = 0;
260
261 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700262 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700263 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700264 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800265
266 fh = fc_frame_header_get(fp);
267
268 skb = fp_skb(fp);
269 if (!lport->link_up) {
270 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
271 kfree_skb(skb);
272 return 0;
273 }
274
275 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Robert Lovefd8f8902012-05-22 19:06:16 -0700276 if (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800277 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
278 kfree_skb(skb);
279 return -EINVAL;
280 }
Robert Lovefd8f8902012-05-22 19:06:16 -0700281 if (fcoe_ctlr_els_send(ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800282 return 0;
283 }
284
285 sof = fr_sof(fp);
286 eof = fr_eof(fp);
287
288 /*
289 * Snoop the frame header to check if the frame is for
290 * an offloaded session
291 */
292 /*
293 * tgt_ofld_list access is synchronized using
294 * both hba mutex and hba lock. Atleast hba mutex or
295 * hba lock needs to be held for read access.
296 */
297
298 spin_lock_bh(&hba->hba_lock);
299 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
300 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
301 /* This frame is for offloaded session */
302 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
303 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
304 spin_unlock_bh(&hba->hba_lock);
305 rc = bnx2fc_xmit_l2_frame(tgt, fp);
306 if (rc != -ENODEV) {
307 kfree_skb(skb);
308 return rc;
309 }
310 } else {
311 spin_unlock_bh(&hba->hba_lock);
312 }
313
314 elen = sizeof(struct ethhdr);
315 hlen = sizeof(struct fcoe_hdr);
316 tlen = sizeof(struct fcoe_crc_eof);
317 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
318
319 skb->ip_summed = CHECKSUM_NONE;
320 crc = fcoe_fc_crc(fp);
321
322 /* copy port crc and eof to the skb buff */
323 if (skb_is_nonlinear(skb)) {
324 skb_frag_t *frag;
325 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
326 kfree_skb(skb);
327 return -ENOMEM;
328 }
329 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +0800330 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800331 } else {
332 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
333 }
334
335 memset(cp, 0, sizeof(*cp));
336 cp->fcoe_eof = eof;
337 cp->fcoe_crc32 = cpu_to_le32(~crc);
338 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +0800339 kunmap_atomic(cp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800340 cp = NULL;
341 }
342
343 /* adjust skb network/transport offsets to match mac/fcoe/port */
344 skb_push(skb, elen + hlen);
345 skb_reset_mac_header(skb);
346 skb_reset_network_header(skb);
347 skb->mac_len = elen;
348 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700349 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800350
351 /* fill up mac and fcoe headers */
352 eh = eth_hdr(skb);
353 eh->h_proto = htons(ETH_P_FCOE);
Robert Lovefd8f8902012-05-22 19:06:16 -0700354 if (ctlr->map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800355 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
356 else
357 /* insert GW address */
Robert Lovefd8f8902012-05-22 19:06:16 -0700358 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800359
Robert Lovefd8f8902012-05-22 19:06:16 -0700360 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
361 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800362 else
363 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
364
365 hp = (struct fcoe_hdr *)(eh + 1);
366 memset(hp, 0, sizeof(*hp));
367 if (FC_FCOE_VER)
368 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
369 hp->fcoe_sof = sof;
370
371 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
372 if (lport->seq_offload && fr_max_payload(fp)) {
373 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
374 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
375 } else {
376 skb_shinfo(skb)->gso_type = 0;
377 skb_shinfo(skb)->gso_size = 0;
378 }
379
380 /*update tx stats */
Vasu Dev1bd49b42012-05-25 10:26:43 -0700381 stats = per_cpu_ptr(lport->stats, get_cpu());
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800382 stats->TxFrames++;
383 stats->TxWords += wlen;
384 put_cpu();
385
386 /* send down to lld */
387 fr_dev(fp) = lport;
388 if (port->fcoe_pending_queue.qlen)
389 fcoe_check_wait_queue(lport, skb);
390 else if (fcoe_start_io(skb))
391 fcoe_check_wait_queue(lport, skb);
392
393 return 0;
394}
395
396/**
397 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
398 *
399 * @skb: the receive socket buffer
400 * @dev: associated net device
401 * @ptype: context
402 * @olddev: last device
403 *
404 * This function receives the packet and builds FC frame and passes it up
405 */
406static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
407 struct packet_type *ptype, struct net_device *olddev)
408{
409 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700410 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700411 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800412 struct fc_frame_header *fh;
413 struct fcoe_rcv_info *fr;
414 struct fcoe_percpu_s *bg;
Maurizio Lombardi01a4cc42014-11-20 11:17:33 +0100415 struct sk_buff *tmp_skb;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800416 unsigned short oxid;
417
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700418 interface = container_of(ptype, struct bnx2fc_interface,
419 fcoe_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -0700420 ctlr = bnx2fc_to_ctlr(interface);
421 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800422
423 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700424 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800425 goto err;
426 }
427
Maurizio Lombardi01a4cc42014-11-20 11:17:33 +0100428 tmp_skb = skb_share_check(skb, GFP_ATOMIC);
429 if (!tmp_skb)
430 goto err;
431
432 skb = tmp_skb;
433
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800434 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700435 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800436 goto err;
437 }
438
439 /*
440 * Check for minimum frame length, and make sure required FCoE
441 * and FC headers are pulled into the linear data area.
442 */
443 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
444 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
445 goto err;
446
447 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
448 fh = (struct fc_frame_header *) skb_transport_header(skb);
449
450 oxid = ntohs(fh->fh_ox_id);
451
452 fr = fcoe_dev_from_skb(skb);
453 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800454
455 bg = &bnx2fc_global;
Neil Hormanfc05ab72012-03-09 14:50:13 -0800456 spin_lock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800457
458 __skb_queue_tail(&bg->fcoe_rx_list, skb);
459 if (bg->fcoe_rx_list.qlen == 1)
460 wake_up_process(bg->thread);
461
Neil Hormanfc05ab72012-03-09 14:50:13 -0800462 spin_unlock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800463
464 return 0;
465err:
466 kfree_skb(skb);
467 return -1;
468}
469
470static int bnx2fc_l2_rcv_thread(void *arg)
471{
472 struct fcoe_percpu_s *bg = arg;
473 struct sk_buff *skb;
474
Dongsheng Yang8698a742014-03-11 18:09:12 +0800475 set_user_nice(current, MIN_NICE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800476 set_current_state(TASK_INTERRUPTIBLE);
477 while (!kthread_should_stop()) {
478 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800479 spin_lock_bh(&bg->fcoe_rx_list.lock);
480 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
481 spin_unlock_bh(&bg->fcoe_rx_list.lock);
482 bnx2fc_recv_frame(skb);
483 spin_lock_bh(&bg->fcoe_rx_list.lock);
484 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700485 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800486 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800487 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700488 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800489 return 0;
490}
491
492
493static void bnx2fc_recv_frame(struct sk_buff *skb)
494{
495 u32 fr_len;
496 struct fc_lport *lport;
497 struct fcoe_rcv_info *fr;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700498 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800499 struct fc_frame_header *fh;
500 struct fcoe_crc_eof crc_eof;
501 struct fc_frame *fp;
502 struct fc_lport *vn_port;
503 struct fcoe_port *port;
504 u8 *mac = NULL;
505 u8 *dest_mac = NULL;
506 struct fcoe_hdr *hp;
507
508 fr = fcoe_dev_from_skb(skb);
509 lport = fr->fr_dev;
510 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700511 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800512 kfree_skb(skb);
513 return;
514 }
515
516 if (skb_is_nonlinear(skb))
517 skb_linearize(skb);
518 mac = eth_hdr(skb)->h_source;
519 dest_mac = eth_hdr(skb)->h_dest;
520
521 /* Pull the header */
522 hp = (struct fcoe_hdr *) skb_network_header(skb);
523 fh = (struct fc_frame_header *) skb_transport_header(skb);
524 skb_pull(skb, sizeof(struct fcoe_hdr));
525 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
526
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800527 fp = (struct fc_frame *)skb;
528 fc_frame_init(fp);
529 fr_dev(fp) = lport;
530 fr_sof(fp) = hp->fcoe_sof;
531 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800532 kfree_skb(skb);
533 return;
534 }
535 fr_eof(fp) = crc_eof.fcoe_eof;
536 fr_crc(fp) = crc_eof.fcoe_crc32;
537 if (pskb_trim(skb, fr_len)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800538 kfree_skb(skb);
539 return;
540 }
541
542 fh = fc_frame_header_get(fp);
543
544 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
545 if (vn_port) {
546 port = lport_priv(vn_port);
Joe Perches6942df72013-09-02 03:32:33 +0000547 if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800548 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800549 kfree_skb(skb);
550 return;
551 }
552 }
553 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
554 fh->fh_type == FC_TYPE_FCP) {
555 /* Drop FCP data. We dont this in L2 path */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800556 kfree_skb(skb);
557 return;
558 }
559 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
560 fh->fh_type == FC_TYPE_ELS) {
561 switch (fc_frame_payload_op(fp)) {
562 case ELS_LOGO:
563 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
564 /* drop non-FIP LOGO */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800565 kfree_skb(skb);
566 return;
567 }
568 break;
569 }
570 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700571
572 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
573 /* Drop incoming ABTS */
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700574 kfree_skb(skb);
575 return;
576 }
577
Neil Hormand576a5e2014-06-23 10:41:02 -0400578 stats = per_cpu_ptr(lport->stats, smp_processor_id());
579 stats->RxFrames++;
580 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
581
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800582 if (le32_to_cpu(fr_crc(fp)) !=
583 ~crc32(~0, skb->data, fr_len)) {
584 if (stats->InvalidCRCCount < 5)
585 printk(KERN_WARNING PFX "dropping frame with "
586 "CRC error\n");
587 stats->InvalidCRCCount++;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800588 kfree_skb(skb);
589 return;
590 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800591 fc_exch_recv(lport, fp);
592}
593
594/**
595 * bnx2fc_percpu_io_thread - thread per cpu for ios
596 *
597 * @arg: ptr to bnx2fc_percpu_info structure
598 */
599int bnx2fc_percpu_io_thread(void *arg)
600{
601 struct bnx2fc_percpu_s *p = arg;
602 struct bnx2fc_work *work, *tmp;
603 LIST_HEAD(work_list);
604
Dongsheng Yang8698a742014-03-11 18:09:12 +0800605 set_user_nice(current, MIN_NICE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800606 set_current_state(TASK_INTERRUPTIBLE);
607 while (!kthread_should_stop()) {
608 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800609 spin_lock_bh(&p->fp_work_lock);
610 while (!list_empty(&p->work_list)) {
611 list_splice_init(&p->work_list, &work_list);
612 spin_unlock_bh(&p->fp_work_lock);
613
614 list_for_each_entry_safe(work, tmp, &work_list, list) {
615 list_del_init(&work->list);
616 bnx2fc_process_cq_compl(work->tgt, work->wqe);
617 kfree(work);
618 }
619
620 spin_lock_bh(&p->fp_work_lock);
621 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700622 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800623 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800624 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700625 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800626
627 return 0;
628}
629
630static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
631{
632 struct fc_host_statistics *bnx2fc_stats;
633 struct fc_lport *lport = shost_priv(shost);
634 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700635 struct bnx2fc_interface *interface = port->priv;
636 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800637 struct fcoe_statistics_params *fw_stats;
638 int rc = 0;
639
640 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
641 if (!fw_stats)
642 return NULL;
643
644 bnx2fc_stats = fc_get_host_stats(shost);
645
646 init_completion(&hba->stat_req_done);
647 if (bnx2fc_send_stat_req(hba))
648 return bnx2fc_stats;
649 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
650 if (!rc) {
651 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
652 return bnx2fc_stats;
653 }
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800654 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
655 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
656 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
657 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
658 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
659 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
660 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
661 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
662 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
663 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800664
665 bnx2fc_stats->dumped_frames = 0;
666 bnx2fc_stats->lip_count = 0;
667 bnx2fc_stats->nos_count = 0;
668 bnx2fc_stats->loss_of_sync_count = 0;
669 bnx2fc_stats->loss_of_signal_count = 0;
670 bnx2fc_stats->prim_seq_protocol_err_count = 0;
671
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800672 memcpy(&hba->prev_stats, hba->stats_buffer,
673 sizeof(struct fcoe_statistics_params));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800674 return bnx2fc_stats;
675}
676
677static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
678{
679 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700680 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800681 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800682 struct Scsi_Host *shost = lport->host;
683 int rc = 0;
684
685 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
686 shost->max_lun = BNX2FC_MAX_LUN;
687 shost->max_id = BNX2FC_MAX_FCP_TGT;
688 shost->max_channel = 0;
689 if (lport->vport)
690 shost->transportt = bnx2fc_vport_xport_template;
691 else
692 shost->transportt = bnx2fc_transport_template;
693
694 /* Add the new host to SCSI-ml */
695 rc = scsi_add_host(lport->host, dev);
696 if (rc) {
697 printk(KERN_ERR PFX "Error on scsi_add_host\n");
698 return rc;
699 }
700 if (!lport->vport)
701 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800702 snprintf(fc_host_symbolic_name(lport->host), 256,
Saurav Kashyap17d87c42014-07-03 08:18:28 -0400703 "%s (QLogic %s) v%s over %s",
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800704 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700705 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800706
707 return 0;
708}
709
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800710static int bnx2fc_link_ok(struct fc_lport *lport)
711{
712 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700713 struct bnx2fc_interface *interface = port->priv;
714 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800715 struct net_device *dev = hba->phys_dev;
716 int rc = 0;
717
718 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
719 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
720 else {
721 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
722 rc = -1;
723 }
724 return rc;
725}
726
727/**
728 * bnx2fc_get_link_state - get network link state
729 *
730 * @hba: adapter instance pointer
731 *
732 * updates adapter structure flag based on netdev state
733 */
734void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
735{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700736 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800737 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
738 else
739 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
740}
741
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700742static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800743{
744 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700745 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700746 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800747 struct fcoe_port *port;
748 u64 wwnn, wwpn;
749
750 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700751 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700752 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700753 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800754
755 /* require support for get_pauseparam ethtool op. */
756 if (!hba->phys_dev->ethtool_ops ||
757 !hba->phys_dev->ethtool_ops->get_pauseparam)
758 return -EOPNOTSUPP;
759
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700760 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800761 return -EINVAL;
762
763 skb_queue_head_init(&port->fcoe_pending_queue);
764 port->fcoe_pending_queue_active = 0;
765 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
766
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000767 fcoe_link_speed_update(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800768
769 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700770 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700771 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700772 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800773 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
774 fc_set_wwnn(lport, wwnn);
775
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700776 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700777 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700778 2, 0);
779
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800780 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
781 fc_set_wwpn(lport, wwpn);
782 }
783
784 return 0;
785}
786
787static void bnx2fc_destroy_timer(unsigned long data)
788{
789 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
790
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700791 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
792 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700793 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800794 wake_up_interruptible(&hba->destroy_wait);
795}
796
797/**
798 * bnx2fc_indicate_netevent - Generic netdev event handler
799 *
800 * @context: adapter structure pointer
801 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000802 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800803 *
804 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700805 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800806 */
Michael Chan415199f2011-07-20 14:55:24 +0000807static void bnx2fc_indicate_netevent(void *context, unsigned long event,
808 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800809{
810 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Robert Love6e89ea32012-11-27 06:53:40 +0000811 struct fcoe_ctlr_device *cdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700812 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800813 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700814 struct bnx2fc_interface *interface, *tmp;
Robert Lovefd8f8902012-05-22 19:06:16 -0700815 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700816 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800817 u32 link_possible = 1;
818
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700819 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000820 return;
821
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800822 switch (event) {
823 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800824 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
825 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700826 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800827 break;
828
829 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800830 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
831 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
832 link_possible = 0;
833 break;
834
835 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800836 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
837 link_possible = 0;
838 break;
839
840 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800841 break;
842
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700843 case NETDEV_UNREGISTER:
844 if (!vlan_id)
845 return;
846 mutex_lock(&bnx2fc_dev_lock);
847 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700848 if (interface->hba == hba &&
849 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
850 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700851 }
852 mutex_unlock(&bnx2fc_dev_lock);
Eddie Wai06c4f202013-12-11 15:30:21 -0800853
854 /* Ensure ALL destroy work has been completed before return */
855 flush_workqueue(bnx2fc_wq);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700856 return;
857
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800858 default:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800859 return;
860 }
861
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700862 mutex_lock(&bnx2fc_dev_lock);
863 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800864
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700865 if (interface->hba != hba)
866 continue;
867
Robert Lovefd8f8902012-05-22 19:06:16 -0700868 ctlr = bnx2fc_to_ctlr(interface);
869 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700870 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
871 interface->netdev->name, event);
872
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000873 fcoe_link_speed_update(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700874
Robert Love6e89ea32012-11-27 06:53:40 +0000875 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700876
877 if (link_possible && !bnx2fc_link_ok(lport)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000878 switch (cdev->enabled) {
879 case FCOE_CTLR_DISABLED:
880 pr_info("Link up while interface is disabled.\n");
881 break;
882 case FCOE_CTLR_ENABLED:
883 case FCOE_CTLR_UNUSED:
884 /* Reset max recv frame size to default */
885 fc_set_mfs(lport, BNX2FC_MFS);
886 /*
887 * ctlr link up will only be handled during
888 * enable to avoid sending discovery
889 * solicitation on a stale vlan
890 */
891 if (interface->enabled)
892 fcoe_ctlr_link_up(ctlr);
893 };
Robert Lovefd8f8902012-05-22 19:06:16 -0700894 } else if (fcoe_ctlr_link_down(ctlr)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000895 switch (cdev->enabled) {
896 case FCOE_CTLR_DISABLED:
897 pr_info("Link down while interface is disabled.\n");
898 break;
899 case FCOE_CTLR_ENABLED:
900 case FCOE_CTLR_UNUSED:
901 mutex_lock(&lport->lp_mutex);
902 list_for_each_entry(vport, &lport->vports, list)
903 fc_host_port_type(vport->host) =
904 FC_PORTTYPE_UNKNOWN;
905 mutex_unlock(&lport->lp_mutex);
906 fc_host_port_type(lport->host) =
907 FC_PORTTYPE_UNKNOWN;
908 per_cpu_ptr(lport->stats,
909 get_cpu())->LinkFailureCount++;
910 put_cpu();
911 fcoe_clean_pending_queue(lport);
912 wait_for_upload = 1;
913 };
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800914 }
915 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700916 mutex_unlock(&bnx2fc_dev_lock);
917
918 if (wait_for_upload) {
919 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
920 init_waitqueue_head(&hba->shutdown_wait);
921 BNX2FC_MISC_DBG("indicate_netevent "
922 "num_ofld_sess = %d\n",
923 hba->num_ofld_sess);
924 hba->wait_for_link_down = 1;
925 wait_event_interruptible(hba->shutdown_wait,
926 (hba->num_ofld_sess == 0));
927 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
928 hba->num_ofld_sess);
929 hba->wait_for_link_down = 0;
930
931 if (signal_pending(current))
932 flush_signals(current);
933 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800934}
935
936static int bnx2fc_libfc_config(struct fc_lport *lport)
937{
938
939 /* Set the function pointers set by bnx2fc driver */
940 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
941 sizeof(struct libfc_function_template));
942 fc_elsct_init(lport);
943 fc_exch_init(lport);
944 fc_rport_init(lport);
Robert Love08076192013-03-25 11:00:28 -0700945 fc_disc_init(lport);
946 fc_disc_config(lport, lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800947 return 0;
948}
949
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000950static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800951{
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000952 int fcoe_min_xid, fcoe_max_xid;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800953
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000954 fcoe_min_xid = hba->max_xid + 1;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800955 if (nr_cpu_ids <= 2)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000956 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800957 else
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000958 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
959 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
960 fcoe_max_xid, NULL)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800961 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
962 return -ENOMEM;
963 }
964
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800965 return 0;
966}
967
968static int bnx2fc_lport_config(struct fc_lport *lport)
969{
970 lport->link_up = 0;
971 lport->qfull = 0;
Bhanu Prakash Gollapudi44c570b2012-01-23 18:00:47 -0800972 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
973 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800974 lport->e_d_tov = 2 * 1000;
975 lport->r_a_tov = 10 * 1000;
976
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800977 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
978 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800979 lport->does_npiv = 1;
980
981 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
982 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
983
984 /* alloc stats structure */
985 if (fc_lport_init_stats(lport))
986 return -ENOMEM;
987
988 /* Finish fc_lport configuration */
989 fc_lport_config(lport);
990
991 return 0;
992}
993
994/**
995 * bnx2fc_fip_recv - handle a received FIP frame.
996 *
997 * @skb: the received skb
998 * @dev: associated &net_device
999 * @ptype: the &packet_type structure which was used to register this handler.
1000 * @orig_dev: original receive &net_device, in case @ dev is a bond.
1001 *
1002 * Returns: 0 for success
1003 */
1004static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1005 struct packet_type *ptype,
1006 struct net_device *orig_dev)
1007{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001008 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001009 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001010 interface = container_of(ptype, struct bnx2fc_interface,
1011 fip_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -07001012 ctlr = bnx2fc_to_ctlr(interface);
1013 fcoe_ctlr_recv(ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001014 return 0;
1015}
1016
1017/**
1018 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1019 *
1020 * @fip: FCoE controller.
1021 * @old: Unicast MAC address to delete if the MAC is non-zero.
1022 * @new: Unicast MAC address to add.
1023 *
1024 * Remove any previously-set unicast MAC filter.
1025 * Add secondary FCoE MAC address filter for our OUI.
1026 */
1027static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1028{
1029 struct fcoe_port *port = lport_priv(lport);
1030
1031 memcpy(port->data_src_addr, addr, ETH_ALEN);
1032}
1033
1034/**
1035 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1036 *
1037 * @lport: libfc port
1038 */
1039static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1040{
1041 struct fcoe_port *port;
1042
1043 port = (struct fcoe_port *)lport_priv(lport);
1044 return port->data_src_addr;
1045}
1046
1047/**
1048 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1049 *
1050 * @fip: FCoE controller.
1051 * @skb: FIP Packet.
1052 */
1053static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1054{
1055 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1056 dev_queue_xmit(skb);
1057}
1058
1059static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1060{
1061 struct Scsi_Host *shost = vport_to_shost(vport);
1062 struct fc_lport *n_port = shost_priv(shost);
1063 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001064 struct bnx2fc_interface *interface = port->priv;
1065 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001066 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001067 int rc;
1068 char buf[32];
1069
1070 rc = fcoe_validate_vport_create(vport);
1071 if (rc) {
1072 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1073 printk(KERN_ERR PFX "Failed to create vport, "
1074 "WWPN (0x%s) already exists\n",
1075 buf);
1076 return rc;
1077 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001078
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001079 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001080 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001081 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001082 return -EIO;
1083 }
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001084 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001085 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001086 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001087 mutex_unlock(&bnx2fc_dev_lock);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001088 rtnl_unlock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001089
Dan Carpenterb6829c72014-11-04 13:37:17 +03001090 if (!vn_port) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001091 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1092 netdev->name);
1093 return -EIO;
1094 }
1095
1096 if (disabled) {
1097 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1098 } else {
1099 vn_port->boot_time = jiffies;
1100 fc_lport_init(vn_port);
1101 fc_fabric_login(vn_port);
1102 fc_vport_setlink(vn_port);
1103 }
1104 return 0;
1105}
1106
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001107static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1108{
1109 struct bnx2fc_lport *blport, *tmp;
1110
1111 spin_lock_bh(&hba->hba_lock);
1112 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1113 if (blport->lport == lport) {
1114 list_del(&blport->list);
1115 kfree(blport);
1116 }
1117 }
1118 spin_unlock_bh(&hba->hba_lock);
1119}
1120
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001121static int bnx2fc_vport_destroy(struct fc_vport *vport)
1122{
1123 struct Scsi_Host *shost = vport_to_shost(vport);
1124 struct fc_lport *n_port = shost_priv(shost);
1125 struct fc_lport *vn_port = vport->dd_data;
1126 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001127 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001128 struct fc_lport *v_port;
1129 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001130
1131 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001132 list_for_each_entry(v_port, &n_port->vports, list)
1133 if (v_port->vport == vport) {
1134 found = true;
1135 break;
1136 }
1137
1138 if (!found) {
1139 mutex_unlock(&n_port->lp_mutex);
1140 return -ENOENT;
1141 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001142 list_del(&vn_port->list);
1143 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001144 bnx2fc_free_vport(interface->hba, port->lport);
1145 bnx2fc_port_shutdown(port->lport);
1146 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001147 queue_work(bnx2fc_wq, &port->destroy_work);
1148 return 0;
1149}
1150
1151static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1152{
1153 struct fc_lport *lport = vport->dd_data;
1154
1155 if (disable) {
1156 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1157 fc_fabric_logoff(lport);
1158 } else {
1159 lport->boot_time = jiffies;
1160 fc_fabric_login(lport);
1161 fc_vport_setlink(lport);
1162 }
1163 return 0;
1164}
1165
1166
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001167static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001168{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001169 struct net_device *netdev = interface->netdev;
1170 struct net_device *physdev = interface->hba->phys_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001171 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001172 struct netdev_hw_addr *ha;
1173 int sel_san_mac = 0;
1174
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001175 /* setup Source MAC Address */
1176 rcu_read_lock();
1177 for_each_dev_addr(physdev, ha) {
1178 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1179 ha->type);
1180 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1181 ha->addr[1], ha->addr[2], ha->addr[3],
1182 ha->addr[4], ha->addr[5]);
1183
1184 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1185 (is_valid_ether_addr(ha->addr))) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001186 memcpy(ctlr->ctl_src_addr, ha->addr,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001187 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001188 sel_san_mac = 1;
1189 BNX2FC_MISC_DBG("Found SAN MAC\n");
1190 }
1191 }
1192 rcu_read_unlock();
1193
1194 if (!sel_san_mac)
1195 return -ENODEV;
1196
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001197 interface->fip_packet_type.func = bnx2fc_fip_recv;
1198 interface->fip_packet_type.type = htons(ETH_P_FIP);
1199 interface->fip_packet_type.dev = netdev;
1200 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001201
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001202 interface->fcoe_packet_type.func = bnx2fc_rcv;
1203 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1204 interface->fcoe_packet_type.dev = netdev;
1205 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001206
1207 return 0;
1208}
1209
1210static int bnx2fc_attach_transport(void)
1211{
1212 bnx2fc_transport_template =
1213 fc_attach_transport(&bnx2fc_transport_function);
1214
1215 if (bnx2fc_transport_template == NULL) {
1216 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1217 return -ENODEV;
1218 }
1219
1220 bnx2fc_vport_xport_template =
1221 fc_attach_transport(&bnx2fc_vport_xport_function);
1222 if (bnx2fc_vport_xport_template == NULL) {
1223 printk(KERN_ERR PFX
1224 "Failed to attach FC transport for vport\n");
1225 fc_release_transport(bnx2fc_transport_template);
1226 bnx2fc_transport_template = NULL;
1227 return -ENODEV;
1228 }
1229 return 0;
1230}
1231static void bnx2fc_release_transport(void)
1232{
1233 fc_release_transport(bnx2fc_transport_template);
1234 fc_release_transport(bnx2fc_vport_xport_template);
1235 bnx2fc_transport_template = NULL;
1236 bnx2fc_vport_xport_template = NULL;
1237}
1238
1239static void bnx2fc_interface_release(struct kref *kref)
1240{
Robert Love8d55e502012-05-22 19:06:26 -07001241 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001242 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001243 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001244 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001245
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001246 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001247 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001248
Robert Lovefd8f8902012-05-22 19:06:16 -07001249 ctlr = bnx2fc_to_ctlr(interface);
Robert Love8d55e502012-05-22 19:06:26 -07001250 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001251 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001252
1253 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001254 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
Robert Lovefd8f8902012-05-22 19:06:16 -07001255 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001256
Robert Love8d55e502012-05-22 19:06:26 -07001257 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001258
1259 dev_put(netdev);
1260 module_put(THIS_MODULE);
1261}
1262
1263static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1264{
1265 kref_get(&interface->kref);
1266}
1267
1268static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1269{
1270 kref_put(&interface->kref, bnx2fc_interface_release);
1271}
1272static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1273{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001274 /* Free the command manager */
1275 if (hba->cmd_mgr) {
1276 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1277 hba->cmd_mgr = NULL;
1278 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001279 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001280 bnx2fc_unbind_pcidev(hba);
1281 kfree(hba);
1282}
1283
1284/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001285 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001286 *
1287 * @cnic: pointer to cnic device
1288 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001289 * Creates a new FCoE hba on the given device.
1290 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001291 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001292static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001293{
1294 struct bnx2fc_hba *hba;
Barak Witkowski2e499d32012-06-26 01:31:19 +00001295 struct fcoe_capabilities *fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001296 int rc;
1297
1298 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1299 if (!hba) {
1300 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1301 return NULL;
1302 }
1303 spin_lock_init(&hba->hba_lock);
1304 mutex_init(&hba->hba_mutex);
1305
1306 hba->cnic = cnic;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001307
1308 hba->max_tasks = cnic->max_fcoe_exchanges;
1309 hba->elstm_xids = (hba->max_tasks / 2);
1310 hba->max_outstanding_cmds = hba->elstm_xids;
1311 hba->max_xid = (hba->max_tasks - 1);
1312
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001313 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001314 if (rc) {
1315 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001316 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001317 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001318 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001319 hba->next_conn_id = 0;
1320
1321 hba->tgt_ofld_list =
1322 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1323 GFP_KERNEL);
1324 if (!hba->tgt_ofld_list) {
1325 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1326 goto tgtofld_err;
1327 }
1328
1329 hba->num_ofld_sess = 0;
1330
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001331 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001332 if (!hba->cmd_mgr) {
1333 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1334 goto cmgr_err;
1335 }
Barak Witkowski2e499d32012-06-26 01:31:19 +00001336 fcoe_cap = &hba->fcoe_cap;
1337
1338 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1339 FCOE_IOS_PER_CONNECTION_SHIFT;
1340 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1341 FCOE_LOGINS_PER_PORT_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001342 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001343 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1344 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1345 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1346 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1347 FCOE_TARGETS_SUPPORTED_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001348 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001349 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1350 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001351
1352 init_waitqueue_head(&hba->shutdown_wait);
1353 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001354 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001355
1356 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001357
1358cmgr_err:
1359 kfree(hba->tgt_ofld_list);
1360tgtofld_err:
1361 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001362bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001363 kfree(hba);
1364 return NULL;
1365}
1366
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001367struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1368 struct net_device *netdev,
1369 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001370{
Robert Love8d55e502012-05-22 19:06:26 -07001371 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001372 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001373 struct fcoe_ctlr *ctlr;
1374 int size;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001375 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001376
Robert Lovefd8f8902012-05-22 19:06:16 -07001377 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
Robert Love8d55e502012-05-22 19:06:26 -07001378 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1379 size);
1380 if (!ctlr_dev) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001381 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1382 return NULL;
1383 }
Robert Love8d55e502012-05-22 19:06:26 -07001384 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
Robert Love9d348762013-09-05 07:47:27 +00001385 ctlr->cdev = ctlr_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001386 interface = fcoe_ctlr_priv(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001387 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001388 kref_init(&interface->kref);
1389 interface->hba = hba;
1390 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001391
1392 /* Initialize FIP */
Robert Lovefd8f8902012-05-22 19:06:16 -07001393 fcoe_ctlr_init(ctlr, fip_mode);
1394 ctlr->send = bnx2fc_fip_send;
1395 ctlr->update_mac = bnx2fc_update_src_mac;
1396 ctlr->get_src_addr = bnx2fc_get_src_mac;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001397 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001398
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001399 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001400 if (!rc)
1401 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001402
Robert Lovefd8f8902012-05-22 19:06:16 -07001403 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001404 dev_put(netdev);
Robert Love8d55e502012-05-22 19:06:26 -07001405 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001406 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001407}
1408
1409/**
1410 * bnx2fc_if_create - Create FCoE instance on a given interface
1411 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001412 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001413 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1414 * @npiv: Indicates if the port is vport or not
1415 *
1416 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1417 *
1418 * Returns: Allocated fc_lport or an error pointer
1419 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001420static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001421 struct device *parent, int npiv)
1422{
Robert Lovefd8f8902012-05-22 19:06:16 -07001423 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Vasu Dev134a4e22011-04-28 15:55:44 -07001424 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001425 struct fcoe_port *port;
1426 struct Scsi_Host *shost;
1427 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001428 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001429 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001430 int rc = 0;
1431
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001432 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1433 if (!blport) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001434 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001435 return NULL;
1436 }
1437
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001438 /* Allocate Scsi_Host structure */
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001439 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
Vasu Dev134a4e22011-04-28 15:55:44 -07001440 if (!npiv)
1441 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1442 else
1443 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001444
1445 if (!lport) {
1446 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001447 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001448 }
1449 shost = lport->host;
1450 port = lport_priv(lport);
1451 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001452 port->priv = interface;
Yi Zouc3d79092012-12-06 06:24:29 +00001453 port->get_netdev = bnx2fc_netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001454 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1455
1456 /* Configure fcoe_port */
1457 rc = bnx2fc_lport_config(lport);
1458 if (rc)
1459 goto lp_config_err;
1460
1461 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001462 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1463 vport->node_name, vport->port_name);
1464 fc_set_wwnn(lport, vport->node_name);
1465 fc_set_wwpn(lport, vport->port_name);
1466 }
1467 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001468 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001469 if (rc) {
1470 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1471 goto lp_config_err;
1472 }
1473
1474 rc = bnx2fc_shost_config(lport, parent);
1475 if (rc) {
1476 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001477 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001478 goto lp_config_err;
1479 }
1480
1481 /* Initialize the libfc library */
1482 rc = bnx2fc_libfc_config(lport);
1483 if (rc) {
1484 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1485 goto shost_err;
1486 }
1487 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1488
1489 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001490 if (!npiv)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001491 rc = bnx2fc_em_config(lport, hba);
Vasu Dev134a4e22011-04-28 15:55:44 -07001492 else {
1493 shost = vport_to_shost(vport);
1494 n_port = shost_priv(shost);
1495 rc = fc_exch_mgr_list_clone(n_port, lport);
1496 }
1497
1498 if (rc) {
1499 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1500 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001501 }
1502
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001503 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001504
1505 spin_lock_bh(&hba->hba_lock);
1506 blport->lport = lport;
1507 list_add_tail(&blport->list, &hba->vports);
1508 spin_unlock_bh(&hba->hba_lock);
1509
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001510 return lport;
1511
1512shost_err:
1513 scsi_remove_host(shost);
1514lp_config_err:
1515 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001516free_blport:
1517 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001518 return NULL;
1519}
1520
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001521static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001522{
1523 /* Dont listen for Ethernet packets anymore */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001524 __dev_remove_pack(&interface->fcoe_packet_type);
1525 __dev_remove_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001526 synchronize_net();
1527}
1528
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001529static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001530{
Robert Lovefd8f8902012-05-22 19:06:16 -07001531 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1532 struct fc_lport *lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001533 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001534 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001535
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001536 /* Stop the transmit retry timer */
1537 del_timer_sync(&port->timer);
1538
1539 /* Free existing transmit skbs */
1540 fcoe_clean_pending_queue(lport);
1541
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001542 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001543
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001544 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001545}
1546
1547static void bnx2fc_if_destroy(struct fc_lport *lport)
1548{
1549
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001550 /* Free queued packets for the receive thread */
1551 bnx2fc_clean_rx_queue(lport);
1552
1553 /* Detach from scsi-ml */
1554 fc_remove_host(lport->host);
1555 scsi_remove_host(lport->host);
1556
1557 /*
1558 * Note that only the physical lport will have the exchange manager.
1559 * for vports, this function is NOP
1560 */
1561 fc_exch_mgr_free(lport);
1562
1563 /* Free memory used by statistical counters */
1564 fc_lport_free_stats(lport);
1565
1566 /* Release Scsi_Host */
1567 scsi_host_put(lport->host);
1568}
1569
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001570static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001571{
Robert Lovefd8f8902012-05-22 19:06:16 -07001572 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1573 struct fc_lport *lport = ctlr->lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001574 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001575
1576 bnx2fc_interface_cleanup(interface);
1577 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001578 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001579 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001580 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001581}
1582
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001583/**
1584 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1585 *
1586 * @buffer: The name of the Ethernet interface to be destroyed
1587 * @kp: The associated kernel parameter
1588 *
1589 * Called from sysfs.
1590 *
1591 * Returns: 0 for success
1592 */
1593static int bnx2fc_destroy(struct net_device *netdev)
1594{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001595 struct bnx2fc_interface *interface = NULL;
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001596 struct workqueue_struct *timer_work_queue;
Robert Lovefd8f8902012-05-22 19:06:16 -07001597 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001598 int rc = 0;
1599
Nithin Sujir6702ca12011-03-17 17:13:27 -07001600 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001601 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001602
1603 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001604 ctlr = bnx2fc_to_ctlr(interface);
1605 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001606 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001607 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001608 goto netdev_err;
1609 }
1610
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001611 timer_work_queue = interface->timer_work_queue;
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001612 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001613 destroy_workqueue(timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001614
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001615netdev_err:
1616 mutex_unlock(&bnx2fc_dev_lock);
1617 rtnl_unlock();
1618 return rc;
1619}
1620
1621static void bnx2fc_destroy_work(struct work_struct *work)
1622{
1623 struct fcoe_port *port;
1624 struct fc_lport *lport;
1625
1626 port = container_of(work, struct fcoe_port, destroy_work);
1627 lport = port->lport;
1628
1629 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1630
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001631 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001632}
1633
1634static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1635{
1636 bnx2fc_free_fw_resc(hba);
1637 bnx2fc_free_task_ctx(hba);
1638}
1639
1640/**
1641 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1642 * pci structure
1643 *
1644 * @hba: Adapter instance
1645 */
1646static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1647{
1648 if (bnx2fc_setup_task_ctx(hba))
1649 goto mem_err;
1650
1651 if (bnx2fc_setup_fw_resc(hba))
1652 goto mem_err;
1653
1654 return 0;
1655mem_err:
1656 bnx2fc_unbind_adapter_devices(hba);
1657 return -ENOMEM;
1658}
1659
1660static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1661{
1662 struct cnic_dev *cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001663 struct pci_dev *pdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001664
1665 if (!hba->cnic) {
1666 printk(KERN_ERR PFX "cnic is NULL\n");
1667 return -ENODEV;
1668 }
1669 cnic = hba->cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001670 pdev = hba->pcidev = cnic->pcidev;
1671 if (!hba->pcidev)
1672 return -ENODEV;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001673
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001674 switch (pdev->device) {
1675 case PCI_DEVICE_ID_NX2_57710:
1676 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1677 break;
1678 case PCI_DEVICE_ID_NX2_57711:
1679 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1680 break;
1681 case PCI_DEVICE_ID_NX2_57712:
1682 case PCI_DEVICE_ID_NX2_57712_MF:
1683 case PCI_DEVICE_ID_NX2_57712_VF:
1684 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1685 break;
1686 case PCI_DEVICE_ID_NX2_57800:
1687 case PCI_DEVICE_ID_NX2_57800_MF:
1688 case PCI_DEVICE_ID_NX2_57800_VF:
1689 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1690 break;
1691 case PCI_DEVICE_ID_NX2_57810:
1692 case PCI_DEVICE_ID_NX2_57810_MF:
1693 case PCI_DEVICE_ID_NX2_57810_VF:
1694 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1695 break;
1696 case PCI_DEVICE_ID_NX2_57840:
1697 case PCI_DEVICE_ID_NX2_57840_MF:
1698 case PCI_DEVICE_ID_NX2_57840_VF:
1699 case PCI_DEVICE_ID_NX2_57840_2_20:
1700 case PCI_DEVICE_ID_NX2_57840_4_10:
1701 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1702 break;
1703 default:
1704 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1705 break;
1706 }
1707 pci_dev_get(hba->pcidev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001708 return 0;
1709}
1710
1711static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1712{
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001713 if (hba->pcidev) {
1714 hba->chip_num[0] = '\0';
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001715 pci_dev_put(hba->pcidev);
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001716 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001717 hba->pcidev = NULL;
1718}
1719
Barak Witkowski2e499d32012-06-26 01:31:19 +00001720/**
1721 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1722 *
1723 * @handle: transport handle pointing to adapter struture
1724 */
1725static int bnx2fc_ulp_get_stats(void *handle)
1726{
1727 struct bnx2fc_hba *hba = handle;
1728 struct cnic_dev *cnic;
1729 struct fcoe_stats_info *stats_addr;
1730
1731 if (!hba)
1732 return -EINVAL;
1733
1734 cnic = hba->cnic;
1735 stats_addr = &cnic->stats_addr->fcoe_stat;
1736 if (!stats_addr)
1737 return -EINVAL;
1738
1739 strncpy(stats_addr->version, BNX2FC_VERSION,
1740 sizeof(stats_addr->version));
1741 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1742 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1743
1744 return 0;
1745}
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001746
1747
1748/**
1749 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1750 *
Masanari Iida59e13d42012-04-25 00:24:16 +09001751 * @handle: transport handle pointing to adapter structure
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001752 *
1753 * This function maps adapter structure to pcidev structure and initiates
1754 * firmware handshake to enable/initialize on-chip FCoE components.
1755 * This bnx2fc - cnic interface api callback is used after following
1756 * conditions are met -
1757 * a) underlying network interface is up (marked by event NETDEV_UP
1758 * from netdev
1759 * b) bnx2fc adatper structure is registered.
1760 */
1761static void bnx2fc_ulp_start(void *handle)
1762{
1763 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001764 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001765 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001766 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001767
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001768 mutex_lock(&bnx2fc_dev_lock);
1769
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001770 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001771 bnx2fc_fw_init(hba);
1772
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001773 BNX2FC_MISC_DBG("bnx2fc started.\n");
1774
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001775 list_for_each_entry(interface, &if_list, list) {
1776 if (interface->hba == hba) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001777 ctlr = bnx2fc_to_ctlr(interface);
1778 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001779 /* Kick off Fabric discovery*/
1780 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1781 lport->tt.frame_send = bnx2fc_xmit;
1782 bnx2fc_start_disc(interface);
1783 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001784 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001785
1786 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001787}
1788
1789static void bnx2fc_port_shutdown(struct fc_lport *lport)
1790{
1791 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1792 fc_fabric_logoff(lport);
1793 fc_lport_destroy(lport);
1794}
1795
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001796static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001797{
Robert Lovefd8f8902012-05-22 19:06:16 -07001798 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001799 struct fc_lport *lport;
1800 struct fc_lport *vport;
1801
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001802 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1803 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001804
Robert Lovefd8f8902012-05-22 19:06:16 -07001805 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001806 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001807
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001808 mutex_lock(&lport->lp_mutex);
1809 list_for_each_entry(vport, &lport->vports, list)
1810 fc_host_port_type(vport->host) =
1811 FC_PORTTYPE_UNKNOWN;
1812 mutex_unlock(&lport->lp_mutex);
1813 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
Robert Lovefd8f8902012-05-22 19:06:16 -07001814 fcoe_ctlr_link_down(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001815 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001816}
1817
1818static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1819{
1820#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1821 int rc = -1;
1822 int i = HZ;
1823
1824 rc = bnx2fc_bind_adapter_devices(hba);
1825 if (rc) {
1826 printk(KERN_ALERT PFX
1827 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1828 goto err_out;
1829 }
1830
1831 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1832 if (rc) {
1833 printk(KERN_ALERT PFX
1834 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1835 goto err_unbind;
1836 }
1837
1838 /*
1839 * Wait until the adapter init message is complete, and adapter
1840 * state is UP.
1841 */
1842 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1843 msleep(BNX2FC_INIT_POLL_TIME);
1844
1845 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1846 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1847 "Ignoring...\n",
1848 hba->cnic->netdev->name);
1849 rc = -1;
1850 goto err_unbind;
1851 }
1852
1853
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001854 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001855 return 0;
1856
1857err_unbind:
1858 bnx2fc_unbind_adapter_devices(hba);
1859err_out:
1860 return rc;
1861}
1862
1863static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1864{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001865 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001866 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1867 init_timer(&hba->destroy_timer);
1868 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1869 jiffies;
1870 hba->destroy_timer.function = bnx2fc_destroy_timer;
1871 hba->destroy_timer.data = (unsigned long)hba;
1872 add_timer(&hba->destroy_timer);
1873 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001874 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1875 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001876 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001877 /* This should never happen */
1878 if (signal_pending(current))
1879 flush_signals(current);
1880
1881 del_timer_sync(&hba->destroy_timer);
1882 }
1883 bnx2fc_unbind_adapter_devices(hba);
1884 }
1885}
1886
1887/**
1888 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1889 *
1890 * @handle: transport handle pointing to adapter structure
1891 *
1892 * Driver checks if adapter is already in shutdown mode, if not start
1893 * the shutdown process.
1894 */
1895static void bnx2fc_ulp_stop(void *handle)
1896{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001897 struct bnx2fc_hba *hba = handle;
1898 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001899
1900 printk(KERN_ERR "ULP_STOP\n");
1901
1902 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001903 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1904 goto exit;
1905 list_for_each_entry(interface, &if_list, list) {
1906 if (interface->hba == hba)
1907 bnx2fc_stop(interface);
1908 }
1909 BUG_ON(hba->num_ofld_sess != 0);
1910
1911 mutex_lock(&hba->hba_mutex);
1912 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1913 clear_bit(ADAPTER_STATE_GOING_DOWN,
1914 &hba->adapter_state);
1915
1916 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1917 mutex_unlock(&hba->hba_mutex);
1918
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001919 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001920exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001921 mutex_unlock(&bnx2fc_dev_lock);
1922}
1923
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001924static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001925{
Robert Lovefd8f8902012-05-22 19:06:16 -07001926 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001927 struct fc_lport *lport;
1928 int wait_cnt = 0;
1929
1930 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1931 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001932 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001933 printk(KERN_ERR PFX "Init not done yet\n");
1934 return;
1935 }
1936
Robert Lovefd8f8902012-05-22 19:06:16 -07001937 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001938 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1939
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001940 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001941 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Robert Lovefd8f8902012-05-22 19:06:16 -07001942 fcoe_ctlr_link_up(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001943 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001944 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001945 }
1946
1947 /* wait for the FCF to be selected before issuing FLOGI */
Robert Lovefd8f8902012-05-22 19:06:16 -07001948 while (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001949 msleep(250);
1950 /* give up after 3 secs */
1951 if (++wait_cnt > 12)
1952 break;
1953 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001954
1955 /* Reset max receive frame size to default */
1956 if (fc_set_mfs(lport, BNX2FC_MFS))
1957 return;
1958
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001959 fc_lport_init(lport);
1960 fc_fabric_login(lport);
1961}
1962
1963
1964/**
1965 * bnx2fc_ulp_init - Initialize an adapter instance
1966 *
1967 * @dev : cnic device handle
1968 * Called from cnic_register_driver() context to initialize all
1969 * enumerated cnic devices. This routine allocates adapter structure
1970 * and other device specific resources.
1971 */
1972static void bnx2fc_ulp_init(struct cnic_dev *dev)
1973{
1974 struct bnx2fc_hba *hba;
1975 int rc = 0;
1976
1977 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1978 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001979 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1980 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001981 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001982 " flags: %lx fcoe_conn: %d\n",
1983 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001984 return;
1985 }
1986
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001987 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001988 if (!hba) {
1989 printk(KERN_ERR PFX "hba initialization failed\n");
1990 return;
1991 }
1992
1993 /* Add HBA to the adapter list */
1994 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001995 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001996 adapter_count++;
1997 mutex_unlock(&bnx2fc_dev_lock);
1998
Barak Witkowski2e499d32012-06-26 01:31:19 +00001999 dev->fcoe_cap = &hba->fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002000 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2001 rc = dev->register_device(dev, CNIC_ULP_FCOE,
2002 (void *) hba);
2003 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07002004 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002005 else
2006 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2007}
2008
Eddie Wai06808102013-09-25 22:01:20 -07002009/* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2010static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2011{
2012 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2013
2014 if (interface->enabled == true) {
2015 if (!ctlr->lp) {
2016 pr_err(PFX "__bnx2fc_disable: lport not found\n");
2017 return -ENODEV;
2018 } else {
2019 interface->enabled = false;
2020 fcoe_ctlr_link_down(ctlr);
2021 fcoe_clean_pending_queue(ctlr->lp);
2022 }
2023 }
2024 return 0;
2025}
2026
Robert Love6e89ea32012-11-27 06:53:40 +00002027/**
2028 * Deperecated: Use bnx2fc_enabled()
2029 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002030static int bnx2fc_disable(struct net_device *netdev)
2031{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002032 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002033 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002034 int rc = 0;
2035
Nithin Sujir6702ca12011-03-17 17:13:27 -07002036 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002037 mutex_lock(&bnx2fc_dev_lock);
2038
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002039 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002040 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002041
Eddie Wai06808102013-09-25 22:01:20 -07002042 if (!interface) {
2043 rc = -ENODEV;
2044 pr_err(PFX "bnx2fc_disable: interface not found\n");
2045 } else {
2046 rc = __bnx2fc_disable(ctlr);
2047 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002048 mutex_unlock(&bnx2fc_dev_lock);
2049 rtnl_unlock();
2050 return rc;
2051}
2052
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002053static uint bnx2fc_npiv_create_vports(struct fc_lport *lport,
2054 struct cnic_fc_npiv_tbl *npiv_tbl)
2055{
2056 struct fc_vport_identifiers vpid;
2057 uint i, created = 0;
2058
2059 if (npiv_tbl->count > MAX_NPIV_ENTRIES) {
2060 BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n");
2061 goto done;
2062 }
2063
2064 /* Sanity check the first entry to make sure it's not 0 */
2065 if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 &&
2066 wwn_to_u64(npiv_tbl->wwpn[0]) == 0) {
2067 BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n");
2068 goto done;
2069 }
2070
2071 vpid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2072 vpid.vport_type = FC_PORTTYPE_NPIV;
2073 vpid.disable = false;
2074
2075 for (i = 0; i < npiv_tbl->count; i++) {
2076 vpid.node_name = wwn_to_u64(npiv_tbl->wwnn[i]);
2077 vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]);
2078 scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name),
2079 "NPIV[%u]:%016llx-%016llx",
2080 created, vpid.port_name, vpid.node_name);
2081 if (fc_vport_create(lport->host, 0, &vpid))
2082 created++;
2083 else
2084 BNX2FC_HBA_DBG(lport, "Failed to create vport\n");
2085 }
2086done:
2087 return created;
2088}
2089
Eddie Wai06808102013-09-25 22:01:20 -07002090static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2091{
2092 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002093 struct bnx2fc_hba *hba;
2094 struct cnic_fc_npiv_tbl npiv_tbl;
2095 struct fc_lport *lport;
Eddie Wai06808102013-09-25 22:01:20 -07002096
2097 if (interface->enabled == false) {
2098 if (!ctlr->lp) {
2099 pr_err(PFX "__bnx2fc_enable: lport not found\n");
2100 return -ENODEV;
2101 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2102 fcoe_ctlr_link_up(ctlr);
2103 interface->enabled = true;
2104 }
2105 }
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002106
2107 /* Create static NPIV ports if any are contained in NVRAM */
2108 hba = interface->hba;
2109 lport = ctlr->lp;
2110
2111 if (!hba)
2112 goto done;
2113
2114 if (!hba->cnic)
2115 goto done;
2116
2117 if (!lport)
2118 goto done;
2119
2120 if (!lport->host)
2121 goto done;
2122
2123 if (!hba->cnic->get_fc_npiv_tbl)
2124 goto done;
2125
2126 memset(&npiv_tbl, 0, sizeof(npiv_tbl));
2127 if (hba->cnic->get_fc_npiv_tbl(hba->cnic, &npiv_tbl))
2128 goto done;
2129
2130 bnx2fc_npiv_create_vports(lport, &npiv_tbl);
2131done:
Eddie Wai06808102013-09-25 22:01:20 -07002132 return 0;
2133}
2134
Robert Love6e89ea32012-11-27 06:53:40 +00002135/**
2136 * Deprecated: Use bnx2fc_enabled()
2137 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002138static int bnx2fc_enable(struct net_device *netdev)
2139{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002140 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002141 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002142 int rc = 0;
2143
Nithin Sujir6702ca12011-03-17 17:13:27 -07002144 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002145 mutex_lock(&bnx2fc_dev_lock);
2146
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002147 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002148 ctlr = bnx2fc_to_ctlr(interface);
Eddie Wai06808102013-09-25 22:01:20 -07002149 if (!interface) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002150 rc = -ENODEV;
Eddie Wai06808102013-09-25 22:01:20 -07002151 pr_err(PFX "bnx2fc_enable: interface not found\n");
2152 } else {
2153 rc = __bnx2fc_enable(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002154 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002155
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002156 mutex_unlock(&bnx2fc_dev_lock);
2157 rtnl_unlock();
2158 return rc;
2159}
2160
2161/**
Robert Love6e89ea32012-11-27 06:53:40 +00002162 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2163 * @cdev: The FCoE Controller that is being enabled or disabled
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002164 *
Robert Love6e89ea32012-11-27 06:53:40 +00002165 * fcoe_sysfs will ensure that the state of 'enabled' has
2166 * changed, so no checking is necessary here. This routine simply
2167 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2168 * When those routines are removed the functionality can be merged
2169 * here.
2170 */
2171static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2172{
2173 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
Robert Love6e89ea32012-11-27 06:53:40 +00002174
2175 switch (cdev->enabled) {
2176 case FCOE_CTLR_ENABLED:
Eddie Wai06808102013-09-25 22:01:20 -07002177 return __bnx2fc_enable(ctlr);
Robert Love6e89ea32012-11-27 06:53:40 +00002178 case FCOE_CTLR_DISABLED:
Eddie Wai06808102013-09-25 22:01:20 -07002179 return __bnx2fc_disable(ctlr);
Robert Love6e89ea32012-11-27 06:53:40 +00002180 case FCOE_CTLR_UNUSED:
2181 default:
2182 return -ENOTSUPP;
2183 };
2184}
2185
2186enum bnx2fc_create_link_state {
2187 BNX2FC_CREATE_LINK_DOWN,
2188 BNX2FC_CREATE_LINK_UP,
2189};
2190
2191/**
2192 * _bnx2fc_create() - Create bnx2fc FCoE interface
2193 * @netdev : The net_device object the Ethernet interface to create on
2194 * @fip_mode: The FIP mode for this creation
2195 * @link_state: The ctlr link state on creation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002196 *
Robert Love6e89ea32012-11-27 06:53:40 +00002197 * Called from either the libfcoe 'create' module parameter
2198 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2199 *
2200 * libfcoe's 'create' module parameter is deprecated so some
2201 * consolidation of code can be done when that interface is
2202 * removed.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002203 *
2204 * Returns: 0 for success
2205 */
Robert Love6e89ea32012-11-27 06:53:40 +00002206static int _bnx2fc_create(struct net_device *netdev,
2207 enum fip_state fip_mode,
2208 enum bnx2fc_create_link_state link_state)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002209{
Robert Love6e89ea32012-11-27 06:53:40 +00002210 struct fcoe_ctlr_device *cdev;
Robert Lovefd8f8902012-05-22 19:06:16 -07002211 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002212 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002213 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002214 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002215 struct fc_lport *lport;
2216 struct ethtool_drvinfo drvinfo;
2217 int rc = 0;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002218 int vlan_id = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002219
2220 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2221 if (fip_mode != FIP_MODE_FABRIC) {
2222 printk(KERN_ERR "fip mode not FABRIC\n");
2223 return -EIO;
2224 }
2225
Nithin Sujir6702ca12011-03-17 17:13:27 -07002226 rtnl_lock();
2227
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002228 mutex_lock(&bnx2fc_dev_lock);
2229
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002230 if (!try_module_get(THIS_MODULE)) {
2231 rc = -EINVAL;
2232 goto mod_err;
2233 }
2234
2235 /* obtain physical netdev */
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002236 if (netdev->priv_flags & IFF_802_1Q_VLAN)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002237 phys_dev = vlan_dev_real_dev(netdev);
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002238
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002239 /* verify if the physical device is a netxtreme2 device */
2240 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2241 memset(&drvinfo, 0, sizeof(drvinfo));
2242 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002243 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002244 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2245 rc = -EINVAL;
2246 goto netdev_err;
2247 }
2248 } else {
2249 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2250 rc = -EINVAL;
2251 goto netdev_err;
2252 }
2253
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002254 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002255 hba = bnx2fc_hba_lookup(phys_dev);
2256 if (!hba) {
2257 rc = -ENODEV;
2258 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2259 goto netdev_err;
2260 }
2261
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002262 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002263 rc = -EEXIST;
2264 goto netdev_err;
2265 }
2266
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002267 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2268 if (!interface) {
2269 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Dan Carpenter2043e1f2014-11-04 13:37:59 +03002270 rc = -ENOMEM;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002271 goto ifput_err;
2272 }
2273
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002274 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2275 vlan_id = vlan_dev_vlan_id(netdev);
2276 interface->vlan_enabled = 1;
2277 }
2278
Robert Lovefd8f8902012-05-22 19:06:16 -07002279 ctlr = bnx2fc_to_ctlr(interface);
Robert Love01bdcb62013-03-25 11:00:26 -07002280 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002281 interface->vlan_id = vlan_id;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002282
2283 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002284 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002285 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002286 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2287 rc = -EINVAL;
2288 goto ifput_err;
2289 }
2290
Robert Love01bdcb62013-03-25 11:00:26 -07002291 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002292 if (!lport) {
2293 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2294 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002295 rc = -EINVAL;
2296 goto if_create_err;
2297 }
2298
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002299 /* Add interface to if_list */
2300 list_add_tail(&interface->list, &if_list);
2301
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002302 lport->boot_time = jiffies;
2303
2304 /* Make this master N_port */
Robert Lovefd8f8902012-05-22 19:06:16 -07002305 ctlr->lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002306
Robert Love6e89ea32012-11-27 06:53:40 +00002307 if (link_state == BNX2FC_CREATE_LINK_UP)
2308 cdev->enabled = FCOE_CTLR_ENABLED;
2309 else
2310 cdev->enabled = FCOE_CTLR_DISABLED;
2311
2312 if (link_state == BNX2FC_CREATE_LINK_UP &&
2313 !bnx2fc_link_ok(lport)) {
Robert Lovefd8f8902012-05-22 19:06:16 -07002314 fcoe_ctlr_link_up(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002315 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2316 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2317 }
2318
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002319 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2320 bnx2fc_start_disc(interface);
Robert Love6e89ea32012-11-27 06:53:40 +00002321
2322 if (link_state == BNX2FC_CREATE_LINK_UP)
2323 interface->enabled = true;
2324
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002325 /*
2326 * Release from kref_init in bnx2fc_interface_setup, on success
2327 * lport should be holding a reference taken in bnx2fc_if_create
2328 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002329 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002330 /* put netdev that was held while calling dev_get_by_name */
2331 mutex_unlock(&bnx2fc_dev_lock);
2332 rtnl_unlock();
2333 return 0;
2334
2335if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002336 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002337ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002338 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002339 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -08002340 goto mod_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002341netdev_err:
2342 module_put(THIS_MODULE);
2343mod_err:
2344 mutex_unlock(&bnx2fc_dev_lock);
2345 rtnl_unlock();
2346 return rc;
2347}
2348
2349/**
Robert Love6e89ea32012-11-27 06:53:40 +00002350 * bnx2fc_create() - Create a bnx2fc interface
2351 * @netdev : The net_device object the Ethernet interface to create on
2352 * @fip_mode: The FIP mode for this creation
2353 *
2354 * Called from fcoe transport
2355 *
2356 * Returns: 0 for success
2357 */
2358static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2359{
2360 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2361}
2362
2363/**
2364 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2365 * @netdev: The net_device to be used by the allocated FCoE Controller
2366 *
2367 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2368 * in a link_down state. The allows the user an opportunity to configure
2369 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2370 *
2371 * Creating in with this routine starts the FCoE Controller in Fabric
2372 * mode. The user can change to VN2VN or another mode before enabling.
2373 */
2374static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2375{
2376 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2377 BNX2FC_CREATE_LINK_DOWN);
2378}
2379
2380/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002381 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002382 *
2383 * @cnic: Pointer to cnic device instance
2384 *
2385 **/
2386static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2387{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002388 struct bnx2fc_hba *hba;
2389
2390 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -07002391 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002392 if (hba->cnic == cnic)
2393 return hba;
2394 }
2395 return NULL;
2396}
2397
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002398static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2399 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002400{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002401 struct bnx2fc_interface *interface;
2402
2403 /* Called with bnx2fc_dev_lock held */
2404 list_for_each_entry(interface, &if_list, list) {
2405 if (interface->netdev == netdev)
2406 return interface;
2407 }
2408 return NULL;
2409}
2410
2411static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2412 *phys_dev)
2413{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002414 struct bnx2fc_hba *hba;
2415
2416 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002417 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002418 if (hba->phys_dev == phys_dev)
2419 return hba;
2420 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002421 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002422 return NULL;
2423}
2424
2425/**
2426 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2427 *
2428 * @dev cnic device handle
2429 */
2430static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2431{
2432 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002433 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002434
2435 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2436
2437 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2438 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2439 dev->netdev->name, dev->flags);
2440 return;
2441 }
2442
2443 mutex_lock(&bnx2fc_dev_lock);
2444 hba = bnx2fc_find_hba_for_cnic(dev);
2445 if (!hba) {
2446 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2447 dev);
2448 mutex_unlock(&bnx2fc_dev_lock);
2449 return;
2450 }
2451
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002452 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002453 adapter_count--;
2454
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002455 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002456 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002457 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002458 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002459 mutex_unlock(&bnx2fc_dev_lock);
2460
Eddie Wai06c4f202013-12-11 15:30:21 -08002461 /* Ensure ALL destroy work has been completed before return */
2462 flush_workqueue(bnx2fc_wq);
2463
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002464 bnx2fc_ulp_stop(hba);
2465 /* unregister cnic device */
2466 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2467 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002468 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002469}
2470
2471/**
2472 * bnx2fc_fcoe_reset - Resets the fcoe
2473 *
2474 * @shost: shost the reset is from
2475 *
2476 * Returns: always 0
2477 */
2478static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2479{
2480 struct fc_lport *lport = shost_priv(shost);
2481 fc_lport_reset(lport);
2482 return 0;
2483}
2484
2485
2486static bool bnx2fc_match(struct net_device *netdev)
2487{
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002488 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002489
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002490 mutex_lock(&bnx2fc_dev_lock);
2491 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2492 phys_dev = vlan_dev_real_dev(netdev);
2493
2494 if (bnx2fc_hba_lookup(phys_dev)) {
2495 mutex_unlock(&bnx2fc_dev_lock);
2496 return true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002497 }
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002498
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002499 mutex_unlock(&bnx2fc_dev_lock);
2500 return false;
2501}
2502
2503
2504static struct fcoe_transport bnx2fc_transport = {
2505 .name = {"bnx2fc"},
2506 .attached = false,
2507 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
Robert Love6e89ea32012-11-27 06:53:40 +00002508 .alloc = bnx2fc_ctlr_alloc,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002509 .match = bnx2fc_match,
2510 .create = bnx2fc_create,
2511 .destroy = bnx2fc_destroy,
2512 .enable = bnx2fc_enable,
2513 .disable = bnx2fc_disable,
2514};
2515
2516/**
2517 * bnx2fc_percpu_thread_create - Create a receive thread for an
2518 * online CPU
2519 *
2520 * @cpu: cpu index for the online cpu
2521 */
2522static void bnx2fc_percpu_thread_create(unsigned int cpu)
2523{
2524 struct bnx2fc_percpu_s *p;
2525 struct task_struct *thread;
2526
2527 p = &per_cpu(bnx2fc_percpu, cpu);
2528
Eric Dumazet69614272012-06-04 16:15:42 -07002529 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2530 (void *)p, cpu_to_node(cpu),
2531 "bnx2fc_thread/%d", cpu);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002532 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002533 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002534 kthread_bind(thread, cpu);
2535 p->iothread = thread;
2536 wake_up_process(thread);
2537 }
2538}
2539
2540static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2541{
2542 struct bnx2fc_percpu_s *p;
2543 struct task_struct *thread;
2544 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002545
2546 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2547
2548 /* Prevent any new work from being queued for this CPU */
2549 p = &per_cpu(bnx2fc_percpu, cpu);
2550 spin_lock_bh(&p->fp_work_lock);
2551 thread = p->iothread;
2552 p->iothread = NULL;
2553
2554
2555 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002556 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002557 list_del_init(&work->list);
2558 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2559 kfree(work);
2560 }
2561
2562 spin_unlock_bh(&p->fp_work_lock);
2563
2564 if (thread)
2565 kthread_stop(thread);
2566}
2567
2568/**
2569 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2570 *
2571 * @nfb: The callback data block
2572 * @action: The event triggering the callback
2573 * @hcpu: The index of the CPU that the event is for
2574 *
2575 * This creates or destroys per-CPU data for fcoe
2576 *
2577 * Returns NOTIFY_OK always.
2578 */
2579static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2580 unsigned long action, void *hcpu)
2581{
2582 unsigned cpu = (unsigned long)hcpu;
2583
2584 switch (action) {
2585 case CPU_ONLINE:
2586 case CPU_ONLINE_FROZEN:
2587 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2588 bnx2fc_percpu_thread_create(cpu);
2589 break;
2590 case CPU_DEAD:
2591 case CPU_DEAD_FROZEN:
2592 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2593 bnx2fc_percpu_thread_destroy(cpu);
2594 break;
2595 default:
2596 break;
2597 }
2598 return NOTIFY_OK;
2599}
2600
2601/**
2602 * bnx2fc_mod_init - module init entry point
2603 *
2604 * Initialize driver wide global data structures, and register
2605 * with cnic module
2606 **/
2607static int __init bnx2fc_mod_init(void)
2608{
2609 struct fcoe_percpu_s *bg;
2610 struct task_struct *l2_thread;
2611 int rc = 0;
2612 unsigned int cpu = 0;
2613 struct bnx2fc_percpu_s *p;
2614
2615 printk(KERN_INFO PFX "%s", version);
2616
2617 /* register as a fcoe transport */
2618 rc = fcoe_transport_attach(&bnx2fc_transport);
2619 if (rc) {
2620 printk(KERN_ERR "failed to register an fcoe transport, check "
2621 "if libfcoe is loaded\n");
2622 goto out;
2623 }
2624
2625 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002626 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002627 mutex_init(&bnx2fc_dev_lock);
2628 adapter_count = 0;
2629
2630 /* Attach FC transport template */
2631 rc = bnx2fc_attach_transport();
2632 if (rc)
2633 goto detach_ft;
2634
2635 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2636 if (!bnx2fc_wq) {
2637 rc = -ENOMEM;
2638 goto release_bt;
2639 }
2640
2641 bg = &bnx2fc_global;
2642 skb_queue_head_init(&bg->fcoe_rx_list);
2643 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2644 (void *)bg,
2645 "bnx2fc_l2_thread");
2646 if (IS_ERR(l2_thread)) {
2647 rc = PTR_ERR(l2_thread);
2648 goto free_wq;
2649 }
2650 wake_up_process(l2_thread);
2651 spin_lock_bh(&bg->fcoe_rx_list.lock);
2652 bg->thread = l2_thread;
2653 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2654
2655 for_each_possible_cpu(cpu) {
2656 p = &per_cpu(bnx2fc_percpu, cpu);
2657 INIT_LIST_HEAD(&p->work_list);
2658 spin_lock_init(&p->fp_work_lock);
2659 }
2660
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302661 cpu_notifier_register_begin();
2662
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002663 for_each_online_cpu(cpu) {
2664 bnx2fc_percpu_thread_create(cpu);
2665 }
2666
2667 /* Initialize per CPU interrupt thread */
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302668 __register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2669
2670 cpu_notifier_register_done();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002671
2672 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2673
2674 return 0;
2675
2676free_wq:
2677 destroy_workqueue(bnx2fc_wq);
2678release_bt:
2679 bnx2fc_release_transport();
2680detach_ft:
2681 fcoe_transport_detach(&bnx2fc_transport);
2682out:
2683 return rc;
2684}
2685
2686static void __exit bnx2fc_mod_exit(void)
2687{
2688 LIST_HEAD(to_be_deleted);
2689 struct bnx2fc_hba *hba, *next;
2690 struct fcoe_percpu_s *bg;
2691 struct task_struct *l2_thread;
2692 struct sk_buff *skb;
2693 unsigned int cpu = 0;
2694
2695 /*
2696 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2697 * it will have higher precedence than bnx2fc_dev_lock.
2698 * unregister_device() cannot be called with bnx2fc_dev_lock
2699 * held.
2700 */
2701 mutex_lock(&bnx2fc_dev_lock);
2702 list_splice(&adapter_list, &to_be_deleted);
2703 INIT_LIST_HEAD(&adapter_list);
2704 adapter_count = 0;
2705 mutex_unlock(&bnx2fc_dev_lock);
2706
2707 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002708 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2709 list_del_init(&hba->list);
2710 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2711 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002712 bnx2fc_ulp_stop(hba);
2713 /* unregister cnic device */
2714 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2715 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002716 hba->cnic->unregister_device(hba->cnic,
2717 CNIC_ULP_FCOE);
2718 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002719 }
2720 cnic_unregister_driver(CNIC_ULP_FCOE);
2721
2722 /* Destroy global thread */
2723 bg = &bnx2fc_global;
2724 spin_lock_bh(&bg->fcoe_rx_list.lock);
2725 l2_thread = bg->thread;
2726 bg->thread = NULL;
2727 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2728 kfree_skb(skb);
2729
2730 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2731
2732 if (l2_thread)
2733 kthread_stop(l2_thread);
2734
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302735 cpu_notifier_register_begin();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002736
2737 /* Destroy per cpu threads */
2738 for_each_online_cpu(cpu) {
2739 bnx2fc_percpu_thread_destroy(cpu);
2740 }
2741
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302742 __unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2743
2744 cpu_notifier_register_done();
2745
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002746 destroy_workqueue(bnx2fc_wq);
2747 /*
2748 * detach from scsi transport
2749 * must happen after all destroys are done
2750 */
2751 bnx2fc_release_transport();
2752
2753 /* detach from fcoe transport */
2754 fcoe_transport_detach(&bnx2fc_transport);
2755}
2756
2757module_init(bnx2fc_mod_init);
2758module_exit(bnx2fc_mod_exit);
2759
Robert Love8d55e502012-05-22 19:06:26 -07002760static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
Robert Love6e89ea32012-11-27 06:53:40 +00002761 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
Yi Zoub8b3e692012-12-06 06:24:59 +00002762 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2763 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2764 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2765 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2766 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2767 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
Robert Love8d55e502012-05-22 19:06:26 -07002768
2769 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2770 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2771};
2772
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002773static struct fc_function_template bnx2fc_transport_function = {
2774 .show_host_node_name = 1,
2775 .show_host_port_name = 1,
2776 .show_host_supported_classes = 1,
2777 .show_host_supported_fc4s = 1,
2778 .show_host_active_fc4s = 1,
2779 .show_host_maxframe_size = 1,
2780
2781 .show_host_port_id = 1,
2782 .show_host_supported_speeds = 1,
2783 .get_host_speed = fc_get_host_speed,
2784 .show_host_speed = 1,
2785 .show_host_port_type = 1,
2786 .get_host_port_state = fc_get_host_port_state,
2787 .show_host_port_state = 1,
2788 .show_host_symbolic_name = 1,
2789
2790 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2791 sizeof(struct bnx2fc_rport)),
2792 .show_rport_maxframe_size = 1,
2793 .show_rport_supported_classes = 1,
2794
2795 .show_host_fabric_name = 1,
2796 .show_starget_node_name = 1,
2797 .show_starget_port_name = 1,
2798 .show_starget_port_id = 1,
2799 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2800 .show_rport_dev_loss_tmo = 1,
2801 .get_fc_host_stats = bnx2fc_get_host_stats,
2802
2803 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2804
2805 .terminate_rport_io = fc_rport_terminate_io,
2806
2807 .vport_create = bnx2fc_vport_create,
2808 .vport_delete = bnx2fc_vport_destroy,
2809 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002810 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002811};
2812
2813static struct fc_function_template bnx2fc_vport_xport_function = {
2814 .show_host_node_name = 1,
2815 .show_host_port_name = 1,
2816 .show_host_supported_classes = 1,
2817 .show_host_supported_fc4s = 1,
2818 .show_host_active_fc4s = 1,
2819 .show_host_maxframe_size = 1,
2820
2821 .show_host_port_id = 1,
2822 .show_host_supported_speeds = 1,
2823 .get_host_speed = fc_get_host_speed,
2824 .show_host_speed = 1,
2825 .show_host_port_type = 1,
2826 .get_host_port_state = fc_get_host_port_state,
2827 .show_host_port_state = 1,
2828 .show_host_symbolic_name = 1,
2829
2830 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2831 sizeof(struct bnx2fc_rport)),
2832 .show_rport_maxframe_size = 1,
2833 .show_rport_supported_classes = 1,
2834
2835 .show_host_fabric_name = 1,
2836 .show_starget_node_name = 1,
2837 .show_starget_port_name = 1,
2838 .show_starget_port_id = 1,
2839 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2840 .show_rport_dev_loss_tmo = 1,
2841 .get_fc_host_stats = fc_get_host_stats,
2842 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2843 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002844 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002845};
2846
2847/**
2848 * scsi_host_template structure used while registering with SCSI-ml
2849 */
2850static struct scsi_host_template bnx2fc_shost_template = {
2851 .module = THIS_MODULE,
Saurav Kashyap17d87c42014-07-03 08:18:28 -04002852 .name = "QLogic Offload FCoE Initiator",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002853 .queuecommand = bnx2fc_queuecommand,
2854 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2855 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2856 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2857 .eh_host_reset_handler = fc_eh_host_reset,
2858 .slave_alloc = fc_slave_alloc,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002859 .change_queue_depth = scsi_change_queue_depth,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002860 .this_id = -1,
2861 .cmd_per_lun = 3,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002862 .use_clustering = ENABLE_CLUSTERING,
2863 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
Bhanu Prakash Gollapudid450d772012-12-21 19:40:33 -08002864 .max_sectors = 1024,
Christoph Hellwig2ecb2042014-11-03 14:09:02 +01002865 .use_blk_tags = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002866 .track_queue_depth = 1,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002867};
2868
2869static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2870 .frame_send = bnx2fc_xmit,
2871 .elsct_send = bnx2fc_elsct_send,
2872 .fcp_abort_io = bnx2fc_abort_io,
2873 .fcp_cleanup = bnx2fc_cleanup,
Yi Zoub8b3e692012-12-06 06:24:59 +00002874 .get_lesb = fcoe_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002875 .rport_event_callback = bnx2fc_rport_event_handler,
2876};
2877
2878/**
2879 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2880 * structure carrying callback function pointers
2881 */
2882static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2883 .owner = THIS_MODULE,
2884 .cnic_init = bnx2fc_ulp_init,
2885 .cnic_exit = bnx2fc_ulp_exit,
2886 .cnic_start = bnx2fc_ulp_start,
2887 .cnic_stop = bnx2fc_ulp_stop,
2888 .indicate_kcqes = bnx2fc_indicate_kcqe,
2889 .indicate_netevent = bnx2fc_indicate_netevent,
Barak Witkowski2e499d32012-06-26 01:31:19 +00002890 .cnic_get_stats = bnx2fc_ulp_get_stats,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002891};