fcoe.c 64 KB

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