[SCSI] fcoe: moves common FCoE library API functions to libfcoe module
[safe/jmp/linux-2.6] / drivers / scsi / fcoe / libfcoe.c
1 /*
2  * Copyright(c) 2009 Intel Corporation. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * You should have received a copy of the GNU General Public License along with
14  * this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16  *
17  * Maintained at www.Open-FCoE.org
18  */
19
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22
23 #include <scsi/libfc.h>
24
25 MODULE_AUTHOR("Open-FCoE.org");
26 MODULE_DESCRIPTION("FCoE");
27 MODULE_LICENSE("GPL v2");
28
29 /**
30  * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
31  * @mac: mac address
32  * @scheme: check port
33  * @port: port indicator for converting
34  *
35  * Returns: u64 fc world wide name
36  */
37 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
38                       unsigned int scheme, unsigned int port)
39 {
40         u64 wwn;
41         u64 host_mac;
42
43         /* The MAC is in NO, so flip only the low 48 bits */
44         host_mac = ((u64) mac[0] << 40) |
45                 ((u64) mac[1] << 32) |
46                 ((u64) mac[2] << 24) |
47                 ((u64) mac[3] << 16) |
48                 ((u64) mac[4] << 8) |
49                 (u64) mac[5];
50
51         WARN_ON(host_mac >= (1ULL << 48));
52         wwn = host_mac | ((u64) scheme << 60);
53         switch (scheme) {
54         case 1:
55                 WARN_ON(port != 0);
56                 break;
57         case 2:
58                 WARN_ON(port >= 0xfff);
59                 wwn |= (u64) port << 48;
60                 break;
61         default:
62                 WARN_ON(1);
63                 break;
64         }
65
66         return wwn;
67 }
68 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
69
70 /**
71  * fcoe_libfc_config() - sets up libfc related properties for lport
72  * @lp: ptr to the fc_lport
73  * @tt: libfc function template
74  *
75  * Returns : 0 for success
76  */
77 int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
78 {
79         /* Set the function pointers set by the LLDD */
80         memcpy(&lp->tt, tt, sizeof(*tt));
81         if (fc_fcp_init(lp))
82                 return -ENOMEM;
83         fc_exch_init(lp);
84         fc_elsct_init(lp);
85         fc_lport_init(lp);
86         fc_rport_init(lp);
87         fc_disc_init(lp);
88
89         return 0;
90 }
91 EXPORT_SYMBOL_GPL(fcoe_libfc_config);