fcoe.c 71 KB

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