blob: 8c6156a10d90233a45bee5f27f343a33c7311929 [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 Gollapudifd254182011-10-23 23:23:58 -070025#define DRV_MODULE_RELDATE "Oct 21, 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];
Ian Campbell94b06fd2011-08-24 22:28:14 +0000325 cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800326 + frag->page_offset;
327 } else {
328 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
329 }
330
331 memset(cp, 0, sizeof(*cp));
332 cp->fcoe_eof = eof;
333 cp->fcoe_crc32 = cpu_to_le32(~crc);
334 if (skb_is_nonlinear(skb)) {
335 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
336 cp = NULL;
337 }
338
339 /* adjust skb network/transport offsets to match mac/fcoe/port */
340 skb_push(skb, elen + hlen);
341 skb_reset_mac_header(skb);
342 skb_reset_network_header(skb);
343 skb->mac_len = elen;
344 skb->protocol = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700345 skb->dev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800346
347 /* fill up mac and fcoe headers */
348 eh = eth_hdr(skb);
349 eh->h_proto = htons(ETH_P_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700350 if (interface->ctlr.map_dest)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800351 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
352 else
353 /* insert GW address */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700354 memcpy(eh->h_dest, interface->ctlr.dest_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800355
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700356 if (unlikely(interface->ctlr.flogi_oxid != FC_XID_UNKNOWN))
357 memcpy(eh->h_source, interface->ctlr.ctl_src_addr, ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800358 else
359 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
360
361 hp = (struct fcoe_hdr *)(eh + 1);
362 memset(hp, 0, sizeof(*hp));
363 if (FC_FCOE_VER)
364 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
365 hp->fcoe_sof = sof;
366
367 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
368 if (lport->seq_offload && fr_max_payload(fp)) {
369 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
370 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
371 } else {
372 skb_shinfo(skb)->gso_type = 0;
373 skb_shinfo(skb)->gso_size = 0;
374 }
375
376 /*update tx stats */
377 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
378 stats->TxFrames++;
379 stats->TxWords += wlen;
380 put_cpu();
381
382 /* send down to lld */
383 fr_dev(fp) = lport;
384 if (port->fcoe_pending_queue.qlen)
385 fcoe_check_wait_queue(lport, skb);
386 else if (fcoe_start_io(skb))
387 fcoe_check_wait_queue(lport, skb);
388
389 return 0;
390}
391
392/**
393 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
394 *
395 * @skb: the receive socket buffer
396 * @dev: associated net device
397 * @ptype: context
398 * @olddev: last device
399 *
400 * This function receives the packet and builds FC frame and passes it up
401 */
402static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
403 struct packet_type *ptype, struct net_device *olddev)
404{
405 struct fc_lport *lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700406 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800407 struct fc_frame_header *fh;
408 struct fcoe_rcv_info *fr;
409 struct fcoe_percpu_s *bg;
410 unsigned short oxid;
411
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700412 interface = container_of(ptype, struct bnx2fc_interface,
413 fcoe_packet_type);
414 lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800415
416 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700417 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800418 goto err;
419 }
420
421 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700422 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800423 goto err;
424 }
425
426 /*
427 * Check for minimum frame length, and make sure required FCoE
428 * and FC headers are pulled into the linear data area.
429 */
430 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
431 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
432 goto err;
433
434 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
435 fh = (struct fc_frame_header *) skb_transport_header(skb);
436
437 oxid = ntohs(fh->fh_ox_id);
438
439 fr = fcoe_dev_from_skb(skb);
440 fr->fr_dev = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800441
442 bg = &bnx2fc_global;
443 spin_lock_bh(&bg->fcoe_rx_list.lock);
444
445 __skb_queue_tail(&bg->fcoe_rx_list, skb);
446 if (bg->fcoe_rx_list.qlen == 1)
447 wake_up_process(bg->thread);
448
449 spin_unlock_bh(&bg->fcoe_rx_list.lock);
450
451 return 0;
452err:
453 kfree_skb(skb);
454 return -1;
455}
456
457static int bnx2fc_l2_rcv_thread(void *arg)
458{
459 struct fcoe_percpu_s *bg = arg;
460 struct sk_buff *skb;
461
462 set_user_nice(current, -20);
463 set_current_state(TASK_INTERRUPTIBLE);
464 while (!kthread_should_stop()) {
465 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800466 spin_lock_bh(&bg->fcoe_rx_list.lock);
467 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
468 spin_unlock_bh(&bg->fcoe_rx_list.lock);
469 bnx2fc_recv_frame(skb);
470 spin_lock_bh(&bg->fcoe_rx_list.lock);
471 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700472 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800473 spin_unlock_bh(&bg->fcoe_rx_list.lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800474 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700475 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800476 return 0;
477}
478
479
480static void bnx2fc_recv_frame(struct sk_buff *skb)
481{
482 u32 fr_len;
483 struct fc_lport *lport;
484 struct fcoe_rcv_info *fr;
485 struct fcoe_dev_stats *stats;
486 struct fc_frame_header *fh;
487 struct fcoe_crc_eof crc_eof;
488 struct fc_frame *fp;
489 struct fc_lport *vn_port;
490 struct fcoe_port *port;
491 u8 *mac = NULL;
492 u8 *dest_mac = NULL;
493 struct fcoe_hdr *hp;
494
495 fr = fcoe_dev_from_skb(skb);
496 lport = fr->fr_dev;
497 if (unlikely(lport == NULL)) {
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -0700498 printk(KERN_ERR PFX "Invalid lport struct\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800499 kfree_skb(skb);
500 return;
501 }
502
503 if (skb_is_nonlinear(skb))
504 skb_linearize(skb);
505 mac = eth_hdr(skb)->h_source;
506 dest_mac = eth_hdr(skb)->h_dest;
507
508 /* Pull the header */
509 hp = (struct fcoe_hdr *) skb_network_header(skb);
510 fh = (struct fc_frame_header *) skb_transport_header(skb);
511 skb_pull(skb, sizeof(struct fcoe_hdr));
512 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
513
514 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
515 stats->RxFrames++;
516 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
517
518 fp = (struct fc_frame *)skb;
519 fc_frame_init(fp);
520 fr_dev(fp) = lport;
521 fr_sof(fp) = hp->fcoe_sof;
522 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
523 put_cpu();
524 kfree_skb(skb);
525 return;
526 }
527 fr_eof(fp) = crc_eof.fcoe_eof;
528 fr_crc(fp) = crc_eof.fcoe_crc32;
529 if (pskb_trim(skb, fr_len)) {
530 put_cpu();
531 kfree_skb(skb);
532 return;
533 }
534
535 fh = fc_frame_header_get(fp);
536
537 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
538 if (vn_port) {
539 port = lport_priv(vn_port);
540 if (compare_ether_addr(port->data_src_addr, dest_mac)
541 != 0) {
542 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
543 put_cpu();
544 kfree_skb(skb);
545 return;
546 }
547 }
548 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
549 fh->fh_type == FC_TYPE_FCP) {
550 /* Drop FCP data. We dont this in L2 path */
551 put_cpu();
552 kfree_skb(skb);
553 return;
554 }
555 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
556 fh->fh_type == FC_TYPE_ELS) {
557 switch (fc_frame_payload_op(fp)) {
558 case ELS_LOGO:
559 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
560 /* drop non-FIP LOGO */
561 put_cpu();
562 kfree_skb(skb);
563 return;
564 }
565 break;
566 }
567 }
Bhanu Prakash Gollapudi3f8744d2011-08-04 17:38:48 -0700568
569 if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
570 /* Drop incoming ABTS */
571 put_cpu();
572 kfree_skb(skb);
573 return;
574 }
575
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800576 if (le32_to_cpu(fr_crc(fp)) !=
577 ~crc32(~0, skb->data, fr_len)) {
578 if (stats->InvalidCRCCount < 5)
579 printk(KERN_WARNING PFX "dropping frame with "
580 "CRC error\n");
581 stats->InvalidCRCCount++;
582 put_cpu();
583 kfree_skb(skb);
584 return;
585 }
586 put_cpu();
587 fc_exch_recv(lport, fp);
588}
589
590/**
591 * bnx2fc_percpu_io_thread - thread per cpu for ios
592 *
593 * @arg: ptr to bnx2fc_percpu_info structure
594 */
595int bnx2fc_percpu_io_thread(void *arg)
596{
597 struct bnx2fc_percpu_s *p = arg;
598 struct bnx2fc_work *work, *tmp;
599 LIST_HEAD(work_list);
600
601 set_user_nice(current, -20);
602 set_current_state(TASK_INTERRUPTIBLE);
603 while (!kthread_should_stop()) {
604 schedule();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800605 spin_lock_bh(&p->fp_work_lock);
606 while (!list_empty(&p->work_list)) {
607 list_splice_init(&p->work_list, &work_list);
608 spin_unlock_bh(&p->fp_work_lock);
609
610 list_for_each_entry_safe(work, tmp, &work_list, list) {
611 list_del_init(&work->list);
612 bnx2fc_process_cq_compl(work->tgt, work->wqe);
613 kfree(work);
614 }
615
616 spin_lock_bh(&p->fp_work_lock);
617 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700618 __set_current_state(TASK_INTERRUPTIBLE);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800619 spin_unlock_bh(&p->fp_work_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800620 }
Bhanu Gollapudifee78712011-03-21 18:51:13 -0700621 __set_current_state(TASK_RUNNING);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800622
623 return 0;
624}
625
626static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
627{
628 struct fc_host_statistics *bnx2fc_stats;
629 struct fc_lport *lport = shost_priv(shost);
630 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700631 struct bnx2fc_interface *interface = port->priv;
632 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800633 struct fcoe_statistics_params *fw_stats;
634 int rc = 0;
635
636 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
637 if (!fw_stats)
638 return NULL;
639
640 bnx2fc_stats = fc_get_host_stats(shost);
641
642 init_completion(&hba->stat_req_done);
643 if (bnx2fc_send_stat_req(hba))
644 return bnx2fc_stats;
645 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
646 if (!rc) {
647 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
648 return bnx2fc_stats;
649 }
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +0300650 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800651 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
652 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
653 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
654 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
655
656 bnx2fc_stats->dumped_frames = 0;
657 bnx2fc_stats->lip_count = 0;
658 bnx2fc_stats->nos_count = 0;
659 bnx2fc_stats->loss_of_sync_count = 0;
660 bnx2fc_stats->loss_of_signal_count = 0;
661 bnx2fc_stats->prim_seq_protocol_err_count = 0;
662
663 return bnx2fc_stats;
664}
665
666static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
667{
668 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700669 struct bnx2fc_interface *interface = port->priv;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800670 struct Scsi_Host *shost = lport->host;
671 int rc = 0;
672
673 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
674 shost->max_lun = BNX2FC_MAX_LUN;
675 shost->max_id = BNX2FC_MAX_FCP_TGT;
676 shost->max_channel = 0;
677 if (lport->vport)
678 shost->transportt = bnx2fc_vport_xport_template;
679 else
680 shost->transportt = bnx2fc_transport_template;
681
682 /* Add the new host to SCSI-ml */
683 rc = scsi_add_host(lport->host, dev);
684 if (rc) {
685 printk(KERN_ERR PFX "Error on scsi_add_host\n");
686 return rc;
687 }
688 if (!lport->vport)
689 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
690 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
691 BNX2FC_NAME, BNX2FC_VERSION,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700692 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800693
694 return 0;
695}
696
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800697static void bnx2fc_link_speed_update(struct fc_lport *lport)
698{
699 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700700 struct bnx2fc_interface *interface = port->priv;
701 struct net_device *netdev = interface->netdev;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000702 struct ethtool_cmd ecmd;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800703
Jiri Pirko4bc71cb2011-09-03 03:34:30 +0000704 if (!__ethtool_get_settings(netdev, &ecmd)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800705 lport->link_supported_speeds &=
706 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
707 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
708 SUPPORTED_1000baseT_Full))
709 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
710 if (ecmd.supported & SUPPORTED_10000baseT_Full)
711 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
712
David Decotigny8ae6dac2011-04-27 18:32:38 +0000713 switch (ethtool_cmd_speed(&ecmd)) {
714 case SPEED_1000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800715 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000716 break;
Bhanu Prakash Gollapudia4dc08c2011-05-27 11:47:25 -0700717 case SPEED_2500:
718 lport->link_speed = FC_PORTSPEED_2GBIT;
719 break;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000720 case SPEED_10000:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800721 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +0000722 break;
723 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800724 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800725}
726static int bnx2fc_link_ok(struct fc_lport *lport)
727{
728 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700729 struct bnx2fc_interface *interface = port->priv;
730 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800731 struct net_device *dev = hba->phys_dev;
732 int rc = 0;
733
734 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
735 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
736 else {
737 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
738 rc = -1;
739 }
740 return rc;
741}
742
743/**
744 * bnx2fc_get_link_state - get network link state
745 *
746 * @hba: adapter instance pointer
747 *
748 * updates adapter structure flag based on netdev state
749 */
750void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
751{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700752 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800753 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
754 else
755 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
756}
757
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700758static int bnx2fc_net_config(struct fc_lport *lport, struct net_device *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800759{
760 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700761 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800762 struct fcoe_port *port;
763 u64 wwnn, wwpn;
764
765 port = lport_priv(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700766 interface = port->priv;
767 hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800768
769 /* require support for get_pauseparam ethtool op. */
770 if (!hba->phys_dev->ethtool_ops ||
771 !hba->phys_dev->ethtool_ops->get_pauseparam)
772 return -EOPNOTSUPP;
773
Bhanu Gollapudi1294bfe2011-03-17 17:13:34 -0700774 if (fc_set_mfs(lport, BNX2FC_MFS))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800775 return -EINVAL;
776
777 skb_queue_head_init(&port->fcoe_pending_queue);
778 port->fcoe_pending_queue_active = 0;
779 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
780
781 bnx2fc_link_speed_update(lport);
782
783 if (!lport->vport) {
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700784 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
785 wwnn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
786 1, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800787 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
788 fc_set_wwnn(lport, wwnn);
789
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -0700790 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
791 wwpn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr,
792 2, 0);
793
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800794 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
795 fc_set_wwpn(lport, wwpn);
796 }
797
798 return 0;
799}
800
801static void bnx2fc_destroy_timer(unsigned long data)
802{
803 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
804
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -0700805 printk(KERN_ERR PFX "ERROR:bnx2fc_destroy_timer - "
806 "Destroy compl not received!!\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700807 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800808 wake_up_interruptible(&hba->destroy_wait);
809}
810
811/**
812 * bnx2fc_indicate_netevent - Generic netdev event handler
813 *
814 * @context: adapter structure pointer
815 * @event: event type
Michael Chan415199f2011-07-20 14:55:24 +0000816 * @vlan_id: vlan id - associated vlan id with this event
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800817 *
818 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700819 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800820 */
Michael Chan415199f2011-07-20 14:55:24 +0000821static void bnx2fc_indicate_netevent(void *context, unsigned long event,
822 u16 vlan_id)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800823{
824 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700825 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800826 struct fc_lport *vport;
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700827 struct bnx2fc_interface *interface, *tmp;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700828 int wait_for_upload = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800829 u32 link_possible = 1;
830
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700831 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
Michael Chan415199f2011-07-20 14:55:24 +0000832 return;
833
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800834 switch (event) {
835 case NETDEV_UP:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800836 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
837 printk(KERN_ERR "indicate_netevent: "\
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700838 "hba is not UP!!\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800839 break;
840
841 case NETDEV_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800842 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
843 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
844 link_possible = 0;
845 break;
846
847 case NETDEV_GOING_DOWN:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800848 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
849 link_possible = 0;
850 break;
851
852 case NETDEV_CHANGE:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800853 break;
854
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700855 case NETDEV_UNREGISTER:
856 if (!vlan_id)
857 return;
858 mutex_lock(&bnx2fc_dev_lock);
859 list_for_each_entry_safe(interface, tmp, &if_list, list) {
Nithin Nayak Sujir26b29822011-08-30 15:54:50 -0700860 if (interface->hba == hba &&
861 interface->vlan_id == (vlan_id & VLAN_VID_MASK))
862 __bnx2fc_destroy(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -0700863 }
864 mutex_unlock(&bnx2fc_dev_lock);
865 return;
866
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800867 default:
868 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
869 return;
870 }
871
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700872 mutex_lock(&bnx2fc_dev_lock);
873 list_for_each_entry(interface, &if_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800874
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700875 if (interface->hba != hba)
876 continue;
877
878 lport = interface->ctlr.lp;
879 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
880 interface->netdev->name, event);
881
882 bnx2fc_link_speed_update(lport);
883
884 if (link_possible && !bnx2fc_link_ok(lport)) {
Bhanu Prakash Gollapudic7806732011-08-30 15:54:47 -0700885 /* Reset max recv frame size to default */
886 fc_set_mfs(lport, BNX2FC_MFS);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -0700887 /*
888 * ctlr link up will only be handled during
889 * enable to avoid sending discovery solicitation
890 * on a stale vlan
891 */
Bhanu Prakash Gollapudifd8fa902011-10-03 16:45:00 -0700892 if (interface->enabled)
893 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700894 } else if (fcoe_ctlr_link_down(&interface->ctlr)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800895 mutex_lock(&lport->lp_mutex);
896 list_for_each_entry(vport, &lport->vports, list)
897 fc_host_port_type(vport->host) =
898 FC_PORTTYPE_UNKNOWN;
899 mutex_unlock(&lport->lp_mutex);
900 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
901 per_cpu_ptr(lport->dev_stats,
902 get_cpu())->LinkFailureCount++;
903 put_cpu();
904 fcoe_clean_pending_queue(lport);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700905 wait_for_upload = 1;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800906 }
907 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700908 mutex_unlock(&bnx2fc_dev_lock);
909
910 if (wait_for_upload) {
911 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
912 init_waitqueue_head(&hba->shutdown_wait);
913 BNX2FC_MISC_DBG("indicate_netevent "
914 "num_ofld_sess = %d\n",
915 hba->num_ofld_sess);
916 hba->wait_for_link_down = 1;
917 wait_event_interruptible(hba->shutdown_wait,
918 (hba->num_ofld_sess == 0));
919 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
920 hba->num_ofld_sess);
921 hba->wait_for_link_down = 0;
922
923 if (signal_pending(current))
924 flush_signals(current);
925 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800926}
927
928static int bnx2fc_libfc_config(struct fc_lport *lport)
929{
930
931 /* Set the function pointers set by bnx2fc driver */
932 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
933 sizeof(struct libfc_function_template));
934 fc_elsct_init(lport);
935 fc_exch_init(lport);
936 fc_rport_init(lport);
937 fc_disc_init(lport);
938 return 0;
939}
940
941static int bnx2fc_em_config(struct fc_lport *lport)
942{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800943 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
944 FCOE_MAX_XID, NULL)) {
945 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
946 return -ENOMEM;
947 }
948
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800949 return 0;
950}
951
952static int bnx2fc_lport_config(struct fc_lport *lport)
953{
954 lport->link_up = 0;
955 lport->qfull = 0;
956 lport->max_retry_count = 3;
957 lport->max_rport_retry_count = 3;
958 lport->e_d_tov = 2 * 1000;
959 lport->r_a_tov = 10 * 1000;
960
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800961 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
962 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800963 lport->does_npiv = 1;
964
965 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
966 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
967
968 /* alloc stats structure */
969 if (fc_lport_init_stats(lport))
970 return -ENOMEM;
971
972 /* Finish fc_lport configuration */
973 fc_lport_config(lport);
974
975 return 0;
976}
977
978/**
979 * bnx2fc_fip_recv - handle a received FIP frame.
980 *
981 * @skb: the received skb
982 * @dev: associated &net_device
983 * @ptype: the &packet_type structure which was used to register this handler.
984 * @orig_dev: original receive &net_device, in case @ dev is a bond.
985 *
986 * Returns: 0 for success
987 */
988static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
989 struct packet_type *ptype,
990 struct net_device *orig_dev)
991{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -0700992 struct bnx2fc_interface *interface;
993 interface = container_of(ptype, struct bnx2fc_interface,
994 fip_packet_type);
995 fcoe_ctlr_recv(&interface->ctlr, skb);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -0800996 return 0;
997}
998
999/**
1000 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
1001 *
1002 * @fip: FCoE controller.
1003 * @old: Unicast MAC address to delete if the MAC is non-zero.
1004 * @new: Unicast MAC address to add.
1005 *
1006 * Remove any previously-set unicast MAC filter.
1007 * Add secondary FCoE MAC address filter for our OUI.
1008 */
1009static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
1010{
1011 struct fcoe_port *port = lport_priv(lport);
1012
1013 memcpy(port->data_src_addr, addr, ETH_ALEN);
1014}
1015
1016/**
1017 * bnx2fc_get_src_mac - return the ethernet source address for an lport
1018 *
1019 * @lport: libfc port
1020 */
1021static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
1022{
1023 struct fcoe_port *port;
1024
1025 port = (struct fcoe_port *)lport_priv(lport);
1026 return port->data_src_addr;
1027}
1028
1029/**
1030 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
1031 *
1032 * @fip: FCoE controller.
1033 * @skb: FIP Packet.
1034 */
1035static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
1036{
1037 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1038 dev_queue_xmit(skb);
1039}
1040
1041static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1042{
1043 struct Scsi_Host *shost = vport_to_shost(vport);
1044 struct fc_lport *n_port = shost_priv(shost);
1045 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001046 struct bnx2fc_interface *interface = port->priv;
1047 struct net_device *netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001048 struct fc_lport *vn_port;
Bhanu Prakash Gollapudi861efc52011-08-04 17:38:51 -07001049 int rc;
1050 char buf[32];
1051
1052 rc = fcoe_validate_vport_create(vport);
1053 if (rc) {
1054 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
1055 printk(KERN_ERR PFX "Failed to create vport, "
1056 "WWPN (0x%s) already exists\n",
1057 buf);
1058 return rc;
1059 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001060
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001061 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001062 printk(KERN_ERR PFX "vn ports cannot be created on"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001063 "this interface\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001064 return -EIO;
1065 }
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001066 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001067 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001068 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001069 mutex_unlock(&bnx2fc_dev_lock);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00001070 rtnl_unlock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001071
1072 if (IS_ERR(vn_port)) {
1073 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1074 netdev->name);
1075 return -EIO;
1076 }
1077
1078 if (disabled) {
1079 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1080 } else {
1081 vn_port->boot_time = jiffies;
1082 fc_lport_init(vn_port);
1083 fc_fabric_login(vn_port);
1084 fc_vport_setlink(vn_port);
1085 }
1086 return 0;
1087}
1088
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001089static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1090{
1091 struct bnx2fc_lport *blport, *tmp;
1092
1093 spin_lock_bh(&hba->hba_lock);
1094 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1095 if (blport->lport == lport) {
1096 list_del(&blport->list);
1097 kfree(blport);
1098 }
1099 }
1100 spin_unlock_bh(&hba->hba_lock);
1101}
1102
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001103static int bnx2fc_vport_destroy(struct fc_vport *vport)
1104{
1105 struct Scsi_Host *shost = vport_to_shost(vport);
1106 struct fc_lport *n_port = shost_priv(shost);
1107 struct fc_lport *vn_port = vport->dd_data;
1108 struct fcoe_port *port = lport_priv(vn_port);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001109 struct bnx2fc_interface *interface = port->priv;
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001110 struct fc_lport *v_port;
1111 bool found = false;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001112
1113 mutex_lock(&n_port->lp_mutex);
Bhanu Prakash Gollapudicdf54662011-08-04 17:38:39 -07001114 list_for_each_entry(v_port, &n_port->vports, list)
1115 if (v_port->vport == vport) {
1116 found = true;
1117 break;
1118 }
1119
1120 if (!found) {
1121 mutex_unlock(&n_port->lp_mutex);
1122 return -ENOENT;
1123 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001124 list_del(&vn_port->list);
1125 mutex_unlock(&n_port->lp_mutex);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001126 bnx2fc_free_vport(interface->hba, port->lport);
1127 bnx2fc_port_shutdown(port->lport);
1128 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001129 queue_work(bnx2fc_wq, &port->destroy_work);
1130 return 0;
1131}
1132
1133static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1134{
1135 struct fc_lport *lport = vport->dd_data;
1136
1137 if (disable) {
1138 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1139 fc_fabric_logoff(lport);
1140 } else {
1141 lport->boot_time = jiffies;
1142 fc_fabric_login(lport);
1143 fc_vport_setlink(lport);
1144 }
1145 return 0;
1146}
1147
1148
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001149static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001150{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001151 struct net_device *netdev = interface->netdev;
1152 struct net_device *physdev = interface->hba->phys_dev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001153 struct netdev_hw_addr *ha;
1154 int sel_san_mac = 0;
1155
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001156 /* setup Source MAC Address */
1157 rcu_read_lock();
1158 for_each_dev_addr(physdev, ha) {
1159 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1160 ha->type);
1161 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1162 ha->addr[1], ha->addr[2], ha->addr[3],
1163 ha->addr[4], ha->addr[5]);
1164
1165 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1166 (is_valid_ether_addr(ha->addr))) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001167 memcpy(interface->ctlr.ctl_src_addr, ha->addr,
1168 ETH_ALEN);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001169 sel_san_mac = 1;
1170 BNX2FC_MISC_DBG("Found SAN MAC\n");
1171 }
1172 }
1173 rcu_read_unlock();
1174
1175 if (!sel_san_mac)
1176 return -ENODEV;
1177
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001178 interface->fip_packet_type.func = bnx2fc_fip_recv;
1179 interface->fip_packet_type.type = htons(ETH_P_FIP);
1180 interface->fip_packet_type.dev = netdev;
1181 dev_add_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001182
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001183 interface->fcoe_packet_type.func = bnx2fc_rcv;
1184 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1185 interface->fcoe_packet_type.dev = netdev;
1186 dev_add_pack(&interface->fcoe_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001187
1188 return 0;
1189}
1190
1191static int bnx2fc_attach_transport(void)
1192{
1193 bnx2fc_transport_template =
1194 fc_attach_transport(&bnx2fc_transport_function);
1195
1196 if (bnx2fc_transport_template == NULL) {
1197 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1198 return -ENODEV;
1199 }
1200
1201 bnx2fc_vport_xport_template =
1202 fc_attach_transport(&bnx2fc_vport_xport_function);
1203 if (bnx2fc_vport_xport_template == NULL) {
1204 printk(KERN_ERR PFX
1205 "Failed to attach FC transport for vport\n");
1206 fc_release_transport(bnx2fc_transport_template);
1207 bnx2fc_transport_template = NULL;
1208 return -ENODEV;
1209 }
1210 return 0;
1211}
1212static void bnx2fc_release_transport(void)
1213{
1214 fc_release_transport(bnx2fc_transport_template);
1215 fc_release_transport(bnx2fc_vport_xport_template);
1216 bnx2fc_transport_template = NULL;
1217 bnx2fc_vport_xport_template = NULL;
1218}
1219
1220static void bnx2fc_interface_release(struct kref *kref)
1221{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001222 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001223 struct net_device *netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001224
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001225 interface = container_of(kref, struct bnx2fc_interface, kref);
Nithin Nayak Sujir068bdce2011-04-25 12:30:09 -07001226 BNX2FC_MISC_DBG("Interface is being released\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001227
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001228 netdev = interface->netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001229
1230 /* tear-down FIP controller */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001231 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1232 fcoe_ctlr_destroy(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001233
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001234 kfree(interface);
1235
1236 dev_put(netdev);
1237 module_put(THIS_MODULE);
1238}
1239
1240static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1241{
1242 kref_get(&interface->kref);
1243}
1244
1245static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1246{
1247 kref_put(&interface->kref, bnx2fc_interface_release);
1248}
1249static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1250{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001251 /* Free the command manager */
1252 if (hba->cmd_mgr) {
1253 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1254 hba->cmd_mgr = NULL;
1255 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001256 kfree(hba->tgt_ofld_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001257 bnx2fc_unbind_pcidev(hba);
1258 kfree(hba);
1259}
1260
1261/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001262 * bnx2fc_hba_create - create a new bnx2fc hba
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001263 *
1264 * @cnic: pointer to cnic device
1265 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001266 * Creates a new FCoE hba on the given device.
1267 *
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001268 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001269static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001270{
1271 struct bnx2fc_hba *hba;
1272 int rc;
1273
1274 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1275 if (!hba) {
1276 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1277 return NULL;
1278 }
1279 spin_lock_init(&hba->hba_lock);
1280 mutex_init(&hba->hba_mutex);
1281
1282 hba->cnic = cnic;
1283 rc = bnx2fc_bind_pcidev(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001284 if (rc) {
1285 printk(KERN_ERR PFX "create_adapter: bind error\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001286 goto bind_err;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001287 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001288 hba->phys_dev = cnic->netdev;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001289 hba->next_conn_id = 0;
1290
1291 hba->tgt_ofld_list =
1292 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1293 GFP_KERNEL);
1294 if (!hba->tgt_ofld_list) {
1295 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1296 goto tgtofld_err;
1297 }
1298
1299 hba->num_ofld_sess = 0;
1300
1301 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1302 BNX2FC_MAX_XID);
1303 if (!hba->cmd_mgr) {
1304 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1305 goto cmgr_err;
1306 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001307
1308 init_waitqueue_head(&hba->shutdown_wait);
1309 init_waitqueue_head(&hba->destroy_wait);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001310 INIT_LIST_HEAD(&hba->vports);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001311
1312 return hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001313
1314cmgr_err:
1315 kfree(hba->tgt_ofld_list);
1316tgtofld_err:
1317 bnx2fc_unbind_pcidev(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001318bind_err:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001319 kfree(hba);
1320 return NULL;
1321}
1322
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001323struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1324 struct net_device *netdev,
1325 enum fip_state fip_mode)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001326{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001327 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001328 int rc = 0;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001329
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001330 interface = kzalloc(sizeof(*interface), GFP_KERNEL);
1331 if (!interface) {
1332 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1333 return NULL;
1334 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001335 dev_hold(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001336 kref_init(&interface->kref);
1337 interface->hba = hba;
1338 interface->netdev = netdev;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001339
1340 /* Initialize FIP */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001341 fcoe_ctlr_init(&interface->ctlr, fip_mode);
1342 interface->ctlr.send = bnx2fc_fip_send;
1343 interface->ctlr.update_mac = bnx2fc_update_src_mac;
1344 interface->ctlr.get_src_addr = bnx2fc_get_src_mac;
1345 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001346
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001347 rc = bnx2fc_interface_setup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001348 if (!rc)
1349 return interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001350
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001351 fcoe_ctlr_destroy(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001352 dev_put(netdev);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001353 kfree(interface);
1354 return NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001355}
1356
1357/**
1358 * bnx2fc_if_create - Create FCoE instance on a given interface
1359 *
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001360 * @interface: FCoE interface to create a local port on
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001361 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1362 * @npiv: Indicates if the port is vport or not
1363 *
1364 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1365 *
1366 * Returns: Allocated fc_lport or an error pointer
1367 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001368static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001369 struct device *parent, int npiv)
1370{
Vasu Dev134a4e22011-04-28 15:55:44 -07001371 struct fc_lport *lport, *n_port;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001372 struct fcoe_port *port;
1373 struct Scsi_Host *shost;
1374 struct fc_vport *vport = dev_to_vport(parent);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001375 struct bnx2fc_lport *blport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001376 struct bnx2fc_hba *hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001377 int rc = 0;
1378
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001379 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1380 if (!blport) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001381 BNX2FC_HBA_DBG(interface->ctlr.lp, "Unable to alloc blport\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001382 return NULL;
1383 }
1384
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001385 /* Allocate Scsi_Host structure */
Vasu Dev134a4e22011-04-28 15:55:44 -07001386 if (!npiv)
1387 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1388 else
1389 lport = libfc_vport_create(vport, sizeof(*port));
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001390
1391 if (!lport) {
1392 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001393 goto free_blport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001394 }
1395 shost = lport->host;
1396 port = lport_priv(lport);
1397 port->lport = lport;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001398 port->priv = interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001399 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1400
1401 /* Configure fcoe_port */
1402 rc = bnx2fc_lport_config(lport);
1403 if (rc)
1404 goto lp_config_err;
1405
1406 if (npiv) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001407 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1408 vport->node_name, vport->port_name);
1409 fc_set_wwnn(lport, vport->node_name);
1410 fc_set_wwpn(lport, vport->port_name);
1411 }
1412 /* Configure netdev and networking properties of the lport */
Bhanu Prakash Gollapudi52439602011-08-04 17:38:50 -07001413 rc = bnx2fc_net_config(lport, interface->netdev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001414 if (rc) {
1415 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1416 goto lp_config_err;
1417 }
1418
1419 rc = bnx2fc_shost_config(lport, parent);
1420 if (rc) {
1421 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001422 interface->netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001423 goto lp_config_err;
1424 }
1425
1426 /* Initialize the libfc library */
1427 rc = bnx2fc_libfc_config(lport);
1428 if (rc) {
1429 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1430 goto shost_err;
1431 }
1432 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1433
1434 /* Allocate exchange manager */
Vasu Dev134a4e22011-04-28 15:55:44 -07001435 if (!npiv)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001436 rc = bnx2fc_em_config(lport);
Vasu Dev134a4e22011-04-28 15:55:44 -07001437 else {
1438 shost = vport_to_shost(vport);
1439 n_port = shost_priv(shost);
1440 rc = fc_exch_mgr_list_clone(n_port, lport);
1441 }
1442
1443 if (rc) {
1444 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1445 goto shost_err;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001446 }
1447
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001448 bnx2fc_interface_get(interface);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001449
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001450 hba = interface->hba;
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001451 spin_lock_bh(&hba->hba_lock);
1452 blport->lport = lport;
1453 list_add_tail(&blport->list, &hba->vports);
1454 spin_unlock_bh(&hba->hba_lock);
1455
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001456 return lport;
1457
1458shost_err:
1459 scsi_remove_host(shost);
1460lp_config_err:
1461 scsi_host_put(lport->host);
Bhanu Prakash Gollapudid36b3272011-05-27 11:47:27 -07001462free_blport:
1463 kfree(blport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001464 return NULL;
1465}
1466
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001467static void bnx2fc_net_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001468{
1469 /* Dont listen for Ethernet packets anymore */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001470 __dev_remove_pack(&interface->fcoe_packet_type);
1471 __dev_remove_pack(&interface->fip_packet_type);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001472 synchronize_net();
1473}
1474
Bhanu Prakash Gollapudi776cebc2011-08-04 17:38:40 -07001475static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001476{
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001477 struct fc_lport *lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001478 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001479 struct bnx2fc_hba *hba = interface->hba;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001480
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001481 /* Stop the transmit retry timer */
1482 del_timer_sync(&port->timer);
1483
1484 /* Free existing transmit skbs */
1485 fcoe_clean_pending_queue(lport);
1486
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07001487 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001488
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001489 bnx2fc_free_vport(hba, lport);
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001490}
1491
1492static void bnx2fc_if_destroy(struct fc_lport *lport)
1493{
1494
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001495 /* Free queued packets for the receive thread */
1496 bnx2fc_clean_rx_queue(lport);
1497
1498 /* Detach from scsi-ml */
1499 fc_remove_host(lport->host);
1500 scsi_remove_host(lport->host);
1501
1502 /*
1503 * Note that only the physical lport will have the exchange manager.
1504 * for vports, this function is NOP
1505 */
1506 fc_exch_mgr_free(lport);
1507
1508 /* Free memory used by statistical counters */
1509 fc_lport_free_stats(lport);
1510
1511 /* Release Scsi_Host */
1512 scsi_host_put(lport->host);
1513}
1514
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001515static void __bnx2fc_destroy(struct bnx2fc_interface *interface)
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001516{
1517 struct fc_lport *lport = interface->ctlr.lp;
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001518 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001519
1520 bnx2fc_interface_cleanup(interface);
1521 bnx2fc_stop(interface);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001522 list_del(&interface->list);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001523 bnx2fc_interface_put(interface);
Bhanu Prakash Gollapudi0cbf32e2011-08-30 15:54:51 -07001524 queue_work(bnx2fc_wq, &port->destroy_work);
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07001525}
1526
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001527/**
1528 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1529 *
1530 * @buffer: The name of the Ethernet interface to be destroyed
1531 * @kp: The associated kernel parameter
1532 *
1533 * Called from sysfs.
1534 *
1535 * Returns: 0 for success
1536 */
1537static int bnx2fc_destroy(struct net_device *netdev)
1538{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001539 struct bnx2fc_interface *interface = NULL;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001540 int rc = 0;
1541
Nithin Sujir6702ca12011-03-17 17:13:27 -07001542 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001543 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001544
1545 interface = bnx2fc_interface_lookup(netdev);
1546 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001547 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001548 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001549 goto netdev_err;
1550 }
1551
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001552
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001553 destroy_workqueue(interface->timer_work_queue);
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07001554 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001555
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001556netdev_err:
1557 mutex_unlock(&bnx2fc_dev_lock);
1558 rtnl_unlock();
1559 return rc;
1560}
1561
1562static void bnx2fc_destroy_work(struct work_struct *work)
1563{
1564 struct fcoe_port *port;
1565 struct fc_lport *lport;
1566
1567 port = container_of(work, struct fcoe_port, destroy_work);
1568 lport = port->lport;
1569
1570 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1571
Bhanu Prakash Gollapudi9be17fc2011-08-04 17:38:41 -07001572 bnx2fc_if_destroy(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001573}
1574
1575static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1576{
1577 bnx2fc_free_fw_resc(hba);
1578 bnx2fc_free_task_ctx(hba);
1579}
1580
1581/**
1582 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1583 * pci structure
1584 *
1585 * @hba: Adapter instance
1586 */
1587static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1588{
1589 if (bnx2fc_setup_task_ctx(hba))
1590 goto mem_err;
1591
1592 if (bnx2fc_setup_fw_resc(hba))
1593 goto mem_err;
1594
1595 return 0;
1596mem_err:
1597 bnx2fc_unbind_adapter_devices(hba);
1598 return -ENOMEM;
1599}
1600
1601static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1602{
1603 struct cnic_dev *cnic;
1604
1605 if (!hba->cnic) {
1606 printk(KERN_ERR PFX "cnic is NULL\n");
1607 return -ENODEV;
1608 }
1609 cnic = hba->cnic;
1610 hba->pcidev = cnic->pcidev;
1611 if (hba->pcidev)
1612 pci_dev_get(hba->pcidev);
1613
1614 return 0;
1615}
1616
1617static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1618{
1619 if (hba->pcidev)
1620 pci_dev_put(hba->pcidev);
1621 hba->pcidev = NULL;
1622}
1623
1624
1625
1626/**
1627 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1628 *
1629 * @handle: transport handle pointing to adapter struture
1630 *
1631 * This function maps adapter structure to pcidev structure and initiates
1632 * firmware handshake to enable/initialize on-chip FCoE components.
1633 * This bnx2fc - cnic interface api callback is used after following
1634 * conditions are met -
1635 * a) underlying network interface is up (marked by event NETDEV_UP
1636 * from netdev
1637 * b) bnx2fc adatper structure is registered.
1638 */
1639static void bnx2fc_ulp_start(void *handle)
1640{
1641 struct bnx2fc_hba *hba = handle;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001642 struct bnx2fc_interface *interface;
1643 struct fc_lport *lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001644
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001645 mutex_lock(&bnx2fc_dev_lock);
1646
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001647 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001648 bnx2fc_fw_init(hba);
1649
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001650 BNX2FC_MISC_DBG("bnx2fc started.\n");
1651
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001652 list_for_each_entry(interface, &if_list, list) {
1653 if (interface->hba == hba) {
1654 lport = interface->ctlr.lp;
1655 /* Kick off Fabric discovery*/
1656 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1657 lport->tt.frame_send = bnx2fc_xmit;
1658 bnx2fc_start_disc(interface);
1659 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001660 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001661
1662 mutex_unlock(&bnx2fc_dev_lock);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001663}
1664
1665static void bnx2fc_port_shutdown(struct fc_lport *lport)
1666{
1667 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1668 fc_fabric_logoff(lport);
1669 fc_lport_destroy(lport);
1670}
1671
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001672static void bnx2fc_stop(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001673{
1674 struct fc_lport *lport;
1675 struct fc_lport *vport;
1676
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001677 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1678 return;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001679
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001680 lport = interface->ctlr.lp;
1681 bnx2fc_port_shutdown(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001682
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001683 mutex_lock(&lport->lp_mutex);
1684 list_for_each_entry(vport, &lport->vports, list)
1685 fc_host_port_type(vport->host) =
1686 FC_PORTTYPE_UNKNOWN;
1687 mutex_unlock(&lport->lp_mutex);
1688 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1689 fcoe_ctlr_link_down(&interface->ctlr);
1690 fcoe_clean_pending_queue(lport);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001691}
1692
1693static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1694{
1695#define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1696 int rc = -1;
1697 int i = HZ;
1698
1699 rc = bnx2fc_bind_adapter_devices(hba);
1700 if (rc) {
1701 printk(KERN_ALERT PFX
1702 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1703 goto err_out;
1704 }
1705
1706 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1707 if (rc) {
1708 printk(KERN_ALERT PFX
1709 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1710 goto err_unbind;
1711 }
1712
1713 /*
1714 * Wait until the adapter init message is complete, and adapter
1715 * state is UP.
1716 */
1717 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1718 msleep(BNX2FC_INIT_POLL_TIME);
1719
1720 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1721 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1722 "Ignoring...\n",
1723 hba->cnic->netdev->name);
1724 rc = -1;
1725 goto err_unbind;
1726 }
1727
1728
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001729 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001730 return 0;
1731
1732err_unbind:
1733 bnx2fc_unbind_adapter_devices(hba);
1734err_out:
1735 return rc;
1736}
1737
1738static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1739{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001740 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001741 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1742 init_timer(&hba->destroy_timer);
1743 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1744 jiffies;
1745 hba->destroy_timer.function = bnx2fc_destroy_timer;
1746 hba->destroy_timer.data = (unsigned long)hba;
1747 add_timer(&hba->destroy_timer);
1748 wait_event_interruptible(hba->destroy_wait,
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001749 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1750 &hba->flags));
Bhanu Prakash Gollapudicd703ae2011-08-04 17:38:43 -07001751 clear_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001752 /* This should never happen */
1753 if (signal_pending(current))
1754 flush_signals(current);
1755
1756 del_timer_sync(&hba->destroy_timer);
1757 }
1758 bnx2fc_unbind_adapter_devices(hba);
1759 }
1760}
1761
1762/**
1763 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1764 *
1765 * @handle: transport handle pointing to adapter structure
1766 *
1767 * Driver checks if adapter is already in shutdown mode, if not start
1768 * the shutdown process.
1769 */
1770static void bnx2fc_ulp_stop(void *handle)
1771{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001772 struct bnx2fc_hba *hba = handle;
1773 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001774
1775 printk(KERN_ERR "ULP_STOP\n");
1776
1777 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001778 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1779 goto exit;
1780 list_for_each_entry(interface, &if_list, list) {
1781 if (interface->hba == hba)
1782 bnx2fc_stop(interface);
1783 }
1784 BUG_ON(hba->num_ofld_sess != 0);
1785
1786 mutex_lock(&hba->hba_mutex);
1787 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1788 clear_bit(ADAPTER_STATE_GOING_DOWN,
1789 &hba->adapter_state);
1790
1791 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1792 mutex_unlock(&hba->hba_mutex);
1793
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001794 bnx2fc_fw_destroy(hba);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001795exit:
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001796 mutex_unlock(&bnx2fc_dev_lock);
1797}
1798
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001799static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001800{
1801 struct fc_lport *lport;
1802 int wait_cnt = 0;
1803
1804 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1805 /* Kick off FIP/FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001806 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001807 printk(KERN_ERR PFX "Init not done yet\n");
1808 return;
1809 }
1810
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001811 lport = interface->ctlr.lp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001812 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1813
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001814 if (!bnx2fc_link_ok(lport) && interface->enabled) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001815 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001816 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001817 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001818 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001819 }
1820
1821 /* wait for the FCF to be selected before issuing FLOGI */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001822 while (!interface->ctlr.sel_fcf) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001823 msleep(250);
1824 /* give up after 3 secs */
1825 if (++wait_cnt > 12)
1826 break;
1827 }
Bhanu Prakash Gollapudi627e6282011-08-04 17:38:35 -07001828
1829 /* Reset max receive frame size to default */
1830 if (fc_set_mfs(lport, BNX2FC_MFS))
1831 return;
1832
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001833 fc_lport_init(lport);
1834 fc_fabric_login(lport);
1835}
1836
1837
1838/**
1839 * bnx2fc_ulp_init - Initialize an adapter instance
1840 *
1841 * @dev : cnic device handle
1842 * Called from cnic_register_driver() context to initialize all
1843 * enumerated cnic devices. This routine allocates adapter structure
1844 * and other device specific resources.
1845 */
1846static void bnx2fc_ulp_init(struct cnic_dev *dev)
1847{
1848 struct bnx2fc_hba *hba;
1849 int rc = 0;
1850
1851 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1852 /* bnx2fc works only when bnx2x is loaded */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001853 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1854 (dev->max_fcoe_conn == 0)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001855 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001856 " flags: %lx fcoe_conn: %d\n",
1857 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001858 return;
1859 }
1860
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001861 hba = bnx2fc_hba_create(dev);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001862 if (!hba) {
1863 printk(KERN_ERR PFX "hba initialization failed\n");
1864 return;
1865 }
1866
1867 /* Add HBA to the adapter list */
1868 mutex_lock(&bnx2fc_dev_lock);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001869 list_add_tail(&hba->list, &adapter_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001870 adapter_count++;
1871 mutex_unlock(&bnx2fc_dev_lock);
1872
1873 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1874 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1875 (void *) hba);
1876 if (rc)
Bhanu Prakash Gollapudib2a554f2011-06-27 23:30:53 -07001877 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001878 else
1879 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1880}
1881
1882
1883static int bnx2fc_disable(struct net_device *netdev)
1884{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001885 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001886 int rc = 0;
1887
Nithin Sujir6702ca12011-03-17 17:13:27 -07001888 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001889 mutex_lock(&bnx2fc_dev_lock);
1890
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001891 interface = bnx2fc_interface_lookup(netdev);
1892 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001893 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001894 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001895 } else {
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001896 interface->enabled = false;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001897 fcoe_ctlr_link_down(&interface->ctlr);
1898 fcoe_clean_pending_queue(interface->ctlr.lp);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001899 }
1900
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001901 mutex_unlock(&bnx2fc_dev_lock);
1902 rtnl_unlock();
1903 return rc;
1904}
1905
1906
1907static int bnx2fc_enable(struct net_device *netdev)
1908{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001909 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001910 int rc = 0;
1911
Nithin Sujir6702ca12011-03-17 17:13:27 -07001912 rtnl_lock();
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001913 mutex_lock(&bnx2fc_dev_lock);
1914
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001915 interface = bnx2fc_interface_lookup(netdev);
1916 if (!interface || !interface->ctlr.lp) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001917 rc = -ENODEV;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001918 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001919 } else if (!bnx2fc_link_ok(interface->ctlr.lp)) {
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001920 fcoe_ctlr_link_up(&interface->ctlr);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07001921 interface->enabled = true;
1922 }
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001923
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001924 mutex_unlock(&bnx2fc_dev_lock);
1925 rtnl_unlock();
1926 return rc;
1927}
1928
1929/**
1930 * bnx2fc_create - Create bnx2fc FCoE interface
1931 *
1932 * @buffer: The name of Ethernet interface to create on
1933 * @kp: The associated kernel param
1934 *
1935 * Called from sysfs.
1936 *
1937 * Returns: 0 for success
1938 */
1939static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1940{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001941 struct bnx2fc_interface *interface;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001942 struct bnx2fc_hba *hba;
1943 struct net_device *phys_dev;
1944 struct fc_lport *lport;
1945 struct ethtool_drvinfo drvinfo;
1946 int rc = 0;
1947 int vlan_id;
1948
1949 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1950 if (fip_mode != FIP_MODE_FABRIC) {
1951 printk(KERN_ERR "fip mode not FABRIC\n");
1952 return -EIO;
1953 }
1954
Nithin Sujir6702ca12011-03-17 17:13:27 -07001955 rtnl_lock();
1956
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001957 mutex_lock(&bnx2fc_dev_lock);
1958
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001959 if (!try_module_get(THIS_MODULE)) {
1960 rc = -EINVAL;
1961 goto mod_err;
1962 }
1963
1964 /* obtain physical netdev */
1965 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1966 phys_dev = vlan_dev_real_dev(netdev);
1967 vlan_id = vlan_dev_vlan_id(netdev);
1968 } else {
1969 printk(KERN_ERR PFX "Not a vlan device\n");
1970 rc = -EINVAL;
1971 goto netdev_err;
1972 }
1973 /* verify if the physical device is a netxtreme2 device */
1974 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1975 memset(&drvinfo, 0, sizeof(drvinfo));
1976 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001977 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001978 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1979 rc = -EINVAL;
1980 goto netdev_err;
1981 }
1982 } else {
1983 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1984 rc = -EINVAL;
1985 goto netdev_err;
1986 }
1987
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001988 /* obtain interface and initialize rest of the structure */
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001989 hba = bnx2fc_hba_lookup(phys_dev);
1990 if (!hba) {
1991 rc = -ENODEV;
1992 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1993 goto netdev_err;
1994 }
1995
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07001996 if (bnx2fc_interface_lookup(netdev)) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08001997 rc = -EEXIST;
1998 goto netdev_err;
1999 }
2000
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002001 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
2002 if (!interface) {
2003 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002004 goto ifput_err;
2005 }
2006
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002007 interface->vlan_id = vlan_id;
2008 interface->vlan_enabled = 1;
2009
2010 interface->timer_work_queue =
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002011 create_singlethread_workqueue("bnx2fc_timer_wq");
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002012 if (!interface->timer_work_queue) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002013 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
2014 rc = -EINVAL;
2015 goto ifput_err;
2016 }
2017
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002018 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002019 if (!lport) {
2020 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
2021 netdev->name);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002022 rc = -EINVAL;
2023 goto if_create_err;
2024 }
2025
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002026 /* Add interface to if_list */
2027 list_add_tail(&interface->list, &if_list);
2028
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002029 lport->boot_time = jiffies;
2030
2031 /* Make this master N_port */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002032 interface->ctlr.lp = lport;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002033
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002034 if (!bnx2fc_link_ok(lport)) {
2035 fcoe_ctlr_link_up(&interface->ctlr);
2036 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
2037 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
2038 }
2039
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002040 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
2041 bnx2fc_start_disc(interface);
Bhanu Prakash Gollapudi8a5badf2011-08-30 15:54:48 -07002042 interface->enabled = true;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002043 /*
2044 * Release from kref_init in bnx2fc_interface_setup, on success
2045 * lport should be holding a reference taken in bnx2fc_if_create
2046 */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002047 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002048 /* put netdev that was held while calling dev_get_by_name */
2049 mutex_unlock(&bnx2fc_dev_lock);
2050 rtnl_unlock();
2051 return 0;
2052
2053if_create_err:
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002054 destroy_workqueue(interface->timer_work_queue);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002055ifput_err:
Bhanu Prakash Gollapudi013068f2011-08-30 15:54:52 -07002056 bnx2fc_net_cleanup(interface);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002057 bnx2fc_interface_put(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002058netdev_err:
2059 module_put(THIS_MODULE);
2060mod_err:
2061 mutex_unlock(&bnx2fc_dev_lock);
2062 rtnl_unlock();
2063 return rc;
2064}
2065
2066/**
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002067 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002068 *
2069 * @cnic: Pointer to cnic device instance
2070 *
2071 **/
2072static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2073{
2074 struct list_head *list;
2075 struct list_head *temp;
2076 struct bnx2fc_hba *hba;
2077
2078 /* Called with bnx2fc_dev_lock held */
2079 list_for_each_safe(list, temp, &adapter_list) {
2080 hba = (struct bnx2fc_hba *)list;
2081 if (hba->cnic == cnic)
2082 return hba;
2083 }
2084 return NULL;
2085}
2086
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002087static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2088 *netdev)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002089{
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002090 struct bnx2fc_interface *interface;
2091
2092 /* Called with bnx2fc_dev_lock held */
2093 list_for_each_entry(interface, &if_list, list) {
2094 if (interface->netdev == netdev)
2095 return interface;
2096 }
2097 return NULL;
2098}
2099
2100static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2101 *phys_dev)
2102{
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002103 struct bnx2fc_hba *hba;
2104
2105 /* Called with bnx2fc_dev_lock held */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002106 list_for_each_entry(hba, &adapter_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002107 if (hba->phys_dev == phys_dev)
2108 return hba;
2109 }
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002110 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002111 return NULL;
2112}
2113
2114/**
2115 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2116 *
2117 * @dev cnic device handle
2118 */
2119static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2120{
2121 struct bnx2fc_hba *hba;
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002122 struct bnx2fc_interface *interface, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002123
2124 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2125
2126 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2127 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2128 dev->netdev->name, dev->flags);
2129 return;
2130 }
2131
2132 mutex_lock(&bnx2fc_dev_lock);
2133 hba = bnx2fc_find_hba_for_cnic(dev);
2134 if (!hba) {
2135 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2136 dev);
2137 mutex_unlock(&bnx2fc_dev_lock);
2138 return;
2139 }
2140
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002141 list_del_init(&hba->list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002142 adapter_count--;
2143
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002144 list_for_each_entry_safe(interface, tmp, &if_list, list)
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002145 /* destroy not called yet, move to quiesced list */
Bhanu Prakash Gollapudiabc49a92011-08-04 17:38:42 -07002146 if (interface->hba == hba)
Bhanu Prakash Gollapudieccdcd02011-08-24 18:56:42 -07002147 __bnx2fc_destroy(interface);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002148 mutex_unlock(&bnx2fc_dev_lock);
2149
2150 bnx2fc_ulp_stop(hba);
2151 /* unregister cnic device */
2152 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2153 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002154 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002155}
2156
2157/**
2158 * bnx2fc_fcoe_reset - Resets the fcoe
2159 *
2160 * @shost: shost the reset is from
2161 *
2162 * Returns: always 0
2163 */
2164static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2165{
2166 struct fc_lport *lport = shost_priv(shost);
2167 fc_lport_reset(lport);
2168 return 0;
2169}
2170
2171
2172static bool bnx2fc_match(struct net_device *netdev)
2173{
2174 mutex_lock(&bnx2fc_dev_lock);
2175 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2176 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2177
2178 if (bnx2fc_hba_lookup(phys_dev)) {
2179 mutex_unlock(&bnx2fc_dev_lock);
2180 return true;
2181 }
2182 }
2183 mutex_unlock(&bnx2fc_dev_lock);
2184 return false;
2185}
2186
2187
2188static struct fcoe_transport bnx2fc_transport = {
2189 .name = {"bnx2fc"},
2190 .attached = false,
2191 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2192 .match = bnx2fc_match,
2193 .create = bnx2fc_create,
2194 .destroy = bnx2fc_destroy,
2195 .enable = bnx2fc_enable,
2196 .disable = bnx2fc_disable,
2197};
2198
2199/**
2200 * bnx2fc_percpu_thread_create - Create a receive thread for an
2201 * online CPU
2202 *
2203 * @cpu: cpu index for the online cpu
2204 */
2205static void bnx2fc_percpu_thread_create(unsigned int cpu)
2206{
2207 struct bnx2fc_percpu_s *p;
2208 struct task_struct *thread;
2209
2210 p = &per_cpu(bnx2fc_percpu, cpu);
2211
2212 thread = kthread_create(bnx2fc_percpu_io_thread,
2213 (void *)p,
2214 "bnx2fc_thread/%d", cpu);
2215 /* bind thread to the cpu */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002216 if (likely(!IS_ERR(thread))) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002217 kthread_bind(thread, cpu);
2218 p->iothread = thread;
2219 wake_up_process(thread);
2220 }
2221}
2222
2223static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2224{
2225 struct bnx2fc_percpu_s *p;
2226 struct task_struct *thread;
2227 struct bnx2fc_work *work, *tmp;
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002228
2229 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2230
2231 /* Prevent any new work from being queued for this CPU */
2232 p = &per_cpu(bnx2fc_percpu, cpu);
2233 spin_lock_bh(&p->fp_work_lock);
2234 thread = p->iothread;
2235 p->iothread = NULL;
2236
2237
2238 /* Free all work in the list */
Bhanu Prakash Gollapudi32248762011-08-04 17:38:36 -07002239 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002240 list_del_init(&work->list);
2241 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2242 kfree(work);
2243 }
2244
2245 spin_unlock_bh(&p->fp_work_lock);
2246
2247 if (thread)
2248 kthread_stop(thread);
2249}
2250
2251/**
2252 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2253 *
2254 * @nfb: The callback data block
2255 * @action: The event triggering the callback
2256 * @hcpu: The index of the CPU that the event is for
2257 *
2258 * This creates or destroys per-CPU data for fcoe
2259 *
2260 * Returns NOTIFY_OK always.
2261 */
2262static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2263 unsigned long action, void *hcpu)
2264{
2265 unsigned cpu = (unsigned long)hcpu;
2266
2267 switch (action) {
2268 case CPU_ONLINE:
2269 case CPU_ONLINE_FROZEN:
2270 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2271 bnx2fc_percpu_thread_create(cpu);
2272 break;
2273 case CPU_DEAD:
2274 case CPU_DEAD_FROZEN:
2275 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2276 bnx2fc_percpu_thread_destroy(cpu);
2277 break;
2278 default:
2279 break;
2280 }
2281 return NOTIFY_OK;
2282}
2283
2284/**
2285 * bnx2fc_mod_init - module init entry point
2286 *
2287 * Initialize driver wide global data structures, and register
2288 * with cnic module
2289 **/
2290static int __init bnx2fc_mod_init(void)
2291{
2292 struct fcoe_percpu_s *bg;
2293 struct task_struct *l2_thread;
2294 int rc = 0;
2295 unsigned int cpu = 0;
2296 struct bnx2fc_percpu_s *p;
2297
2298 printk(KERN_INFO PFX "%s", version);
2299
2300 /* register as a fcoe transport */
2301 rc = fcoe_transport_attach(&bnx2fc_transport);
2302 if (rc) {
2303 printk(KERN_ERR "failed to register an fcoe transport, check "
2304 "if libfcoe is loaded\n");
2305 goto out;
2306 }
2307
2308 INIT_LIST_HEAD(&adapter_list);
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002309 INIT_LIST_HEAD(&if_list);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002310 mutex_init(&bnx2fc_dev_lock);
2311 adapter_count = 0;
2312
2313 /* Attach FC transport template */
2314 rc = bnx2fc_attach_transport();
2315 if (rc)
2316 goto detach_ft;
2317
2318 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2319 if (!bnx2fc_wq) {
2320 rc = -ENOMEM;
2321 goto release_bt;
2322 }
2323
2324 bg = &bnx2fc_global;
2325 skb_queue_head_init(&bg->fcoe_rx_list);
2326 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2327 (void *)bg,
2328 "bnx2fc_l2_thread");
2329 if (IS_ERR(l2_thread)) {
2330 rc = PTR_ERR(l2_thread);
2331 goto free_wq;
2332 }
2333 wake_up_process(l2_thread);
2334 spin_lock_bh(&bg->fcoe_rx_list.lock);
2335 bg->thread = l2_thread;
2336 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2337
2338 for_each_possible_cpu(cpu) {
2339 p = &per_cpu(bnx2fc_percpu, cpu);
2340 INIT_LIST_HEAD(&p->work_list);
2341 spin_lock_init(&p->fp_work_lock);
2342 }
2343
2344 for_each_online_cpu(cpu) {
2345 bnx2fc_percpu_thread_create(cpu);
2346 }
2347
2348 /* Initialize per CPU interrupt thread */
2349 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2350
2351 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2352
2353 return 0;
2354
2355free_wq:
2356 destroy_workqueue(bnx2fc_wq);
2357release_bt:
2358 bnx2fc_release_transport();
2359detach_ft:
2360 fcoe_transport_detach(&bnx2fc_transport);
2361out:
2362 return rc;
2363}
2364
2365static void __exit bnx2fc_mod_exit(void)
2366{
2367 LIST_HEAD(to_be_deleted);
2368 struct bnx2fc_hba *hba, *next;
2369 struct fcoe_percpu_s *bg;
2370 struct task_struct *l2_thread;
2371 struct sk_buff *skb;
2372 unsigned int cpu = 0;
2373
2374 /*
2375 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2376 * it will have higher precedence than bnx2fc_dev_lock.
2377 * unregister_device() cannot be called with bnx2fc_dev_lock
2378 * held.
2379 */
2380 mutex_lock(&bnx2fc_dev_lock);
2381 list_splice(&adapter_list, &to_be_deleted);
2382 INIT_LIST_HEAD(&adapter_list);
2383 adapter_count = 0;
2384 mutex_unlock(&bnx2fc_dev_lock);
2385
2386 /* Unregister with cnic */
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002387 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2388 list_del_init(&hba->list);
2389 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2390 hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002391 bnx2fc_ulp_stop(hba);
2392 /* unregister cnic device */
2393 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2394 &hba->reg_with_cnic))
Bhanu Prakash Gollapudiaea71a02011-07-26 14:51:39 -07002395 hba->cnic->unregister_device(hba->cnic,
2396 CNIC_ULP_FCOE);
2397 bnx2fc_hba_destroy(hba);
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002398 }
2399 cnic_unregister_driver(CNIC_ULP_FCOE);
2400
2401 /* Destroy global thread */
2402 bg = &bnx2fc_global;
2403 spin_lock_bh(&bg->fcoe_rx_list.lock);
2404 l2_thread = bg->thread;
2405 bg->thread = NULL;
2406 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2407 kfree_skb(skb);
2408
2409 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2410
2411 if (l2_thread)
2412 kthread_stop(l2_thread);
2413
2414 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2415
2416 /* Destroy per cpu threads */
2417 for_each_online_cpu(cpu) {
2418 bnx2fc_percpu_thread_destroy(cpu);
2419 }
2420
2421 destroy_workqueue(bnx2fc_wq);
2422 /*
2423 * detach from scsi transport
2424 * must happen after all destroys are done
2425 */
2426 bnx2fc_release_transport();
2427
2428 /* detach from fcoe transport */
2429 fcoe_transport_detach(&bnx2fc_transport);
2430}
2431
2432module_init(bnx2fc_mod_init);
2433module_exit(bnx2fc_mod_exit);
2434
2435static struct fc_function_template bnx2fc_transport_function = {
2436 .show_host_node_name = 1,
2437 .show_host_port_name = 1,
2438 .show_host_supported_classes = 1,
2439 .show_host_supported_fc4s = 1,
2440 .show_host_active_fc4s = 1,
2441 .show_host_maxframe_size = 1,
2442
2443 .show_host_port_id = 1,
2444 .show_host_supported_speeds = 1,
2445 .get_host_speed = fc_get_host_speed,
2446 .show_host_speed = 1,
2447 .show_host_port_type = 1,
2448 .get_host_port_state = fc_get_host_port_state,
2449 .show_host_port_state = 1,
2450 .show_host_symbolic_name = 1,
2451
2452 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2453 sizeof(struct bnx2fc_rport)),
2454 .show_rport_maxframe_size = 1,
2455 .show_rport_supported_classes = 1,
2456
2457 .show_host_fabric_name = 1,
2458 .show_starget_node_name = 1,
2459 .show_starget_port_name = 1,
2460 .show_starget_port_id = 1,
2461 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2462 .show_rport_dev_loss_tmo = 1,
2463 .get_fc_host_stats = bnx2fc_get_host_stats,
2464
2465 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2466
2467 .terminate_rport_io = fc_rport_terminate_io,
2468
2469 .vport_create = bnx2fc_vport_create,
2470 .vport_delete = bnx2fc_vport_destroy,
2471 .vport_disable = bnx2fc_vport_disable,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002472 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002473};
2474
2475static struct fc_function_template bnx2fc_vport_xport_function = {
2476 .show_host_node_name = 1,
2477 .show_host_port_name = 1,
2478 .show_host_supported_classes = 1,
2479 .show_host_supported_fc4s = 1,
2480 .show_host_active_fc4s = 1,
2481 .show_host_maxframe_size = 1,
2482
2483 .show_host_port_id = 1,
2484 .show_host_supported_speeds = 1,
2485 .get_host_speed = fc_get_host_speed,
2486 .show_host_speed = 1,
2487 .show_host_port_type = 1,
2488 .get_host_port_state = fc_get_host_port_state,
2489 .show_host_port_state = 1,
2490 .show_host_symbolic_name = 1,
2491
2492 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2493 sizeof(struct bnx2fc_rport)),
2494 .show_rport_maxframe_size = 1,
2495 .show_rport_supported_classes = 1,
2496
2497 .show_host_fabric_name = 1,
2498 .show_starget_node_name = 1,
2499 .show_starget_port_name = 1,
2500 .show_starget_port_id = 1,
2501 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2502 .show_rport_dev_loss_tmo = 1,
2503 .get_fc_host_stats = fc_get_host_stats,
2504 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2505 .terminate_rport_io = fc_rport_terminate_io,
Bhanu Prakash Gollapudie9a5289c2011-08-04 17:38:37 -07002506 .bsg_request = fc_lport_bsg_request,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002507};
2508
2509/**
2510 * scsi_host_template structure used while registering with SCSI-ml
2511 */
2512static struct scsi_host_template bnx2fc_shost_template = {
2513 .module = THIS_MODULE,
2514 .name = "Broadcom Offload FCoE Initiator",
2515 .queuecommand = bnx2fc_queuecommand,
2516 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2517 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2518 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2519 .eh_host_reset_handler = fc_eh_host_reset,
2520 .slave_alloc = fc_slave_alloc,
2521 .change_queue_depth = fc_change_queue_depth,
2522 .change_queue_type = fc_change_queue_type,
2523 .this_id = -1,
2524 .cmd_per_lun = 3,
Bhanu Gollapudi0ea5c272011-03-17 17:13:29 -07002525 .can_queue = BNX2FC_CAN_QUEUE,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002526 .use_clustering = ENABLE_CLUSTERING,
2527 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2528 .max_sectors = 512,
2529};
2530
2531static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2532 .frame_send = bnx2fc_xmit,
2533 .elsct_send = bnx2fc_elsct_send,
2534 .fcp_abort_io = bnx2fc_abort_io,
2535 .fcp_cleanup = bnx2fc_cleanup,
Bhanu Prakash Gollapudif72f6972011-10-03 16:45:02 -07002536 .get_lesb = bnx2fc_get_lesb,
Bhanu Gollapudi853e2bd2011-02-04 12:10:34 -08002537 .rport_event_callback = bnx2fc_rport_event_handler,
2538};
2539
2540/**
2541 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2542 * structure carrying callback function pointers
2543 */
2544static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2545 .owner = THIS_MODULE,
2546 .cnic_init = bnx2fc_ulp_init,
2547 .cnic_exit = bnx2fc_ulp_exit,
2548 .cnic_start = bnx2fc_ulp_start,
2549 .cnic_stop = bnx2fc_ulp_stop,
2550 .indicate_kcqes = bnx2fc_indicate_kcqe,
2551 .indicate_netevent = bnx2fc_indicate_netevent,
2552};