fcoe.c 72 KB

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