fcoe.c 72 KB

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