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