fcoe.c 67 KB

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