]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/scsi/bnx2fc/bnx2fc_fcoe.c
dccabafc3bb567c0c5fd753257cdd0b41da964be
[mirror_ubuntu-artful-kernel.git] / drivers / scsi / bnx2fc / bnx2fc_fcoe.c
1 /* 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 *
6 * Copyright (c) 2008 - 2011 Broadcom Corporation
7 *
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
17 static struct list_head adapter_list;
18 static struct list_head if_list;
19 static u32 adapter_count;
20 static DEFINE_MUTEX(bnx2fc_dev_lock);
21 DEFINE_PER_CPU(struct bnx2fc_percpu_s, bnx2fc_percpu);
22
23 #define DRV_MODULE_NAME "bnx2fc"
24 #define DRV_MODULE_VERSION BNX2FC_VERSION
25 #define DRV_MODULE_RELDATE "Jun 23, 2011"
26
27
28 static char version[] __devinitdata =
29 "Broadcom NetXtreme II FCoE Driver " DRV_MODULE_NAME \
30 " v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31
32
33 MODULE_AUTHOR("Bhanu Prakash Gollapudi <bprakash@broadcom.com>");
34 MODULE_DESCRIPTION("Broadcom NetXtreme II BCM57710 FCoE Driver");
35 MODULE_LICENSE("GPL");
36 MODULE_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
42 static struct scsi_transport_template *bnx2fc_transport_template;
43 static struct scsi_transport_template *bnx2fc_vport_xport_template;
44
45 struct 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 */
50 struct fcoe_percpu_s bnx2fc_global;
51 DEFINE_SPINLOCK(bnx2fc_global_lock);
52
53 static struct cnic_ulp_ops bnx2fc_cnic_cb;
54 static struct libfc_function_template bnx2fc_libfc_fcn_templ;
55 static struct scsi_host_template bnx2fc_shost_template;
56 static struct fc_function_template bnx2fc_transport_function;
57 static struct fc_function_template bnx2fc_vport_xport_function;
58 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode);
59 static void __bnx2fc_destroy(struct bnx2fc_interface *interface, bool schedule);
60 static int bnx2fc_destroy(struct net_device *net_device);
61 static int bnx2fc_enable(struct net_device *netdev);
62 static int bnx2fc_disable(struct net_device *netdev);
63
64 static void bnx2fc_recv_frame(struct sk_buff *skb);
65
66 static void bnx2fc_start_disc(struct bnx2fc_interface *interface);
67 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev);
68 static int bnx2fc_net_config(struct fc_lport *lp);
69 static int bnx2fc_lport_config(struct fc_lport *lport);
70 static int bnx2fc_em_config(struct fc_lport *lport);
71 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba);
72 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba);
73 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba);
74 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba);
75 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
76 struct device *parent, int npiv);
77 static void bnx2fc_destroy_work(struct work_struct *work);
78
79 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device *phys_dev);
80 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
81 *phys_dev);
82 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface);
83 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic);
84
85 static int bnx2fc_fw_init(struct bnx2fc_hba *hba);
86 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba);
87
88 static void bnx2fc_port_shutdown(struct fc_lport *lport);
89 static void bnx2fc_stop(struct bnx2fc_interface *interface);
90 static int __init bnx2fc_mod_init(void);
91 static void __exit bnx2fc_mod_exit(void);
92
93 unsigned int bnx2fc_debug_level;
94 module_param_named(debug_logging, bnx2fc_debug_level, int, S_IRUGO|S_IWUSR);
95
96 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
97 unsigned long action, void *hcpu);
98 /* notification function for CPU hotplug events */
99 static struct notifier_block bnx2fc_cpu_notifier = {
100 .notifier_call = bnx2fc_cpu_callback,
101 };
102
103 static void bnx2fc_clean_rx_queue(struct fc_lport *lp)
104 {
105 struct fcoe_percpu_s *bg;
106 struct fcoe_rcv_info *fr;
107 struct sk_buff_head *list;
108 struct sk_buff *skb, *next;
109 struct sk_buff *head;
110
111 bg = &bnx2fc_global;
112 spin_lock_bh(&bg->fcoe_rx_list.lock);
113 list = &bg->fcoe_rx_list;
114 head = list->next;
115 for (skb = head; skb != (struct sk_buff *)list;
116 skb = next) {
117 next = skb->next;
118 fr = fcoe_dev_from_skb(skb);
119 if (fr->fr_dev == lp) {
120 __skb_unlink(skb, list);
121 kfree_skb(skb);
122 }
123 }
124 spin_unlock_bh(&bg->fcoe_rx_list.lock);
125 }
126
127 int bnx2fc_get_paged_crc_eof(struct sk_buff *skb, int tlen)
128 {
129 int rc;
130 spin_lock(&bnx2fc_global_lock);
131 rc = fcoe_get_paged_crc_eof(skb, tlen, &bnx2fc_global);
132 spin_unlock(&bnx2fc_global_lock);
133
134 return rc;
135 }
136
137 static void bnx2fc_abort_io(struct fc_lport *lport)
138 {
139 /*
140 * This function is no-op for bnx2fc, but we do
141 * not want to leave it as NULL either, as libfc
142 * can call the default function which is
143 * fc_fcp_abort_io.
144 */
145 }
146
147 static void bnx2fc_cleanup(struct fc_lport *lport)
148 {
149 struct fcoe_port *port = lport_priv(lport);
150 struct bnx2fc_interface *interface = port->priv;
151 struct bnx2fc_hba *hba = interface->hba;
152 struct bnx2fc_rport *tgt;
153 int i;
154
155 BNX2FC_MISC_DBG("Entered %s\n", __func__);
156 mutex_lock(&hba->hba_mutex);
157 spin_lock_bh(&hba->hba_lock);
158 for (i = 0; i < BNX2FC_NUM_MAX_SESS; i++) {
159 tgt = hba->tgt_ofld_list[i];
160 if (tgt) {
161 /* Cleanup IOs belonging to requested vport */
162 if (tgt->port == port) {
163 spin_unlock_bh(&hba->hba_lock);
164 BNX2FC_TGT_DBG(tgt, "flush/cleanup\n");
165 bnx2fc_flush_active_ios(tgt);
166 spin_lock_bh(&hba->hba_lock);
167 }
168 }
169 }
170 spin_unlock_bh(&hba->hba_lock);
171 mutex_unlock(&hba->hba_mutex);
172 }
173
174 static int bnx2fc_xmit_l2_frame(struct bnx2fc_rport *tgt,
175 struct fc_frame *fp)
176 {
177 struct fc_rport_priv *rdata = tgt->rdata;
178 struct fc_frame_header *fh;
179 int rc = 0;
180
181 fh = fc_frame_header_get(fp);
182 BNX2FC_TGT_DBG(tgt, "Xmit L2 frame rport = 0x%x, oxid = 0x%x, "
183 "r_ctl = 0x%x\n", rdata->ids.port_id,
184 ntohs(fh->fh_ox_id), fh->fh_r_ctl);
185 if ((fh->fh_type == FC_TYPE_ELS) &&
186 (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
187
188 switch (fc_frame_payload_op(fp)) {
189 case ELS_ADISC:
190 rc = bnx2fc_send_adisc(tgt, fp);
191 break;
192 case ELS_LOGO:
193 rc = bnx2fc_send_logo(tgt, fp);
194 break;
195 case ELS_RLS:
196 rc = bnx2fc_send_rls(tgt, fp);
197 break;
198 default:
199 break;
200 }
201 } else if ((fh->fh_type == FC_TYPE_BLS) &&
202 (fh->fh_r_ctl == FC_RCTL_BA_ABTS))
203 BNX2FC_TGT_DBG(tgt, "ABTS frame\n");
204 else {
205 BNX2FC_TGT_DBG(tgt, "Send L2 frame type 0x%x "
206 "rctl 0x%x thru non-offload path\n",
207 fh->fh_type, fh->fh_r_ctl);
208 return -ENODEV;
209 }
210 if (rc)
211 return -ENOMEM;
212 else
213 return 0;
214 }
215
216 /**
217 * bnx2fc_xmit - bnx2fc's FCoE frame transmit function
218 *
219 * @lport: the associated local port
220 * @fp: the fc_frame to be transmitted
221 */
222 static int bnx2fc_xmit(struct fc_lport *lport, struct fc_frame *fp)
223 {
224 struct ethhdr *eh;
225 struct fcoe_crc_eof *cp;
226 struct sk_buff *skb;
227 struct fc_frame_header *fh;
228 struct bnx2fc_interface *interface;
229 struct bnx2fc_hba *hba;
230 struct fcoe_port *port;
231 struct fcoe_hdr *hp;
232 struct bnx2fc_rport *tgt;
233 struct fcoe_dev_stats *stats;
234 u8 sof, eof;
235 u32 crc;
236 unsigned int hlen, tlen, elen;
237 int wlen, rc = 0;
238
239 port = (struct fcoe_port *)lport_priv(lport);
240 interface = port->priv;
241 hba = interface->hba;
242
243 fh = fc_frame_header_get(fp);
244
245 skb = fp_skb(fp);
246 if (!lport->link_up) {
247 BNX2FC_HBA_DBG(lport, "bnx2fc_xmit link down\n");
248 kfree_skb(skb);
249 return 0;
250 }
251
252 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
253 if (!interface->ctlr.sel_fcf) {
254 BNX2FC_HBA_DBG(lport, "FCF not selected yet!\n");
255 kfree_skb(skb);
256 return -EINVAL;
257 }
258 if (fcoe_ctlr_els_send(&interface->ctlr, lport, skb))
259 return 0;
260 }
261
262 sof = fr_sof(fp);
263 eof = fr_eof(fp);
264
265 /*
266 * Snoop the frame header to check if the frame is for
267 * an offloaded session
268 */
269 /*
270 * tgt_ofld_list access is synchronized using
271 * both hba mutex and hba lock. Atleast hba mutex or
272 * hba lock needs to be held for read access.
273 */
274
275 spin_lock_bh(&hba->hba_lock);
276 tgt = bnx2fc_tgt_lookup(port, ntoh24(fh->fh_d_id));
277 if (tgt && (test_bit(BNX2FC_FLAG_SESSION_READY, &tgt->flags))) {
278 /* This frame is for offloaded session */
279 BNX2FC_HBA_DBG(lport, "xmit: Frame is for offloaded session "
280 "port_id = 0x%x\n", ntoh24(fh->fh_d_id));
281 spin_unlock_bh(&hba->hba_lock);
282 rc = bnx2fc_xmit_l2_frame(tgt, fp);
283 if (rc != -ENODEV) {
284 kfree_skb(skb);
285 return rc;
286 }
287 } else {
288 spin_unlock_bh(&hba->hba_lock);
289 }
290
291 elen = sizeof(struct ethhdr);
292 hlen = sizeof(struct fcoe_hdr);
293 tlen = sizeof(struct fcoe_crc_eof);
294 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
295
296 skb->ip_summed = CHECKSUM_NONE;
297 crc = fcoe_fc_crc(fp);
298
299 /* copy port crc and eof to the skb buff */
300 if (skb_is_nonlinear(skb)) {
301 skb_frag_t *frag;
302 if (bnx2fc_get_paged_crc_eof(skb, tlen)) {
303 kfree_skb(skb);
304 return -ENOMEM;
305 }
306 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
307 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
308 + frag->page_offset;
309 } else {
310 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
311 }
312
313 memset(cp, 0, sizeof(*cp));
314 cp->fcoe_eof = eof;
315 cp->fcoe_crc32 = cpu_to_le32(~crc);
316 if (skb_is_nonlinear(skb)) {
317 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
318 cp = NULL;
319 }
320
321 /* adjust skb network/transport offsets to match mac/fcoe/port */
322 skb_push(skb, elen + hlen);
323 skb_reset_mac_header(skb);
324 skb_reset_network_header(skb);
325 skb->mac_len = elen;
326 skb->protocol = htons(ETH_P_FCOE);
327 skb->dev = interface->netdev;
328
329 /* fill up mac and fcoe headers */
330 eh = eth_hdr(skb);
331 eh->h_proto = htons(ETH_P_FCOE);
332 if (interface->ctlr.map_dest)
333 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
334 else
335 /* insert GW address */
336 memcpy(eh->h_dest, interface->ctlr.dest_addr, ETH_ALEN);
337
338 if (unlikely(interface->ctlr.flogi_oxid != FC_XID_UNKNOWN))
339 memcpy(eh->h_source, interface->ctlr.ctl_src_addr, ETH_ALEN);
340 else
341 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
342
343 hp = (struct fcoe_hdr *)(eh + 1);
344 memset(hp, 0, sizeof(*hp));
345 if (FC_FCOE_VER)
346 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
347 hp->fcoe_sof = sof;
348
349 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
350 if (lport->seq_offload && fr_max_payload(fp)) {
351 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
352 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
353 } else {
354 skb_shinfo(skb)->gso_type = 0;
355 skb_shinfo(skb)->gso_size = 0;
356 }
357
358 /*update tx stats */
359 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
360 stats->TxFrames++;
361 stats->TxWords += wlen;
362 put_cpu();
363
364 /* send down to lld */
365 fr_dev(fp) = lport;
366 if (port->fcoe_pending_queue.qlen)
367 fcoe_check_wait_queue(lport, skb);
368 else if (fcoe_start_io(skb))
369 fcoe_check_wait_queue(lport, skb);
370
371 return 0;
372 }
373
374 /**
375 * bnx2fc_rcv - This is bnx2fc's receive function called by NET_RX_SOFTIRQ
376 *
377 * @skb: the receive socket buffer
378 * @dev: associated net device
379 * @ptype: context
380 * @olddev: last device
381 *
382 * This function receives the packet and builds FC frame and passes it up
383 */
384 static int bnx2fc_rcv(struct sk_buff *skb, struct net_device *dev,
385 struct packet_type *ptype, struct net_device *olddev)
386 {
387 struct fc_lport *lport;
388 struct bnx2fc_interface *interface;
389 struct fc_frame_header *fh;
390 struct fcoe_rcv_info *fr;
391 struct fcoe_percpu_s *bg;
392 unsigned short oxid;
393
394 interface = container_of(ptype, struct bnx2fc_interface,
395 fcoe_packet_type);
396 lport = interface->ctlr.lp;
397
398 if (unlikely(lport == NULL)) {
399 printk(KERN_ERR PFX "bnx2fc_rcv: lport is NULL\n");
400 goto err;
401 }
402
403 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
404 printk(KERN_ERR PFX "bnx2fc_rcv: Wrong FC type frame\n");
405 goto err;
406 }
407
408 /*
409 * Check for minimum frame length, and make sure required FCoE
410 * and FC headers are pulled into the linear data area.
411 */
412 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
413 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
414 goto err;
415
416 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
417 fh = (struct fc_frame_header *) skb_transport_header(skb);
418
419 oxid = ntohs(fh->fh_ox_id);
420
421 fr = fcoe_dev_from_skb(skb);
422 fr->fr_dev = lport;
423
424 bg = &bnx2fc_global;
425 spin_lock_bh(&bg->fcoe_rx_list.lock);
426
427 __skb_queue_tail(&bg->fcoe_rx_list, skb);
428 if (bg->fcoe_rx_list.qlen == 1)
429 wake_up_process(bg->thread);
430
431 spin_unlock_bh(&bg->fcoe_rx_list.lock);
432
433 return 0;
434 err:
435 kfree_skb(skb);
436 return -1;
437 }
438
439 static int bnx2fc_l2_rcv_thread(void *arg)
440 {
441 struct fcoe_percpu_s *bg = arg;
442 struct sk_buff *skb;
443
444 set_user_nice(current, -20);
445 set_current_state(TASK_INTERRUPTIBLE);
446 while (!kthread_should_stop()) {
447 schedule();
448 spin_lock_bh(&bg->fcoe_rx_list.lock);
449 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL) {
450 spin_unlock_bh(&bg->fcoe_rx_list.lock);
451 bnx2fc_recv_frame(skb);
452 spin_lock_bh(&bg->fcoe_rx_list.lock);
453 }
454 __set_current_state(TASK_INTERRUPTIBLE);
455 spin_unlock_bh(&bg->fcoe_rx_list.lock);
456 }
457 __set_current_state(TASK_RUNNING);
458 return 0;
459 }
460
461
462 static void bnx2fc_recv_frame(struct sk_buff *skb)
463 {
464 u32 fr_len;
465 struct fc_lport *lport;
466 struct fcoe_rcv_info *fr;
467 struct fcoe_dev_stats *stats;
468 struct fc_frame_header *fh;
469 struct fcoe_crc_eof crc_eof;
470 struct fc_frame *fp;
471 struct fc_lport *vn_port;
472 struct fcoe_port *port;
473 u8 *mac = NULL;
474 u8 *dest_mac = NULL;
475 struct fcoe_hdr *hp;
476
477 fr = fcoe_dev_from_skb(skb);
478 lport = fr->fr_dev;
479 if (unlikely(lport == NULL)) {
480 printk(KERN_ERR PFX "Invalid lport struct\n");
481 kfree_skb(skb);
482 return;
483 }
484
485 if (skb_is_nonlinear(skb))
486 skb_linearize(skb);
487 mac = eth_hdr(skb)->h_source;
488 dest_mac = eth_hdr(skb)->h_dest;
489
490 /* Pull the header */
491 hp = (struct fcoe_hdr *) skb_network_header(skb);
492 fh = (struct fc_frame_header *) skb_transport_header(skb);
493 skb_pull(skb, sizeof(struct fcoe_hdr));
494 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
495
496 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
497 stats->RxFrames++;
498 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
499
500 fp = (struct fc_frame *)skb;
501 fc_frame_init(fp);
502 fr_dev(fp) = lport;
503 fr_sof(fp) = hp->fcoe_sof;
504 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
505 put_cpu();
506 kfree_skb(skb);
507 return;
508 }
509 fr_eof(fp) = crc_eof.fcoe_eof;
510 fr_crc(fp) = crc_eof.fcoe_crc32;
511 if (pskb_trim(skb, fr_len)) {
512 put_cpu();
513 kfree_skb(skb);
514 return;
515 }
516
517 fh = fc_frame_header_get(fp);
518
519 vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
520 if (vn_port) {
521 port = lport_priv(vn_port);
522 if (compare_ether_addr(port->data_src_addr, dest_mac)
523 != 0) {
524 BNX2FC_HBA_DBG(lport, "fpma mismatch\n");
525 put_cpu();
526 kfree_skb(skb);
527 return;
528 }
529 }
530 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
531 fh->fh_type == FC_TYPE_FCP) {
532 /* Drop FCP data. We dont this in L2 path */
533 put_cpu();
534 kfree_skb(skb);
535 return;
536 }
537 if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
538 fh->fh_type == FC_TYPE_ELS) {
539 switch (fc_frame_payload_op(fp)) {
540 case ELS_LOGO:
541 if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
542 /* drop non-FIP LOGO */
543 put_cpu();
544 kfree_skb(skb);
545 return;
546 }
547 break;
548 }
549 }
550 if (le32_to_cpu(fr_crc(fp)) !=
551 ~crc32(~0, skb->data, fr_len)) {
552 if (stats->InvalidCRCCount < 5)
553 printk(KERN_WARNING PFX "dropping frame with "
554 "CRC error\n");
555 stats->InvalidCRCCount++;
556 put_cpu();
557 kfree_skb(skb);
558 return;
559 }
560 put_cpu();
561 fc_exch_recv(lport, fp);
562 }
563
564 /**
565 * bnx2fc_percpu_io_thread - thread per cpu for ios
566 *
567 * @arg: ptr to bnx2fc_percpu_info structure
568 */
569 int bnx2fc_percpu_io_thread(void *arg)
570 {
571 struct bnx2fc_percpu_s *p = arg;
572 struct bnx2fc_work *work, *tmp;
573 LIST_HEAD(work_list);
574
575 set_user_nice(current, -20);
576 set_current_state(TASK_INTERRUPTIBLE);
577 while (!kthread_should_stop()) {
578 schedule();
579 spin_lock_bh(&p->fp_work_lock);
580 while (!list_empty(&p->work_list)) {
581 list_splice_init(&p->work_list, &work_list);
582 spin_unlock_bh(&p->fp_work_lock);
583
584 list_for_each_entry_safe(work, tmp, &work_list, list) {
585 list_del_init(&work->list);
586 bnx2fc_process_cq_compl(work->tgt, work->wqe);
587 kfree(work);
588 }
589
590 spin_lock_bh(&p->fp_work_lock);
591 }
592 __set_current_state(TASK_INTERRUPTIBLE);
593 spin_unlock_bh(&p->fp_work_lock);
594 }
595 __set_current_state(TASK_RUNNING);
596
597 return 0;
598 }
599
600 static struct fc_host_statistics *bnx2fc_get_host_stats(struct Scsi_Host *shost)
601 {
602 struct fc_host_statistics *bnx2fc_stats;
603 struct fc_lport *lport = shost_priv(shost);
604 struct fcoe_port *port = lport_priv(lport);
605 struct bnx2fc_interface *interface = port->priv;
606 struct bnx2fc_hba *hba = interface->hba;
607 struct fcoe_statistics_params *fw_stats;
608 int rc = 0;
609
610 fw_stats = (struct fcoe_statistics_params *)hba->stats_buffer;
611 if (!fw_stats)
612 return NULL;
613
614 bnx2fc_stats = fc_get_host_stats(shost);
615
616 init_completion(&hba->stat_req_done);
617 if (bnx2fc_send_stat_req(hba))
618 return bnx2fc_stats;
619 rc = wait_for_completion_timeout(&hba->stat_req_done, (2 * HZ));
620 if (!rc) {
621 BNX2FC_HBA_DBG(lport, "FW stat req timed out\n");
622 return bnx2fc_stats;
623 }
624 bnx2fc_stats->invalid_crc_count += fw_stats->rx_stat2.fc_crc_cnt;
625 bnx2fc_stats->tx_frames += fw_stats->tx_stat.fcoe_tx_pkt_cnt;
626 bnx2fc_stats->tx_words += (fw_stats->tx_stat.fcoe_tx_byte_cnt) / 4;
627 bnx2fc_stats->rx_frames += fw_stats->rx_stat0.fcoe_rx_pkt_cnt;
628 bnx2fc_stats->rx_words += (fw_stats->rx_stat0.fcoe_rx_byte_cnt) / 4;
629
630 bnx2fc_stats->dumped_frames = 0;
631 bnx2fc_stats->lip_count = 0;
632 bnx2fc_stats->nos_count = 0;
633 bnx2fc_stats->loss_of_sync_count = 0;
634 bnx2fc_stats->loss_of_signal_count = 0;
635 bnx2fc_stats->prim_seq_protocol_err_count = 0;
636
637 return bnx2fc_stats;
638 }
639
640 static int bnx2fc_shost_config(struct fc_lport *lport, struct device *dev)
641 {
642 struct fcoe_port *port = lport_priv(lport);
643 struct bnx2fc_interface *interface = port->priv;
644 struct Scsi_Host *shost = lport->host;
645 int rc = 0;
646
647 shost->max_cmd_len = BNX2FC_MAX_CMD_LEN;
648 shost->max_lun = BNX2FC_MAX_LUN;
649 shost->max_id = BNX2FC_MAX_FCP_TGT;
650 shost->max_channel = 0;
651 if (lport->vport)
652 shost->transportt = bnx2fc_vport_xport_template;
653 else
654 shost->transportt = bnx2fc_transport_template;
655
656 /* Add the new host to SCSI-ml */
657 rc = scsi_add_host(lport->host, dev);
658 if (rc) {
659 printk(KERN_ERR PFX "Error on scsi_add_host\n");
660 return rc;
661 }
662 if (!lport->vport)
663 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
664 sprintf(fc_host_symbolic_name(lport->host), "%s v%s over %s",
665 BNX2FC_NAME, BNX2FC_VERSION,
666 interface->netdev->name);
667
668 return 0;
669 }
670
671 static void bnx2fc_link_speed_update(struct fc_lport *lport)
672 {
673 struct fcoe_port *port = lport_priv(lport);
674 struct bnx2fc_interface *interface = port->priv;
675 struct net_device *netdev = interface->netdev;
676 struct ethtool_cmd ecmd;
677
678 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
679 lport->link_supported_speeds &=
680 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
681 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
682 SUPPORTED_1000baseT_Full))
683 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
684 if (ecmd.supported & SUPPORTED_10000baseT_Full)
685 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
686
687 switch (ethtool_cmd_speed(&ecmd)) {
688 case SPEED_1000:
689 lport->link_speed = FC_PORTSPEED_1GBIT;
690 break;
691 case SPEED_2500:
692 lport->link_speed = FC_PORTSPEED_2GBIT;
693 break;
694 case SPEED_10000:
695 lport->link_speed = FC_PORTSPEED_10GBIT;
696 break;
697 }
698 }
699 }
700 static int bnx2fc_link_ok(struct fc_lport *lport)
701 {
702 struct fcoe_port *port = lport_priv(lport);
703 struct bnx2fc_interface *interface = port->priv;
704 struct bnx2fc_hba *hba = interface->hba;
705 struct net_device *dev = hba->phys_dev;
706 int rc = 0;
707
708 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev))
709 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
710 else {
711 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
712 rc = -1;
713 }
714 return rc;
715 }
716
717 /**
718 * bnx2fc_get_link_state - get network link state
719 *
720 * @hba: adapter instance pointer
721 *
722 * updates adapter structure flag based on netdev state
723 */
724 void bnx2fc_get_link_state(struct bnx2fc_hba *hba)
725 {
726 if (test_bit(__LINK_STATE_NOCARRIER, &hba->phys_dev->state))
727 set_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
728 else
729 clear_bit(ADAPTER_STATE_LINK_DOWN, &hba->adapter_state);
730 }
731
732 static int bnx2fc_net_config(struct fc_lport *lport)
733 {
734 struct bnx2fc_hba *hba;
735 struct bnx2fc_interface *interface;
736 struct fcoe_port *port;
737 u64 wwnn, wwpn;
738
739 port = lport_priv(lport);
740 interface = port->priv;
741 hba = interface->hba;
742
743 /* require support for get_pauseparam ethtool op. */
744 if (!hba->phys_dev->ethtool_ops ||
745 !hba->phys_dev->ethtool_ops->get_pauseparam)
746 return -EOPNOTSUPP;
747
748 if (fc_set_mfs(lport, BNX2FC_MFS))
749 return -EINVAL;
750
751 skb_queue_head_init(&port->fcoe_pending_queue);
752 port->fcoe_pending_queue_active = 0;
753 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long) lport);
754
755 bnx2fc_link_speed_update(lport);
756
757 if (!lport->vport) {
758 wwnn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr, 1, 0);
759 BNX2FC_HBA_DBG(lport, "WWNN = 0x%llx\n", wwnn);
760 fc_set_wwnn(lport, wwnn);
761
762 wwpn = fcoe_wwn_from_mac(interface->ctlr.ctl_src_addr, 2, 0);
763 BNX2FC_HBA_DBG(lport, "WWPN = 0x%llx\n", wwpn);
764 fc_set_wwpn(lport, wwpn);
765 }
766
767 return 0;
768 }
769
770 static void bnx2fc_destroy_timer(unsigned long data)
771 {
772 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)data;
773
774 BNX2FC_MISC_DBG("ERROR:bnx2fc_destroy_timer - "
775 "Destroy compl not received!!\n");
776 set_bit(BNX2FC_FLAG_DESTROY_CMPL, &hba->flags);
777 wake_up_interruptible(&hba->destroy_wait);
778 }
779
780 /**
781 * bnx2fc_indicate_netevent - Generic netdev event handler
782 *
783 * @context: adapter structure pointer
784 * @event: event type
785 * @vlan_id: vlan id - associated vlan id with this event
786 *
787 * Handles NETDEV_UP, NETDEV_DOWN, NETDEV_GOING_DOWN,NETDEV_CHANGE and
788 * NETDEV_CHANGE_MTU events. Handle NETDEV_UNREGISTER only for vlans.
789 */
790 static void bnx2fc_indicate_netevent(void *context, unsigned long event,
791 u16 vlan_id)
792 {
793 struct bnx2fc_hba *hba = (struct bnx2fc_hba *)context;
794 struct fc_lport *lport;
795 struct fc_lport *vport;
796 struct bnx2fc_interface *interface, *tmp;
797 int wait_for_upload = 0;
798 u32 link_possible = 1;
799
800 if (vlan_id != 0 && event != NETDEV_UNREGISTER)
801 return;
802
803 switch (event) {
804 case NETDEV_UP:
805 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state))
806 printk(KERN_ERR "indicate_netevent: "\
807 "hba is not UP!!\n");
808 break;
809
810 case NETDEV_DOWN:
811 clear_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
812 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
813 link_possible = 0;
814 break;
815
816 case NETDEV_GOING_DOWN:
817 set_bit(ADAPTER_STATE_GOING_DOWN, &hba->adapter_state);
818 link_possible = 0;
819 break;
820
821 case NETDEV_CHANGE:
822 break;
823
824 case NETDEV_UNREGISTER:
825 if (!vlan_id)
826 return;
827 mutex_lock(&bnx2fc_dev_lock);
828 list_for_each_entry_safe(interface, tmp, &if_list, list) {
829 if (interface->hba != hba)
830 continue;
831 __bnx2fc_destroy(interface, true);
832 }
833 mutex_unlock(&bnx2fc_dev_lock);
834 return;
835
836 default:
837 printk(KERN_ERR PFX "Unkonwn netevent %ld", event);
838 return;
839 }
840
841 mutex_lock(&bnx2fc_dev_lock);
842 list_for_each_entry(interface, &if_list, list) {
843
844 if (interface->hba != hba)
845 continue;
846
847 lport = interface->ctlr.lp;
848 BNX2FC_HBA_DBG(lport, "netevent handler - event=%s %ld\n",
849 interface->netdev->name, event);
850
851 bnx2fc_link_speed_update(lport);
852
853 if (link_possible && !bnx2fc_link_ok(lport)) {
854 printk(KERN_ERR "indicate_netevent: ctlr_link_up\n");
855 fcoe_ctlr_link_up(&interface->ctlr);
856 } else if (fcoe_ctlr_link_down(&interface->ctlr)) {
857 mutex_lock(&lport->lp_mutex);
858 list_for_each_entry(vport, &lport->vports, list)
859 fc_host_port_type(vport->host) =
860 FC_PORTTYPE_UNKNOWN;
861 mutex_unlock(&lport->lp_mutex);
862 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
863 per_cpu_ptr(lport->dev_stats,
864 get_cpu())->LinkFailureCount++;
865 put_cpu();
866 fcoe_clean_pending_queue(lport);
867 wait_for_upload = 1;
868 }
869 }
870 mutex_unlock(&bnx2fc_dev_lock);
871
872 if (wait_for_upload) {
873 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
874 init_waitqueue_head(&hba->shutdown_wait);
875 BNX2FC_MISC_DBG("indicate_netevent "
876 "num_ofld_sess = %d\n",
877 hba->num_ofld_sess);
878 hba->wait_for_link_down = 1;
879 wait_event_interruptible(hba->shutdown_wait,
880 (hba->num_ofld_sess == 0));
881 BNX2FC_MISC_DBG("wakeup - num_ofld_sess = %d\n",
882 hba->num_ofld_sess);
883 hba->wait_for_link_down = 0;
884
885 if (signal_pending(current))
886 flush_signals(current);
887 }
888 }
889
890 static int bnx2fc_libfc_config(struct fc_lport *lport)
891 {
892
893 /* Set the function pointers set by bnx2fc driver */
894 memcpy(&lport->tt, &bnx2fc_libfc_fcn_templ,
895 sizeof(struct libfc_function_template));
896 fc_elsct_init(lport);
897 fc_exch_init(lport);
898 fc_rport_init(lport);
899 fc_disc_init(lport);
900 return 0;
901 }
902
903 static int bnx2fc_em_config(struct fc_lport *lport)
904 {
905 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, FCOE_MIN_XID,
906 FCOE_MAX_XID, NULL)) {
907 printk(KERN_ERR PFX "em_config:fc_exch_mgr_alloc failed\n");
908 return -ENOMEM;
909 }
910
911 return 0;
912 }
913
914 static int bnx2fc_lport_config(struct fc_lport *lport)
915 {
916 lport->link_up = 0;
917 lport->qfull = 0;
918 lport->max_retry_count = 3;
919 lport->max_rport_retry_count = 3;
920 lport->e_d_tov = 2 * 1000;
921 lport->r_a_tov = 10 * 1000;
922
923 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
924 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
925 lport->does_npiv = 1;
926
927 memset(&lport->rnid_gen, 0, sizeof(struct fc_els_rnid_gen));
928 lport->rnid_gen.rnid_atype = BNX2FC_RNID_HBA;
929
930 /* alloc stats structure */
931 if (fc_lport_init_stats(lport))
932 return -ENOMEM;
933
934 /* Finish fc_lport configuration */
935 fc_lport_config(lport);
936
937 return 0;
938 }
939
940 /**
941 * bnx2fc_fip_recv - handle a received FIP frame.
942 *
943 * @skb: the received skb
944 * @dev: associated &net_device
945 * @ptype: the &packet_type structure which was used to register this handler.
946 * @orig_dev: original receive &net_device, in case @ dev is a bond.
947 *
948 * Returns: 0 for success
949 */
950 static int bnx2fc_fip_recv(struct sk_buff *skb, struct net_device *dev,
951 struct packet_type *ptype,
952 struct net_device *orig_dev)
953 {
954 struct bnx2fc_interface *interface;
955 interface = container_of(ptype, struct bnx2fc_interface,
956 fip_packet_type);
957 fcoe_ctlr_recv(&interface->ctlr, skb);
958 return 0;
959 }
960
961 /**
962 * bnx2fc_update_src_mac - Update Ethernet MAC filters.
963 *
964 * @fip: FCoE controller.
965 * @old: Unicast MAC address to delete if the MAC is non-zero.
966 * @new: Unicast MAC address to add.
967 *
968 * Remove any previously-set unicast MAC filter.
969 * Add secondary FCoE MAC address filter for our OUI.
970 */
971 static void bnx2fc_update_src_mac(struct fc_lport *lport, u8 *addr)
972 {
973 struct fcoe_port *port = lport_priv(lport);
974
975 memcpy(port->data_src_addr, addr, ETH_ALEN);
976 }
977
978 /**
979 * bnx2fc_get_src_mac - return the ethernet source address for an lport
980 *
981 * @lport: libfc port
982 */
983 static u8 *bnx2fc_get_src_mac(struct fc_lport *lport)
984 {
985 struct fcoe_port *port;
986
987 port = (struct fcoe_port *)lport_priv(lport);
988 return port->data_src_addr;
989 }
990
991 /**
992 * bnx2fc_fip_send - send an Ethernet-encapsulated FIP frame.
993 *
994 * @fip: FCoE controller.
995 * @skb: FIP Packet.
996 */
997 static void bnx2fc_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
998 {
999 skb->dev = bnx2fc_from_ctlr(fip)->netdev;
1000 dev_queue_xmit(skb);
1001 }
1002
1003 static int bnx2fc_vport_create(struct fc_vport *vport, bool disabled)
1004 {
1005 struct Scsi_Host *shost = vport_to_shost(vport);
1006 struct fc_lport *n_port = shost_priv(shost);
1007 struct fcoe_port *port = lport_priv(n_port);
1008 struct bnx2fc_interface *interface = port->priv;
1009 struct net_device *netdev = interface->netdev;
1010 struct fc_lport *vn_port;
1011
1012 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1013 printk(KERN_ERR PFX "vn ports cannot be created on"
1014 "this interface\n");
1015 return -EIO;
1016 }
1017 mutex_lock(&bnx2fc_dev_lock);
1018 vn_port = bnx2fc_if_create(interface, &vport->dev, 1);
1019 mutex_unlock(&bnx2fc_dev_lock);
1020
1021 if (IS_ERR(vn_port)) {
1022 printk(KERN_ERR PFX "bnx2fc_vport_create (%s) failed\n",
1023 netdev->name);
1024 return -EIO;
1025 }
1026
1027 if (disabled) {
1028 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1029 } else {
1030 vn_port->boot_time = jiffies;
1031 fc_lport_init(vn_port);
1032 fc_fabric_login(vn_port);
1033 fc_vport_setlink(vn_port);
1034 }
1035 return 0;
1036 }
1037
1038 static void bnx2fc_free_vport(struct bnx2fc_hba *hba, struct fc_lport *lport)
1039 {
1040 struct bnx2fc_lport *blport, *tmp;
1041
1042 spin_lock_bh(&hba->hba_lock);
1043 list_for_each_entry_safe(blport, tmp, &hba->vports, list) {
1044 if (blport->lport == lport) {
1045 list_del(&blport->list);
1046 kfree(blport);
1047 }
1048 }
1049 spin_unlock_bh(&hba->hba_lock);
1050 }
1051
1052 static int bnx2fc_vport_destroy(struct fc_vport *vport)
1053 {
1054 struct Scsi_Host *shost = vport_to_shost(vport);
1055 struct fc_lport *n_port = shost_priv(shost);
1056 struct fc_lport *vn_port = vport->dd_data;
1057 struct fcoe_port *port = lport_priv(vn_port);
1058 struct bnx2fc_interface *interface = port->priv;
1059 struct fc_lport *v_port;
1060 bool found = false;
1061
1062 mutex_lock(&n_port->lp_mutex);
1063 list_for_each_entry(v_port, &n_port->vports, list)
1064 if (v_port->vport == vport) {
1065 found = true;
1066 break;
1067 }
1068
1069 if (!found) {
1070 mutex_unlock(&n_port->lp_mutex);
1071 return -ENOENT;
1072 }
1073 list_del(&vn_port->list);
1074 mutex_unlock(&n_port->lp_mutex);
1075 bnx2fc_free_vport(interface->hba, port->lport);
1076 bnx2fc_port_shutdown(port->lport);
1077 bnx2fc_interface_put(interface);
1078 queue_work(bnx2fc_wq, &port->destroy_work);
1079 return 0;
1080 }
1081
1082 static int bnx2fc_vport_disable(struct fc_vport *vport, bool disable)
1083 {
1084 struct fc_lport *lport = vport->dd_data;
1085
1086 if (disable) {
1087 fc_vport_set_state(vport, FC_VPORT_DISABLED);
1088 fc_fabric_logoff(lport);
1089 } else {
1090 lport->boot_time = jiffies;
1091 fc_fabric_login(lport);
1092 fc_vport_setlink(lport);
1093 }
1094 return 0;
1095 }
1096
1097
1098 static int bnx2fc_interface_setup(struct bnx2fc_interface *interface)
1099 {
1100 struct net_device *netdev = interface->netdev;
1101 struct net_device *physdev = interface->hba->phys_dev;
1102 struct netdev_hw_addr *ha;
1103 int sel_san_mac = 0;
1104
1105 /* setup Source MAC Address */
1106 rcu_read_lock();
1107 for_each_dev_addr(physdev, ha) {
1108 BNX2FC_MISC_DBG("net_config: ha->type = %d, fip_mac = ",
1109 ha->type);
1110 printk(KERN_INFO "%2x:%2x:%2x:%2x:%2x:%2x\n", ha->addr[0],
1111 ha->addr[1], ha->addr[2], ha->addr[3],
1112 ha->addr[4], ha->addr[5]);
1113
1114 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
1115 (is_valid_ether_addr(ha->addr))) {
1116 memcpy(interface->ctlr.ctl_src_addr, ha->addr,
1117 ETH_ALEN);
1118 sel_san_mac = 1;
1119 BNX2FC_MISC_DBG("Found SAN MAC\n");
1120 }
1121 }
1122 rcu_read_unlock();
1123
1124 if (!sel_san_mac)
1125 return -ENODEV;
1126
1127 interface->fip_packet_type.func = bnx2fc_fip_recv;
1128 interface->fip_packet_type.type = htons(ETH_P_FIP);
1129 interface->fip_packet_type.dev = netdev;
1130 dev_add_pack(&interface->fip_packet_type);
1131
1132 interface->fcoe_packet_type.func = bnx2fc_rcv;
1133 interface->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
1134 interface->fcoe_packet_type.dev = netdev;
1135 dev_add_pack(&interface->fcoe_packet_type);
1136
1137 return 0;
1138 }
1139
1140 static int bnx2fc_attach_transport(void)
1141 {
1142 bnx2fc_transport_template =
1143 fc_attach_transport(&bnx2fc_transport_function);
1144
1145 if (bnx2fc_transport_template == NULL) {
1146 printk(KERN_ERR PFX "Failed to attach FC transport\n");
1147 return -ENODEV;
1148 }
1149
1150 bnx2fc_vport_xport_template =
1151 fc_attach_transport(&bnx2fc_vport_xport_function);
1152 if (bnx2fc_vport_xport_template == NULL) {
1153 printk(KERN_ERR PFX
1154 "Failed to attach FC transport for vport\n");
1155 fc_release_transport(bnx2fc_transport_template);
1156 bnx2fc_transport_template = NULL;
1157 return -ENODEV;
1158 }
1159 return 0;
1160 }
1161 static void bnx2fc_release_transport(void)
1162 {
1163 fc_release_transport(bnx2fc_transport_template);
1164 fc_release_transport(bnx2fc_vport_xport_template);
1165 bnx2fc_transport_template = NULL;
1166 bnx2fc_vport_xport_template = NULL;
1167 }
1168
1169 static void bnx2fc_interface_release(struct kref *kref)
1170 {
1171 struct bnx2fc_interface *interface;
1172 struct net_device *netdev;
1173
1174 interface = container_of(kref, struct bnx2fc_interface, kref);
1175 BNX2FC_MISC_DBG("Interface is being released\n");
1176
1177 netdev = interface->netdev;
1178
1179 /* tear-down FIP controller */
1180 if (test_and_clear_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags))
1181 fcoe_ctlr_destroy(&interface->ctlr);
1182
1183 kfree(interface);
1184
1185 dev_put(netdev);
1186 module_put(THIS_MODULE);
1187 }
1188
1189 static inline void bnx2fc_interface_get(struct bnx2fc_interface *interface)
1190 {
1191 kref_get(&interface->kref);
1192 }
1193
1194 static inline void bnx2fc_interface_put(struct bnx2fc_interface *interface)
1195 {
1196 kref_put(&interface->kref, bnx2fc_interface_release);
1197 }
1198 static void bnx2fc_hba_destroy(struct bnx2fc_hba *hba)
1199 {
1200 /* Free the command manager */
1201 if (hba->cmd_mgr) {
1202 bnx2fc_cmd_mgr_free(hba->cmd_mgr);
1203 hba->cmd_mgr = NULL;
1204 }
1205 kfree(hba->tgt_ofld_list);
1206 bnx2fc_unbind_pcidev(hba);
1207 kfree(hba);
1208 }
1209
1210 /**
1211 * bnx2fc_hba_create - create a new bnx2fc hba
1212 *
1213 * @cnic: pointer to cnic device
1214 *
1215 * Creates a new FCoE hba on the given device.
1216 *
1217 */
1218 static struct bnx2fc_hba *bnx2fc_hba_create(struct cnic_dev *cnic)
1219 {
1220 struct bnx2fc_hba *hba;
1221 int rc;
1222
1223 hba = kzalloc(sizeof(*hba), GFP_KERNEL);
1224 if (!hba) {
1225 printk(KERN_ERR PFX "Unable to allocate hba structure\n");
1226 return NULL;
1227 }
1228 spin_lock_init(&hba->hba_lock);
1229 mutex_init(&hba->hba_mutex);
1230
1231 hba->cnic = cnic;
1232 rc = bnx2fc_bind_pcidev(hba);
1233 if (rc) {
1234 printk(KERN_ERR PFX "create_adapter: bind error\n");
1235 goto bind_err;
1236 }
1237 hba->phys_dev = cnic->netdev;
1238 hba->next_conn_id = 0;
1239
1240 hba->tgt_ofld_list =
1241 kzalloc(sizeof(struct bnx2fc_rport *) * BNX2FC_NUM_MAX_SESS,
1242 GFP_KERNEL);
1243 if (!hba->tgt_ofld_list) {
1244 printk(KERN_ERR PFX "Unable to allocate tgt offload list\n");
1245 goto tgtofld_err;
1246 }
1247
1248 hba->num_ofld_sess = 0;
1249
1250 hba->cmd_mgr = bnx2fc_cmd_mgr_alloc(hba, BNX2FC_MIN_XID,
1251 BNX2FC_MAX_XID);
1252 if (!hba->cmd_mgr) {
1253 printk(KERN_ERR PFX "em_config:bnx2fc_cmd_mgr_alloc failed\n");
1254 goto cmgr_err;
1255 }
1256
1257 init_waitqueue_head(&hba->shutdown_wait);
1258 init_waitqueue_head(&hba->destroy_wait);
1259 INIT_LIST_HEAD(&hba->vports);
1260
1261 return hba;
1262
1263 cmgr_err:
1264 kfree(hba->tgt_ofld_list);
1265 tgtofld_err:
1266 bnx2fc_unbind_pcidev(hba);
1267 bind_err:
1268 kfree(hba);
1269 return NULL;
1270 }
1271
1272 struct bnx2fc_interface *bnx2fc_interface_create(struct bnx2fc_hba *hba,
1273 struct net_device *netdev,
1274 enum fip_state fip_mode)
1275 {
1276 struct bnx2fc_interface *interface;
1277 int rc = 0;
1278
1279 interface = kzalloc(sizeof(*interface), GFP_KERNEL);
1280 if (!interface) {
1281 printk(KERN_ERR PFX "Unable to allocate interface structure\n");
1282 return NULL;
1283 }
1284 dev_hold(netdev);
1285 kref_init(&interface->kref);
1286 interface->hba = hba;
1287 interface->netdev = netdev;
1288
1289 /* Initialize FIP */
1290 fcoe_ctlr_init(&interface->ctlr, fip_mode);
1291 interface->ctlr.send = bnx2fc_fip_send;
1292 interface->ctlr.update_mac = bnx2fc_update_src_mac;
1293 interface->ctlr.get_src_addr = bnx2fc_get_src_mac;
1294 set_bit(BNX2FC_CTLR_INIT_DONE, &interface->if_flags);
1295
1296 rc = bnx2fc_interface_setup(interface);
1297 if (!rc)
1298 return interface;
1299
1300 fcoe_ctlr_destroy(&interface->ctlr);
1301 dev_put(netdev);
1302 kfree(interface);
1303 return NULL;
1304 }
1305
1306 /**
1307 * bnx2fc_if_create - Create FCoE instance on a given interface
1308 *
1309 * @interface: FCoE interface to create a local port on
1310 * @parent: Device pointer to be the parent in sysfs for the SCSI host
1311 * @npiv: Indicates if the port is vport or not
1312 *
1313 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1314 *
1315 * Returns: Allocated fc_lport or an error pointer
1316 */
1317 static struct fc_lport *bnx2fc_if_create(struct bnx2fc_interface *interface,
1318 struct device *parent, int npiv)
1319 {
1320 struct fc_lport *lport, *n_port;
1321 struct fcoe_port *port;
1322 struct Scsi_Host *shost;
1323 struct fc_vport *vport = dev_to_vport(parent);
1324 struct bnx2fc_lport *blport;
1325 struct bnx2fc_hba *hba;
1326 int rc = 0;
1327
1328 blport = kzalloc(sizeof(struct bnx2fc_lport), GFP_KERNEL);
1329 if (!blport) {
1330 BNX2FC_HBA_DBG(interface->ctlr.lp, "Unable to alloc blport\n");
1331 return NULL;
1332 }
1333
1334 /* Allocate Scsi_Host structure */
1335 if (!npiv)
1336 lport = libfc_host_alloc(&bnx2fc_shost_template, sizeof(*port));
1337 else
1338 lport = libfc_vport_create(vport, sizeof(*port));
1339
1340 if (!lport) {
1341 printk(KERN_ERR PFX "could not allocate scsi host structure\n");
1342 goto free_blport;
1343 }
1344 shost = lport->host;
1345 port = lport_priv(lport);
1346 port->lport = lport;
1347 port->priv = interface;
1348 INIT_WORK(&port->destroy_work, bnx2fc_destroy_work);
1349
1350 /* Configure fcoe_port */
1351 rc = bnx2fc_lport_config(lport);
1352 if (rc)
1353 goto lp_config_err;
1354
1355 if (npiv) {
1356 printk(KERN_ERR PFX "Setting vport names, 0x%llX 0x%llX\n",
1357 vport->node_name, vport->port_name);
1358 fc_set_wwnn(lport, vport->node_name);
1359 fc_set_wwpn(lport, vport->port_name);
1360 }
1361 /* Configure netdev and networking properties of the lport */
1362 rc = bnx2fc_net_config(lport);
1363 if (rc) {
1364 printk(KERN_ERR PFX "Error on bnx2fc_net_config\n");
1365 goto lp_config_err;
1366 }
1367
1368 rc = bnx2fc_shost_config(lport, parent);
1369 if (rc) {
1370 printk(KERN_ERR PFX "Couldnt configure shost for %s\n",
1371 interface->netdev->name);
1372 goto lp_config_err;
1373 }
1374
1375 /* Initialize the libfc library */
1376 rc = bnx2fc_libfc_config(lport);
1377 if (rc) {
1378 printk(KERN_ERR PFX "Couldnt configure libfc\n");
1379 goto shost_err;
1380 }
1381 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1382
1383 /* Allocate exchange manager */
1384 if (!npiv)
1385 rc = bnx2fc_em_config(lport);
1386 else {
1387 shost = vport_to_shost(vport);
1388 n_port = shost_priv(shost);
1389 rc = fc_exch_mgr_list_clone(n_port, lport);
1390 }
1391
1392 if (rc) {
1393 printk(KERN_ERR PFX "Error on bnx2fc_em_config\n");
1394 goto shost_err;
1395 }
1396
1397 bnx2fc_interface_get(interface);
1398
1399 hba = interface->hba;
1400 spin_lock_bh(&hba->hba_lock);
1401 blport->lport = lport;
1402 list_add_tail(&blport->list, &hba->vports);
1403 spin_unlock_bh(&hba->hba_lock);
1404
1405 return lport;
1406
1407 shost_err:
1408 scsi_remove_host(shost);
1409 lp_config_err:
1410 scsi_host_put(lport->host);
1411 free_blport:
1412 kfree(blport);
1413 return NULL;
1414 }
1415
1416 static void bnx2fc_interface_cleanup(struct bnx2fc_interface *interface)
1417 {
1418 struct fc_lport *lport = interface->ctlr.lp;
1419 struct fcoe_port *port = lport_priv(lport);
1420 struct bnx2fc_hba *hba = interface->hba;
1421
1422 /* Stop the transmit retry timer */
1423 del_timer_sync(&port->timer);
1424
1425 /* Free existing transmit skbs */
1426 fcoe_clean_pending_queue(lport);
1427
1428 /* Dont listen for Ethernet packets anymore */
1429 __dev_remove_pack(&interface->fcoe_packet_type);
1430 __dev_remove_pack(&interface->fip_packet_type);
1431 synchronize_net();
1432
1433 bnx2fc_free_vport(hba, lport);
1434 }
1435
1436 static void bnx2fc_if_destroy(struct fc_lport *lport)
1437 {
1438
1439 /* Free queued packets for the receive thread */
1440 bnx2fc_clean_rx_queue(lport);
1441
1442 /* Detach from scsi-ml */
1443 fc_remove_host(lport->host);
1444 scsi_remove_host(lport->host);
1445
1446 /*
1447 * Note that only the physical lport will have the exchange manager.
1448 * for vports, this function is NOP
1449 */
1450 fc_exch_mgr_free(lport);
1451
1452 /* Free memory used by statistical counters */
1453 fc_lport_free_stats(lport);
1454
1455 /* Release Scsi_Host */
1456 scsi_host_put(lport->host);
1457 }
1458
1459 static void __bnx2fc_destroy(struct bnx2fc_interface *interface, bool schedule)
1460 {
1461 struct fc_lport *lport = interface->ctlr.lp;
1462 struct fcoe_port *port = lport_priv(lport);
1463
1464 bnx2fc_interface_cleanup(interface);
1465 bnx2fc_stop(interface);
1466
1467 list_del(&interface->list);
1468 lport = interface->ctlr.lp;
1469 bnx2fc_interface_put(interface);
1470 if (schedule)
1471 queue_work(bnx2fc_wq, &port->destroy_work);
1472 else
1473 bnx2fc_if_destroy(lport);
1474 }
1475
1476 /**
1477 * bnx2fc_destroy - Destroy a bnx2fc FCoE interface
1478 *
1479 * @buffer: The name of the Ethernet interface to be destroyed
1480 * @kp: The associated kernel parameter
1481 *
1482 * Called from sysfs.
1483 *
1484 * Returns: 0 for success
1485 */
1486 static int bnx2fc_destroy(struct net_device *netdev)
1487 {
1488 struct bnx2fc_interface *interface = NULL;
1489 int rc = 0;
1490
1491 rtnl_lock();
1492 mutex_lock(&bnx2fc_dev_lock);
1493
1494 interface = bnx2fc_interface_lookup(netdev);
1495 if (!interface || !interface->ctlr.lp) {
1496 rc = -ENODEV;
1497 printk(KERN_ERR PFX "bnx2fc_destroy: interface or lport not found\n");
1498 goto netdev_err;
1499 }
1500
1501
1502 destroy_workqueue(interface->timer_work_queue);
1503 __bnx2fc_destroy(interface, false);
1504
1505 netdev_err:
1506 mutex_unlock(&bnx2fc_dev_lock);
1507 rtnl_unlock();
1508 return rc;
1509 }
1510
1511 static void bnx2fc_destroy_work(struct work_struct *work)
1512 {
1513 struct fcoe_port *port;
1514 struct fc_lport *lport;
1515
1516 port = container_of(work, struct fcoe_port, destroy_work);
1517 lport = port->lport;
1518
1519 BNX2FC_HBA_DBG(lport, "Entered bnx2fc_destroy_work\n");
1520
1521 rtnl_lock();
1522 mutex_lock(&bnx2fc_dev_lock);
1523 bnx2fc_if_destroy(lport);
1524 mutex_unlock(&bnx2fc_dev_lock);
1525 rtnl_unlock();
1526 }
1527
1528 static void bnx2fc_unbind_adapter_devices(struct bnx2fc_hba *hba)
1529 {
1530 bnx2fc_free_fw_resc(hba);
1531 bnx2fc_free_task_ctx(hba);
1532 }
1533
1534 /**
1535 * bnx2fc_bind_adapter_devices - binds bnx2fc adapter with the associated
1536 * pci structure
1537 *
1538 * @hba: Adapter instance
1539 */
1540 static int bnx2fc_bind_adapter_devices(struct bnx2fc_hba *hba)
1541 {
1542 if (bnx2fc_setup_task_ctx(hba))
1543 goto mem_err;
1544
1545 if (bnx2fc_setup_fw_resc(hba))
1546 goto mem_err;
1547
1548 return 0;
1549 mem_err:
1550 bnx2fc_unbind_adapter_devices(hba);
1551 return -ENOMEM;
1552 }
1553
1554 static int bnx2fc_bind_pcidev(struct bnx2fc_hba *hba)
1555 {
1556 struct cnic_dev *cnic;
1557
1558 if (!hba->cnic) {
1559 printk(KERN_ERR PFX "cnic is NULL\n");
1560 return -ENODEV;
1561 }
1562 cnic = hba->cnic;
1563 hba->pcidev = cnic->pcidev;
1564 if (hba->pcidev)
1565 pci_dev_get(hba->pcidev);
1566
1567 return 0;
1568 }
1569
1570 static void bnx2fc_unbind_pcidev(struct bnx2fc_hba *hba)
1571 {
1572 if (hba->pcidev)
1573 pci_dev_put(hba->pcidev);
1574 hba->pcidev = NULL;
1575 }
1576
1577
1578
1579 /**
1580 * bnx2fc_ulp_start - cnic callback to initialize & start adapter instance
1581 *
1582 * @handle: transport handle pointing to adapter struture
1583 *
1584 * This function maps adapter structure to pcidev structure and initiates
1585 * firmware handshake to enable/initialize on-chip FCoE components.
1586 * This bnx2fc - cnic interface api callback is used after following
1587 * conditions are met -
1588 * a) underlying network interface is up (marked by event NETDEV_UP
1589 * from netdev
1590 * b) bnx2fc adatper structure is registered.
1591 */
1592 static void bnx2fc_ulp_start(void *handle)
1593 {
1594 struct bnx2fc_hba *hba = handle;
1595 struct bnx2fc_interface *interface;
1596 struct fc_lport *lport;
1597
1598 mutex_lock(&bnx2fc_dev_lock);
1599
1600 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1601 bnx2fc_fw_init(hba);
1602
1603 BNX2FC_MISC_DBG("bnx2fc started.\n");
1604
1605 list_for_each_entry(interface, &if_list, list) {
1606 if (interface->hba == hba) {
1607 lport = interface->ctlr.lp;
1608 /* Kick off Fabric discovery*/
1609 printk(KERN_ERR PFX "ulp_init: start discovery\n");
1610 lport->tt.frame_send = bnx2fc_xmit;
1611 bnx2fc_start_disc(interface);
1612 }
1613 }
1614
1615 mutex_unlock(&bnx2fc_dev_lock);
1616 }
1617
1618 static void bnx2fc_port_shutdown(struct fc_lport *lport)
1619 {
1620 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1621 fc_fabric_logoff(lport);
1622 fc_lport_destroy(lport);
1623 }
1624
1625 static void bnx2fc_stop(struct bnx2fc_interface *interface)
1626 {
1627 struct fc_lport *lport;
1628 struct fc_lport *vport;
1629
1630 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags))
1631 return;
1632
1633 lport = interface->ctlr.lp;
1634 bnx2fc_port_shutdown(lport);
1635
1636 mutex_lock(&lport->lp_mutex);
1637 list_for_each_entry(vport, &lport->vports, list)
1638 fc_host_port_type(vport->host) =
1639 FC_PORTTYPE_UNKNOWN;
1640 mutex_unlock(&lport->lp_mutex);
1641 fc_host_port_type(lport->host) = FC_PORTTYPE_UNKNOWN;
1642 fcoe_ctlr_link_down(&interface->ctlr);
1643 fcoe_clean_pending_queue(lport);
1644 }
1645
1646 static int bnx2fc_fw_init(struct bnx2fc_hba *hba)
1647 {
1648 #define BNX2FC_INIT_POLL_TIME (1000 / HZ)
1649 int rc = -1;
1650 int i = HZ;
1651
1652 rc = bnx2fc_bind_adapter_devices(hba);
1653 if (rc) {
1654 printk(KERN_ALERT PFX
1655 "bnx2fc_bind_adapter_devices failed - rc = %d\n", rc);
1656 goto err_out;
1657 }
1658
1659 rc = bnx2fc_send_fw_fcoe_init_msg(hba);
1660 if (rc) {
1661 printk(KERN_ALERT PFX
1662 "bnx2fc_send_fw_fcoe_init_msg failed - rc = %d\n", rc);
1663 goto err_unbind;
1664 }
1665
1666 /*
1667 * Wait until the adapter init message is complete, and adapter
1668 * state is UP.
1669 */
1670 while (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state) && i--)
1671 msleep(BNX2FC_INIT_POLL_TIME);
1672
1673 if (!test_bit(ADAPTER_STATE_UP, &hba->adapter_state)) {
1674 printk(KERN_ERR PFX "bnx2fc_start: %s failed to initialize. "
1675 "Ignoring...\n",
1676 hba->cnic->netdev->name);
1677 rc = -1;
1678 goto err_unbind;
1679 }
1680
1681
1682 set_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags);
1683 return 0;
1684
1685 err_unbind:
1686 bnx2fc_unbind_adapter_devices(hba);
1687 err_out:
1688 return rc;
1689 }
1690
1691 static void bnx2fc_fw_destroy(struct bnx2fc_hba *hba)
1692 {
1693 if (test_and_clear_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags)) {
1694 if (bnx2fc_send_fw_fcoe_destroy_msg(hba) == 0) {
1695 init_timer(&hba->destroy_timer);
1696 hba->destroy_timer.expires = BNX2FC_FW_TIMEOUT +
1697 jiffies;
1698 hba->destroy_timer.function = bnx2fc_destroy_timer;
1699 hba->destroy_timer.data = (unsigned long)hba;
1700 add_timer(&hba->destroy_timer);
1701 wait_event_interruptible(hba->destroy_wait,
1702 test_bit(BNX2FC_FLAG_DESTROY_CMPL,
1703 &hba->flags));
1704 /* This should never happen */
1705 if (signal_pending(current))
1706 flush_signals(current);
1707
1708 del_timer_sync(&hba->destroy_timer);
1709 }
1710 bnx2fc_unbind_adapter_devices(hba);
1711 }
1712 }
1713
1714 /**
1715 * bnx2fc_ulp_stop - cnic callback to shutdown adapter instance
1716 *
1717 * @handle: transport handle pointing to adapter structure
1718 *
1719 * Driver checks if adapter is already in shutdown mode, if not start
1720 * the shutdown process.
1721 */
1722 static void bnx2fc_ulp_stop(void *handle)
1723 {
1724 struct bnx2fc_hba *hba = handle;
1725 struct bnx2fc_interface *interface;
1726
1727 printk(KERN_ERR "ULP_STOP\n");
1728
1729 mutex_lock(&bnx2fc_dev_lock);
1730 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &hba->flags))
1731 goto exit;
1732 list_for_each_entry(interface, &if_list, list) {
1733 if (interface->hba == hba)
1734 bnx2fc_stop(interface);
1735 }
1736 BUG_ON(hba->num_ofld_sess != 0);
1737
1738 mutex_lock(&hba->hba_mutex);
1739 clear_bit(ADAPTER_STATE_UP, &hba->adapter_state);
1740 clear_bit(ADAPTER_STATE_GOING_DOWN,
1741 &hba->adapter_state);
1742
1743 clear_bit(ADAPTER_STATE_READY, &hba->adapter_state);
1744 mutex_unlock(&hba->hba_mutex);
1745
1746 bnx2fc_fw_destroy(hba);
1747 exit:
1748 mutex_unlock(&bnx2fc_dev_lock);
1749 }
1750
1751 static void bnx2fc_start_disc(struct bnx2fc_interface *interface)
1752 {
1753 struct fc_lport *lport;
1754 int wait_cnt = 0;
1755
1756 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1757 /* Kick off FIP/FLOGI */
1758 if (!test_bit(BNX2FC_FLAG_FW_INIT_DONE, &interface->hba->flags)) {
1759 printk(KERN_ERR PFX "Init not done yet\n");
1760 return;
1761 }
1762
1763 lport = interface->ctlr.lp;
1764 BNX2FC_HBA_DBG(lport, "calling fc_fabric_login\n");
1765
1766 if (!bnx2fc_link_ok(lport)) {
1767 BNX2FC_HBA_DBG(lport, "ctlr_link_up\n");
1768 fcoe_ctlr_link_up(&interface->ctlr);
1769 fc_host_port_type(lport->host) = FC_PORTTYPE_NPORT;
1770 set_bit(ADAPTER_STATE_READY, &interface->hba->adapter_state);
1771 }
1772
1773 /* wait for the FCF to be selected before issuing FLOGI */
1774 while (!interface->ctlr.sel_fcf) {
1775 msleep(250);
1776 /* give up after 3 secs */
1777 if (++wait_cnt > 12)
1778 break;
1779 }
1780
1781 /* Reset max receive frame size to default */
1782 if (fc_set_mfs(lport, BNX2FC_MFS))
1783 return;
1784
1785 fc_lport_init(lport);
1786 fc_fabric_login(lport);
1787 }
1788
1789
1790 /**
1791 * bnx2fc_ulp_init - Initialize an adapter instance
1792 *
1793 * @dev : cnic device handle
1794 * Called from cnic_register_driver() context to initialize all
1795 * enumerated cnic devices. This routine allocates adapter structure
1796 * and other device specific resources.
1797 */
1798 static void bnx2fc_ulp_init(struct cnic_dev *dev)
1799 {
1800 struct bnx2fc_hba *hba;
1801 int rc = 0;
1802
1803 BNX2FC_MISC_DBG("Entered %s\n", __func__);
1804 /* bnx2fc works only when bnx2x is loaded */
1805 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) ||
1806 (dev->max_fcoe_conn == 0)) {
1807 printk(KERN_ERR PFX "bnx2fc FCoE not supported on %s,"
1808 " flags: %lx fcoe_conn: %d\n",
1809 dev->netdev->name, dev->flags, dev->max_fcoe_conn);
1810 return;
1811 }
1812
1813 hba = bnx2fc_hba_create(dev);
1814 if (!hba) {
1815 printk(KERN_ERR PFX "hba initialization failed\n");
1816 return;
1817 }
1818
1819 /* Add HBA to the adapter list */
1820 mutex_lock(&bnx2fc_dev_lock);
1821 list_add_tail(&hba->list, &adapter_list);
1822 adapter_count++;
1823 mutex_unlock(&bnx2fc_dev_lock);
1824
1825 clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1826 rc = dev->register_device(dev, CNIC_ULP_FCOE,
1827 (void *) hba);
1828 if (rc)
1829 printk(KERN_ERR PFX "register_device failed, rc = %d\n", rc);
1830 else
1831 set_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic);
1832 }
1833
1834
1835 static int bnx2fc_disable(struct net_device *netdev)
1836 {
1837 struct bnx2fc_interface *interface;
1838 int rc = 0;
1839
1840 rtnl_lock();
1841 mutex_lock(&bnx2fc_dev_lock);
1842
1843 interface = bnx2fc_interface_lookup(netdev);
1844 if (!interface || !interface->ctlr.lp) {
1845 rc = -ENODEV;
1846 printk(KERN_ERR PFX "bnx2fc_disable: interface or lport not found\n");
1847 } else {
1848 fcoe_ctlr_link_down(&interface->ctlr);
1849 fcoe_clean_pending_queue(interface->ctlr.lp);
1850 }
1851
1852 mutex_unlock(&bnx2fc_dev_lock);
1853 rtnl_unlock();
1854 return rc;
1855 }
1856
1857
1858 static int bnx2fc_enable(struct net_device *netdev)
1859 {
1860 struct bnx2fc_interface *interface;
1861 int rc = 0;
1862
1863 rtnl_lock();
1864 mutex_lock(&bnx2fc_dev_lock);
1865
1866 interface = bnx2fc_interface_lookup(netdev);
1867 if (!interface || !interface->ctlr.lp) {
1868 rc = -ENODEV;
1869 printk(KERN_ERR PFX "bnx2fc_enable: interface or lport not found\n");
1870 } else if (!bnx2fc_link_ok(interface->ctlr.lp))
1871 fcoe_ctlr_link_up(&interface->ctlr);
1872
1873 mutex_unlock(&bnx2fc_dev_lock);
1874 rtnl_unlock();
1875 return rc;
1876 }
1877
1878 /**
1879 * bnx2fc_create - Create bnx2fc FCoE interface
1880 *
1881 * @buffer: The name of Ethernet interface to create on
1882 * @kp: The associated kernel param
1883 *
1884 * Called from sysfs.
1885 *
1886 * Returns: 0 for success
1887 */
1888 static int bnx2fc_create(struct net_device *netdev, enum fip_state fip_mode)
1889 {
1890 struct bnx2fc_interface *interface;
1891 struct bnx2fc_hba *hba;
1892 struct net_device *phys_dev;
1893 struct fc_lport *lport;
1894 struct ethtool_drvinfo drvinfo;
1895 int rc = 0;
1896 int vlan_id;
1897
1898 BNX2FC_MISC_DBG("Entered bnx2fc_create\n");
1899 if (fip_mode != FIP_MODE_FABRIC) {
1900 printk(KERN_ERR "fip mode not FABRIC\n");
1901 return -EIO;
1902 }
1903
1904 rtnl_lock();
1905
1906 mutex_lock(&bnx2fc_dev_lock);
1907
1908 if (!try_module_get(THIS_MODULE)) {
1909 rc = -EINVAL;
1910 goto mod_err;
1911 }
1912
1913 /* obtain physical netdev */
1914 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
1915 phys_dev = vlan_dev_real_dev(netdev);
1916 vlan_id = vlan_dev_vlan_id(netdev);
1917 } else {
1918 printk(KERN_ERR PFX "Not a vlan device\n");
1919 rc = -EINVAL;
1920 goto netdev_err;
1921 }
1922 /* verify if the physical device is a netxtreme2 device */
1923 if (phys_dev->ethtool_ops && phys_dev->ethtool_ops->get_drvinfo) {
1924 memset(&drvinfo, 0, sizeof(drvinfo));
1925 phys_dev->ethtool_ops->get_drvinfo(phys_dev, &drvinfo);
1926 if (strncmp(drvinfo.driver, "bnx2x", strlen("bnx2x"))) {
1927 printk(KERN_ERR PFX "Not a netxtreme2 device\n");
1928 rc = -EINVAL;
1929 goto netdev_err;
1930 }
1931 } else {
1932 printk(KERN_ERR PFX "unable to obtain drv_info\n");
1933 rc = -EINVAL;
1934 goto netdev_err;
1935 }
1936
1937 /* obtain interface and initialize rest of the structure */
1938 hba = bnx2fc_hba_lookup(phys_dev);
1939 if (!hba) {
1940 rc = -ENODEV;
1941 printk(KERN_ERR PFX "bnx2fc_create: hba not found\n");
1942 goto netdev_err;
1943 }
1944
1945 if (bnx2fc_interface_lookup(netdev)) {
1946 rc = -EEXIST;
1947 goto netdev_err;
1948 }
1949
1950 interface = bnx2fc_interface_create(hba, netdev, fip_mode);
1951 if (!interface) {
1952 printk(KERN_ERR PFX "bnx2fc_interface_create failed\n");
1953 goto ifput_err;
1954 }
1955
1956 interface->vlan_id = vlan_id;
1957 interface->vlan_enabled = 1;
1958
1959 interface->timer_work_queue =
1960 create_singlethread_workqueue("bnx2fc_timer_wq");
1961 if (!interface->timer_work_queue) {
1962 printk(KERN_ERR PFX "ulp_init could not create timer_wq\n");
1963 rc = -EINVAL;
1964 goto ifput_err;
1965 }
1966
1967 lport = bnx2fc_if_create(interface, &interface->hba->pcidev->dev, 0);
1968 if (!lport) {
1969 printk(KERN_ERR PFX "Failed to create interface (%s)\n",
1970 netdev->name);
1971 bnx2fc_interface_cleanup(interface);
1972 rc = -EINVAL;
1973 goto if_create_err;
1974 }
1975
1976 /* Add interface to if_list */
1977 list_add_tail(&interface->list, &if_list);
1978
1979 lport->boot_time = jiffies;
1980
1981 /* Make this master N_port */
1982 interface->ctlr.lp = lport;
1983
1984 BNX2FC_HBA_DBG(lport, "create: START DISC\n");
1985 bnx2fc_start_disc(interface);
1986 /*
1987 * Release from kref_init in bnx2fc_interface_setup, on success
1988 * lport should be holding a reference taken in bnx2fc_if_create
1989 */
1990 bnx2fc_interface_put(interface);
1991 /* put netdev that was held while calling dev_get_by_name */
1992 mutex_unlock(&bnx2fc_dev_lock);
1993 rtnl_unlock();
1994 return 0;
1995
1996 if_create_err:
1997 destroy_workqueue(interface->timer_work_queue);
1998 ifput_err:
1999 bnx2fc_interface_put(interface);
2000 netdev_err:
2001 module_put(THIS_MODULE);
2002 mod_err:
2003 mutex_unlock(&bnx2fc_dev_lock);
2004 rtnl_unlock();
2005 return rc;
2006 }
2007
2008 /**
2009 * bnx2fc_find_hba_for_cnic - maps cnic instance to bnx2fc hba instance
2010 *
2011 * @cnic: Pointer to cnic device instance
2012 *
2013 **/
2014 static struct bnx2fc_hba *bnx2fc_find_hba_for_cnic(struct cnic_dev *cnic)
2015 {
2016 struct list_head *list;
2017 struct list_head *temp;
2018 struct bnx2fc_hba *hba;
2019
2020 /* Called with bnx2fc_dev_lock held */
2021 list_for_each_safe(list, temp, &adapter_list) {
2022 hba = (struct bnx2fc_hba *)list;
2023 if (hba->cnic == cnic)
2024 return hba;
2025 }
2026 return NULL;
2027 }
2028
2029 static struct bnx2fc_interface *bnx2fc_interface_lookup(struct net_device
2030 *netdev)
2031 {
2032 struct bnx2fc_interface *interface;
2033
2034 /* Called with bnx2fc_dev_lock held */
2035 list_for_each_entry(interface, &if_list, list) {
2036 if (interface->netdev == netdev)
2037 return interface;
2038 }
2039 return NULL;
2040 }
2041
2042 static struct bnx2fc_hba *bnx2fc_hba_lookup(struct net_device
2043 *phys_dev)
2044 {
2045 struct bnx2fc_hba *hba;
2046
2047 /* Called with bnx2fc_dev_lock held */
2048 list_for_each_entry(hba, &adapter_list, list) {
2049 if (hba->phys_dev == phys_dev)
2050 return hba;
2051 }
2052 printk(KERN_ERR PFX "adapter_lookup: hba NULL\n");
2053 return NULL;
2054 }
2055
2056 /**
2057 * bnx2fc_ulp_exit - shuts down adapter instance and frees all resources
2058 *
2059 * @dev cnic device handle
2060 */
2061 static void bnx2fc_ulp_exit(struct cnic_dev *dev)
2062 {
2063 struct bnx2fc_hba *hba;
2064 struct bnx2fc_interface *interface, *tmp;
2065
2066 BNX2FC_MISC_DBG("Entered bnx2fc_ulp_exit\n");
2067
2068 if (!test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
2069 printk(KERN_ERR PFX "bnx2fc port check: %s, flags: %lx\n",
2070 dev->netdev->name, dev->flags);
2071 return;
2072 }
2073
2074 mutex_lock(&bnx2fc_dev_lock);
2075 hba = bnx2fc_find_hba_for_cnic(dev);
2076 if (!hba) {
2077 printk(KERN_ERR PFX "bnx2fc_ulp_exit: hba not found, dev 0%p\n",
2078 dev);
2079 mutex_unlock(&bnx2fc_dev_lock);
2080 return;
2081 }
2082
2083 list_del_init(&hba->list);
2084 adapter_count--;
2085
2086 list_for_each_entry_safe(interface, tmp, &if_list, list)
2087 /* destroy not called yet, move to quiesced list */
2088 if (interface->hba == hba)
2089 __bnx2fc_destroy(interface, false);
2090 mutex_unlock(&bnx2fc_dev_lock);
2091
2092 bnx2fc_ulp_stop(hba);
2093 /* unregister cnic device */
2094 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED, &hba->reg_with_cnic))
2095 hba->cnic->unregister_device(hba->cnic, CNIC_ULP_FCOE);
2096 bnx2fc_hba_destroy(hba);
2097 }
2098
2099 /**
2100 * bnx2fc_fcoe_reset - Resets the fcoe
2101 *
2102 * @shost: shost the reset is from
2103 *
2104 * Returns: always 0
2105 */
2106 static int bnx2fc_fcoe_reset(struct Scsi_Host *shost)
2107 {
2108 struct fc_lport *lport = shost_priv(shost);
2109 fc_lport_reset(lport);
2110 return 0;
2111 }
2112
2113
2114 static bool bnx2fc_match(struct net_device *netdev)
2115 {
2116 mutex_lock(&bnx2fc_dev_lock);
2117 if (netdev->priv_flags & IFF_802_1Q_VLAN) {
2118 struct net_device *phys_dev = vlan_dev_real_dev(netdev);
2119
2120 if (bnx2fc_hba_lookup(phys_dev)) {
2121 mutex_unlock(&bnx2fc_dev_lock);
2122 return true;
2123 }
2124 }
2125 mutex_unlock(&bnx2fc_dev_lock);
2126 return false;
2127 }
2128
2129
2130 static struct fcoe_transport bnx2fc_transport = {
2131 .name = {"bnx2fc"},
2132 .attached = false,
2133 .list = LIST_HEAD_INIT(bnx2fc_transport.list),
2134 .match = bnx2fc_match,
2135 .create = bnx2fc_create,
2136 .destroy = bnx2fc_destroy,
2137 .enable = bnx2fc_enable,
2138 .disable = bnx2fc_disable,
2139 };
2140
2141 /**
2142 * bnx2fc_percpu_thread_create - Create a receive thread for an
2143 * online CPU
2144 *
2145 * @cpu: cpu index for the online cpu
2146 */
2147 static void bnx2fc_percpu_thread_create(unsigned int cpu)
2148 {
2149 struct bnx2fc_percpu_s *p;
2150 struct task_struct *thread;
2151
2152 p = &per_cpu(bnx2fc_percpu, cpu);
2153
2154 thread = kthread_create(bnx2fc_percpu_io_thread,
2155 (void *)p,
2156 "bnx2fc_thread/%d", cpu);
2157 /* bind thread to the cpu */
2158 if (likely(!IS_ERR(thread))) {
2159 kthread_bind(thread, cpu);
2160 p->iothread = thread;
2161 wake_up_process(thread);
2162 }
2163 }
2164
2165 static void bnx2fc_percpu_thread_destroy(unsigned int cpu)
2166 {
2167 struct bnx2fc_percpu_s *p;
2168 struct task_struct *thread;
2169 struct bnx2fc_work *work, *tmp;
2170
2171 BNX2FC_MISC_DBG("destroying io thread for CPU %d\n", cpu);
2172
2173 /* Prevent any new work from being queued for this CPU */
2174 p = &per_cpu(bnx2fc_percpu, cpu);
2175 spin_lock_bh(&p->fp_work_lock);
2176 thread = p->iothread;
2177 p->iothread = NULL;
2178
2179
2180 /* Free all work in the list */
2181 list_for_each_entry_safe(work, tmp, &p->work_list, list) {
2182 list_del_init(&work->list);
2183 bnx2fc_process_cq_compl(work->tgt, work->wqe);
2184 kfree(work);
2185 }
2186
2187 spin_unlock_bh(&p->fp_work_lock);
2188
2189 if (thread)
2190 kthread_stop(thread);
2191 }
2192
2193 /**
2194 * bnx2fc_cpu_callback - Handler for CPU hotplug events
2195 *
2196 * @nfb: The callback data block
2197 * @action: The event triggering the callback
2198 * @hcpu: The index of the CPU that the event is for
2199 *
2200 * This creates or destroys per-CPU data for fcoe
2201 *
2202 * Returns NOTIFY_OK always.
2203 */
2204 static int bnx2fc_cpu_callback(struct notifier_block *nfb,
2205 unsigned long action, void *hcpu)
2206 {
2207 unsigned cpu = (unsigned long)hcpu;
2208
2209 switch (action) {
2210 case CPU_ONLINE:
2211 case CPU_ONLINE_FROZEN:
2212 printk(PFX "CPU %x online: Create Rx thread\n", cpu);
2213 bnx2fc_percpu_thread_create(cpu);
2214 break;
2215 case CPU_DEAD:
2216 case CPU_DEAD_FROZEN:
2217 printk(PFX "CPU %x offline: Remove Rx thread\n", cpu);
2218 bnx2fc_percpu_thread_destroy(cpu);
2219 break;
2220 default:
2221 break;
2222 }
2223 return NOTIFY_OK;
2224 }
2225
2226 /**
2227 * bnx2fc_mod_init - module init entry point
2228 *
2229 * Initialize driver wide global data structures, and register
2230 * with cnic module
2231 **/
2232 static int __init bnx2fc_mod_init(void)
2233 {
2234 struct fcoe_percpu_s *bg;
2235 struct task_struct *l2_thread;
2236 int rc = 0;
2237 unsigned int cpu = 0;
2238 struct bnx2fc_percpu_s *p;
2239
2240 printk(KERN_INFO PFX "%s", version);
2241
2242 /* register as a fcoe transport */
2243 rc = fcoe_transport_attach(&bnx2fc_transport);
2244 if (rc) {
2245 printk(KERN_ERR "failed to register an fcoe transport, check "
2246 "if libfcoe is loaded\n");
2247 goto out;
2248 }
2249
2250 INIT_LIST_HEAD(&adapter_list);
2251 INIT_LIST_HEAD(&if_list);
2252 mutex_init(&bnx2fc_dev_lock);
2253 adapter_count = 0;
2254
2255 /* Attach FC transport template */
2256 rc = bnx2fc_attach_transport();
2257 if (rc)
2258 goto detach_ft;
2259
2260 bnx2fc_wq = alloc_workqueue("bnx2fc", 0, 0);
2261 if (!bnx2fc_wq) {
2262 rc = -ENOMEM;
2263 goto release_bt;
2264 }
2265
2266 bg = &bnx2fc_global;
2267 skb_queue_head_init(&bg->fcoe_rx_list);
2268 l2_thread = kthread_create(bnx2fc_l2_rcv_thread,
2269 (void *)bg,
2270 "bnx2fc_l2_thread");
2271 if (IS_ERR(l2_thread)) {
2272 rc = PTR_ERR(l2_thread);
2273 goto free_wq;
2274 }
2275 wake_up_process(l2_thread);
2276 spin_lock_bh(&bg->fcoe_rx_list.lock);
2277 bg->thread = l2_thread;
2278 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2279
2280 for_each_possible_cpu(cpu) {
2281 p = &per_cpu(bnx2fc_percpu, cpu);
2282 INIT_LIST_HEAD(&p->work_list);
2283 spin_lock_init(&p->fp_work_lock);
2284 }
2285
2286 for_each_online_cpu(cpu) {
2287 bnx2fc_percpu_thread_create(cpu);
2288 }
2289
2290 /* Initialize per CPU interrupt thread */
2291 register_hotcpu_notifier(&bnx2fc_cpu_notifier);
2292
2293 cnic_register_driver(CNIC_ULP_FCOE, &bnx2fc_cnic_cb);
2294
2295 return 0;
2296
2297 free_wq:
2298 destroy_workqueue(bnx2fc_wq);
2299 release_bt:
2300 bnx2fc_release_transport();
2301 detach_ft:
2302 fcoe_transport_detach(&bnx2fc_transport);
2303 out:
2304 return rc;
2305 }
2306
2307 static void __exit bnx2fc_mod_exit(void)
2308 {
2309 LIST_HEAD(to_be_deleted);
2310 struct bnx2fc_hba *hba, *next;
2311 struct fcoe_percpu_s *bg;
2312 struct task_struct *l2_thread;
2313 struct sk_buff *skb;
2314 unsigned int cpu = 0;
2315
2316 /*
2317 * NOTE: Since cnic calls register_driver routine rtnl_lock,
2318 * it will have higher precedence than bnx2fc_dev_lock.
2319 * unregister_device() cannot be called with bnx2fc_dev_lock
2320 * held.
2321 */
2322 mutex_lock(&bnx2fc_dev_lock);
2323 list_splice(&adapter_list, &to_be_deleted);
2324 INIT_LIST_HEAD(&adapter_list);
2325 adapter_count = 0;
2326 mutex_unlock(&bnx2fc_dev_lock);
2327
2328 /* Unregister with cnic */
2329 list_for_each_entry_safe(hba, next, &to_be_deleted, list) {
2330 list_del_init(&hba->list);
2331 printk(KERN_ERR PFX "MOD_EXIT:destroy hba = 0x%p\n",
2332 hba);
2333 bnx2fc_ulp_stop(hba);
2334 /* unregister cnic device */
2335 if (test_and_clear_bit(BNX2FC_CNIC_REGISTERED,
2336 &hba->reg_with_cnic))
2337 hba->cnic->unregister_device(hba->cnic,
2338 CNIC_ULP_FCOE);
2339 bnx2fc_hba_destroy(hba);
2340 }
2341 cnic_unregister_driver(CNIC_ULP_FCOE);
2342
2343 /* Destroy global thread */
2344 bg = &bnx2fc_global;
2345 spin_lock_bh(&bg->fcoe_rx_list.lock);
2346 l2_thread = bg->thread;
2347 bg->thread = NULL;
2348 while ((skb = __skb_dequeue(&bg->fcoe_rx_list)) != NULL)
2349 kfree_skb(skb);
2350
2351 spin_unlock_bh(&bg->fcoe_rx_list.lock);
2352
2353 if (l2_thread)
2354 kthread_stop(l2_thread);
2355
2356 unregister_hotcpu_notifier(&bnx2fc_cpu_notifier);
2357
2358 /* Destroy per cpu threads */
2359 for_each_online_cpu(cpu) {
2360 bnx2fc_percpu_thread_destroy(cpu);
2361 }
2362
2363 destroy_workqueue(bnx2fc_wq);
2364 /*
2365 * detach from scsi transport
2366 * must happen after all destroys are done
2367 */
2368 bnx2fc_release_transport();
2369
2370 /* detach from fcoe transport */
2371 fcoe_transport_detach(&bnx2fc_transport);
2372 }
2373
2374 module_init(bnx2fc_mod_init);
2375 module_exit(bnx2fc_mod_exit);
2376
2377 static struct fc_function_template bnx2fc_transport_function = {
2378 .show_host_node_name = 1,
2379 .show_host_port_name = 1,
2380 .show_host_supported_classes = 1,
2381 .show_host_supported_fc4s = 1,
2382 .show_host_active_fc4s = 1,
2383 .show_host_maxframe_size = 1,
2384
2385 .show_host_port_id = 1,
2386 .show_host_supported_speeds = 1,
2387 .get_host_speed = fc_get_host_speed,
2388 .show_host_speed = 1,
2389 .show_host_port_type = 1,
2390 .get_host_port_state = fc_get_host_port_state,
2391 .show_host_port_state = 1,
2392 .show_host_symbolic_name = 1,
2393
2394 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2395 sizeof(struct bnx2fc_rport)),
2396 .show_rport_maxframe_size = 1,
2397 .show_rport_supported_classes = 1,
2398
2399 .show_host_fabric_name = 1,
2400 .show_starget_node_name = 1,
2401 .show_starget_port_name = 1,
2402 .show_starget_port_id = 1,
2403 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2404 .show_rport_dev_loss_tmo = 1,
2405 .get_fc_host_stats = bnx2fc_get_host_stats,
2406
2407 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2408
2409 .terminate_rport_io = fc_rport_terminate_io,
2410
2411 .vport_create = bnx2fc_vport_create,
2412 .vport_delete = bnx2fc_vport_destroy,
2413 .vport_disable = bnx2fc_vport_disable,
2414 .bsg_request = fc_lport_bsg_request,
2415 };
2416
2417 static struct fc_function_template bnx2fc_vport_xport_function = {
2418 .show_host_node_name = 1,
2419 .show_host_port_name = 1,
2420 .show_host_supported_classes = 1,
2421 .show_host_supported_fc4s = 1,
2422 .show_host_active_fc4s = 1,
2423 .show_host_maxframe_size = 1,
2424
2425 .show_host_port_id = 1,
2426 .show_host_supported_speeds = 1,
2427 .get_host_speed = fc_get_host_speed,
2428 .show_host_speed = 1,
2429 .show_host_port_type = 1,
2430 .get_host_port_state = fc_get_host_port_state,
2431 .show_host_port_state = 1,
2432 .show_host_symbolic_name = 1,
2433
2434 .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
2435 sizeof(struct bnx2fc_rport)),
2436 .show_rport_maxframe_size = 1,
2437 .show_rport_supported_classes = 1,
2438
2439 .show_host_fabric_name = 1,
2440 .show_starget_node_name = 1,
2441 .show_starget_port_name = 1,
2442 .show_starget_port_id = 1,
2443 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
2444 .show_rport_dev_loss_tmo = 1,
2445 .get_fc_host_stats = fc_get_host_stats,
2446 .issue_fc_host_lip = bnx2fc_fcoe_reset,
2447 .terminate_rport_io = fc_rport_terminate_io,
2448 .bsg_request = fc_lport_bsg_request,
2449 };
2450
2451 /**
2452 * scsi_host_template structure used while registering with SCSI-ml
2453 */
2454 static struct scsi_host_template bnx2fc_shost_template = {
2455 .module = THIS_MODULE,
2456 .name = "Broadcom Offload FCoE Initiator",
2457 .queuecommand = bnx2fc_queuecommand,
2458 .eh_abort_handler = bnx2fc_eh_abort, /* abts */
2459 .eh_device_reset_handler = bnx2fc_eh_device_reset, /* lun reset */
2460 .eh_target_reset_handler = bnx2fc_eh_target_reset, /* tgt reset */
2461 .eh_host_reset_handler = fc_eh_host_reset,
2462 .slave_alloc = fc_slave_alloc,
2463 .change_queue_depth = fc_change_queue_depth,
2464 .change_queue_type = fc_change_queue_type,
2465 .this_id = -1,
2466 .cmd_per_lun = 3,
2467 .can_queue = BNX2FC_CAN_QUEUE,
2468 .use_clustering = ENABLE_CLUSTERING,
2469 .sg_tablesize = BNX2FC_MAX_BDS_PER_CMD,
2470 .max_sectors = 512,
2471 };
2472
2473 static struct libfc_function_template bnx2fc_libfc_fcn_templ = {
2474 .frame_send = bnx2fc_xmit,
2475 .elsct_send = bnx2fc_elsct_send,
2476 .fcp_abort_io = bnx2fc_abort_io,
2477 .fcp_cleanup = bnx2fc_cleanup,
2478 .rport_event_callback = bnx2fc_rport_event_handler,
2479 };
2480
2481 /**
2482 * bnx2fc_cnic_cb - global template of bnx2fc - cnic driver interface
2483 * structure carrying callback function pointers
2484 */
2485 static struct cnic_ulp_ops bnx2fc_cnic_cb = {
2486 .owner = THIS_MODULE,
2487 .cnic_init = bnx2fc_ulp_init,
2488 .cnic_exit = bnx2fc_ulp_exit,
2489 .cnic_start = bnx2fc_ulp_start,
2490 .cnic_stop = bnx2fc_ulp_stop,
2491 .indicate_kcqes = bnx2fc_indicate_kcqe,
2492 .indicate_netevent = bnx2fc_indicate_netevent,
2493 };