blob: c1c6a92a0b989737c9f8a15b81e8e24cd86b758e [file] [log] [blame]
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001/* bnx2fc_fcoe.c: Broadcom NetXtreme II Linux FCoE offload driver.
2 * This file contains the code that interacts with libfc, libfcoe,
3 * cnic modules to create FCoE instances, send/receive non-offloaded
4 * FIP/FCoE packets, listen to link events etc.
5 *
Bhanu Prakash Gollapudi9b35baa2011-07-27 11:32:13 -07006 * Copyright (c) 2008 - 2011 Broadcom Corporation
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation.
11 *
12 * Written by: Bhanu Prakash Gollapudi (bprakash@broadcom.com)
13 */
14
15#include "bnx2fc.h"
16
17static struct list_head adapter_list;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070018static struct list_head if_list;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080019static u32 adapter_count;
20static DEFINE_MUTEX(bnx2fc_dev_lock);
21DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
22
23#define DRV_MODULE_NAME "bnx2fc"
24#define DRV_MODULE_VERSION BNX2FC_VERSION
Bhanu Prakash Gollapudi98788a12012-01-23 18:00:49 -080025#define DRV_MODULE_RELDATE "Jan 22, 2011"
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080026
27
28static char version[] __devinitdata =
29 "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
30 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31
32
33MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
34MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
35MODULE_LICENSE("GPL");
36MODULE_VERSION(DRV_MODULE_VERSION);
37
38#define BNX2FC_MAX_QUEUE_DEPTH 256
39#define BNX2FC_MIN_QUEUE_DEPTH 32
40#define FCOE_WORD_TO_BYTE 4
41
42static struct scsi_transport_template *bnx2fc_transport_template;
43static struct scsi_transport_template *bnx2fc_vport_xport_template;
44
45struct workqueue_struct *bnx2fc_wq;
46
47/* bnx2fc structure needs only one instance of the fcoe_percpu_s structure.
48 * Here the io threads are per cpu but the l2 thread is just one
49 */
50struct fcoe_percpu_s bnx2fc_global;
51DEFINE_SPINLOCK(bnx2fc_global_lock);
52
53static struct cnic_ulp_ops bnx2fc_cnic_cb;
54static struct libfc_function_template bnx2fc_libfc_fcn_templ;
55static struct scsi_host_template bnx2fc_shost_template;
56static struct fc_function_template bnx2fc_transport_function;
57static struct fc_function_template bnx2fc_vport_xport_function;
58static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -070059static void __bnx2fc_destroy(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080060static int bnx2fc_destroy(struct net_device *net_device);
61static int bnx2fc_enable(struct net_device *netdev);
62static int bnx2fc_disable(struct net_device *netdev);
63
64static void bnx2fc_recv_frame(struct sk_buff *skb);
65
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070066static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080067static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080068static int bnx2fc_lport_config(struct fc_lport *lport);
69static int bnx2fc_em_config(struct fc_lport *lport);
70static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
71static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
72static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
73static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070074static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080075 struct device *parent, int npiv);
76static void bnx2fc_destroy_work(struct work_struct *work);
77
78static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070079static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
80 *phys_dev);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -070081static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080082static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
83
84static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
85static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
86
87static void bnx2fc_port_shutdown(struct fc_lport *lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -070088static void bnx2fc_stop(struct bnx2fc_interface *interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -080089static int __init bnx2fc_mod_init(void);
90static void __exit bnx2fc_mod_exit(void);
91
92unsigned int bnx2fc_debug_level;
93module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
94
95static int bnx2fc_cpu_callback(struct notifier_block *nfb,
96 unsigned long action, void *hcpu);
97/* notification function for CPU hotplug events */
98static struct notifier_block bnx2fc_cpu_notifier = {
99 .notifier_call = bnx2fc_cpu_callback,
100};
101
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -0700102static inline struct net_device *bnx2fc_netdev(const struct fc_lport *lport)
103{
104 return ((struct bnx2fc_interface *)
105 ((struct fcoe_port *)lport_priv(lport))->priv)->netdev;
106}
107
108/**
109 * bnx2fc_get_lesb() - Fill the FCoE Link Error Status Block
110 * @lport: the local port
111 * @fc_lesb: the link error status block
112 */
113static void bnx2fc_get_lesb(struct fc_lport *lport,
114 struct fc_els_lesb *fc_lesb)
115{
116 struct net_device *netdev = bnx2fc_netdev(lport);
117
118 __fcoe_get_lesb(lport, fc_lesb, netdev);
119}
120
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800121static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
122{
123 struct fcoe_percpu_s *bg;
124 struct fcoe_rcv_info *fr;
125 struct sk_buff_head *list;
126 struct sk_buff *skb, *next;
127 struct sk_buff *head;
128
129 bg = &bnx2fc_global;
130 spin_lock_bh(&bg->fcoe_rx_list.lock);
131 list = &bg->fcoe_rx_list;
132 head = list->next;
133 for (skb = head; skb != (struct sk_buff *)list;
134 skb = next) {
135 next = skb->next;
136 fr = fcoe_dev_from_skb(skb);
137 if (fr->fr_dev == lp) {
138 __skb_unlink(skb, list);
139 kfree_skb(skb);
140 }
141 }
142 spin_unlock_bh(&bg->fcoe_rx_list.lock);
143}
144
145int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
146{
147 int rc;
148 spin_lock(&bnx2fc_global_lock);
149 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
150 spin_unlock(&bnx2fc_global_lock);
151
152 return rc;
153}
154
155static void bnx2fc_abort_io(struct fc_lport *lport)
156{
157 /*
158 * This function is no-op for bnx2fc, but we do
159 * not want to leave it as NULL either, as libfc
160 * can call the default function which is
161 * fc_fcp_abort_io.
162 */
163}
164
165static void bnx2fc_cleanup(struct fc_lport *lport)
166{
167 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700168 struct bnx2fc_interface *interface = port->priv;
169 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800170 struct bnx2fc_rport *tgt;
171 int i;
172
173 BNX2FC_MISC_DBG("Entered %s\n", __func__);
174 mutex_lock(&hba->hba_mutex);
175 spin_lock_bh(&hba->hba_lock);
176 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
177 tgt = hba->tgt_ofld_list[i];
178 if (tgt) {
179 /* Cleanup IOs belonging to requested vport */
180 if (tgt->port == port) {
181 spin_unlock_bh(&hba->hba_lock);
182 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
183 bnx2fc_flush_active_ios(tgt);
184 spin_lock_bh(&hba->hba_lock);
185 }
186 }
187 }
188 spin_unlock_bh(&hba->hba_lock);
189 mutex_unlock(&hba->hba_mutex);
190}
191
192static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
193 struct fc_frame *fp)
194{
195 struct fc_rport_priv *rdata = tgt->rdata;
196 struct fc_frame_header *fh;
197 int rc = 0;
198
199 fh = fc_frame_header_get(fp);
200 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
201 "r_ctl = 0x%x\n", rdata->ids.port_id,
202 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
203 if ((fh->fh_type == FC_TYPE_ELS) &&
204 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
205
206 switch (fc_frame_payload_op(fp)) {
207 case ELS_ADISC:
208 rc = bnx2fc_send_adisc(tgt, fp);
209 break;
210 case ELS_LOGO:
211 rc = bnx2fc_send_logo(tgt, fp);
212 break;
213 case ELS_RLS:
214 rc = bnx2fc_send_rls(tgt, fp);
215 break;
216 default:
217 break;
218 }
219 } else if ((fh->fh_type == FC_TYPE_BLS) &&
220 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
221 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
222 else {
223 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
224 "rctl 0x%x thru non-offload path\n",
225 fh->fh_type, fh->fh_r_ctl);
226 return -ENODEV;
227 }
228 if (rc)
229 return -ENOMEM;
230 else
231 return 0;
232}
233
234/**
235 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
236 *
237 * @lport: the associated local port
238 * @fp: the fc_frame to be transmitted
239 */
240static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
241{
242 struct ethhdr *eh;
243 struct fcoe_crc_eof *cp;
244 struct sk_buff *skb;
245 struct fc_frame_header *fh;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700246 struct bnx2fc_interface *interface;
247 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800248 struct fcoe_port *port;
249 struct fcoe_hdr *hp;
250 struct bnx2fc_rport *tgt;
251 struct fcoe_dev_stats *stats;
252 u8 sof, eof;
253 u32 crc;
254 unsigned int hlen, tlen, elen;
255 int wlen, rc = 0;
256
257 port = (struct fcoe_port *)lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700258 interface = port->priv;
259 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800260
261 fh = fc_frame_header_get(fp);
262
263 skb = fp_skb(fp);
264 if (!lport->link_up) {
265 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
266 kfree_skb(skb);
267 return 0;
268 }
269
270 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700271 if (!interface->ctlr.sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800272 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
273 kfree_skb(skb);
274 return -EINVAL;
275 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700276 if (fcoe_ctlr_els_send(&interface->ctlr, lport, skb))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800277 return 0;
278 }
279
280 sof = fr_sof(fp);
281 eof = fr_eof(fp);
282
283 /*
284 * Snoop the frame header to check if the frame is for
285 * an offloaded session
286 */
287 /*
288 * tgt_ofld_list access is synchronized using
289 * both hba mutex and hba lock. Atleast hba mutex or
290 * hba lock needs to be held for read access.
291 */
292
293 spin_lock_bh(&hba->hba_lock);
294 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
295 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
296 /* This frame is for offloaded session */
297 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
298 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
299 spin_unlock_bh(&hba->hba_lock);
300 rc = bnx2fc_xmit_l2_frame(tgt, fp);
301 if (rc != -ENODEV) {
302 kfree_skb(skb);
303 return rc;
304 }
305 } else {
306 spin_unlock_bh(&hba->hba_lock);
307 }
308
309 elen = sizeof(struct ethhdr);
310 hlen = sizeof(struct fcoe_hdr);
311 tlen = sizeof(struct fcoe_crc_eof);
312 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
313
314 skb->ip_summed = CHECKSUM_NONE;
315 crc = fcoe_fc_crc(fp);
316
317 /* copy port crc and eof to the skb buff */
318 if (skb_is_nonlinear(skb)) {
319 skb_frag_t *frag;
320 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
321 kfree_skb(skb);
322 return -ENOMEM;
323 }
324 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +0800325 cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800326 } else {
327 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
328 }
329
330 memset(cp, 0, sizeof(*cp));
331 cp->fcoe_eof = eof;
332 cp->fcoe_crc32 = cpu_to_le32(~crc);
333 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +0800334 kunmap_atomic(cp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800335 cp = NULL;
336 }
337
338 /* adjust skb network/transport offsets to match mac/fcoe/port */
339 skb_push(skb, elen + hlen);
340 skb_reset_mac_header(skb);
341 skb_reset_network_header(skb);
342 skb->mac_len = elen;
343 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700344 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800345
346 /* fill up mac and fcoe headers */
347 eh = eth_hdr(skb);
348 eh->h_proto = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700349 if (interface->ctlr.map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800350 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
351 else
352 /* insert GW address */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700353 memcpy(eh->h_dest, interface->ctlr.dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800354
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700355 if (unlikely(interface->ctlr.flogi_oxid != FC_XID_UNKNOWN))
356 memcpy(eh->h_source, interface->ctlr.ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800357 else
358 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
359
360 hp = (struct fcoe_hdr *)(eh + 1);
361 memset(hp, 0, sizeof(*hp));
362 if (FC_FCOE_VER)
363 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
364 hp->fcoe_sof = sof;
365
366 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
367 if (lport->seq_offload && fr_max_payload(fp)) {
368 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
369 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
370 } else {
371 skb_shinfo(skb)->gso_type = 0;
372 skb_shinfo(skb)->gso_size = 0;
373 }
374
375 /*update tx stats */
376 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
377 stats->TxFrames++;
378 stats->TxWords += wlen;
379 put_cpu();
380
381 /* send down to lld */
382 fr_dev(fp) = lport;
383 if (port->fcoe_pending_queue.qlen)
384 fcoe_check_wait_queue(lport, skb);
385 else if (fcoe_start_io(skb))
386 fcoe_check_wait_queue(lport, skb);
387
388 return 0;
389}
390
391/**
392 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
393 *
394 * @skb: the receive socket buffer
395 * @dev: associated net device
396 * @ptype: context
397 * @olddev: last device
398 *
399 * This function receives the packet and builds FC frame and passes it up
400 */
401static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
402 struct packet_type *ptype, struct net_device *olddev)
403{
404 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700405 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800406 struct fc_frame_header *fh;
407 struct fcoe_rcv_info *fr;
408 struct fcoe_percpu_s *bg;
409 unsigned short oxid;
410
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700411 interface = container_of(ptype, struct bnx2fc_interface,
412 fcoe_packet_type);
413 lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800414
415 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700416 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800417 goto err;
418 }
419
420 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700421 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800422 goto err;
423 }
424
425 /*
426 * Check for minimum frame length, and make sure required FCoE
427 * and FC headers are pulled into the linear data area.
428 */
429 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
430 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
431 goto err;
432
433 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
434 fh = (struct fc_frame_header *) skb_transport_header(skb);
435
436 oxid = ntohs(fh->fh_ox_id);
437
438 fr = fcoe_dev_from_skb(skb);
439 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800440
441 bg = &bnx2fc_global;
Neil Hormanfc05ab72012-03-09 14:50:13 -0800442 spin_lock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800443
444 __skb_queue_tail(&bg->fcoe_rx_list, skb);
445 if (bg->fcoe_rx_list.qlen == 1)
446 wake_up_process(bg->thread);
447
Neil Hormanfc05ab72012-03-09 14:50:13 -0800448 spin_unlock(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800449
450 return 0;
451err:
452 kfree_skb(skb);
453 return -1;
454}
455
456static int bnx2fc_l2_rcv_thread(void *arg)
457{
458 struct fcoe_percpu_s *bg = arg;
459 struct sk_buff *skb;
460
461 set_user_nice(current, -20);
462 set_current_state(TASK_INTERRUPTIBLE);
463 while (!kthread_should_stop()) {
464 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800465 spin_lock_bh(&bg->fcoe_rx_list.lock);
466 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
467 spin_unlock_bh(&bg->fcoe_rx_list.lock);
468 bnx2fc_recv_frame(skb);
469 spin_lock_bh(&bg->fcoe_rx_list.lock);
470 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700471 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800472 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800473 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700474 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800475 return 0;
476}
477
478
479static void bnx2fc_recv_frame(struct sk_buff *skb)
480{
481 u32 fr_len;
482 struct fc_lport *lport;
483 struct fcoe_rcv_info *fr;
484 struct fcoe_dev_stats *stats;
485 struct fc_frame_header *fh;
486 struct fcoe_crc_eof crc_eof;
487 struct fc_frame *fp;
488 struct fc_lport *vn_port;
489 struct fcoe_port *port;
490 u8 *mac = NULL;
491 u8 *dest_mac = NULL;
492 struct fcoe_hdr *hp;
493
494 fr = fcoe_dev_from_skb(skb);
495 lport = fr->fr_dev;
496 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700497 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800498 kfree_skb(skb);
499 return;
500 }
501
502 if (skb_is_nonlinear(skb))
503 skb_linearize(skb);
504 mac = eth_hdr(skb)->h_source;
505 dest_mac = eth_hdr(skb)->h_dest;
506
507 /* Pull the header */
508 hp = (struct fcoe_hdr *) skb_network_header(skb);
509 fh = (struct fc_frame_header *) skb_transport_header(skb);
510 skb_pull(skb, sizeof(struct fcoe_hdr));
511 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
512
513 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
514 stats->RxFrames++;
515 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
516
517 fp = (struct fc_frame *)skb;
518 fc_frame_init(fp);
519 fr_dev(fp) = lport;
520 fr_sof(fp) = hp->fcoe_sof;
521 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
522 put_cpu();
523 kfree_skb(skb);
524 return;
525 }
526 fr_eof(fp) = crc_eof.fcoe_eof;
527 fr_crc(fp) = crc_eof.fcoe_crc32;
528 if (pskb_trim(skb, fr_len)) {
529 put_cpu();
530 kfree_skb(skb);
531 return;
532 }
533
534 fh = fc_frame_header_get(fp);
535
536 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
537 if (vn_port) {
538 port = lport_priv(vn_port);
539 if (compare_ether_addr(port->data_src_addr, dest_mac)
540 != 0) {
541 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
542 put_cpu();
543 kfree_skb(skb);
544 return;
545 }
546 }
547 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
548 fh->fh_type == FC_TYPE_FCP) {
549 /* Drop FCP data. We dont this in L2 path */
550 put_cpu();
551 kfree_skb(skb);
552 return;
553 }
554 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
555 fh->fh_type == FC_TYPE_ELS) {
556 switch (fc_frame_payload_op(fp)) {
557 case ELS_LOGO:
558 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
559 /* drop non-FIP LOGO */
560 put_cpu();
561 kfree_skb(skb);
562 return;
563 }
564 break;
565 }
566 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700567
568 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
569 /* Drop incoming ABTS */
570 put_cpu();
571 kfree_skb(skb);
572 return;
573 }
574
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800575 if (le32_to_cpu(fr_crc(fp)) !=
576 ~crc32(~0, skb->data, fr_len)) {
577 if (stats->InvalidCRCCount < 5)
578 printk(KERN_WARNING PFX "dropping frame with "
579 "CRC error\n");
580 stats->InvalidCRCCount++;
581 put_cpu();
582 kfree_skb(skb);
583 return;
584 }
585 put_cpu();
586 fc_exch_recv(lport, fp);
587}
588
589/**
590 * bnx2fc_percpu_io_thread - thread per cpu for ios
591 *
592 * @arg: ptr to bnx2fc_percpu_info structure
593 */
594int bnx2fc_percpu_io_thread(void *arg)
595{
596 struct bnx2fc_percpu_s *p = arg;
597 struct bnx2fc_work *work, *tmp;
598 LIST_HEAD(work_list);
599
600 set_user_nice(current, -20);
601 set_current_state(TASK_INTERRUPTIBLE);
602 while (!kthread_should_stop()) {
603 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800604 spin_lock_bh(&p->fp_work_lock);
605 while (!list_empty(&p->work_list)) {
606 list_splice_init(&p->work_list, &work_list);
607 spin_unlock_bh(&p->fp_work_lock);
608
609 list_for_each_entry_safe(work, tmp, &work_list, list) {
610 list_del_init(&work->list);
611 bnx2fc_process_cq_compl(work->tgt, work->wqe);
612 kfree(work);
613 }
614
615 spin_lock_bh(&p->fp_work_lock);
616 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700617 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800618 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800619 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700620 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800621
622 return 0;
623}
624
625static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
626{
627 struct fc_host_statistics *bnx2fc_stats;
628 struct fc_lport *lport = shost_priv(shost);
629 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700630 struct bnx2fc_interface *interface = port->priv;
631 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800632 struct fcoe_statistics_params *fw_stats;
633 int rc = 0;
634
635 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
636 if (!fw_stats)
637 return NULL;
638
639 bnx2fc_stats = fc_get_host_stats(shost);
640
641 init_completion(&hba->stat_req_done);
642 if (bnx2fc_send_stat_req(hba))
643 return bnx2fc_stats;
644 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
645 if (!rc) {
646 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
647 return bnx2fc_stats;
648 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300649 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800650 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
651 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
652 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
653 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
654
655 bnx2fc_stats->dumped_frames = 0;
656 bnx2fc_stats->lip_count = 0;
657 bnx2fc_stats->nos_count = 0;
658 bnx2fc_stats->loss_of_sync_count = 0;
659 bnx2fc_stats->loss_of_signal_count = 0;
660 bnx2fc_stats->prim_seq_protocol_err_count = 0;
661
662 return bnx2fc_stats;
663}
664
665static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
666{
667 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700668 struct bnx2fc_interface *interface = port->priv;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800669 struct Scsi_Host *shost = lport->host;
670 int rc = 0;
671
672 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
673 shost->max_lun = BNX2FC_MAX_LUN;
674 shost->max_id = BNX2FC_MAX_FCP_TGT;
675 shost->max_channel = 0;
676 if (lport->vport)
677 shost->transportt = bnx2fc_vport_xport_template;
678 else
679 shost->transportt = bnx2fc_transport_template;
680
681 /* Add the new host to SCSI-ml */
682 rc = scsi_add_host(lport->host, dev);
683 if (rc) {
684 printk(KERN_ERR PFX "Error on scsi_add_host\n");
685 return rc;
686 }
687 if (!lport->vport)
688 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
689 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
690 BNX2FC_NAME, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700691 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800692
693 return 0;
694}
695
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800696static void bnx2fc_link_speed_update(struct fc_lport *lport)
697{
698 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700699 struct bnx2fc_interface *interface = port->priv;
700 struct net_device *netdev = interface->netdev;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000701 struct ethtool_cmd ecmd;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800702
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000703 if (!__ethtool_get_settings(netdev, &ecmd)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800704 lport->link_supported_speeds &=
705 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
706 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
707 SUPPORTED_1000baseT_Full))
708 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
709 if (ecmd.supported & SUPPORTED_10000baseT_Full)
710 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
711
David Decotigny8ae6dac2011-04-27 18:32:38 +0000712 switch (ethtool_cmd_speed(&ecmd)) {
713 case SPEED_1000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800714 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000715 break;
Bhanu Prakash Gollapudia4dc08c2011-05-27 11:47:25 -0700716 case SPEED_2500:
717 lport->link_speed = FC_PORTSPEED_2GBIT;
718 break;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000719 case SPEED_10000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800720 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000721 break;
722 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800723 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800724}
725static int bnx2fc_link_ok(struct fc_lport *lport)
726{
727 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700728 struct bnx2fc_interface *interface = port->priv;
729 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800730 struct net_device *dev = hba->phys_dev;
731 int rc = 0;
732
733 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
734 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
735 else {
736 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
737 rc = -1;
738 }
739 return rc;
740}
741
742/**
743 * bnx2fc_get_link_state - get network link state
744 *
745 * @hba: adapter instance pointer
746 *
747 * updates adapter structure flag based on netdev state
748 */
749void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
750{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700751 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800752 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
753 else
754 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
755}
756
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700757static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800758{
759 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700760 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800761 struct fcoe_port *port;
762 u64 wwnn, wwpn;
763
764 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700765 interface = port->priv;
766 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800767
768 /* require support for get_pauseparam ethtool op. */
769 if (!hba->phys_dev->ethtool_ops ||
770 !hba->phys_dev->ethtool_ops->get_pauseparam)
771 return -EOPNOTSUPP;
772
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700773 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800774 return -EINVAL;
775
776 skb_queue_head_init(&port->fcoe_pending_queue);
777 port->fcoe_pending_queue_active = 0;
778 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
779
780 bnx2fc_link_speed_update(lport);
781
782 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700783 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
784 wwnn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
785 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800786 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
787 fc_set_wwnn(lport, wwnn);
788
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700789 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
790 wwpn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
791 2, 0);
792
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800793 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
794 fc_set_wwpn(lport, wwpn);
795 }
796
797 return 0;
798}
799
800static void bnx2fc_destroy_timer(unsigned long data)
801{
802 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
803
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700804 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
805 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700806 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800807 wake_up_interruptible(&hba->destroy_wait);
808}
809
810/**
811 * bnx2fc_indicate_netevent - Generic netdev event handler
812 *
813 * @context: adapter structure pointer
814 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000815 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800816 *
817 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700818 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800819 */
Michael Chan415199f2011-07-20 14:55:24 +0000820static void bnx2fc_indicate_netevent(void *context, unsigned long event,
821 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800822{
823 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700824 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800825 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700826 struct bnx2fc_interface *interface, *tmp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700827 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800828 u32 link_possible = 1;
829
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700830 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000831 return;
832
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800833 switch (event) {
834 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800835 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
836 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700837 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800838 break;
839
840 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800841 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
842 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
843 link_possible = 0;
844 break;
845
846 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800847 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
848 link_possible = 0;
849 break;
850
851 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800852 break;
853
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700854 case NETDEV_UNREGISTER:
855 if (!vlan_id)
856 return;
857 mutex_lock(&bnx2fc_dev_lock);
858 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700859 if (interface->hba == hba &&
860 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
861 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700862 }
863 mutex_unlock(&bnx2fc_dev_lock);
864 return;
865
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800866 default:
867 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
868 return;
869 }
870
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700871 mutex_lock(&bnx2fc_dev_lock);
872 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800873
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700874 if (interface->hba != hba)
875 continue;
876
877 lport = interface->ctlr.lp;
878 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
879 interface->netdev->name, event);
880
881 bnx2fc_link_speed_update(lport);
882
883 if (link_possible && !bnx2fc_link_ok(lport)) {
Bhanu Prakash Gollapudic7806732011-08-30 15:54:47 -0700884 /* Reset max recv frame size to default */
885 fc_set_mfs(lport, BNX2FC_MFS);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -0700886 /*
887 * ctlr link up will only be handled during
888 * enable to avoid sending discovery solicitation
889 * on a stale vlan
890 */
Bhanu Prakash Gollapudifd8fa902011-10-03 16:45:00 -0700891 if (interface->enabled)
892 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700893 } else if (fcoe_ctlr_link_down(&interface->ctlr)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800894 mutex_lock(&lport->lp_mutex);
895 list_for_each_entry(vport, &lport->vports, list)
896 fc_host_port_type(vport->host) =
897 FC_PORTTYPE_UNKNOWN;
898 mutex_unlock(&lport->lp_mutex);
899 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
900 per_cpu_ptr(lport->dev_stats,
901 get_cpu())->LinkFailureCount++;
902 put_cpu();
903 fcoe_clean_pending_queue(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700904 wait_for_upload = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800905 }
906 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700907 mutex_unlock(&bnx2fc_dev_lock);
908
909 if (wait_for_upload) {
910 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
911 init_waitqueue_head(&hba->shutdown_wait);
912 BNX2FC_MISC_DBG("indicate_netevent "
913 "num_ofld_sess = %d\n",
914 hba->num_ofld_sess);
915 hba->wait_for_link_down = 1;
916 wait_event_interruptible(hba->shutdown_wait,
917 (hba->num_ofld_sess == 0));
918 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
919 hba->num_ofld_sess);
920 hba->wait_for_link_down = 0;
921
922 if (signal_pending(current))
923 flush_signals(current);
924 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800925}
926
927static int bnx2fc_libfc_config(struct fc_lport *lport)
928{
929
930 /* Set the function pointers set by bnx2fc driver */
931 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
932 sizeof(struct libfc_function_template));
933 fc_elsct_init(lport);
934 fc_exch_init(lport);
935 fc_rport_init(lport);
936 fc_disc_init(lport);
937 return 0;
938}
939
940static int bnx2fc_em_config(struct fc_lport *lport)
941{
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800942 int max_xid;
943
944 if (nr_cpu_ids <= 2)
945 max_xid = FCOE_XIDS_PER_CPU;
946 else
947 max_xid = FCOE_MAX_XID;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800948 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -0800949 max_xid, NULL)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800950 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
951 return -ENOMEM;
952 }
953
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800954 return 0;
955}
956
957static int bnx2fc_lport_config(struct fc_lport *lport)
958{
959 lport->link_up = 0;
960 lport->qfull = 0;
Bhanu Prakash Gollapudi44c570b2012-01-23 18:00:47 -0800961 lport->max_retry_count = BNX2FC_MAX_RETRY_CNT;
962 lport->max_rport_retry_count = BNX2FC_MAX_RPORT_RETRY_CNT;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800963 lport->e_d_tov = 2 * 1000;
964 lport->r_a_tov = 10 * 1000;
965
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800966 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
967 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800968 lport->does_npiv = 1;
969
970 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
971 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
972
973 /* alloc stats structure */
974 if (fc_lport_init_stats(lport))
975 return -ENOMEM;
976
977 /* Finish fc_lport configuration */
978 fc_lport_config(lport);
979
980 return 0;
981}
982
983/**
984 * bnx2fc_fip_recv - handle a received FIP frame.
985 *
986 * @skb: the received skb
987 * @dev: associated &net_device
988 * @ptype: the &packet_type structure which was used to register this handler.
989 * @orig_dev: original receive &net_device, in case @ dev is a bond.
990 *
991 * Returns: 0 for success
992 */
993static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
994 struct packet_type *ptype,
995 struct net_device *orig_dev)
996{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700997 struct bnx2fc_interface *interface;
998 interface = container_of(ptype, struct bnx2fc_interface,
999 fip_packet_type);
1000 fcoe_ctlr_recv(&interface->ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001001 return 0;
1002}
1003
1004/**
1005 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1006 *
1007 * @fip: FCoE controller.
1008 * @old: Unicast MAC address to delete if the MAC is non-zero.
1009 * @new: Unicast MAC address to add.
1010 *
1011 * Remove any previously-set unicast MAC filter.
1012 * Add secondary FCoE MAC address filter for our OUI.
1013 */
1014static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1015{
1016 struct fcoe_port *port = lport_priv(lport);
1017
1018 memcpy(port->data_src_addr, addr, ETH_ALEN);
1019}
1020
1021/**
1022 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1023 *
1024 * @lport: libfc port
1025 */
1026static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1027{
1028 struct fcoe_port *port;
1029
1030 port = (struct fcoe_port *)lport_priv(lport);
1031 return port->data_src_addr;
1032}
1033
1034/**
1035 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1036 *
1037 * @fip: FCoE controller.
1038 * @skb: FIP Packet.
1039 */
1040static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1041{
1042 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1043 dev_queue_xmit(skb);
1044}
1045
1046static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1047{
1048 struct Scsi_Host *shost = vport_to_shost(vport);
1049 struct fc_lport *n_port = shost_priv(shost);
1050 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001051 struct bnx2fc_interface *interface = port->priv;
1052 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001053 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001054 int rc;
1055 char buf[32];
1056
1057 rc = fcoe_validate_vport_create(vport);
1058 if (rc) {
1059 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1060 printk(KERN_ERR PFX "Failed to create vport, "
1061 "WWPN (0x%s) already exists\n",
1062 buf);
1063 return rc;
1064 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001065
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001066 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001067 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001068 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001069 return -EIO;
1070 }
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001071 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001072 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001073 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001074 mutex_unlock(&bnx2fc_dev_lock);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001075 rtnl_unlock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001076
1077 if (IS_ERR(vn_port)) {
1078 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1079 netdev->name);
1080 return -EIO;
1081 }
1082
1083 if (disabled) {
1084 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1085 } else {
1086 vn_port->boot_time = jiffies;
1087 fc_lport_init(vn_port);
1088 fc_fabric_login(vn_port);
1089 fc_vport_setlink(vn_port);
1090 }
1091 return 0;
1092}
1093
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001094static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1095{
1096 struct bnx2fc_lport *blport, *tmp;
1097
1098 spin_lock_bh(&hba->hba_lock);
1099 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1100 if (blport->lport == lport) {
1101 list_del(&blport->list);
1102 kfree(blport);
1103 }
1104 }
1105 spin_unlock_bh(&hba->hba_lock);
1106}
1107
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001108static int bnx2fc_vport_destroy(struct fc_vport *vport)
1109{
1110 struct Scsi_Host *shost = vport_to_shost(vport);
1111 struct fc_lport *n_port = shost_priv(shost);
1112 struct fc_lport *vn_port = vport->dd_data;
1113 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001114 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001115 struct fc_lport *v_port;
1116 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001117
1118 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001119 list_for_each_entry(v_port, &n_port->vports, list)
1120 if (v_port->vport == vport) {
1121 found = true;
1122 break;
1123 }
1124
1125 if (!found) {
1126 mutex_unlock(&n_port->lp_mutex);
1127 return -ENOENT;
1128 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001129 list_del(&vn_port->list);
1130 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001131 bnx2fc_free_vport(interface->hba, port->lport);
1132 bnx2fc_port_shutdown(port->lport);
1133 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001134 queue_work(bnx2fc_wq, &port->destroy_work);
1135 return 0;
1136}
1137
1138static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1139{
1140 struct fc_lport *lport = vport->dd_data;
1141
1142 if (disable) {
1143 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1144 fc_fabric_logoff(lport);
1145 } else {
1146 lport->boot_time = jiffies;
1147 fc_fabric_login(lport);
1148 fc_vport_setlink(lport);
1149 }
1150 return 0;
1151}
1152
1153
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001154static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001155{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001156 struct net_device *netdev = interface->netdev;
1157 struct net_device *physdev = interface->hba->phys_dev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001158 struct netdev_hw_addr *ha;
1159 int sel_san_mac = 0;
1160
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001161 /* setup Source MAC Address */
1162 rcu_read_lock();
1163 for_each_dev_addr(physdev, ha) {
1164 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1165 ha->type);
1166 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1167 ha->addr[1], ha->addr[2], ha->addr[3],
1168 ha->addr[4], ha->addr[5]);
1169
1170 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1171 (is_valid_ether_addr(ha->addr))) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001172 memcpy(interface->ctlr.ctl_src_addr, ha->addr,
1173 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001174 sel_san_mac = 1;
1175 BNX2FC_MISC_DBG("Found SAN MAC\n");
1176 }
1177 }
1178 rcu_read_unlock();
1179
1180 if (!sel_san_mac)
1181 return -ENODEV;
1182
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001183 interface->fip_packet_type.func = bnx2fc_fip_recv;
1184 interface->fip_packet_type.type = htons(ETH_P_FIP);
1185 interface->fip_packet_type.dev = netdev;
1186 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001187
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001188 interface->fcoe_packet_type.func = bnx2fc_rcv;
1189 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1190 interface->fcoe_packet_type.dev = netdev;
1191 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001192
1193 return 0;
1194}
1195
1196static int bnx2fc_attach_transport(void)
1197{
1198 bnx2fc_transport_template =
1199 fc_attach_transport(&bnx2fc_transport_function);
1200
1201 if (bnx2fc_transport_template == NULL) {
1202 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1203 return -ENODEV;
1204 }
1205
1206 bnx2fc_vport_xport_template =
1207 fc_attach_transport(&bnx2fc_vport_xport_function);
1208 if (bnx2fc_vport_xport_template == NULL) {
1209 printk(KERN_ERR PFX
1210 "Failed to attach FC transport for vport\n");
1211 fc_release_transport(bnx2fc_transport_template);
1212 bnx2fc_transport_template = NULL;
1213 return -ENODEV;
1214 }
1215 return 0;
1216}
1217static void bnx2fc_release_transport(void)
1218{
1219 fc_release_transport(bnx2fc_transport_template);
1220 fc_release_transport(bnx2fc_vport_xport_template);
1221 bnx2fc_transport_template = NULL;
1222 bnx2fc_vport_xport_template = NULL;
1223}
1224
1225static void bnx2fc_interface_release(struct kref *kref)
1226{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001227 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001228 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001229
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001230 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001231 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001232
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001233 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001234
1235 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001236 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1237 fcoe_ctlr_destroy(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001238
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001239 kfree(interface);
1240
1241 dev_put(netdev);
1242 module_put(THIS_MODULE);
1243}
1244
1245static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1246{
1247 kref_get(&interface->kref);
1248}
1249
1250static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1251{
1252 kref_put(&interface->kref, bnx2fc_interface_release);
1253}
1254static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1255{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001256 /* Free the command manager */
1257 if (hba->cmd_mgr) {
1258 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1259 hba->cmd_mgr = NULL;
1260 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001261 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001262 bnx2fc_unbind_pcidev(hba);
1263 kfree(hba);
1264}
1265
1266/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001267 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001268 *
1269 * @cnic: pointer to cnic device
1270 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001271 * Creates a new FCoE hba on the given device.
1272 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001273 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001274static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001275{
1276 struct bnx2fc_hba *hba;
1277 int rc;
1278
1279 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1280 if (!hba) {
1281 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1282 return NULL;
1283 }
1284 spin_lock_init(&hba->hba_lock);
1285 mutex_init(&hba->hba_mutex);
1286
1287 hba->cnic = cnic;
1288 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001289 if (rc) {
1290 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001291 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001292 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001293 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001294 hba->next_conn_id = 0;
1295
1296 hba->tgt_ofld_list =
1297 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1298 GFP_KERNEL);
1299 if (!hba->tgt_ofld_list) {
1300 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1301 goto tgtofld_err;
1302 }
1303
1304 hba->num_ofld_sess = 0;
1305
1306 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1307 BNX2FC_MAX_XID);
1308 if (!hba->cmd_mgr) {
1309 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1310 goto cmgr_err;
1311 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001312
1313 init_waitqueue_head(&hba->shutdown_wait);
1314 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001315 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001316
1317 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001318
1319cmgr_err:
1320 kfree(hba->tgt_ofld_list);
1321tgtofld_err:
1322 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001323bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001324 kfree(hba);
1325 return NULL;
1326}
1327
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001328struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1329 struct net_device *netdev,
1330 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001331{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001332 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001333 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001334
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001335 interface = kzalloc(sizeof(*interface), GFP_KERNEL);
1336 if (!interface) {
1337 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1338 return NULL;
1339 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001340 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001341 kref_init(&interface->kref);
1342 interface->hba = hba;
1343 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001344
1345 /* Initialize FIP */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001346 fcoe_ctlr_init(&interface->ctlr, fip_mode);
1347 interface->ctlr.send = bnx2fc_fip_send;
1348 interface->ctlr.update_mac = bnx2fc_update_src_mac;
1349 interface->ctlr.get_src_addr = bnx2fc_get_src_mac;
1350 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001351
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001352 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001353 if (!rc)
1354 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001355
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001356 fcoe_ctlr_destroy(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001357 dev_put(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001358 kfree(interface);
1359 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001360}
1361
1362/**
1363 * bnx2fc_if_create - Create FCoE instance on a given interface
1364 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001365 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001366 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1367 * @npiv: Indicates if the port is vport or not
1368 *
1369 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1370 *
1371 * Returns: Allocated fc_lport or an error pointer
1372 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001373static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001374 struct device *parent, int npiv)
1375{
Vasu Dev134a4e22011-04-28 15:55:44 -07001376 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001377 struct fcoe_port *port;
1378 struct Scsi_Host *shost;
1379 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001380 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001381 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001382 int rc = 0;
1383
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001384 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1385 if (!blport) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001386 BNX2FC_HBA_DBG(interface->ctlr.lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001387 return NULL;
1388 }
1389
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001390 /* Allocate Scsi_Host structure */
Vasu Dev134a4e22011-04-28 15:55:44 -07001391 if (!npiv)
1392 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1393 else
1394 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001395
1396 if (!lport) {
1397 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001398 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001399 }
1400 shost = lport->host;
1401 port = lport_priv(lport);
1402 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001403 port->priv = interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001404 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1405
1406 /* Configure fcoe_port */
1407 rc = bnx2fc_lport_config(lport);
1408 if (rc)
1409 goto lp_config_err;
1410
1411 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001412 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1413 vport->node_name, vport->port_name);
1414 fc_set_wwnn(lport, vport->node_name);
1415 fc_set_wwpn(lport, vport->port_name);
1416 }
1417 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001418 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001419 if (rc) {
1420 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1421 goto lp_config_err;
1422 }
1423
1424 rc = bnx2fc_shost_config(lport, parent);
1425 if (rc) {
1426 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001427 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001428 goto lp_config_err;
1429 }
1430
1431 /* Initialize the libfc library */
1432 rc = bnx2fc_libfc_config(lport);
1433 if (rc) {
1434 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1435 goto shost_err;
1436 }
1437 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1438
1439 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001440 if (!npiv)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001441 rc = bnx2fc_em_config(lport);
Vasu Dev134a4e22011-04-28 15:55:44 -07001442 else {
1443 shost = vport_to_shost(vport);
1444 n_port = shost_priv(shost);
1445 rc = fc_exch_mgr_list_clone(n_port, lport);
1446 }
1447
1448 if (rc) {
1449 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1450 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001451 }
1452
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001453 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001454
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001455 hba = interface->hba;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001456 spin_lock_bh(&hba->hba_lock);
1457 blport->lport = lport;
1458 list_add_tail(&blport->list, &hba->vports);
1459 spin_unlock_bh(&hba->hba_lock);
1460
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001461 return lport;
1462
1463shost_err:
1464 scsi_remove_host(shost);
1465lp_config_err:
1466 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001467free_blport:
1468 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001469 return NULL;
1470}
1471
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001472static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001473{
1474 /* Dont listen for Ethernet packets anymore */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001475 __dev_remove_pack(&interface->fcoe_packet_type);
1476 __dev_remove_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001477 synchronize_net();
1478}
1479
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001480static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001481{
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001482 struct fc_lport *lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001483 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001484 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001485
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001486 /* Stop the transmit retry timer */
1487 del_timer_sync(&port->timer);
1488
1489 /* Free existing transmit skbs */
1490 fcoe_clean_pending_queue(lport);
1491
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001492 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001493
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001494 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001495}
1496
1497static void bnx2fc_if_destroy(struct fc_lport *lport)
1498{
1499
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001500 /* Free queued packets for the receive thread */
1501 bnx2fc_clean_rx_queue(lport);
1502
1503 /* Detach from scsi-ml */
1504 fc_remove_host(lport->host);
1505 scsi_remove_host(lport->host);
1506
1507 /*
1508 * Note that only the physical lport will have the exchange manager.
1509 * for vports, this function is NOP
1510 */
1511 fc_exch_mgr_free(lport);
1512
1513 /* Free memory used by statistical counters */
1514 fc_lport_free_stats(lport);
1515
1516 /* Release Scsi_Host */
1517 scsi_host_put(lport->host);
1518}
1519
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001520static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001521{
1522 struct fc_lport *lport = interface->ctlr.lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001523 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001524
1525 bnx2fc_interface_cleanup(interface);
1526 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001527 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001528 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001529 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001530}
1531
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001532/**
1533 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1534 *
1535 * @buffer: The name of the Ethernet interface to be destroyed
1536 * @kp: The associated kernel parameter
1537 *
1538 * Called from sysfs.
1539 *
1540 * Returns: 0 for success
1541 */
1542static int bnx2fc_destroy(struct net_device *netdev)
1543{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001544 struct bnx2fc_interface *interface = NULL;
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001545 struct workqueue_struct *timer_work_queue;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001546 int rc = 0;
1547
Nithin Sujir6702ca12011-03-17 17:13:27 -07001548 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001549 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001550
1551 interface = bnx2fc_interface_lookup(netdev);
1552 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001553 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001554 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001555 goto netdev_err;
1556 }
1557
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001558 timer_work_queue = interface->timer_work_queue;
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001559 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudi2a7b29c2012-01-23 18:00:46 -08001560 destroy_workqueue(timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001561
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001562netdev_err:
1563 mutex_unlock(&bnx2fc_dev_lock);
1564 rtnl_unlock();
1565 return rc;
1566}
1567
1568static void bnx2fc_destroy_work(struct work_struct *work)
1569{
1570 struct fcoe_port *port;
1571 struct fc_lport *lport;
1572
1573 port = container_of(work, struct fcoe_port, destroy_work);
1574 lport = port->lport;
1575
1576 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1577
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001578 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001579}
1580
1581static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1582{
1583 bnx2fc_free_fw_resc(hba);
1584 bnx2fc_free_task_ctx(hba);
1585}
1586
1587/**
1588 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1589 * pci structure
1590 *
1591 * @hba: Adapter instance
1592 */
1593static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1594{
1595 if (bnx2fc_setup_task_ctx(hba))
1596 goto mem_err;
1597
1598 if (bnx2fc_setup_fw_resc(hba))
1599 goto mem_err;
1600
1601 return 0;
1602mem_err:
1603 bnx2fc_unbind_adapter_devices(hba);
1604 return -ENOMEM;
1605}
1606
1607static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1608{
1609 struct cnic_dev *cnic;
1610
1611 if (!hba->cnic) {
1612 printk(KERN_ERR PFX "cnic is NULL\n");
1613 return -ENODEV;
1614 }
1615 cnic = hba->cnic;
1616 hba->pcidev = cnic->pcidev;
1617 if (hba->pcidev)
1618 pci_dev_get(hba->pcidev);
1619
1620 return 0;
1621}
1622
1623static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1624{
1625 if (hba->pcidev)
1626 pci_dev_put(hba->pcidev);
1627 hba->pcidev = NULL;
1628}
1629
1630
1631
1632/**
1633 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1634 *
1635 * @handle: transport handle pointing to adapter struture
1636 *
1637 * This function maps adapter structure to pcidev structure and initiates
1638 * firmware handshake to enable/initialize on-chip FCoE components.
1639 * This bnx2fc - cnic interface api callback is used after following
1640 * conditions are met -
1641 * a) underlying network interface is up (marked by event NETDEV_UP
1642 * from netdev
1643 * b) bnx2fc adatper structure is registered.
1644 */
1645static void bnx2fc_ulp_start(void *handle)
1646{
1647 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001648 struct bnx2fc_interface *interface;
1649 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001650
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001651 mutex_lock(&bnx2fc_dev_lock);
1652
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001653 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001654 bnx2fc_fw_init(hba);
1655
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001656 BNX2FC_MISC_DBG("bnx2fc started.\n");
1657
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001658 list_for_each_entry(interface, &if_list, list) {
1659 if (interface->hba == hba) {
1660 lport = interface->ctlr.lp;
1661 /* Kick off Fabric discovery*/
1662 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1663 lport->tt.frame_send = bnx2fc_xmit;
1664 bnx2fc_start_disc(interface);
1665 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001666 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001667
1668 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001669}
1670
1671static void bnx2fc_port_shutdown(struct fc_lport *lport)
1672{
1673 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1674 fc_fabric_logoff(lport);
1675 fc_lport_destroy(lport);
1676}
1677
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001678static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001679{
1680 struct fc_lport *lport;
1681 struct fc_lport *vport;
1682
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001683 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1684 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001685
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001686 lport = interface->ctlr.lp;
1687 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001688
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001689 mutex_lock(&lport->lp_mutex);
1690 list_for_each_entry(vport, &lport->vports, list)
1691 fc_host_port_type(vport->host) =
1692 FC_PORTTYPE_UNKNOWN;
1693 mutex_unlock(&lport->lp_mutex);
1694 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1695 fcoe_ctlr_link_down(&interface->ctlr);
1696 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001697}
1698
1699static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1700{
1701#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1702 int rc = -1;
1703 int i = HZ;
1704
1705 rc = bnx2fc_bind_adapter_devices(hba);
1706 if (rc) {
1707 printk(KERN_ALERT PFX
1708 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1709 goto err_out;
1710 }
1711
1712 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1713 if (rc) {
1714 printk(KERN_ALERT PFX
1715 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1716 goto err_unbind;
1717 }
1718
1719 /*
1720 * Wait until the adapter init message is complete, and adapter
1721 * state is UP.
1722 */
1723 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1724 msleep(BNX2FC_INIT_POLL_TIME);
1725
1726 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1727 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1728 "Ignoring...\n",
1729 hba->cnic->netdev->name);
1730 rc = -1;
1731 goto err_unbind;
1732 }
1733
1734
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001735 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001736 return 0;
1737
1738err_unbind:
1739 bnx2fc_unbind_adapter_devices(hba);
1740err_out:
1741 return rc;
1742}
1743
1744static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1745{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001746 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001747 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1748 init_timer(&hba->destroy_timer);
1749 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1750 jiffies;
1751 hba->destroy_timer.function = bnx2fc_destroy_timer;
1752 hba->destroy_timer.data = (unsigned long)hba;
1753 add_timer(&hba->destroy_timer);
1754 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001755 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1756 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001757 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001758 /* This should never happen */
1759 if (signal_pending(current))
1760 flush_signals(current);
1761
1762 del_timer_sync(&hba->destroy_timer);
1763 }
1764 bnx2fc_unbind_adapter_devices(hba);
1765 }
1766}
1767
1768/**
1769 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1770 *
1771 * @handle: transport handle pointing to adapter structure
1772 *
1773 * Driver checks if adapter is already in shutdown mode, if not start
1774 * the shutdown process.
1775 */
1776static void bnx2fc_ulp_stop(void *handle)
1777{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001778 struct bnx2fc_hba *hba = handle;
1779 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001780
1781 printk(KERN_ERR "ULP_STOP\n");
1782
1783 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001784 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1785 goto exit;
1786 list_for_each_entry(interface, &if_list, list) {
1787 if (interface->hba == hba)
1788 bnx2fc_stop(interface);
1789 }
1790 BUG_ON(hba->num_ofld_sess != 0);
1791
1792 mutex_lock(&hba->hba_mutex);
1793 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1794 clear_bit(ADAPTER_STATE_GOING_DOWN,
1795 &hba->adapter_state);
1796
1797 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1798 mutex_unlock(&hba->hba_mutex);
1799
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001800 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001801exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001802 mutex_unlock(&bnx2fc_dev_lock);
1803}
1804
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001805static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001806{
1807 struct fc_lport *lport;
1808 int wait_cnt = 0;
1809
1810 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1811 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001812 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001813 printk(KERN_ERR PFX "Init not done yet\n");
1814 return;
1815 }
1816
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001817 lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001818 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1819
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001820 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001821 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001822 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001823 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001824 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001825 }
1826
1827 /* wait for the FCF to be selected before issuing FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001828 while (!interface->ctlr.sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001829 msleep(250);
1830 /* give up after 3 secs */
1831 if (++wait_cnt > 12)
1832 break;
1833 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001834
1835 /* Reset max receive frame size to default */
1836 if (fc_set_mfs(lport, BNX2FC_MFS))
1837 return;
1838
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001839 fc_lport_init(lport);
1840 fc_fabric_login(lport);
1841}
1842
1843
1844/**
1845 * bnx2fc_ulp_init - Initialize an adapter instance
1846 *
1847 * @dev : cnic device handle
1848 * Called from cnic_register_driver() context to initialize all
1849 * enumerated cnic devices. This routine allocates adapter structure
1850 * and other device specific resources.
1851 */
1852static void bnx2fc_ulp_init(struct cnic_dev *dev)
1853{
1854 struct bnx2fc_hba *hba;
1855 int rc = 0;
1856
1857 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1858 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001859 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1860 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001861 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001862 " flags: %lx fcoe_conn: %d\n",
1863 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001864 return;
1865 }
1866
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001867 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001868 if (!hba) {
1869 printk(KERN_ERR PFX "hba initialization failed\n");
1870 return;
1871 }
1872
1873 /* Add HBA to the adapter list */
1874 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001875 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001876 adapter_count++;
1877 mutex_unlock(&bnx2fc_dev_lock);
1878
1879 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1880 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1881 (void *) hba);
1882 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001883 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001884 else
1885 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1886}
1887
1888
1889static int bnx2fc_disable(struct net_device *netdev)
1890{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001891 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001892 int rc = 0;
1893
Nithin Sujir6702ca12011-03-17 17:13:27 -07001894 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001895 mutex_lock(&bnx2fc_dev_lock);
1896
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001897 interface = bnx2fc_interface_lookup(netdev);
1898 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001899 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001900 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001901 } else {
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001902 interface->enabled = false;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001903 fcoe_ctlr_link_down(&interface->ctlr);
1904 fcoe_clean_pending_queue(interface->ctlr.lp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001905 }
1906
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001907 mutex_unlock(&bnx2fc_dev_lock);
1908 rtnl_unlock();
1909 return rc;
1910}
1911
1912
1913static int bnx2fc_enable(struct net_device *netdev)
1914{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001915 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001916 int rc = 0;
1917
Nithin Sujir6702ca12011-03-17 17:13:27 -07001918 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001919 mutex_lock(&bnx2fc_dev_lock);
1920
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001921 interface = bnx2fc_interface_lookup(netdev);
1922 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001923 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001924 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001925 } else if (!bnx2fc_link_ok(interface->ctlr.lp)) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001926 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001927 interface->enabled = true;
1928 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001929
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001930 mutex_unlock(&bnx2fc_dev_lock);
1931 rtnl_unlock();
1932 return rc;
1933}
1934
1935/**
1936 * bnx2fc_create - Create bnx2fc FCoE interface
1937 *
1938 * @buffer: The name of Ethernet interface to create on
1939 * @kp: The associated kernel param
1940 *
1941 * Called from sysfs.
1942 *
1943 * Returns: 0 for success
1944 */
1945static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1946{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001947 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001948 struct bnx2fc_hba *hba;
1949 struct net_device *phys_dev;
1950 struct fc_lport *lport;
1951 struct ethtool_drvinfo drvinfo;
1952 int rc = 0;
1953 int vlan_id;
1954
1955 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1956 if (fip_mode != FIP_MODE_FABRIC) {
1957 printk(KERN_ERR "fip mode not FABRIC\n");
1958 return -EIO;
1959 }
1960
Nithin Sujir6702ca12011-03-17 17:13:27 -07001961 rtnl_lock();
1962
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001963 mutex_lock(&bnx2fc_dev_lock);
1964
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001965 if (!try_module_get(THIS_MODULE)) {
1966 rc = -EINVAL;
1967 goto mod_err;
1968 }
1969
1970 /* obtain physical netdev */
1971 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1972 phys_dev = vlan_dev_real_dev(netdev);
1973 vlan_id = vlan_dev_vlan_id(netdev);
1974 } else {
1975 printk(KERN_ERR PFX "Not a vlan device\n");
1976 rc = -EINVAL;
1977 goto netdev_err;
1978 }
1979 /* verify if the physical device is a netxtreme2 device */
1980 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1981 memset(&drvinfo, 0, sizeof(drvinfo));
1982 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001983 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001984 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1985 rc = -EINVAL;
1986 goto netdev_err;
1987 }
1988 } else {
1989 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1990 rc = -EINVAL;
1991 goto netdev_err;
1992 }
1993
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001994 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001995 hba = bnx2fc_hba_lookup(phys_dev);
1996 if (!hba) {
1997 rc = -ENODEV;
1998 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1999 goto netdev_err;
2000 }
2001
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002002 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002003 rc = -EEXIST;
2004 goto netdev_err;
2005 }
2006
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002007 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2008 if (!interface) {
2009 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002010 goto ifput_err;
2011 }
2012
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002013 interface->vlan_id = vlan_id;
2014 interface->vlan_enabled = 1;
2015
2016 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002017 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002018 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002019 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2020 rc = -EINVAL;
2021 goto ifput_err;
2022 }
2023
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002024 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002025 if (!lport) {
2026 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2027 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002028 rc = -EINVAL;
2029 goto if_create_err;
2030 }
2031
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002032 /* Add interface to if_list */
2033 list_add_tail(&interface->list, &if_list);
2034
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002035 lport->boot_time = jiffies;
2036
2037 /* Make this master N_port */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002038 interface->ctlr.lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002039
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002040 if (!bnx2fc_link_ok(lport)) {
2041 fcoe_ctlr_link_up(&interface->ctlr);
2042 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2043 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2044 }
2045
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002046 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2047 bnx2fc_start_disc(interface);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002048 interface->enabled = true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002049 /*
2050 * Release from kref_init in bnx2fc_interface_setup, on success
2051 * lport should be holding a reference taken in bnx2fc_if_create
2052 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002053 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002054 /* put netdev that was held while calling dev_get_by_name */
2055 mutex_unlock(&bnx2fc_dev_lock);
2056 rtnl_unlock();
2057 return 0;
2058
2059if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002060 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002061ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002062 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002063 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi7d742f62012-01-23 18:00:48 -08002064 goto mod_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002065netdev_err:
2066 module_put(THIS_MODULE);
2067mod_err:
2068 mutex_unlock(&bnx2fc_dev_lock);
2069 rtnl_unlock();
2070 return rc;
2071}
2072
2073/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002074 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002075 *
2076 * @cnic: Pointer to cnic device instance
2077 *
2078 **/
2079static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2080{
2081 struct list_head *list;
2082 struct list_head *temp;
2083 struct bnx2fc_hba *hba;
2084
2085 /* Called with bnx2fc_dev_lock held */
2086 list_for_each_safe(list, temp, &adapter_list) {
2087 hba = (struct bnx2fc_hba *)list;
2088 if (hba->cnic == cnic)
2089 return hba;
2090 }
2091 return NULL;
2092}
2093
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002094static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2095 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002096{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002097 struct bnx2fc_interface *interface;
2098
2099 /* Called with bnx2fc_dev_lock held */
2100 list_for_each_entry(interface, &if_list, list) {
2101 if (interface->netdev == netdev)
2102 return interface;
2103 }
2104 return NULL;
2105}
2106
2107static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2108 *phys_dev)
2109{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002110 struct bnx2fc_hba *hba;
2111
2112 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002113 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002114 if (hba->phys_dev == phys_dev)
2115 return hba;
2116 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002117 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002118 return NULL;
2119}
2120
2121/**
2122 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2123 *
2124 * @dev cnic device handle
2125 */
2126static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2127{
2128 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002129 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002130
2131 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2132
2133 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2134 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2135 dev->netdev->name, dev->flags);
2136 return;
2137 }
2138
2139 mutex_lock(&bnx2fc_dev_lock);
2140 hba = bnx2fc_find_hba_for_cnic(dev);
2141 if (!hba) {
2142 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2143 dev);
2144 mutex_unlock(&bnx2fc_dev_lock);
2145 return;
2146 }
2147
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002148 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002149 adapter_count--;
2150
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002151 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002152 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002153 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002154 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002155 mutex_unlock(&bnx2fc_dev_lock);
2156
2157 bnx2fc_ulp_stop(hba);
2158 /* unregister cnic device */
2159 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2160 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002161 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002162}
2163
2164/**
2165 * bnx2fc_fcoe_reset - Resets the fcoe
2166 *
2167 * @shost: shost the reset is from
2168 *
2169 * Returns: always 0
2170 */
2171static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2172{
2173 struct fc_lport *lport = shost_priv(shost);
2174 fc_lport_reset(lport);
2175 return 0;
2176}
2177
2178
2179static bool bnx2fc_match(struct net_device *netdev)
2180{
2181 mutex_lock(&bnx2fc_dev_lock);
2182 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2183 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2184
2185 if (bnx2fc_hba_lookup(phys_dev)) {
2186 mutex_unlock(&bnx2fc_dev_lock);
2187 return true;
2188 }
2189 }
2190 mutex_unlock(&bnx2fc_dev_lock);
2191 return false;
2192}
2193
2194
2195static struct fcoe_transport bnx2fc_transport = {
2196 .name = {"bnx2fc"},
2197 .attached = false,
2198 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2199 .match = bnx2fc_match,
2200 .create = bnx2fc_create,
2201 .destroy = bnx2fc_destroy,
2202 .enable = bnx2fc_enable,
2203 .disable = bnx2fc_disable,
2204};
2205
2206/**
2207 * bnx2fc_percpu_thread_create - Create a receive thread for an
2208 * online CPU
2209 *
2210 * @cpu: cpu index for the online cpu
2211 */
2212static void bnx2fc_percpu_thread_create(unsigned int cpu)
2213{
2214 struct bnx2fc_percpu_s *p;
2215 struct task_struct *thread;
2216
2217 p = &per_cpu(bnx2fc_percpu, cpu);
2218
2219 thread = kthread_create(bnx2fc_percpu_io_thread,
2220 (void *)p,
2221 "bnx2fc_thread/%d", cpu);
2222 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002223 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002224 kthread_bind(thread, cpu);
2225 p->iothread = thread;
2226 wake_up_process(thread);
2227 }
2228}
2229
2230static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2231{
2232 struct bnx2fc_percpu_s *p;
2233 struct task_struct *thread;
2234 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002235
2236 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2237
2238 /* Prevent any new work from being queued for this CPU */
2239 p = &per_cpu(bnx2fc_percpu, cpu);
2240 spin_lock_bh(&p->fp_work_lock);
2241 thread = p->iothread;
2242 p->iothread = NULL;
2243
2244
2245 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002246 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002247 list_del_init(&work->list);
2248 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2249 kfree(work);
2250 }
2251
2252 spin_unlock_bh(&p->fp_work_lock);
2253
2254 if (thread)
2255 kthread_stop(thread);
2256}
2257
2258/**
2259 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2260 *
2261 * @nfb: The callback data block
2262 * @action: The event triggering the callback
2263 * @hcpu: The index of the CPU that the event is for
2264 *
2265 * This creates or destroys per-CPU data for fcoe
2266 *
2267 * Returns NOTIFY_OK always.
2268 */
2269static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2270 unsigned long action, void *hcpu)
2271{
2272 unsigned cpu = (unsigned long)hcpu;
2273
2274 switch (action) {
2275 case CPU_ONLINE:
2276 case CPU_ONLINE_FROZEN:
2277 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2278 bnx2fc_percpu_thread_create(cpu);
2279 break;
2280 case CPU_DEAD:
2281 case CPU_DEAD_FROZEN:
2282 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2283 bnx2fc_percpu_thread_destroy(cpu);
2284 break;
2285 default:
2286 break;
2287 }
2288 return NOTIFY_OK;
2289}
2290
2291/**
2292 * bnx2fc_mod_init - module init entry point
2293 *
2294 * Initialize driver wide global data structures, and register
2295 * with cnic module
2296 **/
2297static int __init bnx2fc_mod_init(void)
2298{
2299 struct fcoe_percpu_s *bg;
2300 struct task_struct *l2_thread;
2301 int rc = 0;
2302 unsigned int cpu = 0;
2303 struct bnx2fc_percpu_s *p;
2304
2305 printk(KERN_INFO PFX "%s", version);
2306
2307 /* register as a fcoe transport */
2308 rc = fcoe_transport_attach(&bnx2fc_transport);
2309 if (rc) {
2310 printk(KERN_ERR "failed to register an fcoe transport, check "
2311 "if libfcoe is loaded\n");
2312 goto out;
2313 }
2314
2315 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002316 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002317 mutex_init(&bnx2fc_dev_lock);
2318 adapter_count = 0;
2319
2320 /* Attach FC transport template */
2321 rc = bnx2fc_attach_transport();
2322 if (rc)
2323 goto detach_ft;
2324
2325 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2326 if (!bnx2fc_wq) {
2327 rc = -ENOMEM;
2328 goto release_bt;
2329 }
2330
2331 bg = &bnx2fc_global;
2332 skb_queue_head_init(&bg->fcoe_rx_list);
2333 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2334 (void *)bg,
2335 "bnx2fc_l2_thread");
2336 if (IS_ERR(l2_thread)) {
2337 rc = PTR_ERR(l2_thread);
2338 goto free_wq;
2339 }
2340 wake_up_process(l2_thread);
2341 spin_lock_bh(&bg->fcoe_rx_list.lock);
2342 bg->thread = l2_thread;
2343 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2344
2345 for_each_possible_cpu(cpu) {
2346 p = &per_cpu(bnx2fc_percpu, cpu);
2347 INIT_LIST_HEAD(&p->work_list);
2348 spin_lock_init(&p->fp_work_lock);
2349 }
2350
2351 for_each_online_cpu(cpu) {
2352 bnx2fc_percpu_thread_create(cpu);
2353 }
2354
2355 /* Initialize per CPU interrupt thread */
2356 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2357
2358 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2359
2360 return 0;
2361
2362free_wq:
2363 destroy_workqueue(bnx2fc_wq);
2364release_bt:
2365 bnx2fc_release_transport();
2366detach_ft:
2367 fcoe_transport_detach(&bnx2fc_transport);
2368out:
2369 return rc;
2370}
2371
2372static void __exit bnx2fc_mod_exit(void)
2373{
2374 LIST_HEAD(to_be_deleted);
2375 struct bnx2fc_hba *hba, *next;
2376 struct fcoe_percpu_s *bg;
2377 struct task_struct *l2_thread;
2378 struct sk_buff *skb;
2379 unsigned int cpu = 0;
2380
2381 /*
2382 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2383 * it will have higher precedence than bnx2fc_dev_lock.
2384 * unregister_device() cannot be called with bnx2fc_dev_lock
2385 * held.
2386 */
2387 mutex_lock(&bnx2fc_dev_lock);
2388 list_splice(&adapter_list, &to_be_deleted);
2389 INIT_LIST_HEAD(&adapter_list);
2390 adapter_count = 0;
2391 mutex_unlock(&bnx2fc_dev_lock);
2392
2393 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002394 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2395 list_del_init(&hba->list);
2396 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2397 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002398 bnx2fc_ulp_stop(hba);
2399 /* unregister cnic device */
2400 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2401 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002402 hba->cnic->unregister_device(hba->cnic,
2403 CNIC_ULP_FCOE);
2404 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002405 }
2406 cnic_unregister_driver(CNIC_ULP_FCOE);
2407
2408 /* Destroy global thread */
2409 bg = &bnx2fc_global;
2410 spin_lock_bh(&bg->fcoe_rx_list.lock);
2411 l2_thread = bg->thread;
2412 bg->thread = NULL;
2413 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2414 kfree_skb(skb);
2415
2416 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2417
2418 if (l2_thread)
2419 kthread_stop(l2_thread);
2420
2421 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2422
2423 /* Destroy per cpu threads */
2424 for_each_online_cpu(cpu) {
2425 bnx2fc_percpu_thread_destroy(cpu);
2426 }
2427
2428 destroy_workqueue(bnx2fc_wq);
2429 /*
2430 * detach from scsi transport
2431 * must happen after all destroys are done
2432 */
2433 bnx2fc_release_transport();
2434
2435 /* detach from fcoe transport */
2436 fcoe_transport_detach(&bnx2fc_transport);
2437}
2438
2439module_init(bnx2fc_mod_init);
2440module_exit(bnx2fc_mod_exit);
2441
2442static struct fc_function_template bnx2fc_transport_function = {
2443 .show_host_node_name = 1,
2444 .show_host_port_name = 1,
2445 .show_host_supported_classes = 1,
2446 .show_host_supported_fc4s = 1,
2447 .show_host_active_fc4s = 1,
2448 .show_host_maxframe_size = 1,
2449
2450 .show_host_port_id = 1,
2451 .show_host_supported_speeds = 1,
2452 .get_host_speed = fc_get_host_speed,
2453 .show_host_speed = 1,
2454 .show_host_port_type = 1,
2455 .get_host_port_state = fc_get_host_port_state,
2456 .show_host_port_state = 1,
2457 .show_host_symbolic_name = 1,
2458
2459 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2460 sizeof(struct bnx2fc_rport)),
2461 .show_rport_maxframe_size = 1,
2462 .show_rport_supported_classes = 1,
2463
2464 .show_host_fabric_name = 1,
2465 .show_starget_node_name = 1,
2466 .show_starget_port_name = 1,
2467 .show_starget_port_id = 1,
2468 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2469 .show_rport_dev_loss_tmo = 1,
2470 .get_fc_host_stats = bnx2fc_get_host_stats,
2471
2472 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2473
2474 .terminate_rport_io = fc_rport_terminate_io,
2475
2476 .vport_create = bnx2fc_vport_create,
2477 .vport_delete = bnx2fc_vport_destroy,
2478 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002479 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002480};
2481
2482static struct fc_function_template bnx2fc_vport_xport_function = {
2483 .show_host_node_name = 1,
2484 .show_host_port_name = 1,
2485 .show_host_supported_classes = 1,
2486 .show_host_supported_fc4s = 1,
2487 .show_host_active_fc4s = 1,
2488 .show_host_maxframe_size = 1,
2489
2490 .show_host_port_id = 1,
2491 .show_host_supported_speeds = 1,
2492 .get_host_speed = fc_get_host_speed,
2493 .show_host_speed = 1,
2494 .show_host_port_type = 1,
2495 .get_host_port_state = fc_get_host_port_state,
2496 .show_host_port_state = 1,
2497 .show_host_symbolic_name = 1,
2498
2499 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2500 sizeof(struct bnx2fc_rport)),
2501 .show_rport_maxframe_size = 1,
2502 .show_rport_supported_classes = 1,
2503
2504 .show_host_fabric_name = 1,
2505 .show_starget_node_name = 1,
2506 .show_starget_port_name = 1,
2507 .show_starget_port_id = 1,
2508 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2509 .show_rport_dev_loss_tmo = 1,
2510 .get_fc_host_stats = fc_get_host_stats,
2511 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2512 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002513 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002514};
2515
2516/**
2517 * scsi_host_template structure used while registering with SCSI-ml
2518 */
2519static struct scsi_host_template bnx2fc_shost_template = {
2520 .module = THIS_MODULE,
2521 .name = "Broadcom Offload FCoE Initiator",
2522 .queuecommand = bnx2fc_queuecommand,
2523 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2524 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2525 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2526 .eh_host_reset_handler = fc_eh_host_reset,
2527 .slave_alloc = fc_slave_alloc,
2528 .change_queue_depth = fc_change_queue_depth,
2529 .change_queue_type = fc_change_queue_type,
2530 .this_id = -1,
2531 .cmd_per_lun = 3,
Bhanu Gollapudi0ea5c272011-03-17 17:13:29 -07002532 .can_queue = BNX2FC_CAN_QUEUE,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002533 .use_clustering = ENABLE_CLUSTERING,
2534 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2535 .max_sectors = 512,
2536};
2537
2538static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2539 .frame_send = bnx2fc_xmit,
2540 .elsct_send = bnx2fc_elsct_send,
2541 .fcp_abort_io = bnx2fc_abort_io,
2542 .fcp_cleanup = bnx2fc_cleanup,
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -07002543 .get_lesb = bnx2fc_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002544 .rport_event_callback = bnx2fc_rport_event_handler,
2545};
2546
2547/**
2548 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2549 * structure carrying callback function pointers
2550 */
2551static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2552 .owner = THIS_MODULE,
2553 .cnic_init = bnx2fc_ulp_init,
2554 .cnic_exit = bnx2fc_ulp_exit,
2555 .cnic_start = bnx2fc_ulp_start,
2556 .cnic_stop = bnx2fc_ulp_stop,
2557 .indicate_kcqes = bnx2fc_indicate_kcqe,
2558 .indicate_netevent = bnx2fc_indicate_netevent,
2559};