fcoe.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690
  1. /*
  2. * Copyright(c) 2007 - 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. #include <linux/module.h>
  20. #include <linux/spinlock.h>
  21. #include <linux/netdevice.h>
  22. #include <linux/etherdevice.h>
  23. #include <linux/ethtool.h>
  24. #include <linux/if_ether.h>
  25. #include <linux/if_vlan.h>
  26. #include <linux/crc32.h>
  27. #include <linux/slab.h>
  28. #include <linux/cpu.h>
  29. #include <linux/fs.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/ctype.h>
  32. #include <linux/workqueue.h>
  33. #include <net/dcbnl.h>
  34. #include <net/dcbevent.h>
  35. #include <scsi/scsi_tcq.h>
  36. #include <scsi/scsicam.h>
  37. #include <scsi/scsi_transport.h>
  38. #include <scsi/scsi_transport_fc.h>
  39. #include <net/rtnetlink.h>
  40. #include <scsi/fc/fc_encaps.h>
  41. #include <scsi/fc/fc_fip.h>
  42. #include <scsi/libfc.h>
  43. #include <scsi/fc_frame.h>
  44. #include <scsi/libfcoe.h>
  45. #include "fcoe.h"
  46. MODULE_AUTHOR("Open-FCoE.org");
  47. MODULE_DESCRIPTION("FCoE");
  48. MODULE_LICENSE("GPL v2");
  49. /* Performance tuning parameters for fcoe */
  50. static unsigned int fcoe_ddp_min = 4096;
  51. module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
  52. MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
  53. "Direct Data Placement (DDP).");
  54. unsigned int fcoe_debug_logging;
  55. module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
  56. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  57. static DEFINE_MUTEX(fcoe_config_mutex);
  58. static struct workqueue_struct *fcoe_wq;
  59. /* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
  60. static DECLARE_COMPLETION(fcoe_flush_completion);
  61. /* fcoe host list */
  62. /* must only by accessed under the RTNL mutex */
  63. static LIST_HEAD(fcoe_hostlist);
  64. static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
  65. /* Function Prototypes */
  66. static int fcoe_reset(struct Scsi_Host *);
  67. static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
  68. static int fcoe_rcv(struct sk_buff *, struct net_device *,
  69. struct packet_type *, struct net_device *);
  70. static int fcoe_percpu_receive_thread(void *);
  71. static void fcoe_percpu_clean(struct fc_lport *);
  72. static int fcoe_link_speed_update(struct fc_lport *);
  73. static int fcoe_link_ok(struct fc_lport *);
  74. static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
  75. static int fcoe_hostlist_add(const struct fc_lport *);
  76. static int fcoe_device_notification(struct notifier_block *, ulong, void *);
  77. static void fcoe_dev_setup(void);
  78. static void fcoe_dev_cleanup(void);
  79. static struct fcoe_interface
  80. *fcoe_hostlist_lookup_port(const struct net_device *);
  81. static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
  82. struct packet_type *, struct net_device *);
  83. static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
  84. static void fcoe_update_src_mac(struct fc_lport *, u8 *);
  85. static u8 *fcoe_get_src_mac(struct fc_lport *);
  86. static void fcoe_destroy_work(struct work_struct *);
  87. static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
  88. unsigned int);
  89. static int fcoe_ddp_done(struct fc_lport *, u16);
  90. static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
  91. unsigned int);
  92. static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
  93. static int fcoe_dcb_app_notification(struct notifier_block *notifier,
  94. ulong event, void *ptr);
  95. static bool fcoe_match(struct net_device *netdev);
  96. static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
  97. static int fcoe_destroy(struct net_device *netdev);
  98. static int fcoe_enable(struct net_device *netdev);
  99. static int fcoe_disable(struct net_device *netdev);
  100. static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
  101. u32 did, struct fc_frame *,
  102. unsigned int op,
  103. void (*resp)(struct fc_seq *,
  104. struct fc_frame *,
  105. void *),
  106. void *, u32 timeout);
  107. static void fcoe_recv_frame(struct sk_buff *skb);
  108. static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
  109. /* notification function for packets from net device */
  110. static struct notifier_block fcoe_notifier = {
  111. .notifier_call = fcoe_device_notification,
  112. };
  113. /* notification function for CPU hotplug events */
  114. static struct notifier_block fcoe_cpu_notifier = {
  115. .notifier_call = fcoe_cpu_callback,
  116. };
  117. /* notification function for DCB events */
  118. static struct notifier_block dcb_notifier = {
  119. .notifier_call = fcoe_dcb_app_notification,
  120. };
  121. static struct scsi_transport_template *fcoe_nport_scsi_transport;
  122. static struct scsi_transport_template *fcoe_vport_scsi_transport;
  123. static int fcoe_vport_destroy(struct fc_vport *);
  124. static int fcoe_vport_create(struct fc_vport *, bool disabled);
  125. static int fcoe_vport_disable(struct fc_vport *, bool disable);
  126. static void fcoe_set_vport_symbolic_name(struct fc_vport *);
  127. static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
  128. static struct libfc_function_template fcoe_libfc_fcn_templ = {
  129. .frame_send = fcoe_xmit,
  130. .ddp_setup = fcoe_ddp_setup,
  131. .ddp_done = fcoe_ddp_done,
  132. .ddp_target = fcoe_ddp_target,
  133. .elsct_send = fcoe_elsct_send,
  134. .get_lesb = fcoe_get_lesb,
  135. .lport_set_port_id = fcoe_set_port_id,
  136. };
  137. static struct fc_function_template fcoe_nport_fc_functions = {
  138. .show_host_node_name = 1,
  139. .show_host_port_name = 1,
  140. .show_host_supported_classes = 1,
  141. .show_host_supported_fc4s = 1,
  142. .show_host_active_fc4s = 1,
  143. .show_host_maxframe_size = 1,
  144. .show_host_port_id = 1,
  145. .show_host_supported_speeds = 1,
  146. .get_host_speed = fc_get_host_speed,
  147. .show_host_speed = 1,
  148. .show_host_port_type = 1,
  149. .get_host_port_state = fc_get_host_port_state,
  150. .show_host_port_state = 1,
  151. .show_host_symbolic_name = 1,
  152. .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
  153. .show_rport_maxframe_size = 1,
  154. .show_rport_supported_classes = 1,
  155. .show_host_fabric_name = 1,
  156. .show_starget_node_name = 1,
  157. .show_starget_port_name = 1,
  158. .show_starget_port_id = 1,
  159. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  160. .show_rport_dev_loss_tmo = 1,
  161. .get_fc_host_stats = fc_get_host_stats,
  162. .issue_fc_host_lip = fcoe_reset,
  163. .terminate_rport_io = fc_rport_terminate_io,
  164. .vport_create = fcoe_vport_create,
  165. .vport_delete = fcoe_vport_destroy,
  166. .vport_disable = fcoe_vport_disable,
  167. .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
  168. .bsg_request = fc_lport_bsg_request,
  169. };
  170. static struct fc_function_template fcoe_vport_fc_functions = {
  171. .show_host_node_name = 1,
  172. .show_host_port_name = 1,
  173. .show_host_supported_classes = 1,
  174. .show_host_supported_fc4s = 1,
  175. .show_host_active_fc4s = 1,
  176. .show_host_maxframe_size = 1,
  177. .show_host_port_id = 1,
  178. .show_host_supported_speeds = 1,
  179. .get_host_speed = fc_get_host_speed,
  180. .show_host_speed = 1,
  181. .show_host_port_type = 1,
  182. .get_host_port_state = fc_get_host_port_state,
  183. .show_host_port_state = 1,
  184. .show_host_symbolic_name = 1,
  185. .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
  186. .show_rport_maxframe_size = 1,
  187. .show_rport_supported_classes = 1,
  188. .show_host_fabric_name = 1,
  189. .show_starget_node_name = 1,
  190. .show_starget_port_name = 1,
  191. .show_starget_port_id = 1,
  192. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  193. .show_rport_dev_loss_tmo = 1,
  194. .get_fc_host_stats = fc_get_host_stats,
  195. .issue_fc_host_lip = fcoe_reset,
  196. .terminate_rport_io = fc_rport_terminate_io,
  197. .bsg_request = fc_lport_bsg_request,
  198. };
  199. static struct scsi_host_template fcoe_shost_template = {
  200. .module = THIS_MODULE,
  201. .name = "FCoE Driver",
  202. .proc_name = FCOE_NAME,
  203. .queuecommand = fc_queuecommand,
  204. .eh_abort_handler = fc_eh_abort,
  205. .eh_device_reset_handler = fc_eh_device_reset,
  206. .eh_host_reset_handler = fc_eh_host_reset,
  207. .slave_alloc = fc_slave_alloc,
  208. .change_queue_depth = fc_change_queue_depth,
  209. .change_queue_type = fc_change_queue_type,
  210. .this_id = -1,
  211. .cmd_per_lun = 3,
  212. .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
  213. .use_clustering = ENABLE_CLUSTERING,
  214. .sg_tablesize = SG_ALL,
  215. .max_sectors = 0xffff,
  216. };
  217. /**
  218. * fcoe_interface_setup() - Setup a FCoE interface
  219. * @fcoe: The new FCoE interface
  220. * @netdev: The net device that the fcoe interface is on
  221. *
  222. * Returns : 0 for success
  223. * Locking: must be called with the RTNL mutex held
  224. */
  225. static int fcoe_interface_setup(struct fcoe_interface *fcoe,
  226. struct net_device *netdev)
  227. {
  228. struct fcoe_ctlr *fip = &fcoe->ctlr;
  229. struct netdev_hw_addr *ha;
  230. struct net_device *real_dev;
  231. u8 flogi_maddr[ETH_ALEN];
  232. const struct net_device_ops *ops;
  233. fcoe->netdev = netdev;
  234. /* Let LLD initialize for FCoE */
  235. ops = netdev->netdev_ops;
  236. if (ops->ndo_fcoe_enable) {
  237. if (ops->ndo_fcoe_enable(netdev))
  238. FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
  239. " specific feature for LLD.\n");
  240. }
  241. /* Do not support for bonding device */
  242. if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
  243. FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
  244. return -EOPNOTSUPP;
  245. }
  246. /* look for SAN MAC address, if multiple SAN MACs exist, only
  247. * use the first one for SPMA */
  248. real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
  249. vlan_dev_real_dev(netdev) : netdev;
  250. fcoe->realdev = real_dev;
  251. rcu_read_lock();
  252. for_each_dev_addr(real_dev, ha) {
  253. if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
  254. (is_valid_ether_addr(ha->addr))) {
  255. memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
  256. fip->spma = 1;
  257. break;
  258. }
  259. }
  260. rcu_read_unlock();
  261. /* setup Source Mac Address */
  262. if (!fip->spma)
  263. memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
  264. /*
  265. * Add FCoE MAC address as second unicast MAC address
  266. * or enter promiscuous mode if not capable of listening
  267. * for multiple unicast MACs.
  268. */
  269. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  270. dev_uc_add(netdev, flogi_maddr);
  271. if (fip->spma)
  272. dev_uc_add(netdev, fip->ctl_src_addr);
  273. if (fip->mode == FIP_MODE_VN2VN) {
  274. dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
  275. dev_mc_add(netdev, FIP_ALL_P2P_MACS);
  276. } else
  277. dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
  278. /*
  279. * setup the receive function from ethernet driver
  280. * on the ethertype for the given device
  281. */
  282. fcoe->fcoe_packet_type.func = fcoe_rcv;
  283. fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  284. fcoe->fcoe_packet_type.dev = netdev;
  285. dev_add_pack(&fcoe->fcoe_packet_type);
  286. fcoe->fip_packet_type.func = fcoe_fip_recv;
  287. fcoe->fip_packet_type.type = htons(ETH_P_FIP);
  288. fcoe->fip_packet_type.dev = netdev;
  289. dev_add_pack(&fcoe->fip_packet_type);
  290. return 0;
  291. }
  292. /**
  293. * fcoe_interface_create() - Create a FCoE interface on a net device
  294. * @netdev: The net device to create the FCoE interface on
  295. * @fip_mode: The mode to use for FIP
  296. *
  297. * Returns: pointer to a struct fcoe_interface or NULL on error
  298. */
  299. static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
  300. enum fip_state fip_mode)
  301. {
  302. struct fcoe_interface *fcoe;
  303. int err;
  304. if (!try_module_get(THIS_MODULE)) {
  305. FCOE_NETDEV_DBG(netdev,
  306. "Could not get a reference to the module\n");
  307. fcoe = ERR_PTR(-EBUSY);
  308. goto out;
  309. }
  310. fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
  311. if (!fcoe) {
  312. FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
  313. fcoe = ERR_PTR(-ENOMEM);
  314. goto out_nomod;
  315. }
  316. dev_hold(netdev);
  317. kref_init(&fcoe->kref);
  318. /*
  319. * Initialize FIP.
  320. */
  321. fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
  322. fcoe->ctlr.send = fcoe_fip_send;
  323. fcoe->ctlr.update_mac = fcoe_update_src_mac;
  324. fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
  325. err = fcoe_interface_setup(fcoe, netdev);
  326. if (err) {
  327. fcoe_ctlr_destroy(&fcoe->ctlr);
  328. kfree(fcoe);
  329. dev_put(netdev);
  330. fcoe = ERR_PTR(err);
  331. goto out_nomod;
  332. }
  333. goto out;
  334. out_nomod:
  335. module_put(THIS_MODULE);
  336. out:
  337. return fcoe;
  338. }
  339. /**
  340. * fcoe_interface_release() - fcoe_port kref release function
  341. * @kref: Embedded reference count in an fcoe_interface struct
  342. */
  343. static void fcoe_interface_release(struct kref *kref)
  344. {
  345. struct fcoe_interface *fcoe;
  346. struct net_device *netdev;
  347. fcoe = container_of(kref, struct fcoe_interface, kref);
  348. netdev = fcoe->netdev;
  349. /* tear-down the FCoE controller */
  350. fcoe_ctlr_destroy(&fcoe->ctlr);
  351. kfree(fcoe);
  352. dev_put(netdev);
  353. module_put(THIS_MODULE);
  354. }
  355. /**
  356. * fcoe_interface_get() - Get a reference to a FCoE interface
  357. * @fcoe: The FCoE interface to be held
  358. */
  359. static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
  360. {
  361. kref_get(&fcoe->kref);
  362. }
  363. /**
  364. * fcoe_interface_put() - Put a reference to a FCoE interface
  365. * @fcoe: The FCoE interface to be released
  366. */
  367. static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
  368. {
  369. kref_put(&fcoe->kref, fcoe_interface_release);
  370. }
  371. /**
  372. * fcoe_interface_cleanup() - Clean up a FCoE interface
  373. * @fcoe: The FCoE interface to be cleaned up
  374. *
  375. * Caller must be holding the RTNL mutex
  376. */
  377. static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
  378. {
  379. struct net_device *netdev = fcoe->netdev;
  380. struct fcoe_ctlr *fip = &fcoe->ctlr;
  381. u8 flogi_maddr[ETH_ALEN];
  382. const struct net_device_ops *ops;
  383. rtnl_lock();
  384. /*
  385. * Don't listen for Ethernet packets anymore.
  386. * synchronize_net() ensures that the packet handlers are not running
  387. * on another CPU. dev_remove_pack() would do that, this calls the
  388. * unsyncronized version __dev_remove_pack() to avoid multiple delays.
  389. */
  390. __dev_remove_pack(&fcoe->fcoe_packet_type);
  391. __dev_remove_pack(&fcoe->fip_packet_type);
  392. synchronize_net();
  393. /* Delete secondary MAC addresses */
  394. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  395. dev_uc_del(netdev, flogi_maddr);
  396. if (fip->spma)
  397. dev_uc_del(netdev, fip->ctl_src_addr);
  398. if (fip->mode == FIP_MODE_VN2VN) {
  399. dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
  400. dev_mc_del(netdev, FIP_ALL_P2P_MACS);
  401. } else
  402. dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
  403. /* Tell the LLD we are done w/ FCoE */
  404. ops = netdev->netdev_ops;
  405. if (ops->ndo_fcoe_disable) {
  406. if (ops->ndo_fcoe_disable(netdev))
  407. FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
  408. " specific feature for LLD.\n");
  409. }
  410. rtnl_unlock();
  411. /* Release the self-reference taken during fcoe_interface_create() */
  412. fcoe_interface_put(fcoe);
  413. }
  414. /**
  415. * fcoe_fip_recv() - Handler for received FIP frames
  416. * @skb: The receive skb
  417. * @netdev: The associated net device
  418. * @ptype: The packet_type structure which was used to register this handler
  419. * @orig_dev: The original net_device the the skb was received on.
  420. * (in case dev is a bond)
  421. *
  422. * Returns: 0 for success
  423. */
  424. static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
  425. struct packet_type *ptype,
  426. struct net_device *orig_dev)
  427. {
  428. struct fcoe_interface *fcoe;
  429. fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
  430. fcoe_ctlr_recv(&fcoe->ctlr, skb);
  431. return 0;
  432. }
  433. /**
  434. * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
  435. * @port: The FCoE port
  436. * @skb: The FIP/FCoE packet to be sent
  437. */
  438. static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
  439. {
  440. if (port->fcoe_pending_queue.qlen)
  441. fcoe_check_wait_queue(port->lport, skb);
  442. else if (fcoe_start_io(skb))
  443. fcoe_check_wait_queue(port->lport, skb);
  444. }
  445. /**
  446. * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
  447. * @fip: The FCoE controller
  448. * @skb: The FIP packet to be sent
  449. */
  450. static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
  451. {
  452. skb->dev = fcoe_from_ctlr(fip)->netdev;
  453. fcoe_port_send(lport_priv(fip->lp), skb);
  454. }
  455. /**
  456. * fcoe_update_src_mac() - Update the Ethernet MAC filters
  457. * @lport: The local port to update the source MAC on
  458. * @addr: Unicast MAC address to add
  459. *
  460. * Remove any previously-set unicast MAC filter.
  461. * Add secondary FCoE MAC address filter for our OUI.
  462. */
  463. static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
  464. {
  465. struct fcoe_port *port = lport_priv(lport);
  466. struct fcoe_interface *fcoe = port->priv;
  467. rtnl_lock();
  468. if (!is_zero_ether_addr(port->data_src_addr))
  469. dev_uc_del(fcoe->netdev, port->data_src_addr);
  470. if (!is_zero_ether_addr(addr))
  471. dev_uc_add(fcoe->netdev, addr);
  472. memcpy(port->data_src_addr, addr, ETH_ALEN);
  473. rtnl_unlock();
  474. }
  475. /**
  476. * fcoe_get_src_mac() - return the Ethernet source address for an lport
  477. * @lport: libfc lport
  478. */
  479. static u8 *fcoe_get_src_mac(struct fc_lport *lport)
  480. {
  481. struct fcoe_port *port = lport_priv(lport);
  482. return port->data_src_addr;
  483. }
  484. /**
  485. * fcoe_lport_config() - Set up a local port
  486. * @lport: The local port to be setup
  487. *
  488. * Returns: 0 for success
  489. */
  490. static int fcoe_lport_config(struct fc_lport *lport)
  491. {
  492. lport->link_up = 0;
  493. lport->qfull = 0;
  494. lport->max_retry_count = 3;
  495. lport->max_rport_retry_count = 3;
  496. lport->e_d_tov = 2 * 1000; /* FC-FS default */
  497. lport->r_a_tov = 2 * 2 * 1000;
  498. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  499. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  500. lport->does_npiv = 1;
  501. fc_lport_init_stats(lport);
  502. /* lport fc_lport related configuration */
  503. fc_lport_config(lport);
  504. /* offload related configuration */
  505. lport->crc_offload = 0;
  506. lport->seq_offload = 0;
  507. lport->lro_enabled = 0;
  508. lport->lro_xid = 0;
  509. lport->lso_max = 0;
  510. return 0;
  511. }
  512. /**
  513. * fcoe_netdev_features_change - Updates the lport's offload flags based
  514. * on the LLD netdev's FCoE feature flags
  515. */
  516. static void fcoe_netdev_features_change(struct fc_lport *lport,
  517. struct net_device *netdev)
  518. {
  519. mutex_lock(&lport->lp_mutex);
  520. if (netdev->features & NETIF_F_SG)
  521. lport->sg_supp = 1;
  522. else
  523. lport->sg_supp = 0;
  524. if (netdev->features & NETIF_F_FCOE_CRC) {
  525. lport->crc_offload = 1;
  526. FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
  527. } else {
  528. lport->crc_offload = 0;
  529. }
  530. if (netdev->features & NETIF_F_FSO) {
  531. lport->seq_offload = 1;
  532. lport->lso_max = netdev->gso_max_size;
  533. FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
  534. lport->lso_max);
  535. } else {
  536. lport->seq_offload = 0;
  537. lport->lso_max = 0;
  538. }
  539. if (netdev->fcoe_ddp_xid) {
  540. lport->lro_enabled = 1;
  541. lport->lro_xid = netdev->fcoe_ddp_xid;
  542. FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
  543. lport->lro_xid);
  544. } else {
  545. lport->lro_enabled = 0;
  546. lport->lro_xid = 0;
  547. }
  548. mutex_unlock(&lport->lp_mutex);
  549. }
  550. /**
  551. * fcoe_netdev_config() - Set up net devive for SW FCoE
  552. * @lport: The local port that is associated with the net device
  553. * @netdev: The associated net device
  554. *
  555. * Must be called after fcoe_lport_config() as it will use local port mutex
  556. *
  557. * Returns: 0 for success
  558. */
  559. static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
  560. {
  561. u32 mfs;
  562. u64 wwnn, wwpn;
  563. struct fcoe_interface *fcoe;
  564. struct fcoe_port *port;
  565. /* Setup lport private data to point to fcoe softc */
  566. port = lport_priv(lport);
  567. fcoe = port->priv;
  568. /*
  569. * Determine max frame size based on underlying device and optional
  570. * user-configured limit. If the MFS is too low, fcoe_link_ok()
  571. * will return 0, so do this first.
  572. */
  573. mfs = netdev->mtu;
  574. if (netdev->features & NETIF_F_FCOE_MTU) {
  575. mfs = FCOE_MTU;
  576. FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
  577. }
  578. mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
  579. if (fc_set_mfs(lport, mfs))
  580. return -EINVAL;
  581. /* offload features support */
  582. fcoe_netdev_features_change(lport, netdev);
  583. skb_queue_head_init(&port->fcoe_pending_queue);
  584. port->fcoe_pending_queue_active = 0;
  585. setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
  586. fcoe_link_speed_update(lport);
  587. if (!lport->vport) {
  588. if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
  589. wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
  590. fc_set_wwnn(lport, wwnn);
  591. if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
  592. wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
  593. 2, 0);
  594. fc_set_wwpn(lport, wwpn);
  595. }
  596. return 0;
  597. }
  598. /**
  599. * fcoe_shost_config() - Set up the SCSI host associated with a local port
  600. * @lport: The local port
  601. * @dev: The device associated with the SCSI host
  602. *
  603. * Must be called after fcoe_lport_config() and fcoe_netdev_config()
  604. *
  605. * Returns: 0 for success
  606. */
  607. static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
  608. {
  609. int rc = 0;
  610. /* lport scsi host config */
  611. lport->host->max_lun = FCOE_MAX_LUN;
  612. lport->host->max_id = FCOE_MAX_FCP_TARGET;
  613. lport->host->max_channel = 0;
  614. lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
  615. if (lport->vport)
  616. lport->host->transportt = fcoe_vport_scsi_transport;
  617. else
  618. lport->host->transportt = fcoe_nport_scsi_transport;
  619. /* add the new host to the SCSI-ml */
  620. rc = scsi_add_host(lport->host, dev);
  621. if (rc) {
  622. FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
  623. "error on scsi_add_host\n");
  624. return rc;
  625. }
  626. if (!lport->vport)
  627. fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
  628. snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
  629. "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
  630. fcoe_netdev(lport)->name);
  631. return 0;
  632. }
  633. /**
  634. * fcoe_oem_match() - The match routine for the offloaded exchange manager
  635. * @fp: The I/O frame
  636. *
  637. * This routine will be associated with an exchange manager (EM). When
  638. * the libfc exchange handling code is looking for an EM to use it will
  639. * call this routine and pass it the frame that it wishes to send. This
  640. * routine will return True if the associated EM is to be used and False
  641. * if the echange code should continue looking for an EM.
  642. *
  643. * The offload EM that this routine is associated with will handle any
  644. * packets that are for SCSI read requests.
  645. *
  646. * This has been enhanced to work when FCoE stack is operating in target
  647. * mode.
  648. *
  649. * Returns: True for read types I/O, otherwise returns false.
  650. */
  651. static bool fcoe_oem_match(struct fc_frame *fp)
  652. {
  653. struct fc_frame_header *fh = fc_frame_header_get(fp);
  654. struct fcp_cmnd *fcp;
  655. if (fc_fcp_is_read(fr_fsp(fp)) &&
  656. (fr_fsp(fp)->data_len > fcoe_ddp_min))
  657. return true;
  658. else if ((fr_fsp(fp) == NULL) &&
  659. (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
  660. (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
  661. fcp = fc_frame_payload_get(fp, sizeof(*fcp));
  662. if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
  663. (ntohl(fcp->fc_dl) > fcoe_ddp_min))
  664. return true;
  665. }
  666. return false;
  667. }
  668. /**
  669. * fcoe_em_config() - Allocate and configure an exchange manager
  670. * @lport: The local port that the new EM will be associated with
  671. *
  672. * Returns: 0 on success
  673. */
  674. static inline int fcoe_em_config(struct fc_lport *lport)
  675. {
  676. struct fcoe_port *port = lport_priv(lport);
  677. struct fcoe_interface *fcoe = port->priv;
  678. struct fcoe_interface *oldfcoe = NULL;
  679. struct net_device *old_real_dev, *cur_real_dev;
  680. u16 min_xid = FCOE_MIN_XID;
  681. u16 max_xid = FCOE_MAX_XID;
  682. /*
  683. * Check if need to allocate an em instance for
  684. * offload exchange ids to be shared across all VN_PORTs/lport.
  685. */
  686. if (!lport->lro_enabled || !lport->lro_xid ||
  687. (lport->lro_xid >= max_xid)) {
  688. lport->lro_xid = 0;
  689. goto skip_oem;
  690. }
  691. /*
  692. * Reuse existing offload em instance in case
  693. * it is already allocated on real eth device
  694. */
  695. if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
  696. cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
  697. else
  698. cur_real_dev = fcoe->netdev;
  699. list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
  700. if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
  701. old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
  702. else
  703. old_real_dev = oldfcoe->netdev;
  704. if (cur_real_dev == old_real_dev) {
  705. fcoe->oem = oldfcoe->oem;
  706. break;
  707. }
  708. }
  709. if (fcoe->oem) {
  710. if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
  711. printk(KERN_ERR "fcoe_em_config: failed to add "
  712. "offload em:%p on interface:%s\n",
  713. fcoe->oem, fcoe->netdev->name);
  714. return -ENOMEM;
  715. }
  716. } else {
  717. fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
  718. FCOE_MIN_XID, lport->lro_xid,
  719. fcoe_oem_match);
  720. if (!fcoe->oem) {
  721. printk(KERN_ERR "fcoe_em_config: failed to allocate "
  722. "em for offload exches on interface:%s\n",
  723. fcoe->netdev->name);
  724. return -ENOMEM;
  725. }
  726. }
  727. /*
  728. * Exclude offload EM xid range from next EM xid range.
  729. */
  730. min_xid += lport->lro_xid + 1;
  731. skip_oem:
  732. if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
  733. printk(KERN_ERR "fcoe_em_config: failed to "
  734. "allocate em on interface %s\n", fcoe->netdev->name);
  735. return -ENOMEM;
  736. }
  737. return 0;
  738. }
  739. /**
  740. * fcoe_if_destroy() - Tear down a SW FCoE instance
  741. * @lport: The local port to be destroyed
  742. *
  743. */
  744. static void fcoe_if_destroy(struct fc_lport *lport)
  745. {
  746. struct fcoe_port *port = lport_priv(lport);
  747. struct fcoe_interface *fcoe = port->priv;
  748. struct net_device *netdev = fcoe->netdev;
  749. FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
  750. /* Logout of the fabric */
  751. fc_fabric_logoff(lport);
  752. /* Cleanup the fc_lport */
  753. fc_lport_destroy(lport);
  754. /* Stop the transmit retry timer */
  755. del_timer_sync(&port->timer);
  756. /* Free existing transmit skbs */
  757. fcoe_clean_pending_queue(lport);
  758. rtnl_lock();
  759. if (!is_zero_ether_addr(port->data_src_addr))
  760. dev_uc_del(netdev, port->data_src_addr);
  761. rtnl_unlock();
  762. /* Release reference held in fcoe_if_create() */
  763. fcoe_interface_put(fcoe);
  764. /* Free queued packets for the per-CPU receive threads */
  765. fcoe_percpu_clean(lport);
  766. /* Detach from the scsi-ml */
  767. fc_remove_host(lport->host);
  768. scsi_remove_host(lport->host);
  769. /* Destroy lport scsi_priv */
  770. fc_fcp_destroy(lport);
  771. /* There are no more rports or I/O, free the EM */
  772. fc_exch_mgr_free(lport);
  773. /* Free memory used by statistical counters */
  774. fc_lport_free_stats(lport);
  775. /* Release the Scsi_Host */
  776. scsi_host_put(lport->host);
  777. }
  778. /**
  779. * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
  780. * @lport: The local port to setup DDP for
  781. * @xid: The exchange ID for this DDP transfer
  782. * @sgl: The scatterlist describing this transfer
  783. * @sgc: The number of sg items
  784. *
  785. * Returns: 0 if the DDP context was not configured
  786. */
  787. static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
  788. struct scatterlist *sgl, unsigned int sgc)
  789. {
  790. struct net_device *netdev = fcoe_netdev(lport);
  791. if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
  792. return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
  793. xid, sgl,
  794. sgc);
  795. return 0;
  796. }
  797. /**
  798. * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
  799. * @lport: The local port to setup DDP for
  800. * @xid: The exchange ID for this DDP transfer
  801. * @sgl: The scatterlist describing this transfer
  802. * @sgc: The number of sg items
  803. *
  804. * Returns: 0 if the DDP context was not configured
  805. */
  806. static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
  807. struct scatterlist *sgl, unsigned int sgc)
  808. {
  809. struct net_device *netdev = fcoe_netdev(lport);
  810. if (netdev->netdev_ops->ndo_fcoe_ddp_target)
  811. return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
  812. sgl, sgc);
  813. return 0;
  814. }
  815. /**
  816. * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
  817. * @lport: The local port to complete DDP on
  818. * @xid: The exchange ID for this DDP transfer
  819. *
  820. * Returns: the length of data that have been completed by DDP
  821. */
  822. static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
  823. {
  824. struct net_device *netdev = fcoe_netdev(lport);
  825. if (netdev->netdev_ops->ndo_fcoe_ddp_done)
  826. return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
  827. return 0;
  828. }
  829. /**
  830. * fcoe_if_create() - Create a FCoE instance on an interface
  831. * @fcoe: The FCoE interface to create a local port on
  832. * @parent: The device pointer to be the parent in sysfs for the SCSI host
  833. * @npiv: Indicates if the port is a vport or not
  834. *
  835. * Creates a fc_lport instance and a Scsi_Host instance and configure them.
  836. *
  837. * Returns: The allocated fc_lport or an error pointer
  838. */
  839. static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
  840. struct device *parent, int npiv)
  841. {
  842. struct net_device *netdev = fcoe->netdev;
  843. struct fc_lport *lport, *n_port;
  844. struct fcoe_port *port;
  845. struct Scsi_Host *shost;
  846. int rc;
  847. /*
  848. * parent is only a vport if npiv is 1,
  849. * but we'll only use vport in that case so go ahead and set it
  850. */
  851. struct fc_vport *vport = dev_to_vport(parent);
  852. FCOE_NETDEV_DBG(netdev, "Create Interface\n");
  853. if (!npiv)
  854. lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
  855. else
  856. lport = libfc_vport_create(vport, sizeof(*port));
  857. if (!lport) {
  858. FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
  859. rc = -ENOMEM;
  860. goto out;
  861. }
  862. port = lport_priv(lport);
  863. port->lport = lport;
  864. port->priv = fcoe;
  865. port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
  866. port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
  867. INIT_WORK(&port->destroy_work, fcoe_destroy_work);
  868. /* configure a fc_lport including the exchange manager */
  869. rc = fcoe_lport_config(lport);
  870. if (rc) {
  871. FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
  872. "interface\n");
  873. goto out_host_put;
  874. }
  875. if (npiv) {
  876. FCOE_NETDEV_DBG(netdev, "Setting vport names, "
  877. "%16.16llx %16.16llx\n",
  878. vport->node_name, vport->port_name);
  879. fc_set_wwnn(lport, vport->node_name);
  880. fc_set_wwpn(lport, vport->port_name);
  881. }
  882. /* configure lport network properties */
  883. rc = fcoe_netdev_config(lport, netdev);
  884. if (rc) {
  885. FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
  886. "interface\n");
  887. goto out_lp_destroy;
  888. }
  889. /* configure lport scsi host properties */
  890. rc = fcoe_shost_config(lport, parent);
  891. if (rc) {
  892. FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
  893. "interface\n");
  894. goto out_lp_destroy;
  895. }
  896. /* Initialize the library */
  897. rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
  898. if (rc) {
  899. FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
  900. "interface\n");
  901. goto out_lp_destroy;
  902. }
  903. /*
  904. * fcoe_em_alloc() and fcoe_hostlist_add() both
  905. * need to be atomic with respect to other changes to the
  906. * hostlist since fcoe_em_alloc() looks for an existing EM
  907. * instance on host list updated by fcoe_hostlist_add().
  908. *
  909. * This is currently handled through the fcoe_config_mutex
  910. * begin held.
  911. */
  912. if (!npiv)
  913. /* lport exch manager allocation */
  914. rc = fcoe_em_config(lport);
  915. else {
  916. shost = vport_to_shost(vport);
  917. n_port = shost_priv(shost);
  918. rc = fc_exch_mgr_list_clone(n_port, lport);
  919. }
  920. if (rc) {
  921. FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
  922. goto out_lp_destroy;
  923. }
  924. fcoe_interface_get(fcoe);
  925. return lport;
  926. out_lp_destroy:
  927. fc_exch_mgr_free(lport);
  928. out_host_put:
  929. scsi_host_put(lport->host);
  930. out:
  931. return ERR_PTR(rc);
  932. }
  933. /**
  934. * fcoe_if_init() - Initialization routine for fcoe.ko
  935. *
  936. * Attaches the SW FCoE transport to the FC transport
  937. *
  938. * Returns: 0 on success
  939. */
  940. static int __init fcoe_if_init(void)
  941. {
  942. /* attach to scsi transport */
  943. fcoe_nport_scsi_transport =
  944. fc_attach_transport(&fcoe_nport_fc_functions);
  945. fcoe_vport_scsi_transport =
  946. fc_attach_transport(&fcoe_vport_fc_functions);
  947. if (!fcoe_nport_scsi_transport) {
  948. printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
  949. return -ENODEV;
  950. }
  951. return 0;
  952. }
  953. /**
  954. * fcoe_if_exit() - Tear down fcoe.ko
  955. *
  956. * Detaches the SW FCoE transport from the FC transport
  957. *
  958. * Returns: 0 on success
  959. */
  960. static int __exit fcoe_if_exit(void)
  961. {
  962. fc_release_transport(fcoe_nport_scsi_transport);
  963. fc_release_transport(fcoe_vport_scsi_transport);
  964. fcoe_nport_scsi_transport = NULL;
  965. fcoe_vport_scsi_transport = NULL;
  966. return 0;
  967. }
  968. /**
  969. * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
  970. * @cpu: The CPU index of the CPU to create a receive thread for
  971. */
  972. static void fcoe_percpu_thread_create(unsigned int cpu)
  973. {
  974. struct fcoe_percpu_s *p;
  975. struct task_struct *thread;
  976. p = &per_cpu(fcoe_percpu, cpu);
  977. thread = kthread_create_on_node(fcoe_percpu_receive_thread,
  978. (void *)p, cpu_to_node(cpu),
  979. "fcoethread/%d", cpu);
  980. if (likely(!IS_ERR(thread))) {
  981. kthread_bind(thread, cpu);
  982. wake_up_process(thread);
  983. spin_lock_bh(&p->fcoe_rx_list.lock);
  984. p->thread = thread;
  985. spin_unlock_bh(&p->fcoe_rx_list.lock);
  986. }
  987. }
  988. /**
  989. * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
  990. * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
  991. *
  992. * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
  993. * current CPU's Rx thread. If the thread being destroyed is bound to
  994. * the CPU processing this context the skbs will be freed.
  995. */
  996. static void fcoe_percpu_thread_destroy(unsigned int cpu)
  997. {
  998. struct fcoe_percpu_s *p;
  999. struct task_struct *thread;
  1000. struct page *crc_eof;
  1001. struct sk_buff *skb;
  1002. #ifdef CONFIG_SMP
  1003. struct fcoe_percpu_s *p0;
  1004. unsigned targ_cpu = get_cpu();
  1005. #endif /* CONFIG_SMP */
  1006. FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
  1007. /* Prevent any new skbs from being queued for this CPU. */
  1008. p = &per_cpu(fcoe_percpu, cpu);
  1009. spin_lock_bh(&p->fcoe_rx_list.lock);
  1010. thread = p->thread;
  1011. p->thread = NULL;
  1012. crc_eof = p->crc_eof_page;
  1013. p->crc_eof_page = NULL;
  1014. p->crc_eof_offset = 0;
  1015. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1016. #ifdef CONFIG_SMP
  1017. /*
  1018. * Don't bother moving the skb's if this context is running
  1019. * on the same CPU that is having its thread destroyed. This
  1020. * can easily happen when the module is removed.
  1021. */
  1022. if (cpu != targ_cpu) {
  1023. p0 = &per_cpu(fcoe_percpu, targ_cpu);
  1024. spin_lock_bh(&p0->fcoe_rx_list.lock);
  1025. if (p0->thread) {
  1026. FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
  1027. cpu, targ_cpu);
  1028. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1029. __skb_queue_tail(&p0->fcoe_rx_list, skb);
  1030. spin_unlock_bh(&p0->fcoe_rx_list.lock);
  1031. } else {
  1032. /*
  1033. * The targeted CPU is not initialized and cannot accept
  1034. * new skbs. Unlock the targeted CPU and drop the skbs
  1035. * on the CPU that is going offline.
  1036. */
  1037. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1038. kfree_skb(skb);
  1039. spin_unlock_bh(&p0->fcoe_rx_list.lock);
  1040. }
  1041. } else {
  1042. /*
  1043. * This scenario occurs when the module is being removed
  1044. * and all threads are being destroyed. skbs will continue
  1045. * to be shifted from the CPU thread that is being removed
  1046. * to the CPU thread associated with the CPU that is processing
  1047. * the module removal. Once there is only one CPU Rx thread it
  1048. * will reach this case and we will drop all skbs and later
  1049. * stop the thread.
  1050. */
  1051. spin_lock_bh(&p->fcoe_rx_list.lock);
  1052. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1053. kfree_skb(skb);
  1054. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1055. }
  1056. put_cpu();
  1057. #else
  1058. /*
  1059. * This a non-SMP scenario where the singular Rx thread is
  1060. * being removed. Free all skbs and stop the thread.
  1061. */
  1062. spin_lock_bh(&p->fcoe_rx_list.lock);
  1063. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  1064. kfree_skb(skb);
  1065. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1066. #endif
  1067. if (thread)
  1068. kthread_stop(thread);
  1069. if (crc_eof)
  1070. put_page(crc_eof);
  1071. }
  1072. /**
  1073. * fcoe_cpu_callback() - Handler for CPU hotplug events
  1074. * @nfb: The callback data block
  1075. * @action: The event triggering the callback
  1076. * @hcpu: The index of the CPU that the event is for
  1077. *
  1078. * This creates or destroys per-CPU data for fcoe
  1079. *
  1080. * Returns NOTIFY_OK always.
  1081. */
  1082. static int fcoe_cpu_callback(struct notifier_block *nfb,
  1083. unsigned long action, void *hcpu)
  1084. {
  1085. unsigned cpu = (unsigned long)hcpu;
  1086. switch (action) {
  1087. case CPU_ONLINE:
  1088. case CPU_ONLINE_FROZEN:
  1089. FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
  1090. fcoe_percpu_thread_create(cpu);
  1091. break;
  1092. case CPU_DEAD:
  1093. case CPU_DEAD_FROZEN:
  1094. FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
  1095. fcoe_percpu_thread_destroy(cpu);
  1096. break;
  1097. default:
  1098. break;
  1099. }
  1100. return NOTIFY_OK;
  1101. }
  1102. /**
  1103. * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
  1104. * command.
  1105. *
  1106. * This routine selects next CPU based on cpumask to distribute
  1107. * incoming requests in round robin.
  1108. *
  1109. * Returns: int CPU number
  1110. */
  1111. static inline unsigned int fcoe_select_cpu(void)
  1112. {
  1113. static unsigned int selected_cpu;
  1114. selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
  1115. if (selected_cpu >= nr_cpu_ids)
  1116. selected_cpu = cpumask_first(cpu_online_mask);
  1117. return selected_cpu;
  1118. }
  1119. /**
  1120. * fcoe_rcv() - Receive packets from a net device
  1121. * @skb: The received packet
  1122. * @netdev: The net device that the packet was received on
  1123. * @ptype: The packet type context
  1124. * @olddev: The last device net device
  1125. *
  1126. * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
  1127. * FC frame and passes the frame to libfc.
  1128. *
  1129. * Returns: 0 for success
  1130. */
  1131. static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
  1132. struct packet_type *ptype, struct net_device *olddev)
  1133. {
  1134. struct fc_lport *lport;
  1135. struct fcoe_rcv_info *fr;
  1136. struct fcoe_interface *fcoe;
  1137. struct fc_frame_header *fh;
  1138. struct fcoe_percpu_s *fps;
  1139. struct ethhdr *eh;
  1140. unsigned int cpu;
  1141. fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
  1142. lport = fcoe->ctlr.lp;
  1143. if (unlikely(!lport)) {
  1144. FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
  1145. goto err2;
  1146. }
  1147. if (!lport->link_up)
  1148. goto err2;
  1149. FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
  1150. "data:%p tail:%p end:%p sum:%d dev:%s",
  1151. skb->len, skb->data_len, skb->head, skb->data,
  1152. skb_tail_pointer(skb), skb_end_pointer(skb),
  1153. skb->csum, skb->dev ? skb->dev->name : "<NULL>");
  1154. eh = eth_hdr(skb);
  1155. if (is_fip_mode(&fcoe->ctlr) &&
  1156. compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
  1157. FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
  1158. eh->h_source);
  1159. goto err;
  1160. }
  1161. /*
  1162. * Check for minimum frame length, and make sure required FCoE
  1163. * and FC headers are pulled into the linear data area.
  1164. */
  1165. if (unlikely((skb->len < FCOE_MIN_FRAME) ||
  1166. !pskb_may_pull(skb, FCOE_HEADER_LEN)))
  1167. goto err;
  1168. skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
  1169. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1170. if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
  1171. FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
  1172. eh->h_dest);
  1173. goto err;
  1174. }
  1175. fr = fcoe_dev_from_skb(skb);
  1176. fr->fr_dev = lport;
  1177. /*
  1178. * In case the incoming frame's exchange is originated from
  1179. * the initiator, then received frame's exchange id is ANDed
  1180. * with fc_cpu_mask bits to get the same cpu on which exchange
  1181. * was originated, otherwise select cpu using rx exchange id
  1182. * or fcoe_select_cpu().
  1183. */
  1184. if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
  1185. cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
  1186. else {
  1187. if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
  1188. cpu = fcoe_select_cpu();
  1189. else
  1190. cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
  1191. }
  1192. if (cpu >= nr_cpu_ids)
  1193. goto err;
  1194. fps = &per_cpu(fcoe_percpu, cpu);
  1195. spin_lock_bh(&fps->fcoe_rx_list.lock);
  1196. if (unlikely(!fps->thread)) {
  1197. /*
  1198. * The targeted CPU is not ready, let's target
  1199. * the first CPU now. For non-SMP systems this
  1200. * will check the same CPU twice.
  1201. */
  1202. FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
  1203. "ready for incoming skb- using first online "
  1204. "CPU.\n");
  1205. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  1206. cpu = cpumask_first(cpu_online_mask);
  1207. fps = &per_cpu(fcoe_percpu, cpu);
  1208. spin_lock_bh(&fps->fcoe_rx_list.lock);
  1209. if (!fps->thread) {
  1210. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  1211. goto err;
  1212. }
  1213. }
  1214. /*
  1215. * We now have a valid CPU that we're targeting for
  1216. * this skb. We also have this receive thread locked,
  1217. * so we're free to queue skbs into it's queue.
  1218. */
  1219. /* If this is a SCSI-FCP frame, and this is already executing on the
  1220. * correct CPU, and the queue for this CPU is empty, then go ahead
  1221. * and process the frame directly in the softirq context.
  1222. * This lets us process completions without context switching from the
  1223. * NET_RX softirq, to our receive processing thread, and then back to
  1224. * BLOCK softirq context.
  1225. */
  1226. if (fh->fh_type == FC_TYPE_FCP &&
  1227. cpu == smp_processor_id() &&
  1228. skb_queue_empty(&fps->fcoe_rx_list)) {
  1229. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  1230. fcoe_recv_frame(skb);
  1231. } else {
  1232. __skb_queue_tail(&fps->fcoe_rx_list, skb);
  1233. if (fps->fcoe_rx_list.qlen == 1)
  1234. wake_up_process(fps->thread);
  1235. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  1236. }
  1237. return 0;
  1238. err:
  1239. per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
  1240. put_cpu();
  1241. err2:
  1242. kfree_skb(skb);
  1243. return -1;
  1244. }
  1245. /**
  1246. * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
  1247. * @skb: The packet to be transmitted
  1248. * @tlen: The total length of the trailer
  1249. *
  1250. * Returns: 0 for success
  1251. */
  1252. static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
  1253. {
  1254. struct fcoe_percpu_s *fps;
  1255. int rc;
  1256. fps = &get_cpu_var(fcoe_percpu);
  1257. rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
  1258. put_cpu_var(fcoe_percpu);
  1259. return rc;
  1260. }
  1261. /**
  1262. * fcoe_xmit() - Transmit a FCoE frame
  1263. * @lport: The local port that the frame is to be transmitted for
  1264. * @fp: The frame to be transmitted
  1265. *
  1266. * Return: 0 for success
  1267. */
  1268. static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
  1269. {
  1270. int wlen;
  1271. u32 crc;
  1272. struct ethhdr *eh;
  1273. struct fcoe_crc_eof *cp;
  1274. struct sk_buff *skb;
  1275. struct fcoe_dev_stats *stats;
  1276. struct fc_frame_header *fh;
  1277. unsigned int hlen; /* header length implies the version */
  1278. unsigned int tlen; /* trailer length */
  1279. unsigned int elen; /* eth header, may include vlan */
  1280. struct fcoe_port *port = lport_priv(lport);
  1281. struct fcoe_interface *fcoe = port->priv;
  1282. u8 sof, eof;
  1283. struct fcoe_hdr *hp;
  1284. WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
  1285. fh = fc_frame_header_get(fp);
  1286. skb = fp_skb(fp);
  1287. wlen = skb->len / FCOE_WORD_TO_BYTE;
  1288. if (!lport->link_up) {
  1289. kfree_skb(skb);
  1290. return 0;
  1291. }
  1292. if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
  1293. fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
  1294. return 0;
  1295. sof = fr_sof(fp);
  1296. eof = fr_eof(fp);
  1297. elen = sizeof(struct ethhdr);
  1298. hlen = sizeof(struct fcoe_hdr);
  1299. tlen = sizeof(struct fcoe_crc_eof);
  1300. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  1301. /* crc offload */
  1302. if (likely(lport->crc_offload)) {
  1303. skb->ip_summed = CHECKSUM_PARTIAL;
  1304. skb->csum_start = skb_headroom(skb);
  1305. skb->csum_offset = skb->len;
  1306. crc = 0;
  1307. } else {
  1308. skb->ip_summed = CHECKSUM_NONE;
  1309. crc = fcoe_fc_crc(fp);
  1310. }
  1311. /* copy port crc and eof to the skb buff */
  1312. if (skb_is_nonlinear(skb)) {
  1313. skb_frag_t *frag;
  1314. if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
  1315. kfree_skb(skb);
  1316. return -ENOMEM;
  1317. }
  1318. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  1319. cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
  1320. + frag->page_offset;
  1321. } else {
  1322. cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
  1323. }
  1324. memset(cp, 0, sizeof(*cp));
  1325. cp->fcoe_eof = eof;
  1326. cp->fcoe_crc32 = cpu_to_le32(~crc);
  1327. if (skb_is_nonlinear(skb)) {
  1328. kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
  1329. cp = NULL;
  1330. }
  1331. /* adjust skb network/transport offsets to match mac/fcoe/port */
  1332. skb_push(skb, elen + hlen);
  1333. skb_reset_mac_header(skb);
  1334. skb_reset_network_header(skb);
  1335. skb->mac_len = elen;
  1336. skb->protocol = htons(ETH_P_FCOE);
  1337. skb->priority = port->priority;
  1338. if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
  1339. fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
  1340. skb->vlan_tci = VLAN_TAG_PRESENT |
  1341. vlan_dev_vlan_id(fcoe->netdev);
  1342. skb->dev = fcoe->realdev;
  1343. } else
  1344. skb->dev = fcoe->netdev;
  1345. /* fill up mac and fcoe headers */
  1346. eh = eth_hdr(skb);
  1347. eh->h_proto = htons(ETH_P_FCOE);
  1348. memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
  1349. if (fcoe->ctlr.map_dest)
  1350. memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
  1351. if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
  1352. memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
  1353. else
  1354. memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
  1355. hp = (struct fcoe_hdr *)(eh + 1);
  1356. memset(hp, 0, sizeof(*hp));
  1357. if (FC_FCOE_VER)
  1358. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  1359. hp->fcoe_sof = sof;
  1360. /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
  1361. if (lport->seq_offload && fr_max_payload(fp)) {
  1362. skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
  1363. skb_shinfo(skb)->gso_size = fr_max_payload(fp);
  1364. } else {
  1365. skb_shinfo(skb)->gso_type = 0;
  1366. skb_shinfo(skb)->gso_size = 0;
  1367. }
  1368. /* update tx stats: regardless if LLD fails */
  1369. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1370. stats->TxFrames++;
  1371. stats->TxWords += wlen;
  1372. put_cpu();
  1373. /* send down to lld */
  1374. fr_dev(fp) = lport;
  1375. fcoe_port_send(port, skb);
  1376. return 0;
  1377. }
  1378. /**
  1379. * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
  1380. * @skb: The completed skb (argument required by destructor)
  1381. */
  1382. static void fcoe_percpu_flush_done(struct sk_buff *skb)
  1383. {
  1384. complete(&fcoe_flush_completion);
  1385. }
  1386. /**
  1387. * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
  1388. * @lport: The local port the frame was received on
  1389. * @fp: The received frame
  1390. *
  1391. * Return: 0 on passing filtering checks
  1392. */
  1393. static inline int fcoe_filter_frames(struct fc_lport *lport,
  1394. struct fc_frame *fp)
  1395. {
  1396. struct fcoe_interface *fcoe;
  1397. struct fc_frame_header *fh;
  1398. struct sk_buff *skb = (struct sk_buff *)fp;
  1399. struct fcoe_dev_stats *stats;
  1400. /*
  1401. * We only check CRC if no offload is available and if it is
  1402. * it's solicited data, in which case, the FCP layer would
  1403. * check it during the copy.
  1404. */
  1405. if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
  1406. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1407. else
  1408. fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
  1409. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1410. fh = fc_frame_header_get(fp);
  1411. if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
  1412. return 0;
  1413. fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
  1414. if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
  1415. ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
  1416. FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
  1417. return -EINVAL;
  1418. }
  1419. if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
  1420. le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
  1421. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1422. return 0;
  1423. }
  1424. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1425. stats->InvalidCRCCount++;
  1426. if (stats->InvalidCRCCount < 5)
  1427. printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
  1428. put_cpu();
  1429. return -EINVAL;
  1430. }
  1431. /**
  1432. * fcoe_recv_frame() - process a single received frame
  1433. * @skb: frame to process
  1434. */
  1435. static void fcoe_recv_frame(struct sk_buff *skb)
  1436. {
  1437. u32 fr_len;
  1438. struct fc_lport *lport;
  1439. struct fcoe_rcv_info *fr;
  1440. struct fcoe_dev_stats *stats;
  1441. struct fcoe_crc_eof crc_eof;
  1442. struct fc_frame *fp;
  1443. struct fcoe_port *port;
  1444. struct fcoe_hdr *hp;
  1445. fr = fcoe_dev_from_skb(skb);
  1446. lport = fr->fr_dev;
  1447. if (unlikely(!lport)) {
  1448. if (skb->destructor != fcoe_percpu_flush_done)
  1449. FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
  1450. kfree_skb(skb);
  1451. return;
  1452. }
  1453. FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
  1454. "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
  1455. skb->len, skb->data_len,
  1456. skb->head, skb->data, skb_tail_pointer(skb),
  1457. skb_end_pointer(skb), skb->csum,
  1458. skb->dev ? skb->dev->name : "<NULL>");
  1459. port = lport_priv(lport);
  1460. skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
  1461. /*
  1462. * Frame length checks and setting up the header pointers
  1463. * was done in fcoe_rcv already.
  1464. */
  1465. hp = (struct fcoe_hdr *) skb_network_header(skb);
  1466. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1467. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  1468. if (stats->ErrorFrames < 5)
  1469. printk(KERN_WARNING "fcoe: FCoE version "
  1470. "mismatch: The frame has "
  1471. "version %x, but the "
  1472. "initiator supports version "
  1473. "%x\n", FC_FCOE_DECAPS_VER(hp),
  1474. FC_FCOE_VER);
  1475. goto drop;
  1476. }
  1477. skb_pull(skb, sizeof(struct fcoe_hdr));
  1478. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  1479. stats->RxFrames++;
  1480. stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
  1481. fp = (struct fc_frame *)skb;
  1482. fc_frame_init(fp);
  1483. fr_dev(fp) = lport;
  1484. fr_sof(fp) = hp->fcoe_sof;
  1485. /* Copy out the CRC and EOF trailer for access */
  1486. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
  1487. goto drop;
  1488. fr_eof(fp) = crc_eof.fcoe_eof;
  1489. fr_crc(fp) = crc_eof.fcoe_crc32;
  1490. if (pskb_trim(skb, fr_len))
  1491. goto drop;
  1492. if (!fcoe_filter_frames(lport, fp)) {
  1493. put_cpu();
  1494. fc_exch_recv(lport, fp);
  1495. return;
  1496. }
  1497. drop:
  1498. stats->ErrorFrames++;
  1499. put_cpu();
  1500. kfree_skb(skb);
  1501. }
  1502. /**
  1503. * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
  1504. * @arg: The per-CPU context
  1505. *
  1506. * Return: 0 for success
  1507. */
  1508. static int fcoe_percpu_receive_thread(void *arg)
  1509. {
  1510. struct fcoe_percpu_s *p = arg;
  1511. struct sk_buff *skb;
  1512. set_user_nice(current, -20);
  1513. while (!kthread_should_stop()) {
  1514. spin_lock_bh(&p->fcoe_rx_list.lock);
  1515. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
  1516. set_current_state(TASK_INTERRUPTIBLE);
  1517. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1518. schedule();
  1519. set_current_state(TASK_RUNNING);
  1520. if (kthread_should_stop())
  1521. return 0;
  1522. spin_lock_bh(&p->fcoe_rx_list.lock);
  1523. }
  1524. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1525. fcoe_recv_frame(skb);
  1526. }
  1527. return 0;
  1528. }
  1529. /**
  1530. * fcoe_dev_setup() - Setup the link change notification interface
  1531. */
  1532. static void fcoe_dev_setup(void)
  1533. {
  1534. register_dcbevent_notifier(&dcb_notifier);
  1535. register_netdevice_notifier(&fcoe_notifier);
  1536. }
  1537. /**
  1538. * fcoe_dev_cleanup() - Cleanup the link change notification interface
  1539. */
  1540. static void fcoe_dev_cleanup(void)
  1541. {
  1542. unregister_dcbevent_notifier(&dcb_notifier);
  1543. unregister_netdevice_notifier(&fcoe_notifier);
  1544. }
  1545. static struct fcoe_interface *
  1546. fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
  1547. {
  1548. struct fcoe_interface *fcoe;
  1549. struct net_device *real_dev;
  1550. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  1551. if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
  1552. real_dev = vlan_dev_real_dev(fcoe->netdev);
  1553. else
  1554. real_dev = fcoe->netdev;
  1555. if (netdev == real_dev)
  1556. return fcoe;
  1557. }
  1558. return NULL;
  1559. }
  1560. static int fcoe_dcb_app_notification(struct notifier_block *notifier,
  1561. ulong event, void *ptr)
  1562. {
  1563. struct dcb_app_type *entry = ptr;
  1564. struct fcoe_interface *fcoe;
  1565. struct net_device *netdev;
  1566. struct fcoe_port *port;
  1567. int prio;
  1568. if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
  1569. return NOTIFY_OK;
  1570. netdev = dev_get_by_index(&init_net, entry->ifindex);
  1571. if (!netdev)
  1572. return NOTIFY_OK;
  1573. fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
  1574. dev_put(netdev);
  1575. if (!fcoe)
  1576. return NOTIFY_OK;
  1577. if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
  1578. prio = ffs(entry->app.priority) - 1;
  1579. else
  1580. prio = entry->app.priority;
  1581. if (prio < 0)
  1582. return NOTIFY_OK;
  1583. if (entry->app.protocol == ETH_P_FIP ||
  1584. entry->app.protocol == ETH_P_FCOE)
  1585. fcoe->ctlr.priority = prio;
  1586. if (entry->app.protocol == ETH_P_FCOE) {
  1587. port = lport_priv(fcoe->ctlr.lp);
  1588. port->priority = prio;
  1589. }
  1590. return NOTIFY_OK;
  1591. }
  1592. /**
  1593. * fcoe_device_notification() - Handler for net device events
  1594. * @notifier: The context of the notification
  1595. * @event: The type of event
  1596. * @ptr: The net device that the event was on
  1597. *
  1598. * This function is called by the Ethernet driver in case of link change event.
  1599. *
  1600. * Returns: 0 for success
  1601. */
  1602. static int fcoe_device_notification(struct notifier_block *notifier,
  1603. ulong event, void *ptr)
  1604. {
  1605. struct fc_lport *lport = NULL;
  1606. struct net_device *netdev = ptr;
  1607. struct fcoe_interface *fcoe;
  1608. struct fcoe_port *port;
  1609. struct fcoe_dev_stats *stats;
  1610. u32 link_possible = 1;
  1611. u32 mfs;
  1612. int rc = NOTIFY_OK;
  1613. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  1614. if (fcoe->netdev == netdev) {
  1615. lport = fcoe->ctlr.lp;
  1616. break;
  1617. }
  1618. }
  1619. if (!lport) {
  1620. rc = NOTIFY_DONE;
  1621. goto out;
  1622. }
  1623. switch (event) {
  1624. case NETDEV_DOWN:
  1625. case NETDEV_GOING_DOWN:
  1626. link_possible = 0;
  1627. break;
  1628. case NETDEV_UP:
  1629. case NETDEV_CHANGE:
  1630. break;
  1631. case NETDEV_CHANGEMTU:
  1632. if (netdev->features & NETIF_F_FCOE_MTU)
  1633. break;
  1634. mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
  1635. sizeof(struct fcoe_crc_eof));
  1636. if (mfs >= FC_MIN_MAX_FRAME)
  1637. fc_set_mfs(lport, mfs);
  1638. break;
  1639. case NETDEV_REGISTER:
  1640. break;
  1641. case NETDEV_UNREGISTER:
  1642. list_del(&fcoe->list);
  1643. port = lport_priv(fcoe->ctlr.lp);
  1644. queue_work(fcoe_wq, &port->destroy_work);
  1645. goto out;
  1646. break;
  1647. case NETDEV_FEAT_CHANGE:
  1648. fcoe_netdev_features_change(lport, netdev);
  1649. break;
  1650. default:
  1651. FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
  1652. "from netdev netlink\n", event);
  1653. }
  1654. fcoe_link_speed_update(lport);
  1655. if (link_possible && !fcoe_link_ok(lport))
  1656. fcoe_ctlr_link_up(&fcoe->ctlr);
  1657. else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
  1658. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1659. stats->LinkFailureCount++;
  1660. put_cpu();
  1661. fcoe_clean_pending_queue(lport);
  1662. }
  1663. out:
  1664. return rc;
  1665. }
  1666. /**
  1667. * fcoe_disable() - Disables a FCoE interface
  1668. * @netdev : The net_device object the Ethernet interface to create on
  1669. *
  1670. * Called from fcoe transport.
  1671. *
  1672. * Returns: 0 for success
  1673. */
  1674. static int fcoe_disable(struct net_device *netdev)
  1675. {
  1676. struct fcoe_interface *fcoe;
  1677. int rc = 0;
  1678. mutex_lock(&fcoe_config_mutex);
  1679. rtnl_lock();
  1680. fcoe = fcoe_hostlist_lookup_port(netdev);
  1681. rtnl_unlock();
  1682. if (fcoe) {
  1683. fcoe_ctlr_link_down(&fcoe->ctlr);
  1684. fcoe_clean_pending_queue(fcoe->ctlr.lp);
  1685. } else
  1686. rc = -ENODEV;
  1687. mutex_unlock(&fcoe_config_mutex);
  1688. return rc;
  1689. }
  1690. /**
  1691. * fcoe_enable() - Enables a FCoE interface
  1692. * @netdev : The net_device object the Ethernet interface to create on
  1693. *
  1694. * Called from fcoe transport.
  1695. *
  1696. * Returns: 0 for success
  1697. */
  1698. static int fcoe_enable(struct net_device *netdev)
  1699. {
  1700. struct fcoe_interface *fcoe;
  1701. int rc = 0;
  1702. mutex_lock(&fcoe_config_mutex);
  1703. rtnl_lock();
  1704. fcoe = fcoe_hostlist_lookup_port(netdev);
  1705. rtnl_unlock();
  1706. if (!fcoe)
  1707. rc = -ENODEV;
  1708. else if (!fcoe_link_ok(fcoe->ctlr.lp))
  1709. fcoe_ctlr_link_up(&fcoe->ctlr);
  1710. mutex_unlock(&fcoe_config_mutex);
  1711. return rc;
  1712. }
  1713. /**
  1714. * fcoe_destroy() - Destroy a FCoE interface
  1715. * @netdev : The net_device object the Ethernet interface to create on
  1716. *
  1717. * Called from fcoe transport
  1718. *
  1719. * Returns: 0 for success
  1720. */
  1721. static int fcoe_destroy(struct net_device *netdev)
  1722. {
  1723. struct fcoe_interface *fcoe;
  1724. struct fc_lport *lport;
  1725. struct fcoe_port *port;
  1726. int rc = 0;
  1727. mutex_lock(&fcoe_config_mutex);
  1728. rtnl_lock();
  1729. fcoe = fcoe_hostlist_lookup_port(netdev);
  1730. if (!fcoe) {
  1731. rc = -ENODEV;
  1732. goto out_nodev;
  1733. }
  1734. lport = fcoe->ctlr.lp;
  1735. port = lport_priv(lport);
  1736. list_del(&fcoe->list);
  1737. queue_work(fcoe_wq, &port->destroy_work);
  1738. out_nodev:
  1739. rtnl_unlock();
  1740. mutex_unlock(&fcoe_config_mutex);
  1741. return rc;
  1742. }
  1743. /**
  1744. * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
  1745. * @work: Handle to the FCoE port to be destroyed
  1746. */
  1747. static void fcoe_destroy_work(struct work_struct *work)
  1748. {
  1749. struct fcoe_port *port;
  1750. struct fcoe_interface *fcoe;
  1751. int npiv = 0;
  1752. port = container_of(work, struct fcoe_port, destroy_work);
  1753. mutex_lock(&fcoe_config_mutex);
  1754. /* set if this is an NPIV port */
  1755. npiv = port->lport->vport ? 1 : 0;
  1756. fcoe = port->priv;
  1757. fcoe_if_destroy(port->lport);
  1758. /* Do not tear down the fcoe interface for NPIV port */
  1759. if (!npiv)
  1760. fcoe_interface_cleanup(fcoe);
  1761. mutex_unlock(&fcoe_config_mutex);
  1762. }
  1763. /**
  1764. * fcoe_match() - Check if the FCoE is supported on the given netdevice
  1765. * @netdev : The net_device object the Ethernet interface to create on
  1766. *
  1767. * Called from fcoe transport.
  1768. *
  1769. * Returns: always returns true as this is the default FCoE transport,
  1770. * i.e., support all netdevs.
  1771. */
  1772. static bool fcoe_match(struct net_device *netdev)
  1773. {
  1774. return true;
  1775. }
  1776. /**
  1777. * fcoe_dcb_create() - Initialize DCB attributes and hooks
  1778. * @netdev: The net_device object of the L2 link that should be queried
  1779. * @port: The fcoe_port to bind FCoE APP priority with
  1780. * @
  1781. */
  1782. static void fcoe_dcb_create(struct fcoe_interface *fcoe)
  1783. {
  1784. #ifdef CONFIG_DCB
  1785. int dcbx;
  1786. u8 fup, up;
  1787. struct net_device *netdev = fcoe->realdev;
  1788. struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
  1789. struct dcb_app app = {
  1790. .priority = 0,
  1791. .protocol = ETH_P_FCOE
  1792. };
  1793. /* setup DCB priority attributes. */
  1794. if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
  1795. dcbx = netdev->dcbnl_ops->getdcbx(netdev);
  1796. if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
  1797. app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
  1798. up = dcb_ieee_getapp_mask(netdev, &app);
  1799. app.protocol = ETH_P_FIP;
  1800. fup = dcb_ieee_getapp_mask(netdev, &app);
  1801. } else {
  1802. app.selector = DCB_APP_IDTYPE_ETHTYPE;
  1803. up = dcb_getapp(netdev, &app);
  1804. app.protocol = ETH_P_FIP;
  1805. fup = dcb_getapp(netdev, &app);
  1806. }
  1807. port->priority = ffs(up) ? ffs(up) - 1 : 0;
  1808. fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;
  1809. }
  1810. #endif
  1811. }
  1812. /**
  1813. * fcoe_create() - Create a fcoe interface
  1814. * @netdev : The net_device object the Ethernet interface to create on
  1815. * @fip_mode: The FIP mode for this creation
  1816. *
  1817. * Called from fcoe transport
  1818. *
  1819. * Returns: 0 for success
  1820. */
  1821. static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
  1822. {
  1823. int rc = 0;
  1824. struct fcoe_interface *fcoe;
  1825. struct fc_lport *lport;
  1826. mutex_lock(&fcoe_config_mutex);
  1827. rtnl_lock();
  1828. /* look for existing lport */
  1829. if (fcoe_hostlist_lookup(netdev)) {
  1830. rc = -EEXIST;
  1831. goto out_nodev;
  1832. }
  1833. fcoe = fcoe_interface_create(netdev, fip_mode);
  1834. if (IS_ERR(fcoe)) {
  1835. rc = PTR_ERR(fcoe);
  1836. goto out_nodev;
  1837. }
  1838. lport = fcoe_if_create(fcoe, &netdev->dev, 0);
  1839. if (IS_ERR(lport)) {
  1840. printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
  1841. netdev->name);
  1842. rc = -EIO;
  1843. rtnl_unlock();
  1844. fcoe_interface_cleanup(fcoe);
  1845. goto out_nortnl;
  1846. }
  1847. /* Make this the "master" N_Port */
  1848. fcoe->ctlr.lp = lport;
  1849. /* setup DCB priority attributes. */
  1850. fcoe_dcb_create(fcoe);
  1851. /* add to lports list */
  1852. fcoe_hostlist_add(lport);
  1853. /* start FIP Discovery and FLOGI */
  1854. lport->boot_time = jiffies;
  1855. fc_fabric_login(lport);
  1856. if (!fcoe_link_ok(lport))
  1857. fcoe_ctlr_link_up(&fcoe->ctlr);
  1858. out_nodev:
  1859. rtnl_unlock();
  1860. out_nortnl:
  1861. mutex_unlock(&fcoe_config_mutex);
  1862. return rc;
  1863. }
  1864. /**
  1865. * fcoe_link_speed_update() - Update the supported and actual link speeds
  1866. * @lport: The local port to update speeds for
  1867. *
  1868. * Returns: 0 if the ethtool query was successful
  1869. * -1 if the ethtool query failed
  1870. */
  1871. static int fcoe_link_speed_update(struct fc_lport *lport)
  1872. {
  1873. struct net_device *netdev = fcoe_netdev(lport);
  1874. struct ethtool_cmd ecmd;
  1875. if (!__ethtool_get_settings(netdev, &ecmd)) {
  1876. lport->link_supported_speeds &=
  1877. ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
  1878. if (ecmd.supported & (SUPPORTED_1000baseT_Half |
  1879. SUPPORTED_1000baseT_Full))
  1880. lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
  1881. if (ecmd.supported & SUPPORTED_10000baseT_Full)
  1882. lport->link_supported_speeds |=
  1883. FC_PORTSPEED_10GBIT;
  1884. switch (ethtool_cmd_speed(&ecmd)) {
  1885. case SPEED_1000:
  1886. lport->link_speed = FC_PORTSPEED_1GBIT;
  1887. break;
  1888. case SPEED_10000:
  1889. lport->link_speed = FC_PORTSPEED_10GBIT;
  1890. break;
  1891. }
  1892. return 0;
  1893. }
  1894. return -1;
  1895. }
  1896. /**
  1897. * fcoe_link_ok() - Check if the link is OK for a local port
  1898. * @lport: The local port to check link on
  1899. *
  1900. * Returns: 0 if link is UP and OK, -1 if not
  1901. *
  1902. */
  1903. static int fcoe_link_ok(struct fc_lport *lport)
  1904. {
  1905. struct net_device *netdev = fcoe_netdev(lport);
  1906. if (netif_oper_up(netdev))
  1907. return 0;
  1908. return -1;
  1909. }
  1910. /**
  1911. * fcoe_percpu_clean() - Clear all pending skbs for an local port
  1912. * @lport: The local port whose skbs are to be cleared
  1913. *
  1914. * Must be called with fcoe_create_mutex held to single-thread completion.
  1915. *
  1916. * This flushes the pending skbs by adding a new skb to each queue and
  1917. * waiting until they are all freed. This assures us that not only are
  1918. * there no packets that will be handled by the lport, but also that any
  1919. * threads already handling packet have returned.
  1920. */
  1921. static void fcoe_percpu_clean(struct fc_lport *lport)
  1922. {
  1923. struct fcoe_percpu_s *pp;
  1924. struct fcoe_rcv_info *fr;
  1925. struct sk_buff_head *list;
  1926. struct sk_buff *skb, *next;
  1927. struct sk_buff *head;
  1928. unsigned int cpu;
  1929. for_each_possible_cpu(cpu) {
  1930. pp = &per_cpu(fcoe_percpu, cpu);
  1931. spin_lock_bh(&pp->fcoe_rx_list.lock);
  1932. list = &pp->fcoe_rx_list;
  1933. head = list->next;
  1934. for (skb = head; skb != (struct sk_buff *)list;
  1935. skb = next) {
  1936. next = skb->next;
  1937. fr = fcoe_dev_from_skb(skb);
  1938. if (fr->fr_dev == lport) {
  1939. __skb_unlink(skb, list);
  1940. kfree_skb(skb);
  1941. }
  1942. }
  1943. if (!pp->thread || !cpu_online(cpu)) {
  1944. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1945. continue;
  1946. }
  1947. skb = dev_alloc_skb(0);
  1948. if (!skb) {
  1949. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1950. continue;
  1951. }
  1952. skb->destructor = fcoe_percpu_flush_done;
  1953. __skb_queue_tail(&pp->fcoe_rx_list, skb);
  1954. if (pp->fcoe_rx_list.qlen == 1)
  1955. wake_up_process(pp->thread);
  1956. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1957. wait_for_completion(&fcoe_flush_completion);
  1958. }
  1959. }
  1960. /**
  1961. * fcoe_reset() - Reset a local port
  1962. * @shost: The SCSI host associated with the local port to be reset
  1963. *
  1964. * Returns: Always 0 (return value required by FC transport template)
  1965. */
  1966. static int fcoe_reset(struct Scsi_Host *shost)
  1967. {
  1968. struct fc_lport *lport = shost_priv(shost);
  1969. struct fcoe_port *port = lport_priv(lport);
  1970. struct fcoe_interface *fcoe = port->priv;
  1971. fcoe_ctlr_link_down(&fcoe->ctlr);
  1972. fcoe_clean_pending_queue(fcoe->ctlr.lp);
  1973. if (!fcoe_link_ok(fcoe->ctlr.lp))
  1974. fcoe_ctlr_link_up(&fcoe->ctlr);
  1975. return 0;
  1976. }
  1977. /**
  1978. * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
  1979. * @netdev: The net device used as a key
  1980. *
  1981. * Locking: Must be called with the RNL mutex held.
  1982. *
  1983. * Returns: NULL or the FCoE interface
  1984. */
  1985. static struct fcoe_interface *
  1986. fcoe_hostlist_lookup_port(const struct net_device *netdev)
  1987. {
  1988. struct fcoe_interface *fcoe;
  1989. list_for_each_entry(fcoe, &fcoe_hostlist, list) {
  1990. if (fcoe->netdev == netdev)
  1991. return fcoe;
  1992. }
  1993. return NULL;
  1994. }
  1995. /**
  1996. * fcoe_hostlist_lookup() - Find the local port associated with a
  1997. * given net device
  1998. * @netdev: The netdevice used as a key
  1999. *
  2000. * Locking: Must be called with the RTNL mutex held
  2001. *
  2002. * Returns: NULL or the local port
  2003. */
  2004. static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
  2005. {
  2006. struct fcoe_interface *fcoe;
  2007. fcoe = fcoe_hostlist_lookup_port(netdev);
  2008. return (fcoe) ? fcoe->ctlr.lp : NULL;
  2009. }
  2010. /**
  2011. * fcoe_hostlist_add() - Add the FCoE interface identified by a local
  2012. * port to the hostlist
  2013. * @lport: The local port that identifies the FCoE interface to be added
  2014. *
  2015. * Locking: must be called with the RTNL mutex held
  2016. *
  2017. * Returns: 0 for success
  2018. */
  2019. static int fcoe_hostlist_add(const struct fc_lport *lport)
  2020. {
  2021. struct fcoe_interface *fcoe;
  2022. struct fcoe_port *port;
  2023. fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
  2024. if (!fcoe) {
  2025. port = lport_priv(lport);
  2026. fcoe = port->priv;
  2027. list_add_tail(&fcoe->list, &fcoe_hostlist);
  2028. }
  2029. return 0;
  2030. }
  2031. static struct fcoe_transport fcoe_sw_transport = {
  2032. .name = {FCOE_TRANSPORT_DEFAULT},
  2033. .attached = false,
  2034. .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
  2035. .match = fcoe_match,
  2036. .create = fcoe_create,
  2037. .destroy = fcoe_destroy,
  2038. .enable = fcoe_enable,
  2039. .disable = fcoe_disable,
  2040. };
  2041. /**
  2042. * fcoe_init() - Initialize fcoe.ko
  2043. *
  2044. * Returns: 0 on success, or a negative value on failure
  2045. */
  2046. static int __init fcoe_init(void)
  2047. {
  2048. struct fcoe_percpu_s *p;
  2049. unsigned int cpu;
  2050. int rc = 0;
  2051. fcoe_wq = alloc_workqueue("fcoe", 0, 0);
  2052. if (!fcoe_wq)
  2053. return -ENOMEM;
  2054. /* register as a fcoe transport */
  2055. rc = fcoe_transport_attach(&fcoe_sw_transport);
  2056. if (rc) {
  2057. printk(KERN_ERR "failed to register an fcoe transport, check "
  2058. "if libfcoe is loaded\n");
  2059. return rc;
  2060. }
  2061. mutex_lock(&fcoe_config_mutex);
  2062. for_each_possible_cpu(cpu) {
  2063. p = &per_cpu(fcoe_percpu, cpu);
  2064. skb_queue_head_init(&p->fcoe_rx_list);
  2065. }
  2066. for_each_online_cpu(cpu)
  2067. fcoe_percpu_thread_create(cpu);
  2068. /* Initialize per CPU interrupt thread */
  2069. rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
  2070. if (rc)
  2071. goto out_free;
  2072. /* Setup link change notification */
  2073. fcoe_dev_setup();
  2074. rc = fcoe_if_init();
  2075. if (rc)
  2076. goto out_free;
  2077. mutex_unlock(&fcoe_config_mutex);
  2078. return 0;
  2079. out_free:
  2080. for_each_online_cpu(cpu) {
  2081. fcoe_percpu_thread_destroy(cpu);
  2082. }
  2083. mutex_unlock(&fcoe_config_mutex);
  2084. destroy_workqueue(fcoe_wq);
  2085. return rc;
  2086. }
  2087. module_init(fcoe_init);
  2088. /**
  2089. * fcoe_exit() - Clean up fcoe.ko
  2090. *
  2091. * Returns: 0 on success or a negative value on failure
  2092. */
  2093. static void __exit fcoe_exit(void)
  2094. {
  2095. struct fcoe_interface *fcoe, *tmp;
  2096. struct fcoe_port *port;
  2097. unsigned int cpu;
  2098. mutex_lock(&fcoe_config_mutex);
  2099. fcoe_dev_cleanup();
  2100. /* releases the associated fcoe hosts */
  2101. rtnl_lock();
  2102. list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
  2103. list_del(&fcoe->list);
  2104. port = lport_priv(fcoe->ctlr.lp);
  2105. queue_work(fcoe_wq, &port->destroy_work);
  2106. }
  2107. rtnl_unlock();
  2108. unregister_hotcpu_notifier(&fcoe_cpu_notifier);
  2109. for_each_online_cpu(cpu)
  2110. fcoe_percpu_thread_destroy(cpu);
  2111. mutex_unlock(&fcoe_config_mutex);
  2112. /*
  2113. * destroy_work's may be chained but destroy_workqueue()
  2114. * can take care of them. Just kill the fcoe_wq.
  2115. */
  2116. destroy_workqueue(fcoe_wq);
  2117. /*
  2118. * Detaching from the scsi transport must happen after all
  2119. * destroys are done on the fcoe_wq. destroy_workqueue will
  2120. * enusre the fcoe_wq is flushed.
  2121. */
  2122. fcoe_if_exit();
  2123. /* detach from fcoe transport */
  2124. fcoe_transport_detach(&fcoe_sw_transport);
  2125. }
  2126. module_exit(fcoe_exit);
  2127. /**
  2128. * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
  2129. * @seq: active sequence in the FLOGI or FDISC exchange
  2130. * @fp: response frame, or error encoded in a pointer (timeout)
  2131. * @arg: pointer the the fcoe_ctlr structure
  2132. *
  2133. * This handles MAC address management for FCoE, then passes control on to
  2134. * the libfc FLOGI response handler.
  2135. */
  2136. static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  2137. {
  2138. struct fcoe_ctlr *fip = arg;
  2139. struct fc_exch *exch = fc_seq_exch(seq);
  2140. struct fc_lport *lport = exch->lp;
  2141. u8 *mac;
  2142. if (IS_ERR(fp))
  2143. goto done;
  2144. mac = fr_cb(fp)->granted_mac;
  2145. /* pre-FIP */
  2146. if (is_zero_ether_addr(mac))
  2147. fcoe_ctlr_recv_flogi(fip, lport, fp);
  2148. if (!is_zero_ether_addr(mac))
  2149. fcoe_update_src_mac(lport, mac);
  2150. done:
  2151. fc_lport_flogi_resp(seq, fp, lport);
  2152. }
  2153. /**
  2154. * fcoe_logo_resp() - FCoE specific LOGO response handler
  2155. * @seq: active sequence in the LOGO exchange
  2156. * @fp: response frame, or error encoded in a pointer (timeout)
  2157. * @arg: pointer the the fcoe_ctlr structure
  2158. *
  2159. * This handles MAC address management for FCoE, then passes control on to
  2160. * the libfc LOGO response handler.
  2161. */
  2162. static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  2163. {
  2164. struct fc_lport *lport = arg;
  2165. static u8 zero_mac[ETH_ALEN] = { 0 };
  2166. if (!IS_ERR(fp))
  2167. fcoe_update_src_mac(lport, zero_mac);
  2168. fc_lport_logo_resp(seq, fp, lport);
  2169. }
  2170. /**
  2171. * fcoe_elsct_send - FCoE specific ELS handler
  2172. *
  2173. * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
  2174. * using FCoE specific response handlers and passing the FIP controller as
  2175. * the argument (the lport is still available from the exchange).
  2176. *
  2177. * Most of the work here is just handed off to the libfc routine.
  2178. */
  2179. static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
  2180. struct fc_frame *fp, unsigned int op,
  2181. void (*resp)(struct fc_seq *,
  2182. struct fc_frame *,
  2183. void *),
  2184. void *arg, u32 timeout)
  2185. {
  2186. struct fcoe_port *port = lport_priv(lport);
  2187. struct fcoe_interface *fcoe = port->priv;
  2188. struct fcoe_ctlr *fip = &fcoe->ctlr;
  2189. struct fc_frame_header *fh = fc_frame_header_get(fp);
  2190. switch (op) {
  2191. case ELS_FLOGI:
  2192. case ELS_FDISC:
  2193. if (lport->point_to_multipoint)
  2194. break;
  2195. return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
  2196. fip, timeout);
  2197. case ELS_LOGO:
  2198. /* only hook onto fabric logouts, not port logouts */
  2199. if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
  2200. break;
  2201. return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
  2202. lport, timeout);
  2203. }
  2204. return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
  2205. }
  2206. /**
  2207. * fcoe_vport_create() - create an fc_host/scsi_host for a vport
  2208. * @vport: fc_vport object to create a new fc_host for
  2209. * @disabled: start the new fc_host in a disabled state by default?
  2210. *
  2211. * Returns: 0 for success
  2212. */
  2213. static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
  2214. {
  2215. struct Scsi_Host *shost = vport_to_shost(vport);
  2216. struct fc_lport *n_port = shost_priv(shost);
  2217. struct fcoe_port *port = lport_priv(n_port);
  2218. struct fcoe_interface *fcoe = port->priv;
  2219. struct net_device *netdev = fcoe->netdev;
  2220. struct fc_lport *vn_port;
  2221. int rc;
  2222. char buf[32];
  2223. rc = fcoe_validate_vport_create(vport);
  2224. if (rc) {
  2225. fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
  2226. printk(KERN_ERR "fcoe: Failed to create vport, "
  2227. "WWPN (0x%s) already exists\n",
  2228. buf);
  2229. return rc;
  2230. }
  2231. mutex_lock(&fcoe_config_mutex);
  2232. rtnl_lock();
  2233. vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
  2234. rtnl_unlock();
  2235. mutex_unlock(&fcoe_config_mutex);
  2236. if (IS_ERR(vn_port)) {
  2237. printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
  2238. netdev->name);
  2239. return -EIO;
  2240. }
  2241. if (disabled) {
  2242. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  2243. } else {
  2244. vn_port->boot_time = jiffies;
  2245. fc_fabric_login(vn_port);
  2246. fc_vport_setlink(vn_port);
  2247. }
  2248. return 0;
  2249. }
  2250. /**
  2251. * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
  2252. * @vport: fc_vport object that is being destroyed
  2253. *
  2254. * Returns: 0 for success
  2255. */
  2256. static int fcoe_vport_destroy(struct fc_vport *vport)
  2257. {
  2258. struct Scsi_Host *shost = vport_to_shost(vport);
  2259. struct fc_lport *n_port = shost_priv(shost);
  2260. struct fc_lport *vn_port = vport->dd_data;
  2261. struct fcoe_port *port = lport_priv(vn_port);
  2262. mutex_lock(&n_port->lp_mutex);
  2263. list_del(&vn_port->list);
  2264. mutex_unlock(&n_port->lp_mutex);
  2265. queue_work(fcoe_wq, &port->destroy_work);
  2266. return 0;
  2267. }
  2268. /**
  2269. * fcoe_vport_disable() - change vport state
  2270. * @vport: vport to bring online/offline
  2271. * @disable: should the vport be disabled?
  2272. */
  2273. static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
  2274. {
  2275. struct fc_lport *lport = vport->dd_data;
  2276. if (disable) {
  2277. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  2278. fc_fabric_logoff(lport);
  2279. } else {
  2280. lport->boot_time = jiffies;
  2281. fc_fabric_login(lport);
  2282. fc_vport_setlink(lport);
  2283. }
  2284. return 0;
  2285. }
  2286. /**
  2287. * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
  2288. * @vport: fc_vport with a new symbolic name string
  2289. *
  2290. * After generating a new symbolic name string, a new RSPN_ID request is
  2291. * sent to the name server. There is no response handler, so if it fails
  2292. * for some reason it will not be retried.
  2293. */
  2294. static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
  2295. {
  2296. struct fc_lport *lport = vport->dd_data;
  2297. struct fc_frame *fp;
  2298. size_t len;
  2299. snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
  2300. "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
  2301. fcoe_netdev(lport)->name, vport->symbolic_name);
  2302. if (lport->state != LPORT_ST_READY)
  2303. return;
  2304. len = strnlen(fc_host_symbolic_name(lport->host), 255);
  2305. fp = fc_frame_alloc(lport,
  2306. sizeof(struct fc_ct_hdr) +
  2307. sizeof(struct fc_ns_rspn) + len);
  2308. if (!fp)
  2309. return;
  2310. lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
  2311. NULL, NULL, 3 * lport->r_a_tov);
  2312. }
  2313. /**
  2314. * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
  2315. * @lport: the local port
  2316. * @fc_lesb: the link error status block
  2317. */
  2318. static void fcoe_get_lesb(struct fc_lport *lport,
  2319. struct fc_els_lesb *fc_lesb)
  2320. {
  2321. struct net_device *netdev = fcoe_netdev(lport);
  2322. __fcoe_get_lesb(lport, fc_lesb, netdev);
  2323. }
  2324. /**
  2325. * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
  2326. * @lport: the local port
  2327. * @port_id: the port ID
  2328. * @fp: the received frame, if any, that caused the port_id to be set.
  2329. *
  2330. * This routine handles the case where we received a FLOGI and are
  2331. * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
  2332. * so it can set the non-mapped mode and gateway address.
  2333. *
  2334. * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
  2335. */
  2336. static void fcoe_set_port_id(struct fc_lport *lport,
  2337. u32 port_id, struct fc_frame *fp)
  2338. {
  2339. struct fcoe_port *port = lport_priv(lport);
  2340. struct fcoe_interface *fcoe = port->priv;
  2341. if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
  2342. fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
  2343. }