fcoe.c 70 KB

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