fcoe.c 72 KB

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