fcoe.c 69 KB

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