fcoe.c 67 KB

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