fcoe.c 72 KB

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