fcoe.c 69 KB

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