fcoe.c 72 KB

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