blob: b0305b0fd21c03dc8a2ce38e9a19174693dbefc4 [file] [log] [blame]
Chad Dupuisde909d82015-10-19 15:40:36 -04001/* bnx2fc_fcoe.c: QLogic 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 *
Chad Dupuis975860902015-10-19 15:40:35 -04006 * Copyright (c) 2008-2013 Broadcom Corporation
7 * Copyright (c) 2014-2015 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
Chad Dupuisedb394b2015-10-19 15:40:41 -040026#define DRV_MODULE_RELDATE "October 15, 2015"
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080027
28
Greg Kroah-Hartman6f039792012-12-21 13:08:55 -080029static char version[] =
Chad Dupuisde909d82015-10-19 15:40:36 -040030 "QLogic 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>");
Chad Dupuisde909d82015-10-19 15:40:36 -040035MODULE_DESCRIPTION("QLogic 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);
Jose Castillod4faee02016-01-29 14:39:26 +0000100MODULE_PARM_DESC(debug_logging,
101 "Option to enable extended logging,\n"
102 "\t\tDefault is 0 - no logging.\n"
103 "\t\t0x01 - SCSI cmd error, cleanup.\n"
104 "\t\t0x02 - Session setup, cleanup, etc.\n"
105 "\t\t0x04 - lport events, link, mtu, etc.\n"
106 "\t\t0x08 - ELS logs.\n"
107 "\t\t0x10 - fcoe L2 fame related logs.\n"
108 "\t\t0xff - LOG all messages.");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800109
Joe Carnuccio10755d32016-04-07 09:07:56 -0400110uint bnx2fc_devloss_tmo;
111module_param_named(devloss_tmo, bnx2fc_devloss_tmo, uint, S_IRUGO);
112MODULE_PARM_DESC(devloss_tmo, " Change devloss_tmo for the remote ports "
113 "attached via bnx2fc.");
114
115uint bnx2fc_max_luns = BNX2FC_MAX_LUN;
116module_param_named(max_luns, bnx2fc_max_luns, uint, S_IRUGO);
117MODULE_PARM_DESC(max_luns, " Change the default max_lun per SCSI host. Default "
118 "0xffff.");
119
120uint bnx2fc_queue_depth;
121module_param_named(queue_depth, bnx2fc_queue_depth, uint, S_IRUGO);
122MODULE_PARM_DESC(queue_depth, " Change the default queue depth of SCSI devices "
123 "attached via bnx2fc.");
124
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800125static int bnx2fc_cpu_callback(struct notifier_block *nfb,
126 unsigned long action, void *hcpu);
127/* notification function for CPU hotplug events */
128static struct notifier_block bnx2fc_cpu_notifier = {
129 .notifier_call = bnx2fc_cpu_callback,
130};
131
Bhanu Prakash Gollapudif72f69792011-10-03 16:45:02 -0700132static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
133{
134 return ((struct bnx2fc_interface *)
135 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
136}
137
Robert Love8d55e502012-05-22 19:06:26 -0700138static void bnx2fc_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
139{
140 struct fcoe_ctlr_device *ctlr_dev =
141 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
142 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
143 struct bnx2fc_interface *fcoe = fcoe_ctlr_priv(ctlr);
144
145 fcf_dev->vlan_id = fcoe->vlan_id;
146}
147
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800148static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
149{
150 struct fcoe_percpu_s *bg;
151 struct fcoe_rcv_info *fr;
152 struct sk_buff_head *list;
153 struct sk_buff *skb, *next;
154 struct sk_buff *head;
155
156 bg = &bnx2fc_global;
157 spin_lock_bh(&bg->fcoe_rx_list.lock);
158 list = &bg->fcoe_rx_list;
159 head = list->next;
160 for (skb = head; skb != (struct sk_buff *)list;
161 skb = next) {
162 next = skb->next;
163 fr = fcoe_dev_from_skb(skb);
164 if (fr->fr_dev == lp) {
165 __skb_unlink(skb, list);
166 kfree_skb(skb);
167 }
168 }
169 spin_unlock_bh(&bg->fcoe_rx_list.lock);
170}
171
172int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
173{
174 int rc;
175 spin_lock(&bnx2fc_global_lock);
176 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
177 spin_unlock(&bnx2fc_global_lock);
178
179 return rc;
180}
181
182static void bnx2fc_abort_io(struct fc_lport *lport)
183{
184 /*
185 * This function is no-op for bnx2fc, but we do
186 * not want to leave it as NULL either, as libfc
187 * can call the default function which is
188 * fc_fcp_abort_io.
189 */
190}
191
192static void bnx2fc_cleanup(struct fc_lport *lport)
193{
194 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700195 struct bnx2fc_interface *interface = port->priv;
196 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800197 struct bnx2fc_rport *tgt;
198 int i;
199
200 BNX2FC_MISC_DBG("Entered %s\n", __func__);
201 mutex_lock(&hba->hba_mutex);
202 spin_lock_bh(&hba->hba_lock);
203 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
204 tgt = hba->tgt_ofld_list[i];
205 if (tgt) {
206 /* Cleanup IOs belonging to requested vport */
207 if (tgt->port == port) {
208 spin_unlock_bh(&hba->hba_lock);
209 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
210 bnx2fc_flush_active_ios(tgt);
211 spin_lock_bh(&hba->hba_lock);
212 }
213 }
214 }
215 spin_unlock_bh(&hba->hba_lock);
216 mutex_unlock(&hba->hba_mutex);
217}
218
219static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
220 struct fc_frame *fp)
221{
222 struct fc_rport_priv *rdata = tgt->rdata;
223 struct fc_frame_header *fh;
224 int rc = 0;
225
226 fh = fc_frame_header_get(fp);
227 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
228 "r_ctl = 0x%x\n", rdata->ids.port_id,
229 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
230 if ((fh->fh_type == FC_TYPE_ELS) &&
231 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
232
233 switch (fc_frame_payload_op(fp)) {
234 case ELS_ADISC:
235 rc = bnx2fc_send_adisc(tgt, fp);
236 break;
237 case ELS_LOGO:
238 rc = bnx2fc_send_logo(tgt, fp);
239 break;
240 case ELS_RLS:
241 rc = bnx2fc_send_rls(tgt, fp);
242 break;
243 default:
244 break;
245 }
246 } else if ((fh->fh_type == FC_TYPE_BLS) &&
247 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
248 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
249 else {
250 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
251 "rctl 0x%x thru non-offload path\n",
252 fh->fh_type, fh->fh_r_ctl);
253 return -ENODEV;
254 }
255 if (rc)
256 return -ENOMEM;
257 else
258 return 0;
259}
260
261/**
262 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
263 *
264 * @lport: the associated local port
265 * @fp: the fc_frame to be transmitted
266 */
267static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
268{
269 struct ethhdr *eh;
270 struct fcoe_crc_eof *cp;
271 struct sk_buff *skb;
272 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700273 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700274 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700275 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800276 struct fcoe_port *port;
277 struct fcoe_hdr *hp;
278 struct bnx2fc_rport *tgt;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700279 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800280 u8 sof, eof;
281 u32 crc;
282 unsigned int hlen, tlen, elen;
283 int wlen, rc = 0;
284
285 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700286 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700287 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700288 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800289
290 fh = fc_frame_header_get(fp);
291
292 skb = fp_skb(fp);
293 if (!lport->link_up) {
294 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
295 kfree_skb(skb);
296 return 0;
297 }
298
299 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Robert Lovefd8f8902012-05-22 19:06:16 -0700300 if (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800301 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
302 kfree_skb(skb);
303 return -EINVAL;
304 }
Robert Lovefd8f8902012-05-22 19:06:16 -0700305 if (fcoe_ctlr_els_send(ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800306 return 0;
307 }
308
309 sof = fr_sof(fp);
310 eof = fr_eof(fp);
311
312 /*
313 * Snoop the frame header to check if the frame is for
314 * an offloaded session
315 */
316 /*
317 * tgt_ofld_list access is synchronized using
318 * both hba mutex and hba lock. Atleast hba mutex or
319 * hba lock needs to be held for read access.
320 */
321
322 spin_lock_bh(&hba->hba_lock);
323 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
324 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
325 /* This frame is for offloaded session */
326 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
327 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
328 spin_unlock_bh(&hba->hba_lock);
329 rc = bnx2fc_xmit_l2_frame(tgt, fp);
330 if (rc != -ENODEV) {
331 kfree_skb(skb);
332 return rc;
333 }
334 } else {
335 spin_unlock_bh(&hba->hba_lock);
336 }
337
338 elen = sizeof(struct ethhdr);
339 hlen = sizeof(struct fcoe_hdr);
340 tlen = sizeof(struct fcoe_crc_eof);
341 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
342
343 skb->ip_summed = CHECKSUM_NONE;
344 crc = fcoe_fc_crc(fp);
345
346 /* copy port crc and eof to the skb buff */
347 if (skb_is_nonlinear(skb)) {
348 skb_frag_t *frag;
349 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
350 kfree_skb(skb);
351 return -ENOMEM;
352 }
353 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +0800354 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800355 } else {
356 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
357 }
358
359 memset(cp, 0, sizeof(*cp));
360 cp->fcoe_eof = eof;
361 cp->fcoe_crc32 = cpu_to_le32(~crc);
362 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +0800363 kunmap_atomic(cp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800364 cp = NULL;
365 }
366
367 /* adjust skb network/transport offsets to match mac/fcoe/port */
368 skb_push(skb, elen + hlen);
369 skb_reset_mac_header(skb);
370 skb_reset_network_header(skb);
371 skb->mac_len = elen;
372 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700373 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800374
375 /* fill up mac and fcoe headers */
376 eh = eth_hdr(skb);
377 eh->h_proto = htons(ETH_P_FCOE);
Robert Lovefd8f8902012-05-22 19:06:16 -0700378 if (ctlr->map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800379 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
380 else
381 /* insert GW address */
Robert Lovefd8f8902012-05-22 19:06:16 -0700382 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800383
Robert Lovefd8f8902012-05-22 19:06:16 -0700384 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
385 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800386 else
387 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
388
389 hp = (struct fcoe_hdr *)(eh + 1);
390 memset(hp, 0, sizeof(*hp));
391 if (FC_FCOE_VER)
392 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
393 hp->fcoe_sof = sof;
394
395 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
396 if (lport->seq_offload && fr_max_payload(fp)) {
397 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
398 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
399 } else {
400 skb_shinfo(skb)->gso_type = 0;
401 skb_shinfo(skb)->gso_size = 0;
402 }
403
404 /*update tx stats */
Vasu Dev1bd49b42012-05-25 10:26:43 -0700405 stats = per_cpu_ptr(lport->stats, get_cpu());
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800406 stats->TxFrames++;
407 stats->TxWords += wlen;
408 put_cpu();
409
410 /* send down to lld */
411 fr_dev(fp) = lport;
412 if (port->fcoe_pending_queue.qlen)
413 fcoe_check_wait_queue(lport, skb);
414 else if (fcoe_start_io(skb))
415 fcoe_check_wait_queue(lport, skb);
416
417 return 0;
418}
419
420/**
421 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
422 *
423 * @skb: the receive socket buffer
424 * @dev: associated net device
425 * @ptype: context
426 * @olddev: last device
427 *
428 * This function receives the packet and builds FC frame and passes it up
429 */
430static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
431 struct packet_type *ptype, struct net_device *olddev)
432{
433 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700434 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700435 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800436 struct fc_frame_header *fh;
437 struct fcoe_rcv_info *fr;
438 struct fcoe_percpu_s *bg;
Maurizio Lombardi01a4cc42014-11-20 11:17:33 +0100439 struct sk_buff *tmp_skb;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800440 unsigned short oxid;
441
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700442 interface = container_of(ptype, struct bnx2fc_interface,
443 fcoe_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -0700444 ctlr = bnx2fc_to_ctlr(interface);
445 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800446
447 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700448 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800449 goto err;
450 }
451
Maurizio Lombardi01a4cc42014-11-20 11:17:33 +0100452 tmp_skb = skb_share_check(skb, GFP_ATOMIC);
453 if (!tmp_skb)
454 goto err;
455
456 skb = tmp_skb;
457
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800458 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700459 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800460 goto err;
461 }
462
463 /*
464 * Check for minimum frame length, and make sure required FCoE
465 * and FC headers are pulled into the linear data area.
466 */
467 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
468 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
469 goto err;
470
471 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
472 fh = (struct fc_frame_header *) skb_transport_header(skb);
473
474 oxid = ntohs(fh->fh_ox_id);
475
476 fr = fcoe_dev_from_skb(skb);
477 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800478
479 bg = &bnx2fc_global;
Neil Hormanfc05ab72012-03-09 14:50:13 -0800480 spin_lock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800481
482 __skb_queue_tail(&bg->fcoe_rx_list, skb);
483 if (bg->fcoe_rx_list.qlen == 1)
484 wake_up_process(bg->thread);
485
Neil Hormanfc05ab72012-03-09 14:50:13 -0800486 spin_unlock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800487
488 return 0;
489err:
490 kfree_skb(skb);
491 return -1;
492}
493
494static int bnx2fc_l2_rcv_thread(void *arg)
495{
496 struct fcoe_percpu_s *bg = arg;
497 struct sk_buff *skb;
498
Dongsheng Yang8698a742014-03-11 18:09:12 +0800499 set_user_nice(current, MIN_NICE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800500 set_current_state(TASK_INTERRUPTIBLE);
501 while (!kthread_should_stop()) {
502 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800503 spin_lock_bh(&bg->fcoe_rx_list.lock);
504 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
505 spin_unlock_bh(&bg->fcoe_rx_list.lock);
506 bnx2fc_recv_frame(skb);
507 spin_lock_bh(&bg->fcoe_rx_list.lock);
508 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700509 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800510 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800511 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700512 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800513 return 0;
514}
515
516
517static void bnx2fc_recv_frame(struct sk_buff *skb)
518{
519 u32 fr_len;
520 struct fc_lport *lport;
521 struct fcoe_rcv_info *fr;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700522 struct fc_stats *stats;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800523 struct fc_frame_header *fh;
524 struct fcoe_crc_eof crc_eof;
525 struct fc_frame *fp;
526 struct fc_lport *vn_port;
527 struct fcoe_port *port;
528 u8 *mac = NULL;
529 u8 *dest_mac = NULL;
530 struct fcoe_hdr *hp;
531
532 fr = fcoe_dev_from_skb(skb);
533 lport = fr->fr_dev;
534 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700535 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800536 kfree_skb(skb);
537 return;
538 }
539
540 if (skb_is_nonlinear(skb))
541 skb_linearize(skb);
542 mac = eth_hdr(skb)->h_source;
543 dest_mac = eth_hdr(skb)->h_dest;
544
545 /* Pull the header */
546 hp = (struct fcoe_hdr *) skb_network_header(skb);
547 fh = (struct fc_frame_header *) skb_transport_header(skb);
548 skb_pull(skb, sizeof(struct fcoe_hdr));
549 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
550
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800551 fp = (struct fc_frame *)skb;
552 fc_frame_init(fp);
553 fr_dev(fp) = lport;
554 fr_sof(fp) = hp->fcoe_sof;
555 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800556 kfree_skb(skb);
557 return;
558 }
559 fr_eof(fp) = crc_eof.fcoe_eof;
560 fr_crc(fp) = crc_eof.fcoe_crc32;
561 if (pskb_trim(skb, fr_len)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800562 kfree_skb(skb);
563 return;
564 }
565
566 fh = fc_frame_header_get(fp);
567
568 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
569 if (vn_port) {
570 port = lport_priv(vn_port);
Joe Perches6942df72013-09-02 03:32:33 +0000571 if (!ether_addr_equal(port->data_src_addr, dest_mac)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800572 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800573 kfree_skb(skb);
574 return;
575 }
576 }
577 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
578 fh->fh_type == FC_TYPE_FCP) {
579 /* Drop FCP data. We dont this in L2 path */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800580 kfree_skb(skb);
581 return;
582 }
583 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
584 fh->fh_type == FC_TYPE_ELS) {
585 switch (fc_frame_payload_op(fp)) {
586 case ELS_LOGO:
587 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
588 /* drop non-FIP LOGO */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800589 kfree_skb(skb);
590 return;
591 }
592 break;
593 }
594 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700595
596 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
597 /* Drop incoming ABTS */
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700598 kfree_skb(skb);
599 return;
600 }
601
Neil Hormand576a5e2014-06-23 10:41:02 -0400602 stats = per_cpu_ptr(lport->stats, smp_processor_id());
603 stats->RxFrames++;
604 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
605
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800606 if (le32_to_cpu(fr_crc(fp)) !=
607 ~crc32(~0, skb->data, fr_len)) {
608 if (stats->InvalidCRCCount < 5)
609 printk(KERN_WARNING PFX "dropping frame with "
610 "CRC error\n");
611 stats->InvalidCRCCount++;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800612 kfree_skb(skb);
613 return;
614 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800615 fc_exch_recv(lport, fp);
616}
617
618/**
619 * bnx2fc_percpu_io_thread - thread per cpu for ios
620 *
621 * @arg: ptr to bnx2fc_percpu_info structure
622 */
623int bnx2fc_percpu_io_thread(void *arg)
624{
625 struct bnx2fc_percpu_s *p = arg;
626 struct bnx2fc_work *work, *tmp;
627 LIST_HEAD(work_list);
628
Dongsheng Yang8698a742014-03-11 18:09:12 +0800629 set_user_nice(current, MIN_NICE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800630 set_current_state(TASK_INTERRUPTIBLE);
631 while (!kthread_should_stop()) {
632 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800633 spin_lock_bh(&p->fp_work_lock);
634 while (!list_empty(&p->work_list)) {
635 list_splice_init(&p->work_list, &work_list);
636 spin_unlock_bh(&p->fp_work_lock);
637
638 list_for_each_entry_safe(work, tmp, &work_list, list) {
639 list_del_init(&work->list);
640 bnx2fc_process_cq_compl(work->tgt, work->wqe);
641 kfree(work);
642 }
643
644 spin_lock_bh(&p->fp_work_lock);
645 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700646 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800647 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800648 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700649 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800650
651 return 0;
652}
653
654static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
655{
656 struct fc_host_statistics *bnx2fc_stats;
657 struct fc_lport *lport = shost_priv(shost);
658 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700659 struct bnx2fc_interface *interface = port->priv;
660 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800661 struct fcoe_statistics_params *fw_stats;
662 int rc = 0;
663
664 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
665 if (!fw_stats)
666 return NULL;
667
668 bnx2fc_stats = fc_get_host_stats(shost);
669
670 init_completion(&hba->stat_req_done);
671 if (bnx2fc_send_stat_req(hba))
672 return bnx2fc_stats;
673 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
674 if (!rc) {
675 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
676 return bnx2fc_stats;
677 }
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800678 BNX2FC_STATS(hba, rx_stat2, fc_crc_cnt);
679 bnx2fc_stats->invalid_crc_count += hba->bfw_stats.fc_crc_cnt;
680 BNX2FC_STATS(hba, tx_stat, fcoe_tx_pkt_cnt);
681 bnx2fc_stats->tx_frames += hba->bfw_stats.fcoe_tx_pkt_cnt;
682 BNX2FC_STATS(hba, tx_stat, fcoe_tx_byte_cnt);
683 bnx2fc_stats->tx_words += ((hba->bfw_stats.fcoe_tx_byte_cnt) / 4);
684 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_pkt_cnt);
685 bnx2fc_stats->rx_frames += hba->bfw_stats.fcoe_rx_pkt_cnt;
686 BNX2FC_STATS(hba, rx_stat0, fcoe_rx_byte_cnt);
687 bnx2fc_stats->rx_words += ((hba->bfw_stats.fcoe_rx_byte_cnt) / 4);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800688
689 bnx2fc_stats->dumped_frames = 0;
690 bnx2fc_stats->lip_count = 0;
691 bnx2fc_stats->nos_count = 0;
692 bnx2fc_stats->loss_of_sync_count = 0;
693 bnx2fc_stats->loss_of_signal_count = 0;
694 bnx2fc_stats->prim_seq_protocol_err_count = 0;
695
Bhanu Prakash Gollapudif246fe22012-12-21 19:40:32 -0800696 memcpy(&hba->prev_stats, hba->stats_buffer,
697 sizeof(struct fcoe_statistics_params));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800698 return bnx2fc_stats;
699}
700
701static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
702{
703 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700704 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800705 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800706 struct Scsi_Host *shost = lport->host;
707 int rc = 0;
708
709 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
Joe Carnuccio10755d32016-04-07 09:07:56 -0400710 shost->max_lun = bnx2fc_max_luns;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800711 shost->max_id = BNX2FC_MAX_FCP_TGT;
712 shost->max_channel = 0;
713 if (lport->vport)
714 shost->transportt = bnx2fc_vport_xport_template;
715 else
716 shost->transportt = bnx2fc_transport_template;
717
718 /* Add the new host to SCSI-ml */
719 rc = scsi_add_host(lport->host, dev);
720 if (rc) {
721 printk(KERN_ERR PFX "Error on scsi_add_host\n");
722 return rc;
723 }
724 if (!lport->vport)
725 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800726 snprintf(fc_host_symbolic_name(lport->host), 256,
Saurav Kashyap17d87c42014-07-03 08:18:28 -0400727 "%s (QLogic %s) v%s over %s",
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -0800728 BNX2FC_NAME, hba->chip_num, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700729 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800730
731 return 0;
732}
733
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800734static int bnx2fc_link_ok(struct fc_lport *lport)
735{
736 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700737 struct bnx2fc_interface *interface = port->priv;
738 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800739 struct net_device *dev = hba->phys_dev;
740 int rc = 0;
741
742 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
743 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
744 else {
745 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
746 rc = -1;
747 }
748 return rc;
749}
750
751/**
752 * bnx2fc_get_link_state - get network link state
753 *
754 * @hba: adapter instance pointer
755 *
756 * updates adapter structure flag based on netdev state
757 */
758void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
759{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700760 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800761 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
762 else
763 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
764}
765
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700766static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800767{
768 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700769 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -0700770 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800771 struct fcoe_port *port;
772 u64 wwnn, wwpn;
773
774 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700775 interface = port->priv;
Robert Lovefd8f8902012-05-22 19:06:16 -0700776 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700777 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800778
779 /* require support for get_pauseparam ethtool op. */
780 if (!hba->phys_dev->ethtool_ops ||
781 !hba->phys_dev->ethtool_ops->get_pauseparam)
782 return -EOPNOTSUPP;
783
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700784 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800785 return -EINVAL;
786
787 skb_queue_head_init(&port->fcoe_pending_queue);
788 port->fcoe_pending_queue_active = 0;
789 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
790
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000791 fcoe_link_speed_update(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800792
793 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700794 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700795 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700796 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800797 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
798 fc_set_wwnn(lport, wwnn);
799
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700800 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Lovefd8f8902012-05-22 19:06:16 -0700801 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700802 2, 0);
803
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800804 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
805 fc_set_wwpn(lport, wwpn);
806 }
807
808 return 0;
809}
810
811static void bnx2fc_destroy_timer(unsigned long data)
812{
813 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
814
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700815 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
816 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700817 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800818 wake_up_interruptible(&hba->destroy_wait);
819}
820
821/**
822 * bnx2fc_indicate_netevent - Generic netdev event handler
823 *
824 * @context: adapter structure pointer
825 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000826 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800827 *
828 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700829 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800830 */
Michael Chan415199f2011-07-20 14:55:24 +0000831static void bnx2fc_indicate_netevent(void *context, unsigned long event,
832 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800833{
834 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Robert Love6e89ea32012-11-27 06:53:40 +0000835 struct fcoe_ctlr_device *cdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700836 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800837 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700838 struct bnx2fc_interface *interface, *tmp;
Robert Lovefd8f8902012-05-22 19:06:16 -0700839 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700840 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800841 u32 link_possible = 1;
842
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700843 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000844 return;
845
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800846 switch (event) {
847 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800848 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
849 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700850 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800851 break;
852
853 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800854 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
855 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
856 link_possible = 0;
857 break;
858
859 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800860 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
861 link_possible = 0;
862 break;
863
864 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800865 break;
866
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700867 case NETDEV_UNREGISTER:
868 if (!vlan_id)
869 return;
870 mutex_lock(&bnx2fc_dev_lock);
871 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700872 if (interface->hba == hba &&
873 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
874 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700875 }
876 mutex_unlock(&bnx2fc_dev_lock);
Eddie Wai06c4f202013-12-11 15:30:21 -0800877
878 /* Ensure ALL destroy work has been completed before return */
879 flush_workqueue(bnx2fc_wq);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700880 return;
881
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800882 default:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800883 return;
884 }
885
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700886 mutex_lock(&bnx2fc_dev_lock);
887 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800888
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700889 if (interface->hba != hba)
890 continue;
891
Robert Lovefd8f8902012-05-22 19:06:16 -0700892 ctlr = bnx2fc_to_ctlr(interface);
893 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700894 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
895 interface->netdev->name, event);
896
Yi Zou0e0f9cd2012-12-06 06:24:44 +0000897 fcoe_link_speed_update(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700898
Robert Love6e89ea32012-11-27 06:53:40 +0000899 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700900
901 if (link_possible && !bnx2fc_link_ok(lport)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000902 switch (cdev->enabled) {
903 case FCOE_CTLR_DISABLED:
904 pr_info("Link up while interface is disabled.\n");
905 break;
906 case FCOE_CTLR_ENABLED:
907 case FCOE_CTLR_UNUSED:
908 /* Reset max recv frame size to default */
909 fc_set_mfs(lport, BNX2FC_MFS);
910 /*
911 * ctlr link up will only be handled during
912 * enable to avoid sending discovery
913 * solicitation on a stale vlan
914 */
915 if (interface->enabled)
916 fcoe_ctlr_link_up(ctlr);
917 };
Robert Lovefd8f8902012-05-22 19:06:16 -0700918 } else if (fcoe_ctlr_link_down(ctlr)) {
Robert Love6e89ea32012-11-27 06:53:40 +0000919 switch (cdev->enabled) {
920 case FCOE_CTLR_DISABLED:
921 pr_info("Link down while interface is disabled.\n");
922 break;
923 case FCOE_CTLR_ENABLED:
924 case FCOE_CTLR_UNUSED:
925 mutex_lock(&lport->lp_mutex);
926 list_for_each_entry(vport, &lport->vports, list)
927 fc_host_port_type(vport->host) =
928 FC_PORTTYPE_UNKNOWN;
929 mutex_unlock(&lport->lp_mutex);
930 fc_host_port_type(lport->host) =
931 FC_PORTTYPE_UNKNOWN;
932 per_cpu_ptr(lport->stats,
933 get_cpu())->LinkFailureCount++;
934 put_cpu();
935 fcoe_clean_pending_queue(lport);
936 wait_for_upload = 1;
937 };
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800938 }
939 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700940 mutex_unlock(&bnx2fc_dev_lock);
941
942 if (wait_for_upload) {
943 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
944 init_waitqueue_head(&hba->shutdown_wait);
945 BNX2FC_MISC_DBG("indicate_netevent "
946 "num_ofld_sess = %d\n",
947 hba->num_ofld_sess);
948 hba->wait_for_link_down = 1;
949 wait_event_interruptible(hba->shutdown_wait,
950 (hba->num_ofld_sess == 0));
951 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
952 hba->num_ofld_sess);
953 hba->wait_for_link_down = 0;
954
955 if (signal_pending(current))
956 flush_signals(current);
957 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800958}
959
960static int bnx2fc_libfc_config(struct fc_lport *lport)
961{
962
963 /* Set the function pointers set by bnx2fc driver */
964 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
965 sizeof(struct libfc_function_template));
966 fc_elsct_init(lport);
967 fc_exch_init(lport);
968 fc_rport_init(lport);
Robert Love08076192013-03-25 11:00:28 -0700969 fc_disc_init(lport);
970 fc_disc_config(lport, lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800971 return 0;
972}
973
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000974static int bnx2fc_em_config(struct fc_lport *lport, struct bnx2fc_hba *hba)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800975{
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000976 int fcoe_min_xid, fcoe_max_xid;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800977
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000978 fcoe_min_xid = hba->max_xid + 1;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800979 if (nr_cpu_ids <= 2)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000980 fcoe_max_xid = hba->max_xid + FCOE_XIDS_PER_CPU_OFFSET;
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800981 else
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +0000982 fcoe_max_xid = hba->max_xid + FCOE_MAX_XID_OFFSET;
983 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, fcoe_min_xid,
984 fcoe_max_xid, NULL)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800985 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
986 return -ENOMEM;
987 }
988
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800989 return 0;
990}
991
992static int bnx2fc_lport_config(struct fc_lport *lport)
993{
994 lport->link_up = 0;
995 lport->qfull = 0;
Bhanu Prakash Gollapudi44c570b2012-01-23 18:00:47 -0800996 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
997 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800998 lport->e_d_tov = 2 * 1000;
999 lport->r_a_tov = 10 * 1000;
1000
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001001 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
1002 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001003 lport->does_npiv = 1;
1004
1005 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
1006 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
1007
1008 /* alloc stats structure */
1009 if (fc_lport_init_stats(lport))
1010 return -ENOMEM;
1011
1012 /* Finish fc_lport configuration */
1013 fc_lport_config(lport);
1014
1015 return 0;
1016}
1017
1018/**
1019 * bnx2fc_fip_recv - handle a received FIP frame.
1020 *
1021 * @skb: the received skb
1022 * @dev: associated &net_device
1023 * @ptype: the &packet_type structure which was used to register this handler.
1024 * @orig_dev: original receive &net_device, in case @ dev is a bond.
1025 *
1026 * Returns: 0 for success
1027 */
1028static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
1029 struct packet_type *ptype,
1030 struct net_device *orig_dev)
1031{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001032 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001033 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001034 interface = container_of(ptype, struct bnx2fc_interface,
1035 fip_packet_type);
Robert Lovefd8f8902012-05-22 19:06:16 -07001036 ctlr = bnx2fc_to_ctlr(interface);
1037 fcoe_ctlr_recv(ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001038 return 0;
1039}
1040
1041/**
1042 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1043 *
1044 * @fip: FCoE controller.
1045 * @old: Unicast MAC address to delete if the MAC is non-zero.
1046 * @new: Unicast MAC address to add.
1047 *
1048 * Remove any previously-set unicast MAC filter.
1049 * Add secondary FCoE MAC address filter for our OUI.
1050 */
1051static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1052{
1053 struct fcoe_port *port = lport_priv(lport);
1054
1055 memcpy(port->data_src_addr, addr, ETH_ALEN);
1056}
1057
1058/**
1059 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1060 *
1061 * @lport: libfc port
1062 */
1063static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1064{
1065 struct fcoe_port *port;
1066
1067 port = (struct fcoe_port *)lport_priv(lport);
1068 return port->data_src_addr;
1069}
1070
1071/**
1072 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1073 *
1074 * @fip: FCoE controller.
1075 * @skb: FIP Packet.
1076 */
1077static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1078{
1079 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1080 dev_queue_xmit(skb);
1081}
1082
1083static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1084{
1085 struct Scsi_Host *shost = vport_to_shost(vport);
1086 struct fc_lport *n_port = shost_priv(shost);
1087 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001088 struct bnx2fc_interface *interface = port->priv;
1089 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001090 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001091 int rc;
1092 char buf[32];
1093
1094 rc = fcoe_validate_vport_create(vport);
1095 if (rc) {
1096 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1097 printk(KERN_ERR PFX "Failed to create vport, "
1098 "WWPN (0x%s) already exists\n",
1099 buf);
1100 return rc;
1101 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001102
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001103 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001104 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001105 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001106 return -EIO;
1107 }
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001108 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001109 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001110 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001111 mutex_unlock(&bnx2fc_dev_lock);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001112 rtnl_unlock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001113
Dan Carpenterb6829c72014-11-04 13:37:17 +03001114 if (!vn_port) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001115 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1116 netdev->name);
1117 return -EIO;
1118 }
1119
Joe Carnuccio10755d32016-04-07 09:07:56 -04001120 if (bnx2fc_devloss_tmo)
1121 fc_host_dev_loss_tmo(vn_port->host) = bnx2fc_devloss_tmo;
1122
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001123 if (disabled) {
1124 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1125 } else {
1126 vn_port->boot_time = jiffies;
1127 fc_lport_init(vn_port);
1128 fc_fabric_login(vn_port);
1129 fc_vport_setlink(vn_port);
1130 }
1131 return 0;
1132}
1133
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001134static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1135{
1136 struct bnx2fc_lport *blport, *tmp;
1137
1138 spin_lock_bh(&hba->hba_lock);
1139 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1140 if (blport->lport == lport) {
1141 list_del(&blport->list);
1142 kfree(blport);
1143 }
1144 }
1145 spin_unlock_bh(&hba->hba_lock);
1146}
1147
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001148static int bnx2fc_vport_destroy(struct fc_vport *vport)
1149{
1150 struct Scsi_Host *shost = vport_to_shost(vport);
1151 struct fc_lport *n_port = shost_priv(shost);
1152 struct fc_lport *vn_port = vport->dd_data;
1153 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001154 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001155 struct fc_lport *v_port;
1156 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001157
1158 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001159 list_for_each_entry(v_port, &n_port->vports, list)
1160 if (v_port->vport == vport) {
1161 found = true;
1162 break;
1163 }
1164
1165 if (!found) {
1166 mutex_unlock(&n_port->lp_mutex);
1167 return -ENOENT;
1168 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001169 list_del(&vn_port->list);
1170 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001171 bnx2fc_free_vport(interface->hba, port->lport);
1172 bnx2fc_port_shutdown(port->lport);
1173 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001174 queue_work(bnx2fc_wq, &port->destroy_work);
1175 return 0;
1176}
1177
1178static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1179{
1180 struct fc_lport *lport = vport->dd_data;
1181
1182 if (disable) {
1183 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1184 fc_fabric_logoff(lport);
1185 } else {
1186 lport->boot_time = jiffies;
1187 fc_fabric_login(lport);
1188 fc_vport_setlink(lport);
1189 }
1190 return 0;
1191}
1192
1193
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001194static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001195{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001196 struct net_device *netdev = interface->netdev;
1197 struct net_device *physdev = interface->hba->phys_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001198 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001199 struct netdev_hw_addr *ha;
1200 int sel_san_mac = 0;
1201
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001202 /* setup Source MAC Address */
1203 rcu_read_lock();
1204 for_each_dev_addr(physdev, ha) {
1205 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1206 ha->type);
1207 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1208 ha->addr[1], ha->addr[2], ha->addr[3],
1209 ha->addr[4], ha->addr[5]);
1210
1211 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1212 (is_valid_ether_addr(ha->addr))) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001213 memcpy(ctlr->ctl_src_addr, ha->addr,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001214 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001215 sel_san_mac = 1;
1216 BNX2FC_MISC_DBG("Found SAN MAC\n");
1217 }
1218 }
1219 rcu_read_unlock();
1220
1221 if (!sel_san_mac)
1222 return -ENODEV;
1223
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001224 interface->fip_packet_type.func = bnx2fc_fip_recv;
1225 interface->fip_packet_type.type = htons(ETH_P_FIP);
1226 interface->fip_packet_type.dev = netdev;
1227 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001228
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001229 interface->fcoe_packet_type.func = bnx2fc_rcv;
1230 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1231 interface->fcoe_packet_type.dev = netdev;
1232 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001233
1234 return 0;
1235}
1236
1237static int bnx2fc_attach_transport(void)
1238{
1239 bnx2fc_transport_template =
1240 fc_attach_transport(&bnx2fc_transport_function);
1241
1242 if (bnx2fc_transport_template == NULL) {
1243 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1244 return -ENODEV;
1245 }
1246
1247 bnx2fc_vport_xport_template =
1248 fc_attach_transport(&bnx2fc_vport_xport_function);
1249 if (bnx2fc_vport_xport_template == NULL) {
1250 printk(KERN_ERR PFX
1251 "Failed to attach FC transport for vport\n");
1252 fc_release_transport(bnx2fc_transport_template);
1253 bnx2fc_transport_template = NULL;
1254 return -ENODEV;
1255 }
1256 return 0;
1257}
1258static void bnx2fc_release_transport(void)
1259{
1260 fc_release_transport(bnx2fc_transport_template);
1261 fc_release_transport(bnx2fc_vport_xport_template);
1262 bnx2fc_transport_template = NULL;
1263 bnx2fc_vport_xport_template = NULL;
1264}
1265
1266static void bnx2fc_interface_release(struct kref *kref)
1267{
Robert Love8d55e502012-05-22 19:06:26 -07001268 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001269 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001270 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001271 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001272
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001273 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001274 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001275
Robert Lovefd8f8902012-05-22 19:06:16 -07001276 ctlr = bnx2fc_to_ctlr(interface);
Robert Love8d55e502012-05-22 19:06:26 -07001277 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001278 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001279
1280 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001281 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
Robert Lovefd8f8902012-05-22 19:06:16 -07001282 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001283
Robert Love8d55e502012-05-22 19:06:26 -07001284 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001285
1286 dev_put(netdev);
1287 module_put(THIS_MODULE);
1288}
1289
1290static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1291{
1292 kref_get(&interface->kref);
1293}
1294
1295static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1296{
1297 kref_put(&interface->kref, bnx2fc_interface_release);
1298}
1299static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1300{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001301 /* Free the command manager */
1302 if (hba->cmd_mgr) {
1303 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1304 hba->cmd_mgr = NULL;
1305 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001306 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001307 bnx2fc_unbind_pcidev(hba);
1308 kfree(hba);
1309}
1310
1311/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001312 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001313 *
1314 * @cnic: pointer to cnic device
1315 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001316 * Creates a new FCoE hba on the given device.
1317 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001318 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001319static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001320{
1321 struct bnx2fc_hba *hba;
Barak Witkowski2e499d32012-06-26 01:31:19 +00001322 struct fcoe_capabilities *fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001323 int rc;
1324
1325 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1326 if (!hba) {
1327 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1328 return NULL;
1329 }
1330 spin_lock_init(&hba->hba_lock);
1331 mutex_init(&hba->hba_mutex);
1332
1333 hba->cnic = cnic;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001334
1335 hba->max_tasks = cnic->max_fcoe_exchanges;
1336 hba->elstm_xids = (hba->max_tasks / 2);
1337 hba->max_outstanding_cmds = hba->elstm_xids;
1338 hba->max_xid = (hba->max_tasks - 1);
1339
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001340 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001341 if (rc) {
1342 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001343 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001344 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001345 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001346 hba->next_conn_id = 0;
1347
1348 hba->tgt_ofld_list =
1349 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1350 GFP_KERNEL);
1351 if (!hba->tgt_ofld_list) {
1352 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1353 goto tgtofld_err;
1354 }
1355
1356 hba->num_ofld_sess = 0;
1357
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001358 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001359 if (!hba->cmd_mgr) {
1360 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1361 goto cmgr_err;
1362 }
Barak Witkowski2e499d32012-06-26 01:31:19 +00001363 fcoe_cap = &hba->fcoe_cap;
1364
1365 fcoe_cap->capability1 = BNX2FC_TM_MAX_SQES <<
1366 FCOE_IOS_PER_CONNECTION_SHIFT;
1367 fcoe_cap->capability1 |= BNX2FC_NUM_MAX_SESS <<
1368 FCOE_LOGINS_PER_PORT_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001369 fcoe_cap->capability2 = hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001370 FCOE_NUMBER_OF_EXCHANGES_SHIFT;
1371 fcoe_cap->capability2 |= BNX2FC_MAX_NPIV <<
1372 FCOE_NPIV_WWN_PER_PORT_SHIFT;
1373 fcoe_cap->capability3 = BNX2FC_NUM_MAX_SESS <<
1374 FCOE_TARGETS_SUPPORTED_SHIFT;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001375 fcoe_cap->capability3 |= hba->max_outstanding_cmds <<
Barak Witkowski2e499d32012-06-26 01:31:19 +00001376 FCOE_OUTSTANDING_COMMANDS_SHIFT;
1377 fcoe_cap->capability4 = FCOE_CAPABILITY4_STATEFUL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001378
1379 init_waitqueue_head(&hba->shutdown_wait);
1380 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001381 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001382
1383 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001384
1385cmgr_err:
1386 kfree(hba->tgt_ofld_list);
1387tgtofld_err:
1388 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001389bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001390 kfree(hba);
1391 return NULL;
1392}
1393
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001394struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1395 struct net_device *netdev,
1396 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001397{
Robert Love8d55e502012-05-22 19:06:26 -07001398 struct fcoe_ctlr_device *ctlr_dev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001399 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001400 struct fcoe_ctlr *ctlr;
1401 int size;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001402 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001403
Robert Lovefd8f8902012-05-22 19:06:16 -07001404 size = (sizeof(*interface) + sizeof(struct fcoe_ctlr));
Robert Love8d55e502012-05-22 19:06:26 -07001405 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &bnx2fc_fcoe_sysfs_templ,
1406 size);
1407 if (!ctlr_dev) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001408 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1409 return NULL;
1410 }
Robert Love8d55e502012-05-22 19:06:26 -07001411 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
Robert Love9d348762013-09-05 07:47:27 +00001412 ctlr->cdev = ctlr_dev;
Robert Lovefd8f8902012-05-22 19:06:16 -07001413 interface = fcoe_ctlr_priv(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001414 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001415 kref_init(&interface->kref);
1416 interface->hba = hba;
1417 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001418
1419 /* Initialize FIP */
Robert Lovefd8f8902012-05-22 19:06:16 -07001420 fcoe_ctlr_init(ctlr, fip_mode);
1421 ctlr->send = bnx2fc_fip_send;
1422 ctlr->update_mac = bnx2fc_update_src_mac;
1423 ctlr->get_src_addr = bnx2fc_get_src_mac;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001424 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001425
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001426 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001427 if (!rc)
1428 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001429
Robert Lovefd8f8902012-05-22 19:06:16 -07001430 fcoe_ctlr_destroy(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001431 dev_put(netdev);
Robert Love8d55e502012-05-22 19:06:26 -07001432 fcoe_ctlr_device_delete(ctlr_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001433 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001434}
1435
1436/**
1437 * bnx2fc_if_create - Create FCoE instance on a given interface
1438 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001439 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001440 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1441 * @npiv: Indicates if the port is vport or not
1442 *
1443 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1444 *
1445 * Returns: Allocated fc_lport or an error pointer
1446 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001447static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001448 struct device *parent, int npiv)
1449{
Robert Lovefd8f8902012-05-22 19:06:16 -07001450 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Vasu Dev134a4e22011-04-28 15:55:44 -07001451 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001452 struct fcoe_port *port;
1453 struct Scsi_Host *shost;
1454 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001455 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001456 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001457 int rc = 0;
1458
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001459 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1460 if (!blport) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001461 BNX2FC_HBA_DBG(ctlr->lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001462 return NULL;
1463 }
1464
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001465 /* Allocate Scsi_Host structure */
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001466 bnx2fc_shost_template.can_queue = hba->max_outstanding_cmds;
Vasu Dev134a4e22011-04-28 15:55:44 -07001467 if (!npiv)
1468 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1469 else
1470 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001471
1472 if (!lport) {
1473 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001474 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001475 }
1476 shost = lport->host;
1477 port = lport_priv(lport);
1478 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001479 port->priv = interface;
Yi Zouc3d79092012-12-06 06:24:29 +00001480 port->get_netdev = bnx2fc_netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001481 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1482
1483 /* Configure fcoe_port */
1484 rc = bnx2fc_lport_config(lport);
1485 if (rc)
1486 goto lp_config_err;
1487
1488 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001489 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1490 vport->node_name, vport->port_name);
1491 fc_set_wwnn(lport, vport->node_name);
1492 fc_set_wwpn(lport, vport->port_name);
1493 }
1494 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001495 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001496 if (rc) {
1497 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1498 goto lp_config_err;
1499 }
1500
1501 rc = bnx2fc_shost_config(lport, parent);
1502 if (rc) {
1503 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001504 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001505 goto lp_config_err;
1506 }
1507
1508 /* Initialize the libfc library */
1509 rc = bnx2fc_libfc_config(lport);
1510 if (rc) {
1511 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1512 goto shost_err;
1513 }
1514 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1515
Joe Carnuccio10755d32016-04-07 09:07:56 -04001516 if (bnx2fc_devloss_tmo)
1517 fc_host_dev_loss_tmo(shost) = bnx2fc_devloss_tmo;
1518
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001519 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001520 if (!npiv)
Bhanu Prakash Gollapudi0eb43b42013-04-22 19:22:30 +00001521 rc = bnx2fc_em_config(lport, hba);
Vasu Dev134a4e22011-04-28 15:55:44 -07001522 else {
1523 shost = vport_to_shost(vport);
1524 n_port = shost_priv(shost);
1525 rc = fc_exch_mgr_list_clone(n_port, lport);
1526 }
1527
1528 if (rc) {
1529 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1530 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001531 }
1532
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001533 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001534
1535 spin_lock_bh(&hba->hba_lock);
1536 blport->lport = lport;
1537 list_add_tail(&blport->list, &hba->vports);
1538 spin_unlock_bh(&hba->hba_lock);
1539
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001540 return lport;
1541
1542shost_err:
1543 scsi_remove_host(shost);
1544lp_config_err:
1545 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001546free_blport:
1547 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001548 return NULL;
1549}
1550
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001551static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001552{
1553 /* Dont listen for Ethernet packets anymore */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001554 __dev_remove_pack(&interface->fcoe_packet_type);
1555 __dev_remove_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001556 synchronize_net();
1557}
1558
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001559static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001560{
Robert Lovefd8f8902012-05-22 19:06:16 -07001561 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1562 struct fc_lport *lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001563 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001564 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001565
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001566 /* Stop the transmit retry timer */
1567 del_timer_sync(&port->timer);
1568
1569 /* Free existing transmit skbs */
1570 fcoe_clean_pending_queue(lport);
1571
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001572 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001573
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001574 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001575}
1576
1577static void bnx2fc_if_destroy(struct fc_lport *lport)
1578{
1579
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001580 /* Free queued packets for the receive thread */
1581 bnx2fc_clean_rx_queue(lport);
1582
1583 /* Detach from scsi-ml */
1584 fc_remove_host(lport->host);
1585 scsi_remove_host(lport->host);
1586
1587 /*
1588 * Note that only the physical lport will have the exchange manager.
1589 * for vports, this function is NOP
1590 */
1591 fc_exch_mgr_free(lport);
1592
1593 /* Free memory used by statistical counters */
1594 fc_lport_free_stats(lport);
1595
1596 /* Release Scsi_Host */
1597 scsi_host_put(lport->host);
1598}
1599
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001600static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001601{
Robert Lovefd8f8902012-05-22 19:06:16 -07001602 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
1603 struct fc_lport *lport = ctlr->lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001604 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001605
1606 bnx2fc_interface_cleanup(interface);
1607 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001608 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001609 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001610 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001611}
1612
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001613/**
1614 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1615 *
1616 * @buffer: The name of the Ethernet interface to be destroyed
1617 * @kp: The associated kernel parameter
1618 *
1619 * Called from sysfs.
1620 *
1621 * Returns: 0 for success
1622 */
1623static int bnx2fc_destroy(struct net_device *netdev)
1624{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001625 struct bnx2fc_interface *interface = NULL;
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001626 struct workqueue_struct *timer_work_queue;
Robert Lovefd8f8902012-05-22 19:06:16 -07001627 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001628 int rc = 0;
1629
Nithin Sujir6702ca12011-03-17 17:13:27 -07001630 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001631 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001632
1633 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07001634 ctlr = bnx2fc_to_ctlr(interface);
1635 if (!interface || !ctlr->lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001636 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001637 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001638 goto netdev_err;
1639 }
1640
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001641 timer_work_queue = interface->timer_work_queue;
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001642 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001643 destroy_workqueue(timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001644
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001645netdev_err:
1646 mutex_unlock(&bnx2fc_dev_lock);
1647 rtnl_unlock();
1648 return rc;
1649}
1650
1651static void bnx2fc_destroy_work(struct work_struct *work)
1652{
1653 struct fcoe_port *port;
1654 struct fc_lport *lport;
1655
1656 port = container_of(work, struct fcoe_port, destroy_work);
1657 lport = port->lport;
1658
1659 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1660
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001661 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001662}
1663
1664static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1665{
1666 bnx2fc_free_fw_resc(hba);
1667 bnx2fc_free_task_ctx(hba);
1668}
1669
1670/**
1671 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1672 * pci structure
1673 *
1674 * @hba: Adapter instance
1675 */
1676static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1677{
1678 if (bnx2fc_setup_task_ctx(hba))
1679 goto mem_err;
1680
1681 if (bnx2fc_setup_fw_resc(hba))
1682 goto mem_err;
1683
1684 return 0;
1685mem_err:
1686 bnx2fc_unbind_adapter_devices(hba);
1687 return -ENOMEM;
1688}
1689
1690static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1691{
1692 struct cnic_dev *cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001693 struct pci_dev *pdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001694
1695 if (!hba->cnic) {
1696 printk(KERN_ERR PFX "cnic is NULL\n");
1697 return -ENODEV;
1698 }
1699 cnic = hba->cnic;
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001700 pdev = hba->pcidev = cnic->pcidev;
1701 if (!hba->pcidev)
1702 return -ENODEV;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001703
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001704 switch (pdev->device) {
1705 case PCI_DEVICE_ID_NX2_57710:
1706 strncpy(hba->chip_num, "BCM57710", BCM_CHIP_LEN);
1707 break;
1708 case PCI_DEVICE_ID_NX2_57711:
1709 strncpy(hba->chip_num, "BCM57711", BCM_CHIP_LEN);
1710 break;
1711 case PCI_DEVICE_ID_NX2_57712:
1712 case PCI_DEVICE_ID_NX2_57712_MF:
1713 case PCI_DEVICE_ID_NX2_57712_VF:
1714 strncpy(hba->chip_num, "BCM57712", BCM_CHIP_LEN);
1715 break;
1716 case PCI_DEVICE_ID_NX2_57800:
1717 case PCI_DEVICE_ID_NX2_57800_MF:
1718 case PCI_DEVICE_ID_NX2_57800_VF:
1719 strncpy(hba->chip_num, "BCM57800", BCM_CHIP_LEN);
1720 break;
1721 case PCI_DEVICE_ID_NX2_57810:
1722 case PCI_DEVICE_ID_NX2_57810_MF:
1723 case PCI_DEVICE_ID_NX2_57810_VF:
1724 strncpy(hba->chip_num, "BCM57810", BCM_CHIP_LEN);
1725 break;
1726 case PCI_DEVICE_ID_NX2_57840:
1727 case PCI_DEVICE_ID_NX2_57840_MF:
1728 case PCI_DEVICE_ID_NX2_57840_VF:
1729 case PCI_DEVICE_ID_NX2_57840_2_20:
1730 case PCI_DEVICE_ID_NX2_57840_4_10:
1731 strncpy(hba->chip_num, "BCM57840", BCM_CHIP_LEN);
1732 break;
1733 default:
1734 pr_err(PFX "Unknown device id 0x%x\n", pdev->device);
1735 break;
1736 }
1737 pci_dev_get(hba->pcidev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001738 return 0;
1739}
1740
1741static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1742{
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001743 if (hba->pcidev) {
1744 hba->chip_num[0] = '\0';
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001745 pci_dev_put(hba->pcidev);
Bhanu Prakash Gollapudic13d2b62013-03-08 13:28:51 -08001746 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001747 hba->pcidev = NULL;
1748}
1749
Barak Witkowski2e499d32012-06-26 01:31:19 +00001750/**
1751 * bnx2fc_ulp_get_stats - cnic callback to populate FCoE stats
1752 *
1753 * @handle: transport handle pointing to adapter struture
1754 */
1755static int bnx2fc_ulp_get_stats(void *handle)
1756{
1757 struct bnx2fc_hba *hba = handle;
1758 struct cnic_dev *cnic;
1759 struct fcoe_stats_info *stats_addr;
1760
1761 if (!hba)
1762 return -EINVAL;
1763
1764 cnic = hba->cnic;
1765 stats_addr = &cnic->stats_addr->fcoe_stat;
1766 if (!stats_addr)
1767 return -EINVAL;
1768
1769 strncpy(stats_addr->version, BNX2FC_VERSION,
1770 sizeof(stats_addr->version));
1771 stats_addr->txq_size = BNX2FC_SQ_WQES_MAX;
1772 stats_addr->rxq_size = BNX2FC_CQ_WQES_MAX;
1773
1774 return 0;
1775}
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001776
1777
1778/**
1779 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1780 *
Masanari Iida59e13d42012-04-25 00:24:16 +09001781 * @handle: transport handle pointing to adapter structure
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001782 *
1783 * This function maps adapter structure to pcidev structure and initiates
1784 * firmware handshake to enable/initialize on-chip FCoE components.
1785 * This bnx2fc - cnic interface api callback is used after following
1786 * conditions are met -
1787 * a) underlying network interface is up (marked by event NETDEV_UP
1788 * from netdev
1789 * b) bnx2fc adatper structure is registered.
1790 */
1791static void bnx2fc_ulp_start(void *handle)
1792{
1793 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001794 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07001795 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001796 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001797
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001798 mutex_lock(&bnx2fc_dev_lock);
1799
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001800 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001801 bnx2fc_fw_init(hba);
1802
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001803 BNX2FC_MISC_DBG("bnx2fc started.\n");
1804
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001805 list_for_each_entry(interface, &if_list, list) {
1806 if (interface->hba == hba) {
Robert Lovefd8f8902012-05-22 19:06:16 -07001807 ctlr = bnx2fc_to_ctlr(interface);
1808 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001809 /* Kick off Fabric discovery*/
1810 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1811 lport->tt.frame_send = bnx2fc_xmit;
1812 bnx2fc_start_disc(interface);
1813 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001814 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001815
1816 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001817}
1818
1819static void bnx2fc_port_shutdown(struct fc_lport *lport)
1820{
1821 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1822 fc_fabric_logoff(lport);
1823 fc_lport_destroy(lport);
1824}
1825
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001826static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001827{
Robert Lovefd8f8902012-05-22 19:06:16 -07001828 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001829 struct fc_lport *lport;
1830 struct fc_lport *vport;
1831
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001832 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1833 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001834
Robert Lovefd8f8902012-05-22 19:06:16 -07001835 lport = ctlr->lp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001836 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001837
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001838 mutex_lock(&lport->lp_mutex);
1839 list_for_each_entry(vport, &lport->vports, list)
1840 fc_host_port_type(vport->host) =
1841 FC_PORTTYPE_UNKNOWN;
1842 mutex_unlock(&lport->lp_mutex);
1843 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
Robert Lovefd8f8902012-05-22 19:06:16 -07001844 fcoe_ctlr_link_down(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001845 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001846}
1847
1848static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1849{
1850#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1851 int rc = -1;
1852 int i = HZ;
1853
1854 rc = bnx2fc_bind_adapter_devices(hba);
1855 if (rc) {
1856 printk(KERN_ALERT PFX
1857 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1858 goto err_out;
1859 }
1860
1861 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1862 if (rc) {
1863 printk(KERN_ALERT PFX
1864 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1865 goto err_unbind;
1866 }
1867
1868 /*
1869 * Wait until the adapter init message is complete, and adapter
1870 * state is UP.
1871 */
1872 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1873 msleep(BNX2FC_INIT_POLL_TIME);
1874
1875 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1876 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1877 "Ignoring...\n",
1878 hba->cnic->netdev->name);
1879 rc = -1;
1880 goto err_unbind;
1881 }
1882
1883
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001884 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001885 return 0;
1886
1887err_unbind:
1888 bnx2fc_unbind_adapter_devices(hba);
1889err_out:
1890 return rc;
1891}
1892
1893static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1894{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001895 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001896 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1897 init_timer(&hba->destroy_timer);
1898 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1899 jiffies;
1900 hba->destroy_timer.function = bnx2fc_destroy_timer;
1901 hba->destroy_timer.data = (unsigned long)hba;
1902 add_timer(&hba->destroy_timer);
1903 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001904 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1905 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001906 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001907 /* This should never happen */
1908 if (signal_pending(current))
1909 flush_signals(current);
1910
1911 del_timer_sync(&hba->destroy_timer);
1912 }
1913 bnx2fc_unbind_adapter_devices(hba);
1914 }
1915}
1916
1917/**
1918 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1919 *
1920 * @handle: transport handle pointing to adapter structure
1921 *
1922 * Driver checks if adapter is already in shutdown mode, if not start
1923 * the shutdown process.
1924 */
1925static void bnx2fc_ulp_stop(void *handle)
1926{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001927 struct bnx2fc_hba *hba = handle;
1928 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001929
1930 printk(KERN_ERR "ULP_STOP\n");
1931
1932 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001933 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1934 goto exit;
1935 list_for_each_entry(interface, &if_list, list) {
1936 if (interface->hba == hba)
1937 bnx2fc_stop(interface);
1938 }
1939 BUG_ON(hba->num_ofld_sess != 0);
1940
1941 mutex_lock(&hba->hba_mutex);
1942 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1943 clear_bit(ADAPTER_STATE_GOING_DOWN,
1944 &hba->adapter_state);
1945
1946 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1947 mutex_unlock(&hba->hba_mutex);
1948
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001949 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001950exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001951 mutex_unlock(&bnx2fc_dev_lock);
1952}
1953
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001954static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001955{
Robert Lovefd8f8902012-05-22 19:06:16 -07001956 struct fcoe_ctlr *ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001957 struct fc_lport *lport;
1958 int wait_cnt = 0;
1959
1960 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1961 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001962 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001963 printk(KERN_ERR PFX "Init not done yet\n");
1964 return;
1965 }
1966
Robert Lovefd8f8902012-05-22 19:06:16 -07001967 lport = ctlr->lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001968 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1969
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001970 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001971 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Robert Lovefd8f8902012-05-22 19:06:16 -07001972 fcoe_ctlr_link_up(ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001973 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001974 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001975 }
1976
1977 /* wait for the FCF to be selected before issuing FLOGI */
Robert Lovefd8f8902012-05-22 19:06:16 -07001978 while (!ctlr->sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001979 msleep(250);
1980 /* give up after 3 secs */
1981 if (++wait_cnt > 12)
1982 break;
1983 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001984
1985 /* Reset max receive frame size to default */
1986 if (fc_set_mfs(lport, BNX2FC_MFS))
1987 return;
1988
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001989 fc_lport_init(lport);
1990 fc_fabric_login(lport);
1991}
1992
1993
1994/**
1995 * bnx2fc_ulp_init - Initialize an adapter instance
1996 *
1997 * @dev : cnic device handle
1998 * Called from cnic_register_driver() context to initialize all
1999 * enumerated cnic devices. This routine allocates adapter structure
2000 * and other device specific resources.
2001 */
2002static void bnx2fc_ulp_init(struct cnic_dev *dev)
2003{
2004 struct bnx2fc_hba *hba;
2005 int rc = 0;
2006
2007 BNX2FC_MISC_DBG("Entered %s\n", __func__);
2008 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002009 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
2010 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002011 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002012 " flags: %lx fcoe_conn: %d\n",
2013 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002014 return;
2015 }
2016
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002017 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002018 if (!hba) {
2019 printk(KERN_ERR PFX "hba initialization failed\n");
2020 return;
2021 }
2022
2023 /* Add HBA to the adapter list */
2024 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002025 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002026 adapter_count++;
2027 mutex_unlock(&bnx2fc_dev_lock);
2028
Barak Witkowski2e499d32012-06-26 01:31:19 +00002029 dev->fcoe_cap = &hba->fcoe_cap;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002030 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2031 rc = dev->register_device(dev, CNIC_ULP_FCOE,
2032 (void *) hba);
2033 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07002034 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002035 else
2036 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
2037}
2038
Eddie Wai06808102013-09-25 22:01:20 -07002039/* Assumes rtnl_lock and the bnx2fc_dev_lock are already taken */
2040static int __bnx2fc_disable(struct fcoe_ctlr *ctlr)
2041{
2042 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
2043
2044 if (interface->enabled == true) {
2045 if (!ctlr->lp) {
2046 pr_err(PFX "__bnx2fc_disable: lport not found\n");
2047 return -ENODEV;
2048 } else {
2049 interface->enabled = false;
2050 fcoe_ctlr_link_down(ctlr);
2051 fcoe_clean_pending_queue(ctlr->lp);
2052 }
2053 }
2054 return 0;
2055}
2056
Robert Love6e89ea32012-11-27 06:53:40 +00002057/**
2058 * Deperecated: Use bnx2fc_enabled()
2059 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002060static int bnx2fc_disable(struct net_device *netdev)
2061{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002062 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002063 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002064 int rc = 0;
2065
Nithin Sujir6702ca12011-03-17 17:13:27 -07002066 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002067 mutex_lock(&bnx2fc_dev_lock);
2068
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002069 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002070 ctlr = bnx2fc_to_ctlr(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002071
Eddie Wai06808102013-09-25 22:01:20 -07002072 if (!interface) {
2073 rc = -ENODEV;
2074 pr_err(PFX "bnx2fc_disable: interface not found\n");
2075 } else {
2076 rc = __bnx2fc_disable(ctlr);
2077 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002078 mutex_unlock(&bnx2fc_dev_lock);
2079 rtnl_unlock();
2080 return rc;
2081}
2082
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002083static uint bnx2fc_npiv_create_vports(struct fc_lport *lport,
2084 struct cnic_fc_npiv_tbl *npiv_tbl)
2085{
2086 struct fc_vport_identifiers vpid;
2087 uint i, created = 0;
2088
2089 if (npiv_tbl->count > MAX_NPIV_ENTRIES) {
2090 BNX2FC_HBA_DBG(lport, "Exceeded count max of npiv table\n");
2091 goto done;
2092 }
2093
2094 /* Sanity check the first entry to make sure it's not 0 */
2095 if (wwn_to_u64(npiv_tbl->wwnn[0]) == 0 &&
2096 wwn_to_u64(npiv_tbl->wwpn[0]) == 0) {
2097 BNX2FC_HBA_DBG(lport, "First NPIV table entries invalid.\n");
2098 goto done;
2099 }
2100
2101 vpid.roles = FC_PORT_ROLE_FCP_INITIATOR;
2102 vpid.vport_type = FC_PORTTYPE_NPIV;
2103 vpid.disable = false;
2104
2105 for (i = 0; i < npiv_tbl->count; i++) {
2106 vpid.node_name = wwn_to_u64(npiv_tbl->wwnn[i]);
2107 vpid.port_name = wwn_to_u64(npiv_tbl->wwpn[i]);
2108 scnprintf(vpid.symbolic_name, sizeof(vpid.symbolic_name),
2109 "NPIV[%u]:%016llx-%016llx",
2110 created, vpid.port_name, vpid.node_name);
2111 if (fc_vport_create(lport->host, 0, &vpid))
2112 created++;
2113 else
2114 BNX2FC_HBA_DBG(lport, "Failed to create vport\n");
2115 }
2116done:
2117 return created;
2118}
2119
Eddie Wai06808102013-09-25 22:01:20 -07002120static int __bnx2fc_enable(struct fcoe_ctlr *ctlr)
2121{
2122 struct bnx2fc_interface *interface = fcoe_ctlr_priv(ctlr);
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002123 struct bnx2fc_hba *hba;
Arnd Bergmann720ba802015-10-07 15:11:04 +02002124 struct cnic_fc_npiv_tbl *npiv_tbl;
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002125 struct fc_lport *lport;
Eddie Wai06808102013-09-25 22:01:20 -07002126
2127 if (interface->enabled == false) {
2128 if (!ctlr->lp) {
2129 pr_err(PFX "__bnx2fc_enable: lport not found\n");
2130 return -ENODEV;
2131 } else if (!bnx2fc_link_ok(ctlr->lp)) {
2132 fcoe_ctlr_link_up(ctlr);
2133 interface->enabled = true;
2134 }
2135 }
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002136
2137 /* Create static NPIV ports if any are contained in NVRAM */
2138 hba = interface->hba;
2139 lport = ctlr->lp;
2140
2141 if (!hba)
2142 goto done;
2143
2144 if (!hba->cnic)
2145 goto done;
2146
2147 if (!lport)
2148 goto done;
2149
2150 if (!lport->host)
2151 goto done;
2152
2153 if (!hba->cnic->get_fc_npiv_tbl)
2154 goto done;
2155
Arnd Bergmann720ba802015-10-07 15:11:04 +02002156 npiv_tbl = kzalloc(sizeof(struct cnic_fc_npiv_tbl), GFP_KERNEL);
2157 if (!npiv_tbl)
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002158 goto done;
2159
Arnd Bergmann720ba802015-10-07 15:11:04 +02002160 if (hba->cnic->get_fc_npiv_tbl(hba->cnic, npiv_tbl))
2161 goto done_free;
2162
2163 bnx2fc_npiv_create_vports(lport, npiv_tbl);
2164done_free:
2165 kfree(npiv_tbl);
Joe Carnuccio2971ff62015-08-04 09:37:30 +03002166done:
Eddie Wai06808102013-09-25 22:01:20 -07002167 return 0;
2168}
2169
Robert Love6e89ea32012-11-27 06:53:40 +00002170/**
2171 * Deprecated: Use bnx2fc_enabled()
2172 */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002173static int bnx2fc_enable(struct net_device *netdev)
2174{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002175 struct bnx2fc_interface *interface;
Robert Lovefd8f8902012-05-22 19:06:16 -07002176 struct fcoe_ctlr *ctlr;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002177 int rc = 0;
2178
Nithin Sujir6702ca12011-03-17 17:13:27 -07002179 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002180 mutex_lock(&bnx2fc_dev_lock);
2181
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002182 interface = bnx2fc_interface_lookup(netdev);
Robert Lovefd8f8902012-05-22 19:06:16 -07002183 ctlr = bnx2fc_to_ctlr(interface);
Eddie Wai06808102013-09-25 22:01:20 -07002184 if (!interface) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002185 rc = -ENODEV;
Eddie Wai06808102013-09-25 22:01:20 -07002186 pr_err(PFX "bnx2fc_enable: interface not found\n");
2187 } else {
2188 rc = __bnx2fc_enable(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002189 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002190
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002191 mutex_unlock(&bnx2fc_dev_lock);
2192 rtnl_unlock();
2193 return rc;
2194}
2195
2196/**
Robert Love6e89ea32012-11-27 06:53:40 +00002197 * bnx2fc_ctlr_enabled() - Enable or disable an FCoE Controller
2198 * @cdev: The FCoE Controller that is being enabled or disabled
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002199 *
Robert Love6e89ea32012-11-27 06:53:40 +00002200 * fcoe_sysfs will ensure that the state of 'enabled' has
2201 * changed, so no checking is necessary here. This routine simply
2202 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2203 * When those routines are removed the functionality can be merged
2204 * here.
2205 */
2206static int bnx2fc_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2207{
2208 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
Robert Love6e89ea32012-11-27 06:53:40 +00002209
2210 switch (cdev->enabled) {
2211 case FCOE_CTLR_ENABLED:
Eddie Wai06808102013-09-25 22:01:20 -07002212 return __bnx2fc_enable(ctlr);
Robert Love6e89ea32012-11-27 06:53:40 +00002213 case FCOE_CTLR_DISABLED:
Eddie Wai06808102013-09-25 22:01:20 -07002214 return __bnx2fc_disable(ctlr);
Robert Love6e89ea32012-11-27 06:53:40 +00002215 case FCOE_CTLR_UNUSED:
2216 default:
2217 return -ENOTSUPP;
2218 };
2219}
2220
2221enum bnx2fc_create_link_state {
2222 BNX2FC_CREATE_LINK_DOWN,
2223 BNX2FC_CREATE_LINK_UP,
2224};
2225
2226/**
2227 * _bnx2fc_create() - Create bnx2fc FCoE interface
2228 * @netdev : The net_device object the Ethernet interface to create on
2229 * @fip_mode: The FIP mode for this creation
2230 * @link_state: The ctlr link state on creation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002231 *
Robert Love6e89ea32012-11-27 06:53:40 +00002232 * Called from either the libfcoe 'create' module parameter
2233 * via fcoe_create or from fcoe_syfs's ctlr_create file.
2234 *
2235 * libfcoe's 'create' module parameter is deprecated so some
2236 * consolidation of code can be done when that interface is
2237 * removed.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002238 *
2239 * Returns: 0 for success
2240 */
Robert Love6e89ea32012-11-27 06:53:40 +00002241static int _bnx2fc_create(struct net_device *netdev,
2242 enum fip_state fip_mode,
2243 enum bnx2fc_create_link_state link_state)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002244{
Robert Love6e89ea32012-11-27 06:53:40 +00002245 struct fcoe_ctlr_device *cdev;
Robert Lovefd8f8902012-05-22 19:06:16 -07002246 struct fcoe_ctlr *ctlr;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002247 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002248 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002249 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002250 struct fc_lport *lport;
2251 struct ethtool_drvinfo drvinfo;
2252 int rc = 0;
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002253 int vlan_id = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002254
2255 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
2256 if (fip_mode != FIP_MODE_FABRIC) {
2257 printk(KERN_ERR "fip mode not FABRIC\n");
2258 return -EIO;
2259 }
2260
Nithin Sujir6702ca12011-03-17 17:13:27 -07002261 rtnl_lock();
2262
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002263 mutex_lock(&bnx2fc_dev_lock);
2264
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002265 if (!try_module_get(THIS_MODULE)) {
2266 rc = -EINVAL;
2267 goto mod_err;
2268 }
2269
2270 /* obtain physical netdev */
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002271 if (netdev->priv_flags & IFF_802_1Q_VLAN)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002272 phys_dev = vlan_dev_real_dev(netdev);
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002273
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002274 /* verify if the physical device is a netxtreme2 device */
2275 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
2276 memset(&drvinfo, 0, sizeof(drvinfo));
2277 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002278 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002279 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
2280 rc = -EINVAL;
2281 goto netdev_err;
2282 }
2283 } else {
2284 printk(KERN_ERR PFX "unable to obtain drv_info\n");
2285 rc = -EINVAL;
2286 goto netdev_err;
2287 }
2288
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002289 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002290 hba = bnx2fc_hba_lookup(phys_dev);
2291 if (!hba) {
2292 rc = -ENODEV;
2293 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
2294 goto netdev_err;
2295 }
2296
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002297 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002298 rc = -EEXIST;
2299 goto netdev_err;
2300 }
2301
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002302 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2303 if (!interface) {
2304 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Dan Carpenter2043e1f2014-11-04 13:37:59 +03002305 rc = -ENOMEM;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002306 goto ifput_err;
2307 }
2308
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002309 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2310 vlan_id = vlan_dev_vlan_id(netdev);
2311 interface->vlan_enabled = 1;
2312 }
2313
Robert Lovefd8f8902012-05-22 19:06:16 -07002314 ctlr = bnx2fc_to_ctlr(interface);
Robert Love01bdcb62013-03-25 11:00:26 -07002315 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002316 interface->vlan_id = vlan_id;
Joe Carnuccio10755d32016-04-07 09:07:56 -04002317 interface->tm_timeout = BNX2FC_TM_TIMEOUT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002318
2319 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002320 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002321 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002322 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2323 rc = -EINVAL;
2324 goto ifput_err;
2325 }
2326
Robert Love01bdcb62013-03-25 11:00:26 -07002327 lport = bnx2fc_if_create(interface, &cdev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002328 if (!lport) {
2329 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2330 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002331 rc = -EINVAL;
2332 goto if_create_err;
2333 }
2334
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002335 /* Add interface to if_list */
2336 list_add_tail(&interface->list, &if_list);
2337
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002338 lport->boot_time = jiffies;
2339
2340 /* Make this master N_port */
Robert Lovefd8f8902012-05-22 19:06:16 -07002341 ctlr->lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002342
Robert Love6e89ea32012-11-27 06:53:40 +00002343 if (link_state == BNX2FC_CREATE_LINK_UP)
2344 cdev->enabled = FCOE_CTLR_ENABLED;
2345 else
2346 cdev->enabled = FCOE_CTLR_DISABLED;
2347
2348 if (link_state == BNX2FC_CREATE_LINK_UP &&
2349 !bnx2fc_link_ok(lport)) {
Robert Lovefd8f8902012-05-22 19:06:16 -07002350 fcoe_ctlr_link_up(ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002351 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2352 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2353 }
2354
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002355 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2356 bnx2fc_start_disc(interface);
Robert Love6e89ea32012-11-27 06:53:40 +00002357
2358 if (link_state == BNX2FC_CREATE_LINK_UP)
2359 interface->enabled = true;
2360
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002361 /*
2362 * Release from kref_init in bnx2fc_interface_setup, on success
2363 * lport should be holding a reference taken in bnx2fc_if_create
2364 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002365 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002366 /* put netdev that was held while calling dev_get_by_name */
2367 mutex_unlock(&bnx2fc_dev_lock);
2368 rtnl_unlock();
2369 return 0;
2370
2371if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002372 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002373ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002374 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002375 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -08002376 goto mod_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002377netdev_err:
2378 module_put(THIS_MODULE);
2379mod_err:
2380 mutex_unlock(&bnx2fc_dev_lock);
2381 rtnl_unlock();
2382 return rc;
2383}
2384
2385/**
Robert Love6e89ea32012-11-27 06:53:40 +00002386 * bnx2fc_create() - Create a bnx2fc interface
2387 * @netdev : The net_device object the Ethernet interface to create on
2388 * @fip_mode: The FIP mode for this creation
2389 *
2390 * Called from fcoe transport
2391 *
2392 * Returns: 0 for success
2393 */
2394static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
2395{
2396 return _bnx2fc_create(netdev, fip_mode, BNX2FC_CREATE_LINK_UP);
2397}
2398
2399/**
2400 * bnx2fc_ctlr_alloc() - Allocate a bnx2fc interface from fcoe_sysfs
2401 * @netdev: The net_device to be used by the allocated FCoE Controller
2402 *
2403 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2404 * in a link_down state. The allows the user an opportunity to configure
2405 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2406 *
2407 * Creating in with this routine starts the FCoE Controller in Fabric
2408 * mode. The user can change to VN2VN or another mode before enabling.
2409 */
2410static int bnx2fc_ctlr_alloc(struct net_device *netdev)
2411{
2412 return _bnx2fc_create(netdev, FIP_MODE_FABRIC,
2413 BNX2FC_CREATE_LINK_DOWN);
2414}
2415
2416/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002417 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002418 *
2419 * @cnic: Pointer to cnic device instance
2420 *
2421 **/
2422static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2423{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002424 struct bnx2fc_hba *hba;
2425
2426 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudid71fb3b2012-06-07 02:19:36 -07002427 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002428 if (hba->cnic == cnic)
2429 return hba;
2430 }
2431 return NULL;
2432}
2433
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002434static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2435 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002436{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002437 struct bnx2fc_interface *interface;
2438
2439 /* Called with bnx2fc_dev_lock held */
2440 list_for_each_entry(interface, &if_list, list) {
2441 if (interface->netdev == netdev)
2442 return interface;
2443 }
2444 return NULL;
2445}
2446
2447static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2448 *phys_dev)
2449{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002450 struct bnx2fc_hba *hba;
2451
2452 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002453 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002454 if (hba->phys_dev == phys_dev)
2455 return hba;
2456 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002457 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002458 return NULL;
2459}
2460
2461/**
2462 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2463 *
2464 * @dev cnic device handle
2465 */
2466static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2467{
2468 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002469 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002470
2471 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2472
2473 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2474 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2475 dev->netdev->name, dev->flags);
2476 return;
2477 }
2478
2479 mutex_lock(&bnx2fc_dev_lock);
2480 hba = bnx2fc_find_hba_for_cnic(dev);
2481 if (!hba) {
2482 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2483 dev);
2484 mutex_unlock(&bnx2fc_dev_lock);
2485 return;
2486 }
2487
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002488 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002489 adapter_count--;
2490
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002491 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002492 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002493 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002494 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002495 mutex_unlock(&bnx2fc_dev_lock);
2496
Eddie Wai06c4f202013-12-11 15:30:21 -08002497 /* Ensure ALL destroy work has been completed before return */
2498 flush_workqueue(bnx2fc_wq);
2499
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002500 bnx2fc_ulp_stop(hba);
2501 /* unregister cnic device */
2502 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2503 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002504 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002505}
2506
2507/**
2508 * bnx2fc_fcoe_reset - Resets the fcoe
2509 *
2510 * @shost: shost the reset is from
2511 *
2512 * Returns: always 0
2513 */
2514static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2515{
2516 struct fc_lport *lport = shost_priv(shost);
2517 fc_lport_reset(lport);
2518 return 0;
2519}
2520
2521
2522static bool bnx2fc_match(struct net_device *netdev)
2523{
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002524 struct net_device *phys_dev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002525
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002526 mutex_lock(&bnx2fc_dev_lock);
2527 if (netdev->priv_flags & IFF_802_1Q_VLAN)
2528 phys_dev = vlan_dev_real_dev(netdev);
2529
2530 if (bnx2fc_hba_lookup(phys_dev)) {
2531 mutex_unlock(&bnx2fc_dev_lock);
2532 return true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002533 }
Bhanu Prakash Gollapudi7adc5a32012-06-04 16:15:44 -07002534
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002535 mutex_unlock(&bnx2fc_dev_lock);
2536 return false;
2537}
2538
2539
2540static struct fcoe_transport bnx2fc_transport = {
2541 .name = {"bnx2fc"},
2542 .attached = false,
2543 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
Robert Love6e89ea32012-11-27 06:53:40 +00002544 .alloc = bnx2fc_ctlr_alloc,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002545 .match = bnx2fc_match,
2546 .create = bnx2fc_create,
2547 .destroy = bnx2fc_destroy,
2548 .enable = bnx2fc_enable,
2549 .disable = bnx2fc_disable,
2550};
2551
2552/**
2553 * bnx2fc_percpu_thread_create - Create a receive thread for an
2554 * online CPU
2555 *
2556 * @cpu: cpu index for the online cpu
2557 */
2558static void bnx2fc_percpu_thread_create(unsigned int cpu)
2559{
2560 struct bnx2fc_percpu_s *p;
2561 struct task_struct *thread;
2562
2563 p = &per_cpu(bnx2fc_percpu, cpu);
2564
Eric Dumazet69614272012-06-04 16:15:42 -07002565 thread = kthread_create_on_node(bnx2fc_percpu_io_thread,
2566 (void *)p, cpu_to_node(cpu),
2567 "bnx2fc_thread/%d", cpu);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002568 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002569 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002570 kthread_bind(thread, cpu);
2571 p->iothread = thread;
2572 wake_up_process(thread);
2573 }
2574}
2575
2576static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2577{
2578 struct bnx2fc_percpu_s *p;
2579 struct task_struct *thread;
2580 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002581
2582 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2583
2584 /* Prevent any new work from being queued for this CPU */
2585 p = &per_cpu(bnx2fc_percpu, cpu);
2586 spin_lock_bh(&p->fp_work_lock);
2587 thread = p->iothread;
2588 p->iothread = NULL;
2589
2590
2591 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002592 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002593 list_del_init(&work->list);
2594 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2595 kfree(work);
2596 }
2597
2598 spin_unlock_bh(&p->fp_work_lock);
2599
2600 if (thread)
2601 kthread_stop(thread);
2602}
2603
2604/**
2605 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2606 *
2607 * @nfb: The callback data block
2608 * @action: The event triggering the callback
2609 * @hcpu: The index of the CPU that the event is for
2610 *
2611 * This creates or destroys per-CPU data for fcoe
2612 *
2613 * Returns NOTIFY_OK always.
2614 */
2615static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2616 unsigned long action, void *hcpu)
2617{
2618 unsigned cpu = (unsigned long)hcpu;
2619
2620 switch (action) {
2621 case CPU_ONLINE:
2622 case CPU_ONLINE_FROZEN:
2623 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2624 bnx2fc_percpu_thread_create(cpu);
2625 break;
2626 case CPU_DEAD:
2627 case CPU_DEAD_FROZEN:
2628 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2629 bnx2fc_percpu_thread_destroy(cpu);
2630 break;
2631 default:
2632 break;
2633 }
2634 return NOTIFY_OK;
2635}
2636
Joe Carnuccio10755d32016-04-07 09:07:56 -04002637static int bnx2fc_slave_configure(struct scsi_device *sdev)
2638{
2639 if (!bnx2fc_queue_depth)
2640 return 0;
2641
2642 scsi_change_queue_depth(sdev, bnx2fc_queue_depth);
2643 return 0;
2644}
2645
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002646/**
2647 * bnx2fc_mod_init - module init entry point
2648 *
2649 * Initialize driver wide global data structures, and register
2650 * with cnic module
2651 **/
2652static int __init bnx2fc_mod_init(void)
2653{
2654 struct fcoe_percpu_s *bg;
2655 struct task_struct *l2_thread;
2656 int rc = 0;
2657 unsigned int cpu = 0;
2658 struct bnx2fc_percpu_s *p;
2659
2660 printk(KERN_INFO PFX "%s", version);
2661
2662 /* register as a fcoe transport */
2663 rc = fcoe_transport_attach(&bnx2fc_transport);
2664 if (rc) {
2665 printk(KERN_ERR "failed to register an fcoe transport, check "
2666 "if libfcoe is loaded\n");
2667 goto out;
2668 }
2669
2670 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002671 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002672 mutex_init(&bnx2fc_dev_lock);
2673 adapter_count = 0;
2674
2675 /* Attach FC transport template */
2676 rc = bnx2fc_attach_transport();
2677 if (rc)
2678 goto detach_ft;
2679
2680 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2681 if (!bnx2fc_wq) {
2682 rc = -ENOMEM;
2683 goto release_bt;
2684 }
2685
2686 bg = &bnx2fc_global;
2687 skb_queue_head_init(&bg->fcoe_rx_list);
2688 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2689 (void *)bg,
2690 "bnx2fc_l2_thread");
2691 if (IS_ERR(l2_thread)) {
2692 rc = PTR_ERR(l2_thread);
2693 goto free_wq;
2694 }
2695 wake_up_process(l2_thread);
2696 spin_lock_bh(&bg->fcoe_rx_list.lock);
2697 bg->thread = l2_thread;
2698 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2699
2700 for_each_possible_cpu(cpu) {
2701 p = &per_cpu(bnx2fc_percpu, cpu);
2702 INIT_LIST_HEAD(&p->work_list);
2703 spin_lock_init(&p->fp_work_lock);
2704 }
2705
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302706 cpu_notifier_register_begin();
2707
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002708 for_each_online_cpu(cpu) {
2709 bnx2fc_percpu_thread_create(cpu);
2710 }
2711
2712 /* Initialize per CPU interrupt thread */
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302713 __register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2714
2715 cpu_notifier_register_done();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002716
2717 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2718
2719 return 0;
2720
2721free_wq:
2722 destroy_workqueue(bnx2fc_wq);
2723release_bt:
2724 bnx2fc_release_transport();
2725detach_ft:
2726 fcoe_transport_detach(&bnx2fc_transport);
2727out:
2728 return rc;
2729}
2730
2731static void __exit bnx2fc_mod_exit(void)
2732{
2733 LIST_HEAD(to_be_deleted);
2734 struct bnx2fc_hba *hba, *next;
2735 struct fcoe_percpu_s *bg;
2736 struct task_struct *l2_thread;
2737 struct sk_buff *skb;
2738 unsigned int cpu = 0;
2739
2740 /*
2741 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2742 * it will have higher precedence than bnx2fc_dev_lock.
2743 * unregister_device() cannot be called with bnx2fc_dev_lock
2744 * held.
2745 */
2746 mutex_lock(&bnx2fc_dev_lock);
2747 list_splice(&adapter_list, &to_be_deleted);
2748 INIT_LIST_HEAD(&adapter_list);
2749 adapter_count = 0;
2750 mutex_unlock(&bnx2fc_dev_lock);
2751
2752 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002753 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2754 list_del_init(&hba->list);
2755 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2756 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002757 bnx2fc_ulp_stop(hba);
2758 /* unregister cnic device */
2759 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2760 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002761 hba->cnic->unregister_device(hba->cnic,
2762 CNIC_ULP_FCOE);
2763 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002764 }
2765 cnic_unregister_driver(CNIC_ULP_FCOE);
2766
2767 /* Destroy global thread */
2768 bg = &bnx2fc_global;
2769 spin_lock_bh(&bg->fcoe_rx_list.lock);
2770 l2_thread = bg->thread;
2771 bg->thread = NULL;
2772 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2773 kfree_skb(skb);
2774
2775 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2776
2777 if (l2_thread)
2778 kthread_stop(l2_thread);
2779
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302780 cpu_notifier_register_begin();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002781
2782 /* Destroy per cpu threads */
2783 for_each_online_cpu(cpu) {
2784 bnx2fc_percpu_thread_destroy(cpu);
2785 }
2786
Srivatsa S. Bhat7229b6d2014-03-11 02:09:45 +05302787 __unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2788
2789 cpu_notifier_register_done();
2790
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002791 destroy_workqueue(bnx2fc_wq);
2792 /*
2793 * detach from scsi transport
2794 * must happen after all destroys are done
2795 */
2796 bnx2fc_release_transport();
2797
2798 /* detach from fcoe transport */
2799 fcoe_transport_detach(&bnx2fc_transport);
2800}
2801
2802module_init(bnx2fc_mod_init);
2803module_exit(bnx2fc_mod_exit);
2804
Robert Love8d55e502012-05-22 19:06:26 -07002805static struct fcoe_sysfs_function_template bnx2fc_fcoe_sysfs_templ = {
Robert Love6e89ea32012-11-27 06:53:40 +00002806 .set_fcoe_ctlr_enabled = bnx2fc_ctlr_enabled,
Yi Zoub8b3e692012-12-06 06:24:59 +00002807 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
2808 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
2809 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
2810 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
2811 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
2812 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
Robert Love8d55e502012-05-22 19:06:26 -07002813
2814 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
2815 .get_fcoe_fcf_vlan_id = bnx2fc_fcf_get_vlan_id,
2816};
2817
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002818static struct fc_function_template bnx2fc_transport_function = {
2819 .show_host_node_name = 1,
2820 .show_host_port_name = 1,
2821 .show_host_supported_classes = 1,
2822 .show_host_supported_fc4s = 1,
2823 .show_host_active_fc4s = 1,
2824 .show_host_maxframe_size = 1,
2825
2826 .show_host_port_id = 1,
2827 .show_host_supported_speeds = 1,
2828 .get_host_speed = fc_get_host_speed,
2829 .show_host_speed = 1,
2830 .show_host_port_type = 1,
2831 .get_host_port_state = fc_get_host_port_state,
2832 .show_host_port_state = 1,
2833 .show_host_symbolic_name = 1,
2834
2835 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2836 sizeof(struct bnx2fc_rport)),
2837 .show_rport_maxframe_size = 1,
2838 .show_rport_supported_classes = 1,
2839
2840 .show_host_fabric_name = 1,
2841 .show_starget_node_name = 1,
2842 .show_starget_port_name = 1,
2843 .show_starget_port_id = 1,
2844 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2845 .show_rport_dev_loss_tmo = 1,
2846 .get_fc_host_stats = bnx2fc_get_host_stats,
2847
2848 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2849
2850 .terminate_rport_io = fc_rport_terminate_io,
2851
2852 .vport_create = bnx2fc_vport_create,
2853 .vport_delete = bnx2fc_vport_destroy,
2854 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002855 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002856};
2857
2858static struct fc_function_template bnx2fc_vport_xport_function = {
2859 .show_host_node_name = 1,
2860 .show_host_port_name = 1,
2861 .show_host_supported_classes = 1,
2862 .show_host_supported_fc4s = 1,
2863 .show_host_active_fc4s = 1,
2864 .show_host_maxframe_size = 1,
2865
2866 .show_host_port_id = 1,
2867 .show_host_supported_speeds = 1,
2868 .get_host_speed = fc_get_host_speed,
2869 .show_host_speed = 1,
2870 .show_host_port_type = 1,
2871 .get_host_port_state = fc_get_host_port_state,
2872 .show_host_port_state = 1,
2873 .show_host_symbolic_name = 1,
2874
2875 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2876 sizeof(struct bnx2fc_rport)),
2877 .show_rport_maxframe_size = 1,
2878 .show_rport_supported_classes = 1,
2879
2880 .show_host_fabric_name = 1,
2881 .show_starget_node_name = 1,
2882 .show_starget_port_name = 1,
2883 .show_starget_port_id = 1,
2884 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2885 .show_rport_dev_loss_tmo = 1,
2886 .get_fc_host_stats = fc_get_host_stats,
2887 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2888 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002889 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002890};
2891
Joe Carnuccio10755d32016-04-07 09:07:56 -04002892/*
2893 * Additional scsi_host attributes.
2894 */
2895static ssize_t
2896bnx2fc_tm_timeout_show(struct device *dev, struct device_attribute *attr,
2897 char *buf)
2898{
2899 struct Scsi_Host *shost = class_to_shost(dev);
2900 struct fc_lport *lport = shost_priv(shost);
2901 struct fcoe_port *port = lport_priv(lport);
2902 struct bnx2fc_interface *interface = port->priv;
2903
2904 sprintf(buf, "%u\n", interface->tm_timeout);
2905 return strlen(buf);
2906}
2907
2908static ssize_t
2909bnx2fc_tm_timeout_store(struct device *dev,
2910 struct device_attribute *attr, const char *buf, size_t count)
2911{
2912 struct Scsi_Host *shost = class_to_shost(dev);
2913 struct fc_lport *lport = shost_priv(shost);
2914 struct fcoe_port *port = lport_priv(lport);
2915 struct bnx2fc_interface *interface = port->priv;
2916 int rval, val;
2917
2918 rval = kstrtouint(buf, 10, &val);
2919 if (rval)
2920 return rval;
2921 if (val > 255)
2922 return -ERANGE;
2923
2924 interface->tm_timeout = (u8)val;
2925 return strlen(buf);
2926}
2927
2928static DEVICE_ATTR(tm_timeout, S_IRUGO|S_IWUSR, bnx2fc_tm_timeout_show,
2929 bnx2fc_tm_timeout_store);
2930
2931static struct device_attribute *bnx2fc_host_attrs[] = {
2932 &dev_attr_tm_timeout,
2933 NULL,
2934};
2935
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002936/**
2937 * scsi_host_template structure used while registering with SCSI-ml
2938 */
2939static struct scsi_host_template bnx2fc_shost_template = {
2940 .module = THIS_MODULE,
Saurav Kashyap17d87c42014-07-03 08:18:28 -04002941 .name = "QLogic Offload FCoE Initiator",
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002942 .queuecommand = bnx2fc_queuecommand,
2943 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2944 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2945 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2946 .eh_host_reset_handler = fc_eh_host_reset,
2947 .slave_alloc = fc_slave_alloc,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +01002948 .change_queue_depth = scsi_change_queue_depth,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002949 .this_id = -1,
2950 .cmd_per_lun = 3,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002951 .use_clustering = ENABLE_CLUSTERING,
2952 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
Bhanu Prakash Gollapudid450d772012-12-21 19:40:33 -08002953 .max_sectors = 1024,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +01002954 .track_queue_depth = 1,
Joe Carnuccio10755d32016-04-07 09:07:56 -04002955 .slave_configure = bnx2fc_slave_configure,
2956 .shost_attrs = bnx2fc_host_attrs,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002957};
2958
2959static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2960 .frame_send = bnx2fc_xmit,
2961 .elsct_send = bnx2fc_elsct_send,
2962 .fcp_abort_io = bnx2fc_abort_io,
2963 .fcp_cleanup = bnx2fc_cleanup,
Yi Zoub8b3e692012-12-06 06:24:59 +00002964 .get_lesb = fcoe_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002965 .rport_event_callback = bnx2fc_rport_event_handler,
2966};
2967
2968/**
2969 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2970 * structure carrying callback function pointers
2971 */
2972static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2973 .owner = THIS_MODULE,
2974 .cnic_init = bnx2fc_ulp_init,
2975 .cnic_exit = bnx2fc_ulp_exit,
2976 .cnic_start = bnx2fc_ulp_start,
2977 .cnic_stop = bnx2fc_ulp_stop,
2978 .indicate_kcqes = bnx2fc_indicate_kcqe,
2979 .indicate_netevent = bnx2fc_indicate_netevent,
Barak Witkowski2e499d32012-06-26 01:31:19 +00002980 .cnic_get_stats = bnx2fc_ulp_get_stats,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002981};