fcoe.c 74 KB

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