fcoe.c 72 KB

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