qedf_main.c 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693
  1. /*
  2. * QLogic FCoE Offload Driver
  3. * Copyright (c) 2016-2018 Cavium Inc.
  4. *
  5. * This software is available under the terms of the GNU General Public License
  6. * (GPL) Version 2, available from the file COPYING in the main directory of
  7. * this source tree.
  8. */
  9. #include <linux/init.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/pci.h>
  13. #include <linux/device.h>
  14. #include <linux/highmem.h>
  15. #include <linux/crc32.h>
  16. #include <linux/interrupt.h>
  17. #include <linux/list.h>
  18. #include <linux/kthread.h>
  19. #include <scsi/libfc.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/fc_frame.h>
  22. #include <linux/if_ether.h>
  23. #include <linux/if_vlan.h>
  24. #include <linux/cpu.h>
  25. #include "qedf.h"
  26. #include "qedf_dbg.h"
  27. #include <uapi/linux/pci_regs.h>
  28. const struct qed_fcoe_ops *qed_ops;
  29. static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id);
  30. static void qedf_remove(struct pci_dev *pdev);
  31. /*
  32. * Driver module parameters.
  33. */
  34. static unsigned int qedf_dev_loss_tmo = 60;
  35. module_param_named(dev_loss_tmo, qedf_dev_loss_tmo, int, S_IRUGO);
  36. MODULE_PARM_DESC(dev_loss_tmo, " dev_loss_tmo setting for attached "
  37. "remote ports (default 60)");
  38. uint qedf_debug = QEDF_LOG_INFO;
  39. module_param_named(debug, qedf_debug, uint, S_IRUGO);
  40. MODULE_PARM_DESC(debug, " Debug mask. Pass '1' to enable default debugging"
  41. " mask");
  42. static uint qedf_fipvlan_retries = 60;
  43. module_param_named(fipvlan_retries, qedf_fipvlan_retries, int, S_IRUGO);
  44. MODULE_PARM_DESC(fipvlan_retries, " Number of FIP VLAN requests to attempt "
  45. "before giving up (default 60)");
  46. static uint qedf_fallback_vlan = QEDF_FALLBACK_VLAN;
  47. module_param_named(fallback_vlan, qedf_fallback_vlan, int, S_IRUGO);
  48. MODULE_PARM_DESC(fallback_vlan, " VLAN ID to try if fip vlan request fails "
  49. "(default 1002).");
  50. static int qedf_default_prio = -1;
  51. module_param_named(default_prio, qedf_default_prio, int, S_IRUGO);
  52. MODULE_PARM_DESC(default_prio, " Override 802.1q priority for FIP and FCoE"
  53. " traffic (value between 0 and 7, default 3).");
  54. uint qedf_dump_frames;
  55. module_param_named(dump_frames, qedf_dump_frames, int, S_IRUGO | S_IWUSR);
  56. MODULE_PARM_DESC(dump_frames, " Print the skb data of FIP and FCoE frames "
  57. "(default off)");
  58. static uint qedf_queue_depth;
  59. module_param_named(queue_depth, qedf_queue_depth, int, S_IRUGO);
  60. MODULE_PARM_DESC(queue_depth, " Sets the queue depth for all LUNs discovered "
  61. "by the qedf driver. Default is 0 (use OS default).");
  62. uint qedf_io_tracing;
  63. module_param_named(io_tracing, qedf_io_tracing, int, S_IRUGO | S_IWUSR);
  64. MODULE_PARM_DESC(io_tracing, " Enable logging of SCSI requests/completions "
  65. "into trace buffer. (default off).");
  66. static uint qedf_max_lun = MAX_FIBRE_LUNS;
  67. module_param_named(max_lun, qedf_max_lun, int, S_IRUGO);
  68. MODULE_PARM_DESC(max_lun, " Sets the maximum luns per target that the driver "
  69. "supports. (default 0xffffffff)");
  70. uint qedf_link_down_tmo;
  71. module_param_named(link_down_tmo, qedf_link_down_tmo, int, S_IRUGO);
  72. MODULE_PARM_DESC(link_down_tmo, " Delays informing the fcoe transport that the "
  73. "link is down by N seconds.");
  74. bool qedf_retry_delay;
  75. module_param_named(retry_delay, qedf_retry_delay, bool, S_IRUGO | S_IWUSR);
  76. MODULE_PARM_DESC(retry_delay, " Enable/disable handling of FCP_RSP IU retry "
  77. "delay handling (default off).");
  78. static bool qedf_dcbx_no_wait;
  79. module_param_named(dcbx_no_wait, qedf_dcbx_no_wait, bool, S_IRUGO | S_IWUSR);
  80. MODULE_PARM_DESC(dcbx_no_wait, " Do not wait for DCBX convergence to start "
  81. "sending FIP VLAN requests on link up (Default: off).");
  82. static uint qedf_dp_module;
  83. module_param_named(dp_module, qedf_dp_module, uint, S_IRUGO);
  84. MODULE_PARM_DESC(dp_module, " bit flags control for verbose printk passed "
  85. "qed module during probe.");
  86. static uint qedf_dp_level = QED_LEVEL_NOTICE;
  87. module_param_named(dp_level, qedf_dp_level, uint, S_IRUGO);
  88. MODULE_PARM_DESC(dp_level, " printk verbosity control passed to qed module "
  89. "during probe (0-3: 0 more verbose).");
  90. struct workqueue_struct *qedf_io_wq;
  91. static struct fcoe_percpu_s qedf_global;
  92. static DEFINE_SPINLOCK(qedf_global_lock);
  93. static struct kmem_cache *qedf_io_work_cache;
  94. void qedf_set_vlan_id(struct qedf_ctx *qedf, int vlan_id)
  95. {
  96. qedf->vlan_id = vlan_id;
  97. qedf->vlan_id |= qedf->prio << VLAN_PRIO_SHIFT;
  98. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Setting vlan_id=%04x "
  99. "prio=%d.\n", vlan_id, qedf->prio);
  100. }
  101. /* Returns true if we have a valid vlan, false otherwise */
  102. static bool qedf_initiate_fipvlan_req(struct qedf_ctx *qedf)
  103. {
  104. int rc;
  105. if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
  106. QEDF_ERR(&(qedf->dbg_ctx), "Link not up.\n");
  107. return false;
  108. }
  109. while (qedf->fipvlan_retries--) {
  110. if (qedf->vlan_id > 0)
  111. return true;
  112. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  113. "Retry %d.\n", qedf->fipvlan_retries);
  114. init_completion(&qedf->fipvlan_compl);
  115. qedf_fcoe_send_vlan_req(qedf);
  116. rc = wait_for_completion_timeout(&qedf->fipvlan_compl,
  117. 1 * HZ);
  118. if (rc > 0) {
  119. fcoe_ctlr_link_up(&qedf->ctlr);
  120. return true;
  121. }
  122. }
  123. return false;
  124. }
  125. static void qedf_handle_link_update(struct work_struct *work)
  126. {
  127. struct qedf_ctx *qedf =
  128. container_of(work, struct qedf_ctx, link_update.work);
  129. int rc;
  130. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Entered.\n");
  131. if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
  132. rc = qedf_initiate_fipvlan_req(qedf);
  133. if (rc)
  134. return;
  135. /*
  136. * If we get here then we never received a repsonse to our
  137. * fip vlan request so set the vlan_id to the default and
  138. * tell FCoE that the link is up
  139. */
  140. QEDF_WARN(&(qedf->dbg_ctx), "Did not receive FIP VLAN "
  141. "response, falling back to default VLAN %d.\n",
  142. qedf_fallback_vlan);
  143. qedf_set_vlan_id(qedf, qedf_fallback_vlan);
  144. /*
  145. * Zero out data_src_addr so we'll update it with the new
  146. * lport port_id
  147. */
  148. eth_zero_addr(qedf->data_src_addr);
  149. fcoe_ctlr_link_up(&qedf->ctlr);
  150. } else if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN) {
  151. /*
  152. * If we hit here and link_down_tmo_valid is still 1 it means
  153. * that link_down_tmo timed out so set it to 0 to make sure any
  154. * other readers have accurate state.
  155. */
  156. atomic_set(&qedf->link_down_tmo_valid, 0);
  157. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  158. "Calling fcoe_ctlr_link_down().\n");
  159. fcoe_ctlr_link_down(&qedf->ctlr);
  160. qedf_wait_for_upload(qedf);
  161. /* Reset the number of FIP VLAN retries */
  162. qedf->fipvlan_retries = qedf_fipvlan_retries;
  163. }
  164. }
  165. #define QEDF_FCOE_MAC_METHOD_GRANGED_MAC 1
  166. #define QEDF_FCOE_MAC_METHOD_FCF_MAP 2
  167. #define QEDF_FCOE_MAC_METHOD_FCOE_SET_MAC 3
  168. static void qedf_set_data_src_addr(struct qedf_ctx *qedf, struct fc_frame *fp)
  169. {
  170. u8 *granted_mac;
  171. struct fc_frame_header *fh = fc_frame_header_get(fp);
  172. u8 fc_map[3];
  173. int method = 0;
  174. /* Get granted MAC address from FIP FLOGI payload */
  175. granted_mac = fr_cb(fp)->granted_mac;
  176. /*
  177. * We set the source MAC for FCoE traffic based on the Granted MAC
  178. * address from the switch.
  179. *
  180. * If granted_mac is non-zero, we used that.
  181. * If the granted_mac is zeroed out, created the FCoE MAC based on
  182. * the sel_fcf->fc_map and the d_id fo the FLOGI frame.
  183. * If sel_fcf->fc_map is 0 then we use the default FCF-MAC plus the
  184. * d_id of the FLOGI frame.
  185. */
  186. if (!is_zero_ether_addr(granted_mac)) {
  187. ether_addr_copy(qedf->data_src_addr, granted_mac);
  188. method = QEDF_FCOE_MAC_METHOD_GRANGED_MAC;
  189. } else if (qedf->ctlr.sel_fcf->fc_map != 0) {
  190. hton24(fc_map, qedf->ctlr.sel_fcf->fc_map);
  191. qedf->data_src_addr[0] = fc_map[0];
  192. qedf->data_src_addr[1] = fc_map[1];
  193. qedf->data_src_addr[2] = fc_map[2];
  194. qedf->data_src_addr[3] = fh->fh_d_id[0];
  195. qedf->data_src_addr[4] = fh->fh_d_id[1];
  196. qedf->data_src_addr[5] = fh->fh_d_id[2];
  197. method = QEDF_FCOE_MAC_METHOD_FCF_MAP;
  198. } else {
  199. fc_fcoe_set_mac(qedf->data_src_addr, fh->fh_d_id);
  200. method = QEDF_FCOE_MAC_METHOD_FCOE_SET_MAC;
  201. }
  202. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  203. "QEDF data_src_mac=%pM method=%d.\n", qedf->data_src_addr, method);
  204. }
  205. static void qedf_flogi_resp(struct fc_seq *seq, struct fc_frame *fp,
  206. void *arg)
  207. {
  208. struct fc_exch *exch = fc_seq_exch(seq);
  209. struct fc_lport *lport = exch->lp;
  210. struct qedf_ctx *qedf = lport_priv(lport);
  211. if (!qedf) {
  212. QEDF_ERR(NULL, "qedf is NULL.\n");
  213. return;
  214. }
  215. /*
  216. * If ERR_PTR is set then don't try to stat anything as it will cause
  217. * a crash when we access fp.
  218. */
  219. if (IS_ERR(fp)) {
  220. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  221. "fp has IS_ERR() set.\n");
  222. goto skip_stat;
  223. }
  224. /* Log stats for FLOGI reject */
  225. if (fc_frame_payload_op(fp) == ELS_LS_RJT)
  226. qedf->flogi_failed++;
  227. else if (fc_frame_payload_op(fp) == ELS_LS_ACC) {
  228. /* Set the source MAC we will use for FCoE traffic */
  229. qedf_set_data_src_addr(qedf, fp);
  230. }
  231. /* Complete flogi_compl so we can proceed to sending ADISCs */
  232. complete(&qedf->flogi_compl);
  233. skip_stat:
  234. /* Report response to libfc */
  235. fc_lport_flogi_resp(seq, fp, lport);
  236. }
  237. static struct fc_seq *qedf_elsct_send(struct fc_lport *lport, u32 did,
  238. struct fc_frame *fp, unsigned int op,
  239. void (*resp)(struct fc_seq *,
  240. struct fc_frame *,
  241. void *),
  242. void *arg, u32 timeout)
  243. {
  244. struct qedf_ctx *qedf = lport_priv(lport);
  245. /*
  246. * Intercept FLOGI for statistic purposes. Note we use the resp
  247. * callback to tell if this is really a flogi.
  248. */
  249. if (resp == fc_lport_flogi_resp) {
  250. qedf->flogi_cnt++;
  251. return fc_elsct_send(lport, did, fp, op, qedf_flogi_resp,
  252. arg, timeout);
  253. }
  254. return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
  255. }
  256. int qedf_send_flogi(struct qedf_ctx *qedf)
  257. {
  258. struct fc_lport *lport;
  259. struct fc_frame *fp;
  260. lport = qedf->lport;
  261. if (!lport->tt.elsct_send)
  262. return -EINVAL;
  263. fp = fc_frame_alloc(lport, sizeof(struct fc_els_flogi));
  264. if (!fp) {
  265. QEDF_ERR(&(qedf->dbg_ctx), "fc_frame_alloc failed.\n");
  266. return -ENOMEM;
  267. }
  268. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_ELS,
  269. "Sending FLOGI to reestablish session with switch.\n");
  270. lport->tt.elsct_send(lport, FC_FID_FLOGI, fp,
  271. ELS_FLOGI, qedf_flogi_resp, lport, lport->r_a_tov);
  272. init_completion(&qedf->flogi_compl);
  273. return 0;
  274. }
  275. struct qedf_tmp_rdata_item {
  276. struct fc_rport_priv *rdata;
  277. struct list_head list;
  278. };
  279. /*
  280. * This function is called if link_down_tmo is in use. If we get a link up and
  281. * link_down_tmo has not expired then use just FLOGI/ADISC to recover our
  282. * sessions with targets. Otherwise, just call fcoe_ctlr_link_up().
  283. */
  284. static void qedf_link_recovery(struct work_struct *work)
  285. {
  286. struct qedf_ctx *qedf =
  287. container_of(work, struct qedf_ctx, link_recovery.work);
  288. struct qedf_rport *fcport;
  289. struct fc_rport_priv *rdata;
  290. struct qedf_tmp_rdata_item *rdata_item, *tmp_rdata_item;
  291. bool rc;
  292. int retries = 30;
  293. int rval, i;
  294. struct list_head rdata_login_list;
  295. INIT_LIST_HEAD(&rdata_login_list);
  296. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  297. "Link down tmo did not expire.\n");
  298. /*
  299. * Essentially reset the fcoe_ctlr here without affecting the state
  300. * of the libfc structs.
  301. */
  302. qedf->ctlr.state = FIP_ST_LINK_WAIT;
  303. fcoe_ctlr_link_down(&qedf->ctlr);
  304. /*
  305. * Bring the link up before we send the fipvlan request so libfcoe
  306. * can select a new fcf in parallel
  307. */
  308. fcoe_ctlr_link_up(&qedf->ctlr);
  309. /* Since the link when down and up to verify which vlan we're on */
  310. qedf->fipvlan_retries = qedf_fipvlan_retries;
  311. rc = qedf_initiate_fipvlan_req(qedf);
  312. /* If getting the VLAN fails, set the VLAN to the fallback one */
  313. if (!rc)
  314. qedf_set_vlan_id(qedf, qedf_fallback_vlan);
  315. /*
  316. * We need to wait for an FCF to be selected due to the
  317. * fcoe_ctlr_link_up other the FLOGI will be rejected.
  318. */
  319. while (retries > 0) {
  320. if (qedf->ctlr.sel_fcf) {
  321. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  322. "FCF reselected, proceeding with FLOGI.\n");
  323. break;
  324. }
  325. msleep(500);
  326. retries--;
  327. }
  328. if (retries < 1) {
  329. QEDF_ERR(&(qedf->dbg_ctx), "Exhausted retries waiting for "
  330. "FCF selection.\n");
  331. return;
  332. }
  333. rval = qedf_send_flogi(qedf);
  334. if (rval)
  335. return;
  336. /* Wait for FLOGI completion before proceeding with sending ADISCs */
  337. i = wait_for_completion_timeout(&qedf->flogi_compl,
  338. qedf->lport->r_a_tov);
  339. if (i == 0) {
  340. QEDF_ERR(&(qedf->dbg_ctx), "FLOGI timed out.\n");
  341. return;
  342. }
  343. /*
  344. * Call lport->tt.rport_login which will cause libfc to send an
  345. * ADISC since the rport is in state ready.
  346. */
  347. rcu_read_lock();
  348. list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
  349. rdata = fcport->rdata;
  350. if (rdata == NULL)
  351. continue;
  352. rdata_item = kzalloc(sizeof(struct qedf_tmp_rdata_item),
  353. GFP_ATOMIC);
  354. if (!rdata_item)
  355. continue;
  356. if (kref_get_unless_zero(&rdata->kref)) {
  357. rdata_item->rdata = rdata;
  358. list_add(&rdata_item->list, &rdata_login_list);
  359. } else
  360. kfree(rdata_item);
  361. }
  362. rcu_read_unlock();
  363. /*
  364. * Do the fc_rport_login outside of the rcu lock so we don't take a
  365. * mutex in an atomic context.
  366. */
  367. list_for_each_entry_safe(rdata_item, tmp_rdata_item, &rdata_login_list,
  368. list) {
  369. list_del(&rdata_item->list);
  370. fc_rport_login(rdata_item->rdata);
  371. kref_put(&rdata_item->rdata->kref, fc_rport_destroy);
  372. kfree(rdata_item);
  373. }
  374. }
  375. static void qedf_update_link_speed(struct qedf_ctx *qedf,
  376. struct qed_link_output *link)
  377. {
  378. struct fc_lport *lport = qedf->lport;
  379. lport->link_speed = FC_PORTSPEED_UNKNOWN;
  380. lport->link_supported_speeds = FC_PORTSPEED_UNKNOWN;
  381. /* Set fc_host link speed */
  382. switch (link->speed) {
  383. case 10000:
  384. lport->link_speed = FC_PORTSPEED_10GBIT;
  385. break;
  386. case 25000:
  387. lport->link_speed = FC_PORTSPEED_25GBIT;
  388. break;
  389. case 40000:
  390. lport->link_speed = FC_PORTSPEED_40GBIT;
  391. break;
  392. case 50000:
  393. lport->link_speed = FC_PORTSPEED_50GBIT;
  394. break;
  395. case 100000:
  396. lport->link_speed = FC_PORTSPEED_100GBIT;
  397. break;
  398. default:
  399. lport->link_speed = FC_PORTSPEED_UNKNOWN;
  400. break;
  401. }
  402. /*
  403. * Set supported link speed by querying the supported
  404. * capabilities of the link.
  405. */
  406. if (link->supported_caps & SUPPORTED_10000baseKR_Full)
  407. lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
  408. if (link->supported_caps & SUPPORTED_25000baseKR_Full)
  409. lport->link_supported_speeds |= FC_PORTSPEED_25GBIT;
  410. if (link->supported_caps & SUPPORTED_40000baseLR4_Full)
  411. lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
  412. if (link->supported_caps & SUPPORTED_50000baseKR2_Full)
  413. lport->link_supported_speeds |= FC_PORTSPEED_50GBIT;
  414. if (link->supported_caps & SUPPORTED_100000baseKR4_Full)
  415. lport->link_supported_speeds |= FC_PORTSPEED_100GBIT;
  416. fc_host_supported_speeds(lport->host) = lport->link_supported_speeds;
  417. }
  418. static void qedf_link_update(void *dev, struct qed_link_output *link)
  419. {
  420. struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
  421. if (link->link_up) {
  422. if (atomic_read(&qedf->link_state) == QEDF_LINK_UP) {
  423. QEDF_INFO((&qedf->dbg_ctx), QEDF_LOG_DISC,
  424. "Ignoring link up event as link is already up.\n");
  425. return;
  426. }
  427. QEDF_ERR(&(qedf->dbg_ctx), "LINK UP (%d GB/s).\n",
  428. link->speed / 1000);
  429. /* Cancel any pending link down work */
  430. cancel_delayed_work(&qedf->link_update);
  431. atomic_set(&qedf->link_state, QEDF_LINK_UP);
  432. qedf_update_link_speed(qedf, link);
  433. if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE ||
  434. qedf_dcbx_no_wait) {
  435. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  436. "DCBx done.\n");
  437. if (atomic_read(&qedf->link_down_tmo_valid) > 0)
  438. queue_delayed_work(qedf->link_update_wq,
  439. &qedf->link_recovery, 0);
  440. else
  441. queue_delayed_work(qedf->link_update_wq,
  442. &qedf->link_update, 0);
  443. atomic_set(&qedf->link_down_tmo_valid, 0);
  444. }
  445. } else {
  446. QEDF_ERR(&(qedf->dbg_ctx), "LINK DOWN.\n");
  447. atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
  448. atomic_set(&qedf->dcbx, QEDF_DCBX_PENDING);
  449. /*
  450. * Flag that we're waiting for the link to come back up before
  451. * informing the fcoe layer of the event.
  452. */
  453. if (qedf_link_down_tmo > 0) {
  454. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  455. "Starting link down tmo.\n");
  456. atomic_set(&qedf->link_down_tmo_valid, 1);
  457. }
  458. qedf->vlan_id = 0;
  459. qedf_update_link_speed(qedf, link);
  460. queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
  461. qedf_link_down_tmo * HZ);
  462. }
  463. }
  464. static void qedf_dcbx_handler(void *dev, struct qed_dcbx_get *get, u32 mib_type)
  465. {
  466. struct qedf_ctx *qedf = (struct qedf_ctx *)dev;
  467. u8 tmp_prio;
  468. QEDF_ERR(&(qedf->dbg_ctx), "DCBx event valid=%d enabled=%d fcoe "
  469. "prio=%d.\n", get->operational.valid, get->operational.enabled,
  470. get->operational.app_prio.fcoe);
  471. if (get->operational.enabled && get->operational.valid) {
  472. /* If DCBX was already negotiated on link up then just exit */
  473. if (atomic_read(&qedf->dcbx) == QEDF_DCBX_DONE) {
  474. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  475. "DCBX already set on link up.\n");
  476. return;
  477. }
  478. atomic_set(&qedf->dcbx, QEDF_DCBX_DONE);
  479. /*
  480. * Set the 8021q priority in the following manner:
  481. *
  482. * 1. If a modparam is set use that
  483. * 2. If the value is not between 0..7 use the default
  484. * 3. Use the priority we get from the DCBX app tag
  485. */
  486. tmp_prio = get->operational.app_prio.fcoe;
  487. if (qedf_default_prio > -1)
  488. qedf->prio = qedf_default_prio;
  489. else if (tmp_prio < 0 || tmp_prio > 7) {
  490. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  491. "FIP/FCoE prio %d out of range, setting to %d.\n",
  492. tmp_prio, QEDF_DEFAULT_PRIO);
  493. qedf->prio = QEDF_DEFAULT_PRIO;
  494. } else
  495. qedf->prio = tmp_prio;
  496. if (atomic_read(&qedf->link_state) == QEDF_LINK_UP &&
  497. !qedf_dcbx_no_wait) {
  498. if (atomic_read(&qedf->link_down_tmo_valid) > 0)
  499. queue_delayed_work(qedf->link_update_wq,
  500. &qedf->link_recovery, 0);
  501. else
  502. queue_delayed_work(qedf->link_update_wq,
  503. &qedf->link_update, 0);
  504. atomic_set(&qedf->link_down_tmo_valid, 0);
  505. }
  506. }
  507. }
  508. static u32 qedf_get_login_failures(void *cookie)
  509. {
  510. struct qedf_ctx *qedf;
  511. qedf = (struct qedf_ctx *)cookie;
  512. return qedf->flogi_failed;
  513. }
  514. static struct qed_fcoe_cb_ops qedf_cb_ops = {
  515. {
  516. .link_update = qedf_link_update,
  517. .dcbx_aen = qedf_dcbx_handler,
  518. .get_generic_tlv_data = qedf_get_generic_tlv_data,
  519. .get_protocol_tlv_data = qedf_get_protocol_tlv_data,
  520. }
  521. };
  522. /*
  523. * Various transport templates.
  524. */
  525. static struct scsi_transport_template *qedf_fc_transport_template;
  526. static struct scsi_transport_template *qedf_fc_vport_transport_template;
  527. /*
  528. * SCSI EH handlers
  529. */
  530. static int qedf_eh_abort(struct scsi_cmnd *sc_cmd)
  531. {
  532. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  533. struct fc_rport_libfc_priv *rp = rport->dd_data;
  534. struct qedf_rport *fcport;
  535. struct fc_lport *lport;
  536. struct qedf_ctx *qedf;
  537. struct qedf_ioreq *io_req;
  538. int rc = FAILED;
  539. int rval;
  540. if (fc_remote_port_chkready(rport)) {
  541. QEDF_ERR(NULL, "rport not ready\n");
  542. goto out;
  543. }
  544. lport = shost_priv(sc_cmd->device->host);
  545. qedf = (struct qedf_ctx *)lport_priv(lport);
  546. if ((lport->state != LPORT_ST_READY) || !(lport->link_up)) {
  547. QEDF_ERR(&(qedf->dbg_ctx), "link not ready.\n");
  548. goto out;
  549. }
  550. fcport = (struct qedf_rport *)&rp[1];
  551. io_req = (struct qedf_ioreq *)sc_cmd->SCp.ptr;
  552. if (!io_req) {
  553. QEDF_ERR(&(qedf->dbg_ctx), "io_req is NULL.\n");
  554. rc = SUCCESS;
  555. goto out;
  556. }
  557. QEDF_ERR(&(qedf->dbg_ctx), "Aborting io_req sc_cmd=%p xid=0x%x "
  558. "fp_idx=%d.\n", sc_cmd, io_req->xid, io_req->fp_idx);
  559. if (qedf->stop_io_on_error) {
  560. qedf_stop_all_io(qedf);
  561. rc = SUCCESS;
  562. goto out;
  563. }
  564. init_completion(&io_req->abts_done);
  565. rval = qedf_initiate_abts(io_req, true);
  566. if (rval) {
  567. QEDF_ERR(&(qedf->dbg_ctx), "Failed to queue ABTS.\n");
  568. goto out;
  569. }
  570. wait_for_completion(&io_req->abts_done);
  571. if (io_req->event == QEDF_IOREQ_EV_ABORT_SUCCESS ||
  572. io_req->event == QEDF_IOREQ_EV_ABORT_FAILED ||
  573. io_req->event == QEDF_IOREQ_EV_CLEANUP_SUCCESS) {
  574. /*
  575. * If we get a reponse to the abort this is success from
  576. * the perspective that all references to the command have
  577. * been removed from the driver and firmware
  578. */
  579. rc = SUCCESS;
  580. } else {
  581. /* If the abort and cleanup failed then return a failure */
  582. rc = FAILED;
  583. }
  584. if (rc == SUCCESS)
  585. QEDF_ERR(&(qedf->dbg_ctx), "ABTS succeeded, xid=0x%x.\n",
  586. io_req->xid);
  587. else
  588. QEDF_ERR(&(qedf->dbg_ctx), "ABTS failed, xid=0x%x.\n",
  589. io_req->xid);
  590. out:
  591. return rc;
  592. }
  593. static int qedf_eh_target_reset(struct scsi_cmnd *sc_cmd)
  594. {
  595. QEDF_ERR(NULL, "TARGET RESET Issued...");
  596. return qedf_initiate_tmf(sc_cmd, FCP_TMF_TGT_RESET);
  597. }
  598. static int qedf_eh_device_reset(struct scsi_cmnd *sc_cmd)
  599. {
  600. QEDF_ERR(NULL, "LUN RESET Issued...\n");
  601. return qedf_initiate_tmf(sc_cmd, FCP_TMF_LUN_RESET);
  602. }
  603. void qedf_wait_for_upload(struct qedf_ctx *qedf)
  604. {
  605. while (1) {
  606. if (atomic_read(&qedf->num_offloads))
  607. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  608. "Waiting for all uploads to complete.\n");
  609. else
  610. break;
  611. msleep(500);
  612. }
  613. }
  614. /* Performs soft reset of qedf_ctx by simulating a link down/up */
  615. static void qedf_ctx_soft_reset(struct fc_lport *lport)
  616. {
  617. struct qedf_ctx *qedf;
  618. if (lport->vport) {
  619. QEDF_ERR(NULL, "Cannot issue host reset on NPIV port.\n");
  620. return;
  621. }
  622. qedf = lport_priv(lport);
  623. /* For host reset, essentially do a soft link up/down */
  624. atomic_set(&qedf->link_state, QEDF_LINK_DOWN);
  625. queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
  626. 0);
  627. qedf_wait_for_upload(qedf);
  628. atomic_set(&qedf->link_state, QEDF_LINK_UP);
  629. qedf->vlan_id = 0;
  630. queue_delayed_work(qedf->link_update_wq, &qedf->link_update,
  631. 0);
  632. }
  633. /* Reset the host by gracefully logging out and then logging back in */
  634. static int qedf_eh_host_reset(struct scsi_cmnd *sc_cmd)
  635. {
  636. struct fc_lport *lport;
  637. struct qedf_ctx *qedf;
  638. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  639. struct fc_rport_libfc_priv *rp = rport->dd_data;
  640. struct qedf_rport *fcport = (struct qedf_rport *)&rp[1];
  641. int rval;
  642. rval = fc_remote_port_chkready(rport);
  643. if (rval) {
  644. QEDF_ERR(NULL, "device_reset rport not ready\n");
  645. return FAILED;
  646. }
  647. if (fcport == NULL) {
  648. QEDF_ERR(NULL, "device_reset: rport is NULL\n");
  649. return FAILED;
  650. }
  651. lport = shost_priv(sc_cmd->device->host);
  652. qedf = lport_priv(lport);
  653. if (atomic_read(&qedf->link_state) == QEDF_LINK_DOWN ||
  654. test_bit(QEDF_UNLOADING, &qedf->flags))
  655. return FAILED;
  656. QEDF_ERR(&(qedf->dbg_ctx), "HOST RESET Issued...");
  657. qedf_ctx_soft_reset(lport);
  658. return SUCCESS;
  659. }
  660. static int qedf_slave_configure(struct scsi_device *sdev)
  661. {
  662. if (qedf_queue_depth) {
  663. scsi_change_queue_depth(sdev, qedf_queue_depth);
  664. }
  665. return 0;
  666. }
  667. static struct scsi_host_template qedf_host_template = {
  668. .module = THIS_MODULE,
  669. .name = QEDF_MODULE_NAME,
  670. .this_id = -1,
  671. .cmd_per_lun = 32,
  672. .use_clustering = ENABLE_CLUSTERING,
  673. .max_sectors = 0xffff,
  674. .queuecommand = qedf_queuecommand,
  675. .shost_attrs = qedf_host_attrs,
  676. .eh_abort_handler = qedf_eh_abort,
  677. .eh_device_reset_handler = qedf_eh_device_reset, /* lun reset */
  678. .eh_target_reset_handler = qedf_eh_target_reset, /* target reset */
  679. .eh_host_reset_handler = qedf_eh_host_reset,
  680. .slave_configure = qedf_slave_configure,
  681. .dma_boundary = QED_HW_DMA_BOUNDARY,
  682. .sg_tablesize = QEDF_MAX_BDS_PER_CMD,
  683. .can_queue = FCOE_PARAMS_NUM_TASKS,
  684. .change_queue_depth = scsi_change_queue_depth,
  685. };
  686. static int qedf_get_paged_crc_eof(struct sk_buff *skb, int tlen)
  687. {
  688. int rc;
  689. spin_lock(&qedf_global_lock);
  690. rc = fcoe_get_paged_crc_eof(skb, tlen, &qedf_global);
  691. spin_unlock(&qedf_global_lock);
  692. return rc;
  693. }
  694. static struct qedf_rport *qedf_fcport_lookup(struct qedf_ctx *qedf, u32 port_id)
  695. {
  696. struct qedf_rport *fcport;
  697. struct fc_rport_priv *rdata;
  698. rcu_read_lock();
  699. list_for_each_entry_rcu(fcport, &qedf->fcports, peers) {
  700. rdata = fcport->rdata;
  701. if (rdata == NULL)
  702. continue;
  703. if (rdata->ids.port_id == port_id) {
  704. rcu_read_unlock();
  705. return fcport;
  706. }
  707. }
  708. rcu_read_unlock();
  709. /* Return NULL to caller to let them know fcport was not found */
  710. return NULL;
  711. }
  712. /* Transmits an ELS frame over an offloaded session */
  713. static int qedf_xmit_l2_frame(struct qedf_rport *fcport, struct fc_frame *fp)
  714. {
  715. struct fc_frame_header *fh;
  716. int rc = 0;
  717. fh = fc_frame_header_get(fp);
  718. if ((fh->fh_type == FC_TYPE_ELS) &&
  719. (fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
  720. switch (fc_frame_payload_op(fp)) {
  721. case ELS_ADISC:
  722. qedf_send_adisc(fcport, fp);
  723. rc = 1;
  724. break;
  725. }
  726. }
  727. return rc;
  728. }
  729. /**
  730. * qedf_xmit - qedf FCoE frame transmit function
  731. *
  732. */
  733. static int qedf_xmit(struct fc_lport *lport, struct fc_frame *fp)
  734. {
  735. struct fc_lport *base_lport;
  736. struct qedf_ctx *qedf;
  737. struct ethhdr *eh;
  738. struct fcoe_crc_eof *cp;
  739. struct sk_buff *skb;
  740. struct fc_frame_header *fh;
  741. struct fcoe_hdr *hp;
  742. u8 sof, eof;
  743. u32 crc;
  744. unsigned int hlen, tlen, elen;
  745. int wlen;
  746. struct fc_stats *stats;
  747. struct fc_lport *tmp_lport;
  748. struct fc_lport *vn_port = NULL;
  749. struct qedf_rport *fcport;
  750. int rc;
  751. u16 vlan_tci = 0;
  752. qedf = (struct qedf_ctx *)lport_priv(lport);
  753. fh = fc_frame_header_get(fp);
  754. skb = fp_skb(fp);
  755. /* Filter out traffic to other NPIV ports on the same host */
  756. if (lport->vport)
  757. base_lport = shost_priv(vport_to_shost(lport->vport));
  758. else
  759. base_lport = lport;
  760. /* Flag if the destination is the base port */
  761. if (base_lport->port_id == ntoh24(fh->fh_d_id)) {
  762. vn_port = base_lport;
  763. } else {
  764. /* Got through the list of vports attached to the base_lport
  765. * and see if we have a match with the destination address.
  766. */
  767. list_for_each_entry(tmp_lport, &base_lport->vports, list) {
  768. if (tmp_lport->port_id == ntoh24(fh->fh_d_id)) {
  769. vn_port = tmp_lport;
  770. break;
  771. }
  772. }
  773. }
  774. if (vn_port && ntoh24(fh->fh_d_id) != FC_FID_FLOGI) {
  775. struct fc_rport_priv *rdata = NULL;
  776. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  777. "Dropping FCoE frame to %06x.\n", ntoh24(fh->fh_d_id));
  778. kfree_skb(skb);
  779. rdata = fc_rport_lookup(lport, ntoh24(fh->fh_d_id));
  780. if (rdata)
  781. rdata->retries = lport->max_rport_retry_count;
  782. return -EINVAL;
  783. }
  784. /* End NPIV filtering */
  785. if (!qedf->ctlr.sel_fcf) {
  786. kfree_skb(skb);
  787. return 0;
  788. }
  789. if (!test_bit(QEDF_LL2_STARTED, &qedf->flags)) {
  790. QEDF_WARN(&(qedf->dbg_ctx), "LL2 not started\n");
  791. kfree_skb(skb);
  792. return 0;
  793. }
  794. if (atomic_read(&qedf->link_state) != QEDF_LINK_UP) {
  795. QEDF_WARN(&(qedf->dbg_ctx), "qedf link down\n");
  796. kfree_skb(skb);
  797. return 0;
  798. }
  799. if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
  800. if (fcoe_ctlr_els_send(&qedf->ctlr, lport, skb))
  801. return 0;
  802. }
  803. /* Check to see if this needs to be sent on an offloaded session */
  804. fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
  805. if (fcport && test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  806. rc = qedf_xmit_l2_frame(fcport, fp);
  807. /*
  808. * If the frame was successfully sent over the middle path
  809. * then do not try to also send it over the LL2 path
  810. */
  811. if (rc)
  812. return 0;
  813. }
  814. sof = fr_sof(fp);
  815. eof = fr_eof(fp);
  816. elen = sizeof(struct ethhdr);
  817. hlen = sizeof(struct fcoe_hdr);
  818. tlen = sizeof(struct fcoe_crc_eof);
  819. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  820. skb->ip_summed = CHECKSUM_NONE;
  821. crc = fcoe_fc_crc(fp);
  822. /* copy port crc and eof to the skb buff */
  823. if (skb_is_nonlinear(skb)) {
  824. skb_frag_t *frag;
  825. if (qedf_get_paged_crc_eof(skb, tlen)) {
  826. kfree_skb(skb);
  827. return -ENOMEM;
  828. }
  829. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  830. cp = kmap_atomic(skb_frag_page(frag)) + frag->page_offset;
  831. } else {
  832. cp = skb_put(skb, tlen);
  833. }
  834. memset(cp, 0, sizeof(*cp));
  835. cp->fcoe_eof = eof;
  836. cp->fcoe_crc32 = cpu_to_le32(~crc);
  837. if (skb_is_nonlinear(skb)) {
  838. kunmap_atomic(cp);
  839. cp = NULL;
  840. }
  841. /* adjust skb network/transport offsets to match mac/fcoe/port */
  842. skb_push(skb, elen + hlen);
  843. skb_reset_mac_header(skb);
  844. skb_reset_network_header(skb);
  845. skb->mac_len = elen;
  846. skb->protocol = htons(ETH_P_FCOE);
  847. /*
  848. * Add VLAN tag to non-offload FCoE frame based on current stored VLAN
  849. * for FIP/FCoE traffic.
  850. */
  851. __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), qedf->vlan_id);
  852. /* fill up mac and fcoe headers */
  853. eh = eth_hdr(skb);
  854. eh->h_proto = htons(ETH_P_FCOE);
  855. if (qedf->ctlr.map_dest)
  856. fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
  857. else
  858. /* insert GW address */
  859. ether_addr_copy(eh->h_dest, qedf->ctlr.dest_addr);
  860. /* Set the source MAC address */
  861. ether_addr_copy(eh->h_source, qedf->data_src_addr);
  862. hp = (struct fcoe_hdr *)(eh + 1);
  863. memset(hp, 0, sizeof(*hp));
  864. if (FC_FCOE_VER)
  865. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  866. hp->fcoe_sof = sof;
  867. /*update tx stats */
  868. stats = per_cpu_ptr(lport->stats, get_cpu());
  869. stats->TxFrames++;
  870. stats->TxWords += wlen;
  871. put_cpu();
  872. /* Get VLAN ID from skb for printing purposes */
  873. __vlan_hwaccel_get_tag(skb, &vlan_tci);
  874. /* send down to lld */
  875. fr_dev(fp) = lport;
  876. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame send: "
  877. "src=%06x dest=%06x r_ctl=%x type=%x vlan=%04x.\n",
  878. ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl, fh->fh_type,
  879. vlan_tci);
  880. if (qedf_dump_frames)
  881. print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
  882. 1, skb->data, skb->len, false);
  883. qed_ops->ll2->start_xmit(qedf->cdev, skb, 0);
  884. return 0;
  885. }
  886. static int qedf_alloc_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
  887. {
  888. int rval = 0;
  889. u32 *pbl;
  890. dma_addr_t page;
  891. int num_pages;
  892. /* Calculate appropriate queue and PBL sizes */
  893. fcport->sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
  894. fcport->sq_mem_size = ALIGN(fcport->sq_mem_size, QEDF_PAGE_SIZE);
  895. fcport->sq_pbl_size = (fcport->sq_mem_size / QEDF_PAGE_SIZE) *
  896. sizeof(void *);
  897. fcport->sq_pbl_size = fcport->sq_pbl_size + QEDF_PAGE_SIZE;
  898. fcport->sq = dma_zalloc_coherent(&qedf->pdev->dev,
  899. fcport->sq_mem_size, &fcport->sq_dma, GFP_KERNEL);
  900. if (!fcport->sq) {
  901. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue.\n");
  902. rval = 1;
  903. goto out;
  904. }
  905. fcport->sq_pbl = dma_zalloc_coherent(&qedf->pdev->dev,
  906. fcport->sq_pbl_size, &fcport->sq_pbl_dma, GFP_KERNEL);
  907. if (!fcport->sq_pbl) {
  908. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate send queue PBL.\n");
  909. rval = 1;
  910. goto out_free_sq;
  911. }
  912. /* Create PBL */
  913. num_pages = fcport->sq_mem_size / QEDF_PAGE_SIZE;
  914. page = fcport->sq_dma;
  915. pbl = (u32 *)fcport->sq_pbl;
  916. while (num_pages--) {
  917. *pbl = U64_LO(page);
  918. pbl++;
  919. *pbl = U64_HI(page);
  920. pbl++;
  921. page += QEDF_PAGE_SIZE;
  922. }
  923. return rval;
  924. out_free_sq:
  925. dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size, fcport->sq,
  926. fcport->sq_dma);
  927. out:
  928. return rval;
  929. }
  930. static void qedf_free_sq(struct qedf_ctx *qedf, struct qedf_rport *fcport)
  931. {
  932. if (fcport->sq_pbl)
  933. dma_free_coherent(&qedf->pdev->dev, fcport->sq_pbl_size,
  934. fcport->sq_pbl, fcport->sq_pbl_dma);
  935. if (fcport->sq)
  936. dma_free_coherent(&qedf->pdev->dev, fcport->sq_mem_size,
  937. fcport->sq, fcport->sq_dma);
  938. }
  939. static int qedf_offload_connection(struct qedf_ctx *qedf,
  940. struct qedf_rport *fcport)
  941. {
  942. struct qed_fcoe_params_offload conn_info;
  943. u32 port_id;
  944. int rval;
  945. uint16_t total_sqe = (fcport->sq_mem_size / sizeof(struct fcoe_wqe));
  946. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offloading connection "
  947. "portid=%06x.\n", fcport->rdata->ids.port_id);
  948. rval = qed_ops->acquire_conn(qedf->cdev, &fcport->handle,
  949. &fcport->fw_cid, &fcport->p_doorbell);
  950. if (rval) {
  951. QEDF_WARN(&(qedf->dbg_ctx), "Could not acquire connection "
  952. "for portid=%06x.\n", fcport->rdata->ids.port_id);
  953. rval = 1; /* For some reason qed returns 0 on failure here */
  954. goto out;
  955. }
  956. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "portid=%06x "
  957. "fw_cid=%08x handle=%d.\n", fcport->rdata->ids.port_id,
  958. fcport->fw_cid, fcport->handle);
  959. memset(&conn_info, 0, sizeof(struct qed_fcoe_params_offload));
  960. /* Fill in the offload connection info */
  961. conn_info.sq_pbl_addr = fcport->sq_pbl_dma;
  962. conn_info.sq_curr_page_addr = (dma_addr_t)(*(u64 *)fcport->sq_pbl);
  963. conn_info.sq_next_page_addr =
  964. (dma_addr_t)(*(u64 *)(fcport->sq_pbl + 8));
  965. /* Need to use our FCoE MAC for the offload session */
  966. ether_addr_copy(conn_info.src_mac, qedf->data_src_addr);
  967. ether_addr_copy(conn_info.dst_mac, qedf->ctlr.dest_addr);
  968. conn_info.tx_max_fc_pay_len = fcport->rdata->maxframe_size;
  969. conn_info.e_d_tov_timer_val = qedf->lport->e_d_tov / 20;
  970. conn_info.rec_tov_timer_val = 3; /* I think this is what E3 was */
  971. conn_info.rx_max_fc_pay_len = fcport->rdata->maxframe_size;
  972. /* Set VLAN data */
  973. conn_info.vlan_tag = qedf->vlan_id <<
  974. FCOE_CONN_OFFLOAD_RAMROD_DATA_VLAN_ID_SHIFT;
  975. conn_info.vlan_tag |=
  976. qedf->prio << FCOE_CONN_OFFLOAD_RAMROD_DATA_PRIORITY_SHIFT;
  977. conn_info.flags |= (FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_MASK <<
  978. FCOE_CONN_OFFLOAD_RAMROD_DATA_B_VLAN_FLAG_SHIFT);
  979. /* Set host port source id */
  980. port_id = fc_host_port_id(qedf->lport->host);
  981. fcport->sid = port_id;
  982. conn_info.s_id.addr_hi = (port_id & 0x000000FF);
  983. conn_info.s_id.addr_mid = (port_id & 0x0000FF00) >> 8;
  984. conn_info.s_id.addr_lo = (port_id & 0x00FF0000) >> 16;
  985. conn_info.max_conc_seqs_c3 = fcport->rdata->max_seq;
  986. /* Set remote port destination id */
  987. port_id = fcport->rdata->rport->port_id;
  988. conn_info.d_id.addr_hi = (port_id & 0x000000FF);
  989. conn_info.d_id.addr_mid = (port_id & 0x0000FF00) >> 8;
  990. conn_info.d_id.addr_lo = (port_id & 0x00FF0000) >> 16;
  991. conn_info.def_q_idx = 0; /* Default index for send queue? */
  992. /* Set FC-TAPE specific flags if needed */
  993. if (fcport->dev_type == QEDF_RPORT_TYPE_TAPE) {
  994. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN,
  995. "Enable CONF, REC for portid=%06x.\n",
  996. fcport->rdata->ids.port_id);
  997. conn_info.flags |= 1 <<
  998. FCOE_CONN_OFFLOAD_RAMROD_DATA_B_CONF_REQ_SHIFT;
  999. conn_info.flags |=
  1000. ((fcport->rdata->sp_features & FC_SP_FT_SEQC) ? 1 : 0) <<
  1001. FCOE_CONN_OFFLOAD_RAMROD_DATA_B_REC_VALID_SHIFT;
  1002. }
  1003. rval = qed_ops->offload_conn(qedf->cdev, fcport->handle, &conn_info);
  1004. if (rval) {
  1005. QEDF_WARN(&(qedf->dbg_ctx), "Could not offload connection "
  1006. "for portid=%06x.\n", fcport->rdata->ids.port_id);
  1007. goto out_free_conn;
  1008. } else
  1009. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Offload "
  1010. "succeeded portid=%06x total_sqe=%d.\n",
  1011. fcport->rdata->ids.port_id, total_sqe);
  1012. spin_lock_init(&fcport->rport_lock);
  1013. atomic_set(&fcport->free_sqes, total_sqe);
  1014. return 0;
  1015. out_free_conn:
  1016. qed_ops->release_conn(qedf->cdev, fcport->handle);
  1017. out:
  1018. return rval;
  1019. }
  1020. #define QEDF_TERM_BUFF_SIZE 10
  1021. static void qedf_upload_connection(struct qedf_ctx *qedf,
  1022. struct qedf_rport *fcport)
  1023. {
  1024. void *term_params;
  1025. dma_addr_t term_params_dma;
  1026. /* Term params needs to be a DMA coherent buffer as qed shared the
  1027. * physical DMA address with the firmware. The buffer may be used in
  1028. * the receive path so we may eventually have to move this.
  1029. */
  1030. term_params = dma_alloc_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE,
  1031. &term_params_dma, GFP_KERNEL);
  1032. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Uploading connection "
  1033. "port_id=%06x.\n", fcport->rdata->ids.port_id);
  1034. qed_ops->destroy_conn(qedf->cdev, fcport->handle, term_params_dma);
  1035. qed_ops->release_conn(qedf->cdev, fcport->handle);
  1036. dma_free_coherent(&qedf->pdev->dev, QEDF_TERM_BUFF_SIZE, term_params,
  1037. term_params_dma);
  1038. }
  1039. static void qedf_cleanup_fcport(struct qedf_ctx *qedf,
  1040. struct qedf_rport *fcport)
  1041. {
  1042. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_CONN, "Cleaning up portid=%06x.\n",
  1043. fcport->rdata->ids.port_id);
  1044. /* Flush any remaining i/o's before we upload the connection */
  1045. qedf_flush_active_ios(fcport, -1);
  1046. if (test_and_clear_bit(QEDF_RPORT_SESSION_READY, &fcport->flags))
  1047. qedf_upload_connection(qedf, fcport);
  1048. qedf_free_sq(qedf, fcport);
  1049. fcport->rdata = NULL;
  1050. fcport->qedf = NULL;
  1051. }
  1052. /**
  1053. * This event_callback is called after successful completion of libfc
  1054. * initiated target login. qedf can proceed with initiating the session
  1055. * establishment.
  1056. */
  1057. static void qedf_rport_event_handler(struct fc_lport *lport,
  1058. struct fc_rport_priv *rdata,
  1059. enum fc_rport_event event)
  1060. {
  1061. struct qedf_ctx *qedf = lport_priv(lport);
  1062. struct fc_rport *rport = rdata->rport;
  1063. struct fc_rport_libfc_priv *rp;
  1064. struct qedf_rport *fcport;
  1065. u32 port_id;
  1066. int rval;
  1067. unsigned long flags;
  1068. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "event = %d, "
  1069. "port_id = 0x%x\n", event, rdata->ids.port_id);
  1070. switch (event) {
  1071. case RPORT_EV_READY:
  1072. if (!rport) {
  1073. QEDF_WARN(&(qedf->dbg_ctx), "rport is NULL.\n");
  1074. break;
  1075. }
  1076. rp = rport->dd_data;
  1077. fcport = (struct qedf_rport *)&rp[1];
  1078. fcport->qedf = qedf;
  1079. if (atomic_read(&qedf->num_offloads) >= QEDF_MAX_SESSIONS) {
  1080. QEDF_ERR(&(qedf->dbg_ctx), "Not offloading "
  1081. "portid=0x%x as max number of offloaded sessions "
  1082. "reached.\n", rdata->ids.port_id);
  1083. return;
  1084. }
  1085. /*
  1086. * Don't try to offload the session again. Can happen when we
  1087. * get an ADISC
  1088. */
  1089. if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  1090. QEDF_WARN(&(qedf->dbg_ctx), "Session already "
  1091. "offloaded, portid=0x%x.\n",
  1092. rdata->ids.port_id);
  1093. return;
  1094. }
  1095. if (rport->port_id == FC_FID_DIR_SERV) {
  1096. /*
  1097. * qedf_rport structure doesn't exist for
  1098. * directory server.
  1099. * We should not come here, as lport will
  1100. * take care of fabric login
  1101. */
  1102. QEDF_WARN(&(qedf->dbg_ctx), "rport struct does not "
  1103. "exist for dir server port_id=%x\n",
  1104. rdata->ids.port_id);
  1105. break;
  1106. }
  1107. if (rdata->spp_type != FC_TYPE_FCP) {
  1108. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1109. "Not offloading since spp type isn't FCP\n");
  1110. break;
  1111. }
  1112. if (!(rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET)) {
  1113. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1114. "Not FCP target so not offloading\n");
  1115. break;
  1116. }
  1117. fcport->rdata = rdata;
  1118. fcport->rport = rport;
  1119. rval = qedf_alloc_sq(qedf, fcport);
  1120. if (rval) {
  1121. qedf_cleanup_fcport(qedf, fcport);
  1122. break;
  1123. }
  1124. /* Set device type */
  1125. if (rdata->flags & FC_RP_FLAGS_RETRY &&
  1126. rdata->ids.roles & FC_RPORT_ROLE_FCP_TARGET &&
  1127. !(rdata->ids.roles & FC_RPORT_ROLE_FCP_INITIATOR)) {
  1128. fcport->dev_type = QEDF_RPORT_TYPE_TAPE;
  1129. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1130. "portid=%06x is a TAPE device.\n",
  1131. rdata->ids.port_id);
  1132. } else {
  1133. fcport->dev_type = QEDF_RPORT_TYPE_DISK;
  1134. }
  1135. rval = qedf_offload_connection(qedf, fcport);
  1136. if (rval) {
  1137. qedf_cleanup_fcport(qedf, fcport);
  1138. break;
  1139. }
  1140. /* Add fcport to list of qedf_ctx list of offloaded ports */
  1141. spin_lock_irqsave(&qedf->hba_lock, flags);
  1142. list_add_rcu(&fcport->peers, &qedf->fcports);
  1143. spin_unlock_irqrestore(&qedf->hba_lock, flags);
  1144. /*
  1145. * Set the session ready bit to let everyone know that this
  1146. * connection is ready for I/O
  1147. */
  1148. set_bit(QEDF_RPORT_SESSION_READY, &fcport->flags);
  1149. atomic_inc(&qedf->num_offloads);
  1150. break;
  1151. case RPORT_EV_LOGO:
  1152. case RPORT_EV_FAILED:
  1153. case RPORT_EV_STOP:
  1154. port_id = rdata->ids.port_id;
  1155. if (port_id == FC_FID_DIR_SERV)
  1156. break;
  1157. if (!rport) {
  1158. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  1159. "port_id=%x - rport notcreated Yet!!\n", port_id);
  1160. break;
  1161. }
  1162. rp = rport->dd_data;
  1163. /*
  1164. * Perform session upload. Note that rdata->peers is already
  1165. * removed from disc->rports list before we get this event.
  1166. */
  1167. fcport = (struct qedf_rport *)&rp[1];
  1168. /* Only free this fcport if it is offloaded already */
  1169. if (test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  1170. set_bit(QEDF_RPORT_UPLOADING_CONNECTION, &fcport->flags);
  1171. qedf_cleanup_fcport(qedf, fcport);
  1172. /*
  1173. * Remove fcport to list of qedf_ctx list of offloaded
  1174. * ports
  1175. */
  1176. spin_lock_irqsave(&qedf->hba_lock, flags);
  1177. list_del_rcu(&fcport->peers);
  1178. spin_unlock_irqrestore(&qedf->hba_lock, flags);
  1179. clear_bit(QEDF_RPORT_UPLOADING_CONNECTION,
  1180. &fcport->flags);
  1181. atomic_dec(&qedf->num_offloads);
  1182. }
  1183. break;
  1184. case RPORT_EV_NONE:
  1185. break;
  1186. }
  1187. }
  1188. static void qedf_abort_io(struct fc_lport *lport)
  1189. {
  1190. /* NO-OP but need to fill in the template */
  1191. }
  1192. static void qedf_fcp_cleanup(struct fc_lport *lport)
  1193. {
  1194. /*
  1195. * NO-OP but need to fill in template to prevent a NULL
  1196. * function pointer dereference during link down. I/Os
  1197. * will be flushed when port is uploaded.
  1198. */
  1199. }
  1200. static struct libfc_function_template qedf_lport_template = {
  1201. .frame_send = qedf_xmit,
  1202. .fcp_abort_io = qedf_abort_io,
  1203. .fcp_cleanup = qedf_fcp_cleanup,
  1204. .rport_event_callback = qedf_rport_event_handler,
  1205. .elsct_send = qedf_elsct_send,
  1206. };
  1207. static void qedf_fcoe_ctlr_setup(struct qedf_ctx *qedf)
  1208. {
  1209. fcoe_ctlr_init(&qedf->ctlr, FIP_MODE_AUTO);
  1210. qedf->ctlr.send = qedf_fip_send;
  1211. qedf->ctlr.get_src_addr = qedf_get_src_mac;
  1212. ether_addr_copy(qedf->ctlr.ctl_src_addr, qedf->mac);
  1213. }
  1214. static void qedf_setup_fdmi(struct qedf_ctx *qedf)
  1215. {
  1216. struct fc_lport *lport = qedf->lport;
  1217. struct fc_host_attrs *fc_host = shost_to_fc_host(lport->host);
  1218. u8 buf[8];
  1219. int i, pos;
  1220. /*
  1221. * fdmi_enabled needs to be set for libfc to execute FDMI registration.
  1222. */
  1223. lport->fdmi_enabled = 1;
  1224. /*
  1225. * Setup the necessary fc_host attributes to that will be used to fill
  1226. * in the FDMI information.
  1227. */
  1228. /* Get the PCI-e Device Serial Number Capability */
  1229. pos = pci_find_ext_capability(qedf->pdev, PCI_EXT_CAP_ID_DSN);
  1230. if (pos) {
  1231. pos += 4;
  1232. for (i = 0; i < 8; i++)
  1233. pci_read_config_byte(qedf->pdev, pos + i, &buf[i]);
  1234. snprintf(fc_host->serial_number,
  1235. sizeof(fc_host->serial_number),
  1236. "%02X%02X%02X%02X%02X%02X%02X%02X",
  1237. buf[7], buf[6], buf[5], buf[4],
  1238. buf[3], buf[2], buf[1], buf[0]);
  1239. } else
  1240. snprintf(fc_host->serial_number,
  1241. sizeof(fc_host->serial_number), "Unknown");
  1242. snprintf(fc_host->manufacturer,
  1243. sizeof(fc_host->manufacturer), "%s", "Cavium Inc.");
  1244. snprintf(fc_host->model, sizeof(fc_host->model), "%s", "QL41000");
  1245. snprintf(fc_host->model_description, sizeof(fc_host->model_description),
  1246. "%s", "QLogic FastLinQ QL41000 Series 10/25/40/50GGbE Controller"
  1247. "(FCoE)");
  1248. snprintf(fc_host->hardware_version, sizeof(fc_host->hardware_version),
  1249. "Rev %d", qedf->pdev->revision);
  1250. snprintf(fc_host->driver_version, sizeof(fc_host->driver_version),
  1251. "%s", QEDF_VERSION);
  1252. snprintf(fc_host->firmware_version, sizeof(fc_host->firmware_version),
  1253. "%d.%d.%d.%d", FW_MAJOR_VERSION, FW_MINOR_VERSION,
  1254. FW_REVISION_VERSION, FW_ENGINEERING_VERSION);
  1255. }
  1256. static int qedf_lport_setup(struct qedf_ctx *qedf)
  1257. {
  1258. struct fc_lport *lport = qedf->lport;
  1259. lport->link_up = 0;
  1260. lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
  1261. lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
  1262. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  1263. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  1264. lport->boot_time = jiffies;
  1265. lport->e_d_tov = 2 * 1000;
  1266. lport->r_a_tov = 10 * 1000;
  1267. /* Set NPIV support */
  1268. lport->does_npiv = 1;
  1269. fc_host_max_npiv_vports(lport->host) = QEDF_MAX_NPIV;
  1270. fc_set_wwnn(lport, qedf->wwnn);
  1271. fc_set_wwpn(lport, qedf->wwpn);
  1272. fcoe_libfc_config(lport, &qedf->ctlr, &qedf_lport_template, 0);
  1273. /* Allocate the exchange manager */
  1274. fc_exch_mgr_alloc(lport, FC_CLASS_3, qedf->max_scsi_xid + 1,
  1275. qedf->max_els_xid, NULL);
  1276. if (fc_lport_init_stats(lport))
  1277. return -ENOMEM;
  1278. /* Finish lport config */
  1279. fc_lport_config(lport);
  1280. /* Set max frame size */
  1281. fc_set_mfs(lport, QEDF_MFS);
  1282. fc_host_maxframe_size(lport->host) = lport->mfs;
  1283. /* Set default dev_loss_tmo based on module parameter */
  1284. fc_host_dev_loss_tmo(lport->host) = qedf_dev_loss_tmo;
  1285. /* Set symbolic node name */
  1286. snprintf(fc_host_symbolic_name(lport->host), 256,
  1287. "QLogic %s v%s", QEDF_MODULE_NAME, QEDF_VERSION);
  1288. qedf_setup_fdmi(qedf);
  1289. return 0;
  1290. }
  1291. /*
  1292. * NPIV functions
  1293. */
  1294. static int qedf_vport_libfc_config(struct fc_vport *vport,
  1295. struct fc_lport *lport)
  1296. {
  1297. lport->link_up = 0;
  1298. lport->qfull = 0;
  1299. lport->max_retry_count = QEDF_FLOGI_RETRY_CNT;
  1300. lport->max_rport_retry_count = QEDF_RPORT_RETRY_CNT;
  1301. lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  1302. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  1303. lport->boot_time = jiffies;
  1304. lport->e_d_tov = 2 * 1000;
  1305. lport->r_a_tov = 10 * 1000;
  1306. lport->does_npiv = 1; /* Temporary until we add NPIV support */
  1307. /* Allocate stats for vport */
  1308. if (fc_lport_init_stats(lport))
  1309. return -ENOMEM;
  1310. /* Finish lport config */
  1311. fc_lport_config(lport);
  1312. /* offload related configuration */
  1313. lport->crc_offload = 0;
  1314. lport->seq_offload = 0;
  1315. lport->lro_enabled = 0;
  1316. lport->lro_xid = 0;
  1317. lport->lso_max = 0;
  1318. return 0;
  1319. }
  1320. static int qedf_vport_create(struct fc_vport *vport, bool disabled)
  1321. {
  1322. struct Scsi_Host *shost = vport_to_shost(vport);
  1323. struct fc_lport *n_port = shost_priv(shost);
  1324. struct fc_lport *vn_port;
  1325. struct qedf_ctx *base_qedf = lport_priv(n_port);
  1326. struct qedf_ctx *vport_qedf;
  1327. char buf[32];
  1328. int rc = 0;
  1329. rc = fcoe_validate_vport_create(vport);
  1330. if (rc) {
  1331. fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
  1332. QEDF_WARN(&(base_qedf->dbg_ctx), "Failed to create vport, "
  1333. "WWPN (0x%s) already exists.\n", buf);
  1334. goto err1;
  1335. }
  1336. if (atomic_read(&base_qedf->link_state) != QEDF_LINK_UP) {
  1337. QEDF_WARN(&(base_qedf->dbg_ctx), "Cannot create vport "
  1338. "because link is not up.\n");
  1339. rc = -EIO;
  1340. goto err1;
  1341. }
  1342. vn_port = libfc_vport_create(vport, sizeof(struct qedf_ctx));
  1343. if (!vn_port) {
  1344. QEDF_WARN(&(base_qedf->dbg_ctx), "Could not create lport "
  1345. "for vport.\n");
  1346. rc = -ENOMEM;
  1347. goto err1;
  1348. }
  1349. fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
  1350. QEDF_ERR(&(base_qedf->dbg_ctx), "Creating NPIV port, WWPN=%s.\n",
  1351. buf);
  1352. /* Copy some fields from base_qedf */
  1353. vport_qedf = lport_priv(vn_port);
  1354. memcpy(vport_qedf, base_qedf, sizeof(struct qedf_ctx));
  1355. /* Set qedf data specific to this vport */
  1356. vport_qedf->lport = vn_port;
  1357. /* Use same hba_lock as base_qedf */
  1358. vport_qedf->hba_lock = base_qedf->hba_lock;
  1359. vport_qedf->pdev = base_qedf->pdev;
  1360. vport_qedf->cmd_mgr = base_qedf->cmd_mgr;
  1361. init_completion(&vport_qedf->flogi_compl);
  1362. INIT_LIST_HEAD(&vport_qedf->fcports);
  1363. rc = qedf_vport_libfc_config(vport, vn_port);
  1364. if (rc) {
  1365. QEDF_ERR(&(base_qedf->dbg_ctx), "Could not allocate memory "
  1366. "for lport stats.\n");
  1367. goto err2;
  1368. }
  1369. fc_set_wwnn(vn_port, vport->node_name);
  1370. fc_set_wwpn(vn_port, vport->port_name);
  1371. vport_qedf->wwnn = vn_port->wwnn;
  1372. vport_qedf->wwpn = vn_port->wwpn;
  1373. vn_port->host->transportt = qedf_fc_vport_transport_template;
  1374. vn_port->host->can_queue = QEDF_MAX_ELS_XID;
  1375. vn_port->host->max_lun = qedf_max_lun;
  1376. vn_port->host->sg_tablesize = QEDF_MAX_BDS_PER_CMD;
  1377. vn_port->host->max_cmd_len = QEDF_MAX_CDB_LEN;
  1378. rc = scsi_add_host(vn_port->host, &vport->dev);
  1379. if (rc) {
  1380. QEDF_WARN(&(base_qedf->dbg_ctx), "Error adding Scsi_Host.\n");
  1381. goto err2;
  1382. }
  1383. /* Set default dev_loss_tmo based on module parameter */
  1384. fc_host_dev_loss_tmo(vn_port->host) = qedf_dev_loss_tmo;
  1385. /* Init libfc stuffs */
  1386. memcpy(&vn_port->tt, &qedf_lport_template,
  1387. sizeof(qedf_lport_template));
  1388. fc_exch_init(vn_port);
  1389. fc_elsct_init(vn_port);
  1390. fc_lport_init(vn_port);
  1391. fc_disc_init(vn_port);
  1392. fc_disc_config(vn_port, vn_port);
  1393. /* Allocate the exchange manager */
  1394. shost = vport_to_shost(vport);
  1395. n_port = shost_priv(shost);
  1396. fc_exch_mgr_list_clone(n_port, vn_port);
  1397. /* Set max frame size */
  1398. fc_set_mfs(vn_port, QEDF_MFS);
  1399. fc_host_port_type(vn_port->host) = FC_PORTTYPE_UNKNOWN;
  1400. if (disabled) {
  1401. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  1402. } else {
  1403. vn_port->boot_time = jiffies;
  1404. fc_fabric_login(vn_port);
  1405. fc_vport_setlink(vn_port);
  1406. }
  1407. QEDF_INFO(&(base_qedf->dbg_ctx), QEDF_LOG_NPIV, "vn_port=%p.\n",
  1408. vn_port);
  1409. /* Set up debug context for vport */
  1410. vport_qedf->dbg_ctx.host_no = vn_port->host->host_no;
  1411. vport_qedf->dbg_ctx.pdev = base_qedf->pdev;
  1412. err2:
  1413. scsi_host_put(vn_port->host);
  1414. err1:
  1415. return rc;
  1416. }
  1417. static int qedf_vport_destroy(struct fc_vport *vport)
  1418. {
  1419. struct Scsi_Host *shost = vport_to_shost(vport);
  1420. struct fc_lport *n_port = shost_priv(shost);
  1421. struct fc_lport *vn_port = vport->dd_data;
  1422. struct qedf_ctx *qedf = lport_priv(vn_port);
  1423. if (!qedf) {
  1424. QEDF_ERR(NULL, "qedf is NULL.\n");
  1425. goto out;
  1426. }
  1427. /* Set unloading bit on vport qedf_ctx to prevent more I/O */
  1428. set_bit(QEDF_UNLOADING, &qedf->flags);
  1429. mutex_lock(&n_port->lp_mutex);
  1430. list_del(&vn_port->list);
  1431. mutex_unlock(&n_port->lp_mutex);
  1432. fc_fabric_logoff(vn_port);
  1433. fc_lport_destroy(vn_port);
  1434. /* Detach from scsi-ml */
  1435. fc_remove_host(vn_port->host);
  1436. scsi_remove_host(vn_port->host);
  1437. /*
  1438. * Only try to release the exchange manager if the vn_port
  1439. * configuration is complete.
  1440. */
  1441. if (vn_port->state == LPORT_ST_READY)
  1442. fc_exch_mgr_free(vn_port);
  1443. /* Free memory used by statistical counters */
  1444. fc_lport_free_stats(vn_port);
  1445. /* Release Scsi_Host */
  1446. if (vn_port->host)
  1447. scsi_host_put(vn_port->host);
  1448. out:
  1449. return 0;
  1450. }
  1451. static int qedf_vport_disable(struct fc_vport *vport, bool disable)
  1452. {
  1453. struct fc_lport *lport = vport->dd_data;
  1454. if (disable) {
  1455. fc_vport_set_state(vport, FC_VPORT_DISABLED);
  1456. fc_fabric_logoff(lport);
  1457. } else {
  1458. lport->boot_time = jiffies;
  1459. fc_fabric_login(lport);
  1460. fc_vport_setlink(lport);
  1461. }
  1462. return 0;
  1463. }
  1464. /*
  1465. * During removal we need to wait for all the vports associated with a port
  1466. * to be destroyed so we avoid a race condition where libfc is still trying
  1467. * to reap vports while the driver remove function has already reaped the
  1468. * driver contexts associated with the physical port.
  1469. */
  1470. static void qedf_wait_for_vport_destroy(struct qedf_ctx *qedf)
  1471. {
  1472. struct fc_host_attrs *fc_host = shost_to_fc_host(qedf->lport->host);
  1473. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
  1474. "Entered.\n");
  1475. while (fc_host->npiv_vports_inuse > 0) {
  1476. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_NPIV,
  1477. "Waiting for all vports to be reaped.\n");
  1478. msleep(1000);
  1479. }
  1480. }
  1481. /**
  1482. * qedf_fcoe_reset - Resets the fcoe
  1483. *
  1484. * @shost: shost the reset is from
  1485. *
  1486. * Returns: always 0
  1487. */
  1488. static int qedf_fcoe_reset(struct Scsi_Host *shost)
  1489. {
  1490. struct fc_lport *lport = shost_priv(shost);
  1491. qedf_ctx_soft_reset(lport);
  1492. return 0;
  1493. }
  1494. static struct fc_host_statistics *qedf_fc_get_host_stats(struct Scsi_Host
  1495. *shost)
  1496. {
  1497. struct fc_host_statistics *qedf_stats;
  1498. struct fc_lport *lport = shost_priv(shost);
  1499. struct qedf_ctx *qedf = lport_priv(lport);
  1500. struct qed_fcoe_stats *fw_fcoe_stats;
  1501. qedf_stats = fc_get_host_stats(shost);
  1502. /* We don't collect offload stats for specific NPIV ports */
  1503. if (lport->vport)
  1504. goto out;
  1505. fw_fcoe_stats = kmalloc(sizeof(struct qed_fcoe_stats), GFP_KERNEL);
  1506. if (!fw_fcoe_stats) {
  1507. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate memory for "
  1508. "fw_fcoe_stats.\n");
  1509. goto out;
  1510. }
  1511. mutex_lock(&qedf->stats_mutex);
  1512. /* Query firmware for offload stats */
  1513. qed_ops->get_stats(qedf->cdev, fw_fcoe_stats);
  1514. /*
  1515. * The expectation is that we add our offload stats to the stats
  1516. * being maintained by libfc each time the fc_get_host_status callback
  1517. * is invoked. The additions are not carried over for each call to
  1518. * the fc_get_host_stats callback.
  1519. */
  1520. qedf_stats->tx_frames += fw_fcoe_stats->fcoe_tx_data_pkt_cnt +
  1521. fw_fcoe_stats->fcoe_tx_xfer_pkt_cnt +
  1522. fw_fcoe_stats->fcoe_tx_other_pkt_cnt;
  1523. qedf_stats->rx_frames += fw_fcoe_stats->fcoe_rx_data_pkt_cnt +
  1524. fw_fcoe_stats->fcoe_rx_xfer_pkt_cnt +
  1525. fw_fcoe_stats->fcoe_rx_other_pkt_cnt;
  1526. qedf_stats->fcp_input_megabytes +=
  1527. do_div(fw_fcoe_stats->fcoe_rx_byte_cnt, 1000000);
  1528. qedf_stats->fcp_output_megabytes +=
  1529. do_div(fw_fcoe_stats->fcoe_tx_byte_cnt, 1000000);
  1530. qedf_stats->rx_words += fw_fcoe_stats->fcoe_rx_byte_cnt / 4;
  1531. qedf_stats->tx_words += fw_fcoe_stats->fcoe_tx_byte_cnt / 4;
  1532. qedf_stats->invalid_crc_count +=
  1533. fw_fcoe_stats->fcoe_silent_drop_pkt_crc_error_cnt;
  1534. qedf_stats->dumped_frames =
  1535. fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
  1536. qedf_stats->error_frames +=
  1537. fw_fcoe_stats->fcoe_silent_drop_total_pkt_cnt;
  1538. qedf_stats->fcp_input_requests += qedf->input_requests;
  1539. qedf_stats->fcp_output_requests += qedf->output_requests;
  1540. qedf_stats->fcp_control_requests += qedf->control_requests;
  1541. qedf_stats->fcp_packet_aborts += qedf->packet_aborts;
  1542. qedf_stats->fcp_frame_alloc_failures += qedf->alloc_failures;
  1543. mutex_unlock(&qedf->stats_mutex);
  1544. kfree(fw_fcoe_stats);
  1545. out:
  1546. return qedf_stats;
  1547. }
  1548. static struct fc_function_template qedf_fc_transport_fn = {
  1549. .show_host_node_name = 1,
  1550. .show_host_port_name = 1,
  1551. .show_host_supported_classes = 1,
  1552. .show_host_supported_fc4s = 1,
  1553. .show_host_active_fc4s = 1,
  1554. .show_host_maxframe_size = 1,
  1555. .show_host_port_id = 1,
  1556. .show_host_supported_speeds = 1,
  1557. .get_host_speed = fc_get_host_speed,
  1558. .show_host_speed = 1,
  1559. .show_host_port_type = 1,
  1560. .get_host_port_state = fc_get_host_port_state,
  1561. .show_host_port_state = 1,
  1562. .show_host_symbolic_name = 1,
  1563. /*
  1564. * Tell FC transport to allocate enough space to store the backpointer
  1565. * for the associate qedf_rport struct.
  1566. */
  1567. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  1568. sizeof(struct qedf_rport)),
  1569. .show_rport_maxframe_size = 1,
  1570. .show_rport_supported_classes = 1,
  1571. .show_host_fabric_name = 1,
  1572. .show_starget_node_name = 1,
  1573. .show_starget_port_name = 1,
  1574. .show_starget_port_id = 1,
  1575. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  1576. .show_rport_dev_loss_tmo = 1,
  1577. .get_fc_host_stats = qedf_fc_get_host_stats,
  1578. .issue_fc_host_lip = qedf_fcoe_reset,
  1579. .vport_create = qedf_vport_create,
  1580. .vport_delete = qedf_vport_destroy,
  1581. .vport_disable = qedf_vport_disable,
  1582. .bsg_request = fc_lport_bsg_request,
  1583. };
  1584. static struct fc_function_template qedf_fc_vport_transport_fn = {
  1585. .show_host_node_name = 1,
  1586. .show_host_port_name = 1,
  1587. .show_host_supported_classes = 1,
  1588. .show_host_supported_fc4s = 1,
  1589. .show_host_active_fc4s = 1,
  1590. .show_host_maxframe_size = 1,
  1591. .show_host_port_id = 1,
  1592. .show_host_supported_speeds = 1,
  1593. .get_host_speed = fc_get_host_speed,
  1594. .show_host_speed = 1,
  1595. .show_host_port_type = 1,
  1596. .get_host_port_state = fc_get_host_port_state,
  1597. .show_host_port_state = 1,
  1598. .show_host_symbolic_name = 1,
  1599. .dd_fcrport_size = (sizeof(struct fc_rport_libfc_priv) +
  1600. sizeof(struct qedf_rport)),
  1601. .show_rport_maxframe_size = 1,
  1602. .show_rport_supported_classes = 1,
  1603. .show_host_fabric_name = 1,
  1604. .show_starget_node_name = 1,
  1605. .show_starget_port_name = 1,
  1606. .show_starget_port_id = 1,
  1607. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  1608. .show_rport_dev_loss_tmo = 1,
  1609. .get_fc_host_stats = fc_get_host_stats,
  1610. .issue_fc_host_lip = qedf_fcoe_reset,
  1611. .bsg_request = fc_lport_bsg_request,
  1612. };
  1613. static bool qedf_fp_has_work(struct qedf_fastpath *fp)
  1614. {
  1615. struct qedf_ctx *qedf = fp->qedf;
  1616. struct global_queue *que;
  1617. struct qed_sb_info *sb_info = fp->sb_info;
  1618. struct status_block_e4 *sb = sb_info->sb_virt;
  1619. u16 prod_idx;
  1620. /* Get the pointer to the global CQ this completion is on */
  1621. que = qedf->global_queues[fp->sb_id];
  1622. /* Be sure all responses have been written to PI */
  1623. rmb();
  1624. /* Get the current firmware producer index */
  1625. prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
  1626. return (que->cq_prod_idx != prod_idx);
  1627. }
  1628. /*
  1629. * Interrupt handler code.
  1630. */
  1631. /* Process completion queue and copy CQE contents for deferred processesing
  1632. *
  1633. * Return true if we should wake the I/O thread, false if not.
  1634. */
  1635. static bool qedf_process_completions(struct qedf_fastpath *fp)
  1636. {
  1637. struct qedf_ctx *qedf = fp->qedf;
  1638. struct qed_sb_info *sb_info = fp->sb_info;
  1639. struct status_block_e4 *sb = sb_info->sb_virt;
  1640. struct global_queue *que;
  1641. u16 prod_idx;
  1642. struct fcoe_cqe *cqe;
  1643. struct qedf_io_work *io_work;
  1644. int num_handled = 0;
  1645. unsigned int cpu;
  1646. struct qedf_ioreq *io_req = NULL;
  1647. u16 xid;
  1648. u16 new_cqes;
  1649. u32 comp_type;
  1650. /* Get the current firmware producer index */
  1651. prod_idx = sb->pi_array[QEDF_FCOE_PARAMS_GL_RQ_PI];
  1652. /* Get the pointer to the global CQ this completion is on */
  1653. que = qedf->global_queues[fp->sb_id];
  1654. /* Calculate the amount of new elements since last processing */
  1655. new_cqes = (prod_idx >= que->cq_prod_idx) ?
  1656. (prod_idx - que->cq_prod_idx) :
  1657. 0x10000 - que->cq_prod_idx + prod_idx;
  1658. /* Save producer index */
  1659. que->cq_prod_idx = prod_idx;
  1660. while (new_cqes) {
  1661. fp->completions++;
  1662. num_handled++;
  1663. cqe = &que->cq[que->cq_cons_idx];
  1664. comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
  1665. FCOE_CQE_CQE_TYPE_MASK;
  1666. /*
  1667. * Process unsolicited CQEs directly in the interrupt handler
  1668. * sine we need the fastpath ID
  1669. */
  1670. if (comp_type == FCOE_UNSOLIC_CQE_TYPE) {
  1671. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_UNSOL,
  1672. "Unsolicated CQE.\n");
  1673. qedf_process_unsol_compl(qedf, fp->sb_id, cqe);
  1674. /*
  1675. * Don't add a work list item. Increment consumer
  1676. * consumer index and move on.
  1677. */
  1678. goto inc_idx;
  1679. }
  1680. xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
  1681. io_req = &qedf->cmd_mgr->cmds[xid];
  1682. /*
  1683. * Figure out which percpu thread we should queue this I/O
  1684. * on.
  1685. */
  1686. if (!io_req)
  1687. /* If there is not io_req assocated with this CQE
  1688. * just queue it on CPU 0
  1689. */
  1690. cpu = 0;
  1691. else {
  1692. cpu = io_req->cpu;
  1693. io_req->int_cpu = smp_processor_id();
  1694. }
  1695. io_work = mempool_alloc(qedf->io_mempool, GFP_ATOMIC);
  1696. if (!io_work) {
  1697. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate "
  1698. "work for I/O completion.\n");
  1699. continue;
  1700. }
  1701. memset(io_work, 0, sizeof(struct qedf_io_work));
  1702. INIT_WORK(&io_work->work, qedf_fp_io_handler);
  1703. /* Copy contents of CQE for deferred processing */
  1704. memcpy(&io_work->cqe, cqe, sizeof(struct fcoe_cqe));
  1705. io_work->qedf = fp->qedf;
  1706. io_work->fp = NULL; /* Only used for unsolicited frames */
  1707. queue_work_on(cpu, qedf_io_wq, &io_work->work);
  1708. inc_idx:
  1709. que->cq_cons_idx++;
  1710. if (que->cq_cons_idx == fp->cq_num_entries)
  1711. que->cq_cons_idx = 0;
  1712. new_cqes--;
  1713. }
  1714. return true;
  1715. }
  1716. /* MSI-X fastpath handler code */
  1717. static irqreturn_t qedf_msix_handler(int irq, void *dev_id)
  1718. {
  1719. struct qedf_fastpath *fp = dev_id;
  1720. if (!fp) {
  1721. QEDF_ERR(NULL, "fp is null.\n");
  1722. return IRQ_HANDLED;
  1723. }
  1724. if (!fp->sb_info) {
  1725. QEDF_ERR(NULL, "fp->sb_info in null.");
  1726. return IRQ_HANDLED;
  1727. }
  1728. /*
  1729. * Disable interrupts for this status block while we process new
  1730. * completions
  1731. */
  1732. qed_sb_ack(fp->sb_info, IGU_INT_DISABLE, 0 /*do not update*/);
  1733. while (1) {
  1734. qedf_process_completions(fp);
  1735. if (qedf_fp_has_work(fp) == 0) {
  1736. /* Update the sb information */
  1737. qed_sb_update_sb_idx(fp->sb_info);
  1738. /* Check for more work */
  1739. rmb();
  1740. if (qedf_fp_has_work(fp) == 0) {
  1741. /* Re-enable interrupts */
  1742. qed_sb_ack(fp->sb_info, IGU_INT_ENABLE, 1);
  1743. return IRQ_HANDLED;
  1744. }
  1745. }
  1746. }
  1747. /* Do we ever want to break out of above loop? */
  1748. return IRQ_HANDLED;
  1749. }
  1750. /* simd handler for MSI/INTa */
  1751. static void qedf_simd_int_handler(void *cookie)
  1752. {
  1753. /* Cookie is qedf_ctx struct */
  1754. struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
  1755. QEDF_WARN(&(qedf->dbg_ctx), "qedf=%p.\n", qedf);
  1756. }
  1757. #define QEDF_SIMD_HANDLER_NUM 0
  1758. static void qedf_sync_free_irqs(struct qedf_ctx *qedf)
  1759. {
  1760. int i;
  1761. if (qedf->int_info.msix_cnt) {
  1762. for (i = 0; i < qedf->int_info.used_cnt; i++) {
  1763. synchronize_irq(qedf->int_info.msix[i].vector);
  1764. irq_set_affinity_hint(qedf->int_info.msix[i].vector,
  1765. NULL);
  1766. irq_set_affinity_notifier(qedf->int_info.msix[i].vector,
  1767. NULL);
  1768. free_irq(qedf->int_info.msix[i].vector,
  1769. &qedf->fp_array[i]);
  1770. }
  1771. } else
  1772. qed_ops->common->simd_handler_clean(qedf->cdev,
  1773. QEDF_SIMD_HANDLER_NUM);
  1774. qedf->int_info.used_cnt = 0;
  1775. qed_ops->common->set_fp_int(qedf->cdev, 0);
  1776. }
  1777. static int qedf_request_msix_irq(struct qedf_ctx *qedf)
  1778. {
  1779. int i, rc, cpu;
  1780. cpu = cpumask_first(cpu_online_mask);
  1781. for (i = 0; i < qedf->num_queues; i++) {
  1782. rc = request_irq(qedf->int_info.msix[i].vector,
  1783. qedf_msix_handler, 0, "qedf", &qedf->fp_array[i]);
  1784. if (rc) {
  1785. QEDF_WARN(&(qedf->dbg_ctx), "request_irq failed.\n");
  1786. qedf_sync_free_irqs(qedf);
  1787. return rc;
  1788. }
  1789. qedf->int_info.used_cnt++;
  1790. rc = irq_set_affinity_hint(qedf->int_info.msix[i].vector,
  1791. get_cpu_mask(cpu));
  1792. cpu = cpumask_next(cpu, cpu_online_mask);
  1793. }
  1794. return 0;
  1795. }
  1796. static int qedf_setup_int(struct qedf_ctx *qedf)
  1797. {
  1798. int rc = 0;
  1799. /*
  1800. * Learn interrupt configuration
  1801. */
  1802. rc = qed_ops->common->set_fp_int(qedf->cdev, num_online_cpus());
  1803. if (rc <= 0)
  1804. return 0;
  1805. rc = qed_ops->common->get_fp_int(qedf->cdev, &qedf->int_info);
  1806. if (rc)
  1807. return 0;
  1808. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of msix_cnt = "
  1809. "0x%x num of cpus = 0x%x\n", qedf->int_info.msix_cnt,
  1810. num_online_cpus());
  1811. if (qedf->int_info.msix_cnt)
  1812. return qedf_request_msix_irq(qedf);
  1813. qed_ops->common->simd_handler_config(qedf->cdev, &qedf,
  1814. QEDF_SIMD_HANDLER_NUM, qedf_simd_int_handler);
  1815. qedf->int_info.used_cnt = 1;
  1816. QEDF_ERR(&qedf->dbg_ctx, "Only MSI-X supported. Failing probe.\n");
  1817. return -EINVAL;
  1818. }
  1819. /* Main function for libfc frame reception */
  1820. static void qedf_recv_frame(struct qedf_ctx *qedf,
  1821. struct sk_buff *skb)
  1822. {
  1823. u32 fr_len;
  1824. struct fc_lport *lport;
  1825. struct fc_frame_header *fh;
  1826. struct fcoe_crc_eof crc_eof;
  1827. struct fc_frame *fp;
  1828. u8 *mac = NULL;
  1829. u8 *dest_mac = NULL;
  1830. struct fcoe_hdr *hp;
  1831. struct qedf_rport *fcport;
  1832. struct fc_lport *vn_port;
  1833. u32 f_ctl;
  1834. lport = qedf->lport;
  1835. if (lport == NULL || lport->state == LPORT_ST_DISABLED) {
  1836. QEDF_WARN(NULL, "Invalid lport struct or lport disabled.\n");
  1837. kfree_skb(skb);
  1838. return;
  1839. }
  1840. if (skb_is_nonlinear(skb))
  1841. skb_linearize(skb);
  1842. mac = eth_hdr(skb)->h_source;
  1843. dest_mac = eth_hdr(skb)->h_dest;
  1844. /* Pull the header */
  1845. hp = (struct fcoe_hdr *)skb->data;
  1846. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1847. skb_pull(skb, sizeof(struct fcoe_hdr));
  1848. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  1849. fp = (struct fc_frame *)skb;
  1850. fc_frame_init(fp);
  1851. fr_dev(fp) = lport;
  1852. fr_sof(fp) = hp->fcoe_sof;
  1853. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
  1854. kfree_skb(skb);
  1855. return;
  1856. }
  1857. fr_eof(fp) = crc_eof.fcoe_eof;
  1858. fr_crc(fp) = crc_eof.fcoe_crc32;
  1859. if (pskb_trim(skb, fr_len)) {
  1860. kfree_skb(skb);
  1861. return;
  1862. }
  1863. fh = fc_frame_header_get(fp);
  1864. /*
  1865. * Invalid frame filters.
  1866. */
  1867. if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
  1868. fh->fh_type == FC_TYPE_FCP) {
  1869. /* Drop FCP data. We dont this in L2 path */
  1870. kfree_skb(skb);
  1871. return;
  1872. }
  1873. if (fh->fh_r_ctl == FC_RCTL_ELS_REQ &&
  1874. fh->fh_type == FC_TYPE_ELS) {
  1875. switch (fc_frame_payload_op(fp)) {
  1876. case ELS_LOGO:
  1877. if (ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
  1878. /* drop non-FIP LOGO */
  1879. kfree_skb(skb);
  1880. return;
  1881. }
  1882. break;
  1883. }
  1884. }
  1885. if (fh->fh_r_ctl == FC_RCTL_BA_ABTS) {
  1886. /* Drop incoming ABTS */
  1887. kfree_skb(skb);
  1888. return;
  1889. }
  1890. if (ntoh24(&dest_mac[3]) != ntoh24(fh->fh_d_id)) {
  1891. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  1892. "FC frame d_id mismatch with MAC %pM.\n", dest_mac);
  1893. kfree_skb(skb);
  1894. return;
  1895. }
  1896. if (qedf->ctlr.state) {
  1897. if (!ether_addr_equal(mac, qedf->ctlr.dest_addr)) {
  1898. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  1899. "Wrong source address: mac:%pM dest_addr:%pM.\n",
  1900. mac, qedf->ctlr.dest_addr);
  1901. kfree_skb(skb);
  1902. return;
  1903. }
  1904. }
  1905. vn_port = fc_vport_id_lookup(lport, ntoh24(fh->fh_d_id));
  1906. /*
  1907. * If the destination ID from the frame header does not match what we
  1908. * have on record for lport and the search for a NPIV port came up
  1909. * empty then this is not addressed to our port so simply drop it.
  1910. */
  1911. if (lport->port_id != ntoh24(fh->fh_d_id) && !vn_port) {
  1912. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  1913. "Dropping frame due to destination mismatch: lport->port_id=%x fh->d_id=%x.\n",
  1914. lport->port_id, ntoh24(fh->fh_d_id));
  1915. kfree_skb(skb);
  1916. return;
  1917. }
  1918. f_ctl = ntoh24(fh->fh_f_ctl);
  1919. if ((fh->fh_type == FC_TYPE_BLS) && (f_ctl & FC_FC_SEQ_CTX) &&
  1920. (f_ctl & FC_FC_EX_CTX)) {
  1921. /* Drop incoming ABTS response that has both SEQ/EX CTX set */
  1922. kfree_skb(skb);
  1923. return;
  1924. }
  1925. /*
  1926. * If a connection is uploading, drop incoming FCoE frames as there
  1927. * is a small window where we could try to return a frame while libfc
  1928. * is trying to clean things up.
  1929. */
  1930. /* Get fcport associated with d_id if it exists */
  1931. fcport = qedf_fcport_lookup(qedf, ntoh24(fh->fh_d_id));
  1932. if (fcport && test_bit(QEDF_RPORT_UPLOADING_CONNECTION,
  1933. &fcport->flags)) {
  1934. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2,
  1935. "Connection uploading, dropping fp=%p.\n", fp);
  1936. kfree_skb(skb);
  1937. return;
  1938. }
  1939. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_LL2, "FCoE frame receive: "
  1940. "skb=%p fp=%p src=%06x dest=%06x r_ctl=%x fh_type=%x.\n", skb, fp,
  1941. ntoh24(fh->fh_s_id), ntoh24(fh->fh_d_id), fh->fh_r_ctl,
  1942. fh->fh_type);
  1943. if (qedf_dump_frames)
  1944. print_hex_dump(KERN_WARNING, "fcoe: ", DUMP_PREFIX_OFFSET, 16,
  1945. 1, skb->data, skb->len, false);
  1946. fc_exch_recv(lport, fp);
  1947. }
  1948. static void qedf_ll2_process_skb(struct work_struct *work)
  1949. {
  1950. struct qedf_skb_work *skb_work =
  1951. container_of(work, struct qedf_skb_work, work);
  1952. struct qedf_ctx *qedf = skb_work->qedf;
  1953. struct sk_buff *skb = skb_work->skb;
  1954. struct ethhdr *eh;
  1955. if (!qedf) {
  1956. QEDF_ERR(NULL, "qedf is NULL\n");
  1957. goto err_out;
  1958. }
  1959. eh = (struct ethhdr *)skb->data;
  1960. /* Undo VLAN encapsulation */
  1961. if (eh->h_proto == htons(ETH_P_8021Q)) {
  1962. memmove((u8 *)eh + VLAN_HLEN, eh, ETH_ALEN * 2);
  1963. eh = skb_pull(skb, VLAN_HLEN);
  1964. skb_reset_mac_header(skb);
  1965. }
  1966. /*
  1967. * Process either a FIP frame or FCoE frame based on the
  1968. * protocol value. If it's not either just drop the
  1969. * frame.
  1970. */
  1971. if (eh->h_proto == htons(ETH_P_FIP)) {
  1972. qedf_fip_recv(qedf, skb);
  1973. goto out;
  1974. } else if (eh->h_proto == htons(ETH_P_FCOE)) {
  1975. __skb_pull(skb, ETH_HLEN);
  1976. qedf_recv_frame(qedf, skb);
  1977. goto out;
  1978. } else
  1979. goto err_out;
  1980. err_out:
  1981. kfree_skb(skb);
  1982. out:
  1983. kfree(skb_work);
  1984. return;
  1985. }
  1986. static int qedf_ll2_rx(void *cookie, struct sk_buff *skb,
  1987. u32 arg1, u32 arg2)
  1988. {
  1989. struct qedf_ctx *qedf = (struct qedf_ctx *)cookie;
  1990. struct qedf_skb_work *skb_work;
  1991. skb_work = kzalloc(sizeof(struct qedf_skb_work), GFP_ATOMIC);
  1992. if (!skb_work) {
  1993. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate skb_work so "
  1994. "dropping frame.\n");
  1995. kfree_skb(skb);
  1996. return 0;
  1997. }
  1998. INIT_WORK(&skb_work->work, qedf_ll2_process_skb);
  1999. skb_work->skb = skb;
  2000. skb_work->qedf = qedf;
  2001. queue_work(qedf->ll2_recv_wq, &skb_work->work);
  2002. return 0;
  2003. }
  2004. static struct qed_ll2_cb_ops qedf_ll2_cb_ops = {
  2005. .rx_cb = qedf_ll2_rx,
  2006. .tx_cb = NULL,
  2007. };
  2008. /* Main thread to process I/O completions */
  2009. void qedf_fp_io_handler(struct work_struct *work)
  2010. {
  2011. struct qedf_io_work *io_work =
  2012. container_of(work, struct qedf_io_work, work);
  2013. u32 comp_type;
  2014. /*
  2015. * Deferred part of unsolicited CQE sends
  2016. * frame to libfc.
  2017. */
  2018. comp_type = (io_work->cqe.cqe_data >>
  2019. FCOE_CQE_CQE_TYPE_SHIFT) &
  2020. FCOE_CQE_CQE_TYPE_MASK;
  2021. if (comp_type == FCOE_UNSOLIC_CQE_TYPE &&
  2022. io_work->fp)
  2023. fc_exch_recv(io_work->qedf->lport, io_work->fp);
  2024. else
  2025. qedf_process_cqe(io_work->qedf, &io_work->cqe);
  2026. kfree(io_work);
  2027. }
  2028. static int qedf_alloc_and_init_sb(struct qedf_ctx *qedf,
  2029. struct qed_sb_info *sb_info, u16 sb_id)
  2030. {
  2031. struct status_block_e4 *sb_virt;
  2032. dma_addr_t sb_phys;
  2033. int ret;
  2034. sb_virt = dma_alloc_coherent(&qedf->pdev->dev,
  2035. sizeof(struct status_block_e4), &sb_phys, GFP_KERNEL);
  2036. if (!sb_virt) {
  2037. QEDF_ERR(&(qedf->dbg_ctx), "Status block allocation failed "
  2038. "for id = %d.\n", sb_id);
  2039. return -ENOMEM;
  2040. }
  2041. ret = qed_ops->common->sb_init(qedf->cdev, sb_info, sb_virt, sb_phys,
  2042. sb_id, QED_SB_TYPE_STORAGE);
  2043. if (ret) {
  2044. QEDF_ERR(&(qedf->dbg_ctx), "Status block initialization "
  2045. "failed for id = %d.\n", sb_id);
  2046. return ret;
  2047. }
  2048. return 0;
  2049. }
  2050. static void qedf_free_sb(struct qedf_ctx *qedf, struct qed_sb_info *sb_info)
  2051. {
  2052. if (sb_info->sb_virt)
  2053. dma_free_coherent(&qedf->pdev->dev, sizeof(*sb_info->sb_virt),
  2054. (void *)sb_info->sb_virt, sb_info->sb_phys);
  2055. }
  2056. static void qedf_destroy_sb(struct qedf_ctx *qedf)
  2057. {
  2058. int id;
  2059. struct qedf_fastpath *fp = NULL;
  2060. for (id = 0; id < qedf->num_queues; id++) {
  2061. fp = &(qedf->fp_array[id]);
  2062. if (fp->sb_id == QEDF_SB_ID_NULL)
  2063. break;
  2064. qedf_free_sb(qedf, fp->sb_info);
  2065. kfree(fp->sb_info);
  2066. }
  2067. kfree(qedf->fp_array);
  2068. }
  2069. static int qedf_prepare_sb(struct qedf_ctx *qedf)
  2070. {
  2071. int id;
  2072. struct qedf_fastpath *fp;
  2073. int ret;
  2074. qedf->fp_array =
  2075. kcalloc(qedf->num_queues, sizeof(struct qedf_fastpath),
  2076. GFP_KERNEL);
  2077. if (!qedf->fp_array) {
  2078. QEDF_ERR(&(qedf->dbg_ctx), "fastpath array allocation "
  2079. "failed.\n");
  2080. return -ENOMEM;
  2081. }
  2082. for (id = 0; id < qedf->num_queues; id++) {
  2083. fp = &(qedf->fp_array[id]);
  2084. fp->sb_id = QEDF_SB_ID_NULL;
  2085. fp->sb_info = kcalloc(1, sizeof(*fp->sb_info), GFP_KERNEL);
  2086. if (!fp->sb_info) {
  2087. QEDF_ERR(&(qedf->dbg_ctx), "SB info struct "
  2088. "allocation failed.\n");
  2089. goto err;
  2090. }
  2091. ret = qedf_alloc_and_init_sb(qedf, fp->sb_info, id);
  2092. if (ret) {
  2093. QEDF_ERR(&(qedf->dbg_ctx), "SB allocation and "
  2094. "initialization failed.\n");
  2095. goto err;
  2096. }
  2097. fp->sb_id = id;
  2098. fp->qedf = qedf;
  2099. fp->cq_num_entries =
  2100. qedf->global_queues[id]->cq_mem_size /
  2101. sizeof(struct fcoe_cqe);
  2102. }
  2103. err:
  2104. return 0;
  2105. }
  2106. void qedf_process_cqe(struct qedf_ctx *qedf, struct fcoe_cqe *cqe)
  2107. {
  2108. u16 xid;
  2109. struct qedf_ioreq *io_req;
  2110. struct qedf_rport *fcport;
  2111. u32 comp_type;
  2112. comp_type = (cqe->cqe_data >> FCOE_CQE_CQE_TYPE_SHIFT) &
  2113. FCOE_CQE_CQE_TYPE_MASK;
  2114. xid = cqe->cqe_data & FCOE_CQE_TASK_ID_MASK;
  2115. io_req = &qedf->cmd_mgr->cmds[xid];
  2116. /* Completion not for a valid I/O anymore so just return */
  2117. if (!io_req)
  2118. return;
  2119. fcport = io_req->fcport;
  2120. if (fcport == NULL) {
  2121. QEDF_ERR(&(qedf->dbg_ctx), "fcport is NULL.\n");
  2122. return;
  2123. }
  2124. /*
  2125. * Check that fcport is offloaded. If it isn't then the spinlock
  2126. * isn't valid and shouldn't be taken. We should just return.
  2127. */
  2128. if (!test_bit(QEDF_RPORT_SESSION_READY, &fcport->flags)) {
  2129. QEDF_ERR(&(qedf->dbg_ctx), "Session not offloaded yet.\n");
  2130. return;
  2131. }
  2132. switch (comp_type) {
  2133. case FCOE_GOOD_COMPLETION_CQE_TYPE:
  2134. atomic_inc(&fcport->free_sqes);
  2135. switch (io_req->cmd_type) {
  2136. case QEDF_SCSI_CMD:
  2137. qedf_scsi_completion(qedf, cqe, io_req);
  2138. break;
  2139. case QEDF_ELS:
  2140. qedf_process_els_compl(qedf, cqe, io_req);
  2141. break;
  2142. case QEDF_TASK_MGMT_CMD:
  2143. qedf_process_tmf_compl(qedf, cqe, io_req);
  2144. break;
  2145. case QEDF_SEQ_CLEANUP:
  2146. qedf_process_seq_cleanup_compl(qedf, cqe, io_req);
  2147. break;
  2148. }
  2149. break;
  2150. case FCOE_ERROR_DETECTION_CQE_TYPE:
  2151. atomic_inc(&fcport->free_sqes);
  2152. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2153. "Error detect CQE.\n");
  2154. qedf_process_error_detect(qedf, cqe, io_req);
  2155. break;
  2156. case FCOE_EXCH_CLEANUP_CQE_TYPE:
  2157. atomic_inc(&fcport->free_sqes);
  2158. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2159. "Cleanup CQE.\n");
  2160. qedf_process_cleanup_compl(qedf, cqe, io_req);
  2161. break;
  2162. case FCOE_ABTS_CQE_TYPE:
  2163. atomic_inc(&fcport->free_sqes);
  2164. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2165. "Abort CQE.\n");
  2166. qedf_process_abts_compl(qedf, cqe, io_req);
  2167. break;
  2168. case FCOE_DUMMY_CQE_TYPE:
  2169. atomic_inc(&fcport->free_sqes);
  2170. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2171. "Dummy CQE.\n");
  2172. break;
  2173. case FCOE_LOCAL_COMP_CQE_TYPE:
  2174. atomic_inc(&fcport->free_sqes);
  2175. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2176. "Local completion CQE.\n");
  2177. break;
  2178. case FCOE_WARNING_CQE_TYPE:
  2179. atomic_inc(&fcport->free_sqes);
  2180. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2181. "Warning CQE.\n");
  2182. qedf_process_warning_compl(qedf, cqe, io_req);
  2183. break;
  2184. case MAX_FCOE_CQE_TYPE:
  2185. atomic_inc(&fcport->free_sqes);
  2186. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2187. "Max FCoE CQE.\n");
  2188. break;
  2189. default:
  2190. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_IO,
  2191. "Default CQE.\n");
  2192. break;
  2193. }
  2194. }
  2195. static void qedf_free_bdq(struct qedf_ctx *qedf)
  2196. {
  2197. int i;
  2198. if (qedf->bdq_pbl_list)
  2199. dma_free_coherent(&qedf->pdev->dev, QEDF_PAGE_SIZE,
  2200. qedf->bdq_pbl_list, qedf->bdq_pbl_list_dma);
  2201. if (qedf->bdq_pbl)
  2202. dma_free_coherent(&qedf->pdev->dev, qedf->bdq_pbl_mem_size,
  2203. qedf->bdq_pbl, qedf->bdq_pbl_dma);
  2204. for (i = 0; i < QEDF_BDQ_SIZE; i++) {
  2205. if (qedf->bdq[i].buf_addr) {
  2206. dma_free_coherent(&qedf->pdev->dev, QEDF_BDQ_BUF_SIZE,
  2207. qedf->bdq[i].buf_addr, qedf->bdq[i].buf_dma);
  2208. }
  2209. }
  2210. }
  2211. static void qedf_free_global_queues(struct qedf_ctx *qedf)
  2212. {
  2213. int i;
  2214. struct global_queue **gl = qedf->global_queues;
  2215. for (i = 0; i < qedf->num_queues; i++) {
  2216. if (!gl[i])
  2217. continue;
  2218. if (gl[i]->cq)
  2219. dma_free_coherent(&qedf->pdev->dev,
  2220. gl[i]->cq_mem_size, gl[i]->cq, gl[i]->cq_dma);
  2221. if (gl[i]->cq_pbl)
  2222. dma_free_coherent(&qedf->pdev->dev, gl[i]->cq_pbl_size,
  2223. gl[i]->cq_pbl, gl[i]->cq_pbl_dma);
  2224. kfree(gl[i]);
  2225. }
  2226. qedf_free_bdq(qedf);
  2227. }
  2228. static int qedf_alloc_bdq(struct qedf_ctx *qedf)
  2229. {
  2230. int i;
  2231. struct scsi_bd *pbl;
  2232. u64 *list;
  2233. dma_addr_t page;
  2234. /* Alloc dma memory for BDQ buffers */
  2235. for (i = 0; i < QEDF_BDQ_SIZE; i++) {
  2236. qedf->bdq[i].buf_addr = dma_alloc_coherent(&qedf->pdev->dev,
  2237. QEDF_BDQ_BUF_SIZE, &qedf->bdq[i].buf_dma, GFP_KERNEL);
  2238. if (!qedf->bdq[i].buf_addr) {
  2239. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ "
  2240. "buffer %d.\n", i);
  2241. return -ENOMEM;
  2242. }
  2243. }
  2244. /* Alloc dma memory for BDQ page buffer list */
  2245. qedf->bdq_pbl_mem_size =
  2246. QEDF_BDQ_SIZE * sizeof(struct scsi_bd);
  2247. qedf->bdq_pbl_mem_size =
  2248. ALIGN(qedf->bdq_pbl_mem_size, QEDF_PAGE_SIZE);
  2249. qedf->bdq_pbl = dma_alloc_coherent(&qedf->pdev->dev,
  2250. qedf->bdq_pbl_mem_size, &qedf->bdq_pbl_dma, GFP_KERNEL);
  2251. if (!qedf->bdq_pbl) {
  2252. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate BDQ PBL.\n");
  2253. return -ENOMEM;
  2254. }
  2255. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2256. "BDQ PBL addr=0x%p dma=%pad\n",
  2257. qedf->bdq_pbl, &qedf->bdq_pbl_dma);
  2258. /*
  2259. * Populate BDQ PBL with physical and virtual address of individual
  2260. * BDQ buffers
  2261. */
  2262. pbl = (struct scsi_bd *)qedf->bdq_pbl;
  2263. for (i = 0; i < QEDF_BDQ_SIZE; i++) {
  2264. pbl->address.hi = cpu_to_le32(U64_HI(qedf->bdq[i].buf_dma));
  2265. pbl->address.lo = cpu_to_le32(U64_LO(qedf->bdq[i].buf_dma));
  2266. pbl->opaque.fcoe_opaque.hi = 0;
  2267. /* Opaque lo data is an index into the BDQ array */
  2268. pbl->opaque.fcoe_opaque.lo = cpu_to_le32(i);
  2269. pbl++;
  2270. }
  2271. /* Allocate list of PBL pages */
  2272. qedf->bdq_pbl_list = dma_zalloc_coherent(&qedf->pdev->dev,
  2273. QEDF_PAGE_SIZE, &qedf->bdq_pbl_list_dma, GFP_KERNEL);
  2274. if (!qedf->bdq_pbl_list) {
  2275. QEDF_ERR(&(qedf->dbg_ctx), "Could not allocate list of PBL pages.\n");
  2276. return -ENOMEM;
  2277. }
  2278. /*
  2279. * Now populate PBL list with pages that contain pointers to the
  2280. * individual buffers.
  2281. */
  2282. qedf->bdq_pbl_list_num_entries = qedf->bdq_pbl_mem_size /
  2283. QEDF_PAGE_SIZE;
  2284. list = (u64 *)qedf->bdq_pbl_list;
  2285. page = qedf->bdq_pbl_list_dma;
  2286. for (i = 0; i < qedf->bdq_pbl_list_num_entries; i++) {
  2287. *list = qedf->bdq_pbl_dma;
  2288. list++;
  2289. page += QEDF_PAGE_SIZE;
  2290. }
  2291. return 0;
  2292. }
  2293. static int qedf_alloc_global_queues(struct qedf_ctx *qedf)
  2294. {
  2295. u32 *list;
  2296. int i;
  2297. int status = 0, rc;
  2298. u32 *pbl;
  2299. dma_addr_t page;
  2300. int num_pages;
  2301. /* Allocate and map CQs, RQs */
  2302. /*
  2303. * Number of global queues (CQ / RQ). This should
  2304. * be <= number of available MSIX vectors for the PF
  2305. */
  2306. if (!qedf->num_queues) {
  2307. QEDF_ERR(&(qedf->dbg_ctx), "No MSI-X vectors available!\n");
  2308. return 1;
  2309. }
  2310. /*
  2311. * Make sure we allocated the PBL that will contain the physical
  2312. * addresses of our queues
  2313. */
  2314. if (!qedf->p_cpuq) {
  2315. status = 1;
  2316. goto mem_alloc_failure;
  2317. }
  2318. qedf->global_queues = kzalloc((sizeof(struct global_queue *)
  2319. * qedf->num_queues), GFP_KERNEL);
  2320. if (!qedf->global_queues) {
  2321. QEDF_ERR(&(qedf->dbg_ctx), "Unable to allocate global "
  2322. "queues array ptr memory\n");
  2323. return -ENOMEM;
  2324. }
  2325. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2326. "qedf->global_queues=%p.\n", qedf->global_queues);
  2327. /* Allocate DMA coherent buffers for BDQ */
  2328. rc = qedf_alloc_bdq(qedf);
  2329. if (rc)
  2330. goto mem_alloc_failure;
  2331. /* Allocate a CQ and an associated PBL for each MSI-X vector */
  2332. for (i = 0; i < qedf->num_queues; i++) {
  2333. qedf->global_queues[i] = kzalloc(sizeof(struct global_queue),
  2334. GFP_KERNEL);
  2335. if (!qedf->global_queues[i]) {
  2336. QEDF_WARN(&(qedf->dbg_ctx), "Unable to allocate "
  2337. "global queue %d.\n", i);
  2338. status = -ENOMEM;
  2339. goto mem_alloc_failure;
  2340. }
  2341. qedf->global_queues[i]->cq_mem_size =
  2342. FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
  2343. qedf->global_queues[i]->cq_mem_size =
  2344. ALIGN(qedf->global_queues[i]->cq_mem_size, QEDF_PAGE_SIZE);
  2345. qedf->global_queues[i]->cq_pbl_size =
  2346. (qedf->global_queues[i]->cq_mem_size /
  2347. PAGE_SIZE) * sizeof(void *);
  2348. qedf->global_queues[i]->cq_pbl_size =
  2349. ALIGN(qedf->global_queues[i]->cq_pbl_size, QEDF_PAGE_SIZE);
  2350. qedf->global_queues[i]->cq =
  2351. dma_zalloc_coherent(&qedf->pdev->dev,
  2352. qedf->global_queues[i]->cq_mem_size,
  2353. &qedf->global_queues[i]->cq_dma, GFP_KERNEL);
  2354. if (!qedf->global_queues[i]->cq) {
  2355. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq.\n");
  2356. status = -ENOMEM;
  2357. goto mem_alloc_failure;
  2358. }
  2359. qedf->global_queues[i]->cq_pbl =
  2360. dma_zalloc_coherent(&qedf->pdev->dev,
  2361. qedf->global_queues[i]->cq_pbl_size,
  2362. &qedf->global_queues[i]->cq_pbl_dma, GFP_KERNEL);
  2363. if (!qedf->global_queues[i]->cq_pbl) {
  2364. QEDF_WARN(&(qedf->dbg_ctx), "Could not allocate cq PBL.\n");
  2365. status = -ENOMEM;
  2366. goto mem_alloc_failure;
  2367. }
  2368. /* Create PBL */
  2369. num_pages = qedf->global_queues[i]->cq_mem_size /
  2370. QEDF_PAGE_SIZE;
  2371. page = qedf->global_queues[i]->cq_dma;
  2372. pbl = (u32 *)qedf->global_queues[i]->cq_pbl;
  2373. while (num_pages--) {
  2374. *pbl = U64_LO(page);
  2375. pbl++;
  2376. *pbl = U64_HI(page);
  2377. pbl++;
  2378. page += QEDF_PAGE_SIZE;
  2379. }
  2380. /* Set the initial consumer index for cq */
  2381. qedf->global_queues[i]->cq_cons_idx = 0;
  2382. }
  2383. list = (u32 *)qedf->p_cpuq;
  2384. /*
  2385. * The list is built as follows: CQ#0 PBL pointer, RQ#0 PBL pointer,
  2386. * CQ#1 PBL pointer, RQ#1 PBL pointer, etc. Each PBL pointer points
  2387. * to the physical address which contains an array of pointers to
  2388. * the physical addresses of the specific queue pages.
  2389. */
  2390. for (i = 0; i < qedf->num_queues; i++) {
  2391. *list = U64_LO(qedf->global_queues[i]->cq_pbl_dma);
  2392. list++;
  2393. *list = U64_HI(qedf->global_queues[i]->cq_pbl_dma);
  2394. list++;
  2395. *list = U64_LO(0);
  2396. list++;
  2397. *list = U64_HI(0);
  2398. list++;
  2399. }
  2400. return 0;
  2401. mem_alloc_failure:
  2402. qedf_free_global_queues(qedf);
  2403. return status;
  2404. }
  2405. static int qedf_set_fcoe_pf_param(struct qedf_ctx *qedf)
  2406. {
  2407. u8 sq_num_pbl_pages;
  2408. u32 sq_mem_size;
  2409. u32 cq_mem_size;
  2410. u32 cq_num_entries;
  2411. int rval;
  2412. /*
  2413. * The number of completion queues/fastpath interrupts/status blocks
  2414. * we allocation is the minimum off:
  2415. *
  2416. * Number of CPUs
  2417. * Number allocated by qed for our PCI function
  2418. */
  2419. qedf->num_queues = MIN_NUM_CPUS_MSIX(qedf);
  2420. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Number of CQs is %d.\n",
  2421. qedf->num_queues);
  2422. qedf->p_cpuq = pci_alloc_consistent(qedf->pdev,
  2423. qedf->num_queues * sizeof(struct qedf_glbl_q_params),
  2424. &qedf->hw_p_cpuq);
  2425. if (!qedf->p_cpuq) {
  2426. QEDF_ERR(&(qedf->dbg_ctx), "pci_alloc_consistent failed.\n");
  2427. return 1;
  2428. }
  2429. rval = qedf_alloc_global_queues(qedf);
  2430. if (rval) {
  2431. QEDF_ERR(&(qedf->dbg_ctx), "Global queue allocation "
  2432. "failed.\n");
  2433. return 1;
  2434. }
  2435. /* Calculate SQ PBL size in the same manner as in qedf_sq_alloc() */
  2436. sq_mem_size = SQ_NUM_ENTRIES * sizeof(struct fcoe_wqe);
  2437. sq_mem_size = ALIGN(sq_mem_size, QEDF_PAGE_SIZE);
  2438. sq_num_pbl_pages = (sq_mem_size / QEDF_PAGE_SIZE);
  2439. /* Calculate CQ num entries */
  2440. cq_mem_size = FCOE_PARAMS_CQ_NUM_ENTRIES * sizeof(struct fcoe_cqe);
  2441. cq_mem_size = ALIGN(cq_mem_size, QEDF_PAGE_SIZE);
  2442. cq_num_entries = cq_mem_size / sizeof(struct fcoe_cqe);
  2443. memset(&(qedf->pf_params), 0, sizeof(qedf->pf_params));
  2444. /* Setup the value for fcoe PF */
  2445. qedf->pf_params.fcoe_pf_params.num_cons = QEDF_MAX_SESSIONS;
  2446. qedf->pf_params.fcoe_pf_params.num_tasks = FCOE_PARAMS_NUM_TASKS;
  2447. qedf->pf_params.fcoe_pf_params.glbl_q_params_addr =
  2448. (u64)qedf->hw_p_cpuq;
  2449. qedf->pf_params.fcoe_pf_params.sq_num_pbl_pages = sq_num_pbl_pages;
  2450. qedf->pf_params.fcoe_pf_params.rq_buffer_log_size = 0;
  2451. qedf->pf_params.fcoe_pf_params.cq_num_entries = cq_num_entries;
  2452. qedf->pf_params.fcoe_pf_params.num_cqs = qedf->num_queues;
  2453. /* log_page_size: 12 for 4KB pages */
  2454. qedf->pf_params.fcoe_pf_params.log_page_size = ilog2(QEDF_PAGE_SIZE);
  2455. qedf->pf_params.fcoe_pf_params.mtu = 9000;
  2456. qedf->pf_params.fcoe_pf_params.gl_rq_pi = QEDF_FCOE_PARAMS_GL_RQ_PI;
  2457. qedf->pf_params.fcoe_pf_params.gl_cmd_pi = QEDF_FCOE_PARAMS_GL_CMD_PI;
  2458. /* BDQ address and size */
  2459. qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0] =
  2460. qedf->bdq_pbl_list_dma;
  2461. qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0] =
  2462. qedf->bdq_pbl_list_num_entries;
  2463. qedf->pf_params.fcoe_pf_params.rq_buffer_size = QEDF_BDQ_BUF_SIZE;
  2464. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2465. "bdq_list=%p bdq_pbl_list_dma=%llx bdq_pbl_list_entries=%d.\n",
  2466. qedf->bdq_pbl_list,
  2467. qedf->pf_params.fcoe_pf_params.bdq_pbl_base_addr[0],
  2468. qedf->pf_params.fcoe_pf_params.bdq_pbl_num_entries[0]);
  2469. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2470. "cq_num_entries=%d.\n",
  2471. qedf->pf_params.fcoe_pf_params.cq_num_entries);
  2472. return 0;
  2473. }
  2474. /* Free DMA coherent memory for array of queue pointers we pass to qed */
  2475. static void qedf_free_fcoe_pf_param(struct qedf_ctx *qedf)
  2476. {
  2477. size_t size = 0;
  2478. if (qedf->p_cpuq) {
  2479. size = qedf->num_queues * sizeof(struct qedf_glbl_q_params);
  2480. pci_free_consistent(qedf->pdev, size, qedf->p_cpuq,
  2481. qedf->hw_p_cpuq);
  2482. }
  2483. qedf_free_global_queues(qedf);
  2484. if (qedf->global_queues)
  2485. kfree(qedf->global_queues);
  2486. }
  2487. /*
  2488. * PCI driver functions
  2489. */
  2490. static const struct pci_device_id qedf_pci_tbl[] = {
  2491. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x165c) },
  2492. { PCI_DEVICE(PCI_VENDOR_ID_QLOGIC, 0x8080) },
  2493. {0}
  2494. };
  2495. MODULE_DEVICE_TABLE(pci, qedf_pci_tbl);
  2496. static struct pci_driver qedf_pci_driver = {
  2497. .name = QEDF_MODULE_NAME,
  2498. .id_table = qedf_pci_tbl,
  2499. .probe = qedf_probe,
  2500. .remove = qedf_remove,
  2501. };
  2502. static int __qedf_probe(struct pci_dev *pdev, int mode)
  2503. {
  2504. int rc = -EINVAL;
  2505. struct fc_lport *lport;
  2506. struct qedf_ctx *qedf;
  2507. struct Scsi_Host *host;
  2508. bool is_vf = false;
  2509. struct qed_ll2_params params;
  2510. char host_buf[20];
  2511. struct qed_link_params link_params;
  2512. int status;
  2513. void *task_start, *task_end;
  2514. struct qed_slowpath_params slowpath_params;
  2515. struct qed_probe_params qed_params;
  2516. u16 tmp;
  2517. /*
  2518. * When doing error recovery we didn't reap the lport so don't try
  2519. * to reallocate it.
  2520. */
  2521. if (mode != QEDF_MODE_RECOVERY) {
  2522. lport = libfc_host_alloc(&qedf_host_template,
  2523. sizeof(struct qedf_ctx));
  2524. if (!lport) {
  2525. QEDF_ERR(NULL, "Could not allocate lport.\n");
  2526. rc = -ENOMEM;
  2527. goto err0;
  2528. }
  2529. /* Initialize qedf_ctx */
  2530. qedf = lport_priv(lport);
  2531. qedf->lport = lport;
  2532. qedf->ctlr.lp = lport;
  2533. qedf->pdev = pdev;
  2534. qedf->dbg_ctx.pdev = pdev;
  2535. qedf->dbg_ctx.host_no = lport->host->host_no;
  2536. spin_lock_init(&qedf->hba_lock);
  2537. INIT_LIST_HEAD(&qedf->fcports);
  2538. qedf->curr_conn_id = QEDF_MAX_SESSIONS - 1;
  2539. atomic_set(&qedf->num_offloads, 0);
  2540. qedf->stop_io_on_error = false;
  2541. pci_set_drvdata(pdev, qedf);
  2542. init_completion(&qedf->fipvlan_compl);
  2543. mutex_init(&qedf->stats_mutex);
  2544. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO,
  2545. "QLogic FastLinQ FCoE Module qedf %s, "
  2546. "FW %d.%d.%d.%d\n", QEDF_VERSION,
  2547. FW_MAJOR_VERSION, FW_MINOR_VERSION, FW_REVISION_VERSION,
  2548. FW_ENGINEERING_VERSION);
  2549. } else {
  2550. /* Init pointers during recovery */
  2551. qedf = pci_get_drvdata(pdev);
  2552. lport = qedf->lport;
  2553. }
  2554. host = lport->host;
  2555. /* Allocate mempool for qedf_io_work structs */
  2556. qedf->io_mempool = mempool_create_slab_pool(QEDF_IO_WORK_MIN,
  2557. qedf_io_work_cache);
  2558. if (qedf->io_mempool == NULL) {
  2559. QEDF_ERR(&(qedf->dbg_ctx), "qedf->io_mempool is NULL.\n");
  2560. goto err1;
  2561. }
  2562. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_INFO, "qedf->io_mempool=%p.\n",
  2563. qedf->io_mempool);
  2564. sprintf(host_buf, "qedf_%u_link",
  2565. qedf->lport->host->host_no);
  2566. qedf->link_update_wq = create_workqueue(host_buf);
  2567. INIT_DELAYED_WORK(&qedf->link_update, qedf_handle_link_update);
  2568. INIT_DELAYED_WORK(&qedf->link_recovery, qedf_link_recovery);
  2569. INIT_DELAYED_WORK(&qedf->grcdump_work, qedf_wq_grcdump);
  2570. qedf->fipvlan_retries = qedf_fipvlan_retries;
  2571. /* Set a default prio in case DCBX doesn't converge */
  2572. if (qedf_default_prio > -1) {
  2573. /*
  2574. * This is the case where we pass a modparam in so we want to
  2575. * honor it even if dcbx doesn't converge.
  2576. */
  2577. qedf->prio = qedf_default_prio;
  2578. } else
  2579. qedf->prio = QEDF_DEFAULT_PRIO;
  2580. /*
  2581. * Common probe. Takes care of basic hardware init and pci_*
  2582. * functions.
  2583. */
  2584. memset(&qed_params, 0, sizeof(qed_params));
  2585. qed_params.protocol = QED_PROTOCOL_FCOE;
  2586. qed_params.dp_module = qedf_dp_module;
  2587. qed_params.dp_level = qedf_dp_level;
  2588. qed_params.is_vf = is_vf;
  2589. qedf->cdev = qed_ops->common->probe(pdev, &qed_params);
  2590. if (!qedf->cdev) {
  2591. rc = -ENODEV;
  2592. goto err1;
  2593. }
  2594. /* Learn information crucial for qedf to progress */
  2595. rc = qed_ops->fill_dev_info(qedf->cdev, &qedf->dev_info);
  2596. if (rc) {
  2597. QEDF_ERR(&(qedf->dbg_ctx), "Failed to dev info.\n");
  2598. goto err1;
  2599. }
  2600. /* queue allocation code should come here
  2601. * order should be
  2602. * slowpath_start
  2603. * status block allocation
  2604. * interrupt registration (to get min number of queues)
  2605. * set_fcoe_pf_param
  2606. * qed_sp_fcoe_func_start
  2607. */
  2608. rc = qedf_set_fcoe_pf_param(qedf);
  2609. if (rc) {
  2610. QEDF_ERR(&(qedf->dbg_ctx), "Cannot set fcoe pf param.\n");
  2611. goto err2;
  2612. }
  2613. qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
  2614. /* Record BDQ producer doorbell addresses */
  2615. qedf->bdq_primary_prod = qedf->dev_info.primary_dbq_rq_addr;
  2616. qedf->bdq_secondary_prod = qedf->dev_info.secondary_bdq_rq_addr;
  2617. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2618. "BDQ primary_prod=%p secondary_prod=%p.\n", qedf->bdq_primary_prod,
  2619. qedf->bdq_secondary_prod);
  2620. qed_ops->register_ops(qedf->cdev, &qedf_cb_ops, qedf);
  2621. rc = qedf_prepare_sb(qedf);
  2622. if (rc) {
  2623. QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
  2624. goto err2;
  2625. }
  2626. /* Start the Slowpath-process */
  2627. slowpath_params.int_mode = QED_INT_MODE_MSIX;
  2628. slowpath_params.drv_major = QEDF_DRIVER_MAJOR_VER;
  2629. slowpath_params.drv_minor = QEDF_DRIVER_MINOR_VER;
  2630. slowpath_params.drv_rev = QEDF_DRIVER_REV_VER;
  2631. slowpath_params.drv_eng = QEDF_DRIVER_ENG_VER;
  2632. strncpy(slowpath_params.name, "qedf", QED_DRV_VER_STR_SIZE);
  2633. rc = qed_ops->common->slowpath_start(qedf->cdev, &slowpath_params);
  2634. if (rc) {
  2635. QEDF_ERR(&(qedf->dbg_ctx), "Cannot start slowpath.\n");
  2636. goto err2;
  2637. }
  2638. /*
  2639. * update_pf_params needs to be called before and after slowpath
  2640. * start
  2641. */
  2642. qed_ops->common->update_pf_params(qedf->cdev, &qedf->pf_params);
  2643. /* Setup interrupts */
  2644. rc = qedf_setup_int(qedf);
  2645. if (rc)
  2646. goto err3;
  2647. rc = qed_ops->start(qedf->cdev, &qedf->tasks);
  2648. if (rc) {
  2649. QEDF_ERR(&(qedf->dbg_ctx), "Cannot start FCoE function.\n");
  2650. goto err4;
  2651. }
  2652. task_start = qedf_get_task_mem(&qedf->tasks, 0);
  2653. task_end = qedf_get_task_mem(&qedf->tasks, MAX_TID_BLOCKS_FCOE - 1);
  2654. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "Task context start=%p, "
  2655. "end=%p block_size=%u.\n", task_start, task_end,
  2656. qedf->tasks.size);
  2657. /*
  2658. * We need to write the number of BDs in the BDQ we've preallocated so
  2659. * the f/w will do a prefetch and we'll get an unsolicited CQE when a
  2660. * packet arrives.
  2661. */
  2662. qedf->bdq_prod_idx = QEDF_BDQ_SIZE;
  2663. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2664. "Writing %d to primary and secondary BDQ doorbell registers.\n",
  2665. qedf->bdq_prod_idx);
  2666. writew(qedf->bdq_prod_idx, qedf->bdq_primary_prod);
  2667. tmp = readw(qedf->bdq_primary_prod);
  2668. writew(qedf->bdq_prod_idx, qedf->bdq_secondary_prod);
  2669. tmp = readw(qedf->bdq_secondary_prod);
  2670. qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
  2671. /* Now that the dev_info struct has been filled in set the MAC
  2672. * address
  2673. */
  2674. ether_addr_copy(qedf->mac, qedf->dev_info.common.hw_mac);
  2675. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "MAC address is %pM.\n",
  2676. qedf->mac);
  2677. /*
  2678. * Set the WWNN and WWPN in the following way:
  2679. *
  2680. * If the info we get from qed is non-zero then use that to set the
  2681. * WWPN and WWNN. Otherwise fall back to use fcoe_wwn_from_mac() based
  2682. * on the MAC address.
  2683. */
  2684. if (qedf->dev_info.wwnn != 0 && qedf->dev_info.wwpn != 0) {
  2685. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2686. "Setting WWPN and WWNN from qed dev_info.\n");
  2687. qedf->wwnn = qedf->dev_info.wwnn;
  2688. qedf->wwpn = qedf->dev_info.wwpn;
  2689. } else {
  2690. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2691. "Setting WWPN and WWNN using fcoe_wwn_from_mac().\n");
  2692. qedf->wwnn = fcoe_wwn_from_mac(qedf->mac, 1, 0);
  2693. qedf->wwpn = fcoe_wwn_from_mac(qedf->mac, 2, 0);
  2694. }
  2695. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC, "WWNN=%016llx "
  2696. "WWPN=%016llx.\n", qedf->wwnn, qedf->wwpn);
  2697. sprintf(host_buf, "host_%d", host->host_no);
  2698. qed_ops->common->set_name(qedf->cdev, host_buf);
  2699. /* Set xid max values */
  2700. qedf->max_scsi_xid = QEDF_MAX_SCSI_XID;
  2701. qedf->max_els_xid = QEDF_MAX_ELS_XID;
  2702. /* Allocate cmd mgr */
  2703. qedf->cmd_mgr = qedf_cmd_mgr_alloc(qedf);
  2704. if (!qedf->cmd_mgr) {
  2705. QEDF_ERR(&(qedf->dbg_ctx), "Failed to allocate cmd mgr.\n");
  2706. rc = -ENOMEM;
  2707. goto err5;
  2708. }
  2709. if (mode != QEDF_MODE_RECOVERY) {
  2710. host->transportt = qedf_fc_transport_template;
  2711. host->can_queue = QEDF_MAX_ELS_XID;
  2712. host->max_lun = qedf_max_lun;
  2713. host->max_cmd_len = QEDF_MAX_CDB_LEN;
  2714. rc = scsi_add_host(host, &pdev->dev);
  2715. if (rc)
  2716. goto err6;
  2717. }
  2718. memset(&params, 0, sizeof(params));
  2719. params.mtu = 9000;
  2720. ether_addr_copy(params.ll2_mac_address, qedf->mac);
  2721. /* Start LL2 processing thread */
  2722. snprintf(host_buf, 20, "qedf_%d_ll2", host->host_no);
  2723. qedf->ll2_recv_wq =
  2724. create_workqueue(host_buf);
  2725. if (!qedf->ll2_recv_wq) {
  2726. QEDF_ERR(&(qedf->dbg_ctx), "Failed to LL2 workqueue.\n");
  2727. rc = -ENOMEM;
  2728. goto err7;
  2729. }
  2730. #ifdef CONFIG_DEBUG_FS
  2731. qedf_dbg_host_init(&(qedf->dbg_ctx), qedf_debugfs_ops,
  2732. qedf_dbg_fops);
  2733. #endif
  2734. /* Start LL2 */
  2735. qed_ops->ll2->register_cb_ops(qedf->cdev, &qedf_ll2_cb_ops, qedf);
  2736. rc = qed_ops->ll2->start(qedf->cdev, &params);
  2737. if (rc) {
  2738. QEDF_ERR(&(qedf->dbg_ctx), "Could not start Light L2.\n");
  2739. goto err7;
  2740. }
  2741. set_bit(QEDF_LL2_STARTED, &qedf->flags);
  2742. /* Set initial FIP/FCoE VLAN to NULL */
  2743. qedf->vlan_id = 0;
  2744. /*
  2745. * No need to setup fcoe_ctlr or fc_lport objects during recovery since
  2746. * they were not reaped during the unload process.
  2747. */
  2748. if (mode != QEDF_MODE_RECOVERY) {
  2749. /* Setup imbedded fcoe controller */
  2750. qedf_fcoe_ctlr_setup(qedf);
  2751. /* Setup lport */
  2752. rc = qedf_lport_setup(qedf);
  2753. if (rc) {
  2754. QEDF_ERR(&(qedf->dbg_ctx),
  2755. "qedf_lport_setup failed.\n");
  2756. goto err7;
  2757. }
  2758. }
  2759. sprintf(host_buf, "qedf_%u_timer", qedf->lport->host->host_no);
  2760. qedf->timer_work_queue =
  2761. create_workqueue(host_buf);
  2762. if (!qedf->timer_work_queue) {
  2763. QEDF_ERR(&(qedf->dbg_ctx), "Failed to start timer "
  2764. "workqueue.\n");
  2765. rc = -ENOMEM;
  2766. goto err7;
  2767. }
  2768. /* DPC workqueue is not reaped during recovery unload */
  2769. if (mode != QEDF_MODE_RECOVERY) {
  2770. sprintf(host_buf, "qedf_%u_dpc",
  2771. qedf->lport->host->host_no);
  2772. qedf->dpc_wq = create_workqueue(host_buf);
  2773. }
  2774. /*
  2775. * GRC dump and sysfs parameters are not reaped during the recovery
  2776. * unload process.
  2777. */
  2778. if (mode != QEDF_MODE_RECOVERY) {
  2779. qedf->grcdump_size =
  2780. qed_ops->common->dbg_all_data_size(qedf->cdev);
  2781. if (qedf->grcdump_size) {
  2782. rc = qedf_alloc_grc_dump_buf(&qedf->grcdump,
  2783. qedf->grcdump_size);
  2784. if (rc) {
  2785. QEDF_ERR(&(qedf->dbg_ctx),
  2786. "GRC Dump buffer alloc failed.\n");
  2787. qedf->grcdump = NULL;
  2788. }
  2789. QEDF_INFO(&(qedf->dbg_ctx), QEDF_LOG_DISC,
  2790. "grcdump: addr=%p, size=%u.\n",
  2791. qedf->grcdump, qedf->grcdump_size);
  2792. }
  2793. qedf_create_sysfs_ctx_attr(qedf);
  2794. /* Initialize I/O tracing for this adapter */
  2795. spin_lock_init(&qedf->io_trace_lock);
  2796. qedf->io_trace_idx = 0;
  2797. }
  2798. init_completion(&qedf->flogi_compl);
  2799. status = qed_ops->common->update_drv_state(qedf->cdev, true);
  2800. if (status)
  2801. QEDF_ERR(&(qedf->dbg_ctx),
  2802. "Failed to send drv state to MFW.\n");
  2803. memset(&link_params, 0, sizeof(struct qed_link_params));
  2804. link_params.link_up = true;
  2805. status = qed_ops->common->set_link(qedf->cdev, &link_params);
  2806. if (status)
  2807. QEDF_WARN(&(qedf->dbg_ctx), "set_link failed.\n");
  2808. /* Start/restart discovery */
  2809. if (mode == QEDF_MODE_RECOVERY)
  2810. fcoe_ctlr_link_up(&qedf->ctlr);
  2811. else
  2812. fc_fabric_login(lport);
  2813. /* All good */
  2814. return 0;
  2815. err7:
  2816. if (qedf->ll2_recv_wq)
  2817. destroy_workqueue(qedf->ll2_recv_wq);
  2818. fc_remove_host(qedf->lport->host);
  2819. scsi_remove_host(qedf->lport->host);
  2820. #ifdef CONFIG_DEBUG_FS
  2821. qedf_dbg_host_exit(&(qedf->dbg_ctx));
  2822. #endif
  2823. err6:
  2824. qedf_cmd_mgr_free(qedf->cmd_mgr);
  2825. err5:
  2826. qed_ops->stop(qedf->cdev);
  2827. err4:
  2828. qedf_free_fcoe_pf_param(qedf);
  2829. qedf_sync_free_irqs(qedf);
  2830. err3:
  2831. qed_ops->common->slowpath_stop(qedf->cdev);
  2832. err2:
  2833. qed_ops->common->remove(qedf->cdev);
  2834. err1:
  2835. scsi_host_put(lport->host);
  2836. err0:
  2837. return rc;
  2838. }
  2839. static int qedf_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  2840. {
  2841. return __qedf_probe(pdev, QEDF_MODE_NORMAL);
  2842. }
  2843. static void __qedf_remove(struct pci_dev *pdev, int mode)
  2844. {
  2845. struct qedf_ctx *qedf;
  2846. int rc;
  2847. if (!pdev) {
  2848. QEDF_ERR(NULL, "pdev is NULL.\n");
  2849. return;
  2850. }
  2851. qedf = pci_get_drvdata(pdev);
  2852. /*
  2853. * Prevent race where we're in board disable work and then try to
  2854. * rmmod the module.
  2855. */
  2856. if (test_bit(QEDF_UNLOADING, &qedf->flags)) {
  2857. QEDF_ERR(&qedf->dbg_ctx, "Already removing PCI function.\n");
  2858. return;
  2859. }
  2860. if (mode != QEDF_MODE_RECOVERY)
  2861. set_bit(QEDF_UNLOADING, &qedf->flags);
  2862. /* Logoff the fabric to upload all connections */
  2863. if (mode == QEDF_MODE_RECOVERY)
  2864. fcoe_ctlr_link_down(&qedf->ctlr);
  2865. else
  2866. fc_fabric_logoff(qedf->lport);
  2867. qedf_wait_for_upload(qedf);
  2868. #ifdef CONFIG_DEBUG_FS
  2869. qedf_dbg_host_exit(&(qedf->dbg_ctx));
  2870. #endif
  2871. /* Stop any link update handling */
  2872. cancel_delayed_work_sync(&qedf->link_update);
  2873. destroy_workqueue(qedf->link_update_wq);
  2874. qedf->link_update_wq = NULL;
  2875. if (qedf->timer_work_queue)
  2876. destroy_workqueue(qedf->timer_work_queue);
  2877. /* Stop Light L2 */
  2878. clear_bit(QEDF_LL2_STARTED, &qedf->flags);
  2879. qed_ops->ll2->stop(qedf->cdev);
  2880. if (qedf->ll2_recv_wq)
  2881. destroy_workqueue(qedf->ll2_recv_wq);
  2882. /* Stop fastpath */
  2883. qedf_sync_free_irqs(qedf);
  2884. qedf_destroy_sb(qedf);
  2885. /*
  2886. * During recovery don't destroy OS constructs that represent the
  2887. * physical port.
  2888. */
  2889. if (mode != QEDF_MODE_RECOVERY) {
  2890. qedf_free_grc_dump_buf(&qedf->grcdump);
  2891. qedf_remove_sysfs_ctx_attr(qedf);
  2892. /* Remove all SCSI/libfc/libfcoe structures */
  2893. fcoe_ctlr_destroy(&qedf->ctlr);
  2894. fc_lport_destroy(qedf->lport);
  2895. fc_remove_host(qedf->lport->host);
  2896. scsi_remove_host(qedf->lport->host);
  2897. }
  2898. qedf_cmd_mgr_free(qedf->cmd_mgr);
  2899. if (mode != QEDF_MODE_RECOVERY) {
  2900. fc_exch_mgr_free(qedf->lport);
  2901. fc_lport_free_stats(qedf->lport);
  2902. /* Wait for all vports to be reaped */
  2903. qedf_wait_for_vport_destroy(qedf);
  2904. }
  2905. /*
  2906. * Now that all connections have been uploaded we can stop the
  2907. * rest of the qed operations
  2908. */
  2909. qed_ops->stop(qedf->cdev);
  2910. if (mode != QEDF_MODE_RECOVERY) {
  2911. if (qedf->dpc_wq) {
  2912. /* Stop general DPC handling */
  2913. destroy_workqueue(qedf->dpc_wq);
  2914. qedf->dpc_wq = NULL;
  2915. }
  2916. }
  2917. /* Final shutdown for the board */
  2918. qedf_free_fcoe_pf_param(qedf);
  2919. if (mode != QEDF_MODE_RECOVERY) {
  2920. qed_ops->common->set_power_state(qedf->cdev, PCI_D0);
  2921. pci_set_drvdata(pdev, NULL);
  2922. }
  2923. rc = qed_ops->common->update_drv_state(qedf->cdev, false);
  2924. if (rc)
  2925. QEDF_ERR(&(qedf->dbg_ctx),
  2926. "Failed to send drv state to MFW.\n");
  2927. qed_ops->common->slowpath_stop(qedf->cdev);
  2928. qed_ops->common->remove(qedf->cdev);
  2929. mempool_destroy(qedf->io_mempool);
  2930. /* Only reap the Scsi_host on a real removal */
  2931. if (mode != QEDF_MODE_RECOVERY)
  2932. scsi_host_put(qedf->lport->host);
  2933. }
  2934. static void qedf_remove(struct pci_dev *pdev)
  2935. {
  2936. /* Check to make sure this function wasn't already disabled */
  2937. if (!atomic_read(&pdev->enable_cnt))
  2938. return;
  2939. __qedf_remove(pdev, QEDF_MODE_NORMAL);
  2940. }
  2941. void qedf_wq_grcdump(struct work_struct *work)
  2942. {
  2943. struct qedf_ctx *qedf =
  2944. container_of(work, struct qedf_ctx, grcdump_work.work);
  2945. QEDF_ERR(&(qedf->dbg_ctx), "Collecting GRC dump.\n");
  2946. qedf_capture_grc_dump(qedf);
  2947. }
  2948. /*
  2949. * Protocol TLV handler
  2950. */
  2951. void qedf_get_protocol_tlv_data(void *dev, void *data)
  2952. {
  2953. struct qedf_ctx *qedf = dev;
  2954. struct qed_mfw_tlv_fcoe *fcoe = data;
  2955. struct fc_lport *lport = qedf->lport;
  2956. struct Scsi_Host *host = lport->host;
  2957. struct fc_host_attrs *fc_host = shost_to_fc_host(host);
  2958. struct fc_host_statistics *hst;
  2959. /* Force a refresh of the fc_host stats including offload stats */
  2960. hst = qedf_fc_get_host_stats(host);
  2961. fcoe->qos_pri_set = true;
  2962. fcoe->qos_pri = 3; /* Hard coded to 3 in driver */
  2963. fcoe->ra_tov_set = true;
  2964. fcoe->ra_tov = lport->r_a_tov;
  2965. fcoe->ed_tov_set = true;
  2966. fcoe->ed_tov = lport->e_d_tov;
  2967. fcoe->npiv_state_set = true;
  2968. fcoe->npiv_state = 1; /* NPIV always enabled */
  2969. fcoe->num_npiv_ids_set = true;
  2970. fcoe->num_npiv_ids = fc_host->npiv_vports_inuse;
  2971. /* Certain attributes we only want to set if we've selected an FCF */
  2972. if (qedf->ctlr.sel_fcf) {
  2973. fcoe->switch_name_set = true;
  2974. u64_to_wwn(qedf->ctlr.sel_fcf->switch_name, fcoe->switch_name);
  2975. }
  2976. fcoe->port_state_set = true;
  2977. /* For qedf we're either link down or fabric attach */
  2978. if (lport->link_up)
  2979. fcoe->port_state = QED_MFW_TLV_PORT_STATE_FABRIC;
  2980. else
  2981. fcoe->port_state = QED_MFW_TLV_PORT_STATE_OFFLINE;
  2982. fcoe->link_failures_set = true;
  2983. fcoe->link_failures = (u16)hst->link_failure_count;
  2984. fcoe->fcoe_txq_depth_set = true;
  2985. fcoe->fcoe_rxq_depth_set = true;
  2986. fcoe->fcoe_rxq_depth = FCOE_PARAMS_NUM_TASKS;
  2987. fcoe->fcoe_txq_depth = FCOE_PARAMS_NUM_TASKS;
  2988. fcoe->fcoe_rx_frames_set = true;
  2989. fcoe->fcoe_rx_frames = hst->rx_frames;
  2990. fcoe->fcoe_tx_frames_set = true;
  2991. fcoe->fcoe_tx_frames = hst->tx_frames;
  2992. fcoe->fcoe_rx_bytes_set = true;
  2993. fcoe->fcoe_rx_bytes = hst->fcp_input_megabytes * 1000000;
  2994. fcoe->fcoe_tx_bytes_set = true;
  2995. fcoe->fcoe_tx_bytes = hst->fcp_output_megabytes * 1000000;
  2996. fcoe->crc_count_set = true;
  2997. fcoe->crc_count = hst->invalid_crc_count;
  2998. fcoe->tx_abts_set = true;
  2999. fcoe->tx_abts = hst->fcp_packet_aborts;
  3000. fcoe->tx_lun_rst_set = true;
  3001. fcoe->tx_lun_rst = qedf->lun_resets;
  3002. fcoe->abort_task_sets_set = true;
  3003. fcoe->abort_task_sets = qedf->packet_aborts;
  3004. fcoe->scsi_busy_set = true;
  3005. fcoe->scsi_busy = qedf->busy;
  3006. fcoe->scsi_tsk_full_set = true;
  3007. fcoe->scsi_tsk_full = qedf->task_set_fulls;
  3008. }
  3009. /* Generic TLV data callback */
  3010. void qedf_get_generic_tlv_data(void *dev, struct qed_generic_tlvs *data)
  3011. {
  3012. struct qedf_ctx *qedf;
  3013. if (!dev) {
  3014. QEDF_INFO(NULL, QEDF_LOG_EVT,
  3015. "dev is NULL so ignoring get_generic_tlv_data request.\n");
  3016. return;
  3017. }
  3018. qedf = (struct qedf_ctx *)dev;
  3019. memset(data, 0, sizeof(struct qed_generic_tlvs));
  3020. ether_addr_copy(data->mac[0], qedf->mac);
  3021. }
  3022. /*
  3023. * Module Init/Remove
  3024. */
  3025. static int __init qedf_init(void)
  3026. {
  3027. int ret;
  3028. /* If debug=1 passed, set the default log mask */
  3029. if (qedf_debug == QEDF_LOG_DEFAULT)
  3030. qedf_debug = QEDF_DEFAULT_LOG_MASK;
  3031. /*
  3032. * Check that default prio for FIP/FCoE traffic is between 0..7 if a
  3033. * value has been set
  3034. */
  3035. if (qedf_default_prio > -1)
  3036. if (qedf_default_prio > 7) {
  3037. qedf_default_prio = QEDF_DEFAULT_PRIO;
  3038. QEDF_ERR(NULL, "FCoE/FIP priority out of range, resetting to %d.\n",
  3039. QEDF_DEFAULT_PRIO);
  3040. }
  3041. /* Print driver banner */
  3042. QEDF_INFO(NULL, QEDF_LOG_INFO, "%s v%s.\n", QEDF_DESCR,
  3043. QEDF_VERSION);
  3044. /* Create kmem_cache for qedf_io_work structs */
  3045. qedf_io_work_cache = kmem_cache_create("qedf_io_work_cache",
  3046. sizeof(struct qedf_io_work), 0, SLAB_HWCACHE_ALIGN, NULL);
  3047. if (qedf_io_work_cache == NULL) {
  3048. QEDF_ERR(NULL, "qedf_io_work_cache is NULL.\n");
  3049. goto err1;
  3050. }
  3051. QEDF_INFO(NULL, QEDF_LOG_DISC, "qedf_io_work_cache=%p.\n",
  3052. qedf_io_work_cache);
  3053. qed_ops = qed_get_fcoe_ops();
  3054. if (!qed_ops) {
  3055. QEDF_ERR(NULL, "Failed to get qed fcoe operations\n");
  3056. goto err1;
  3057. }
  3058. #ifdef CONFIG_DEBUG_FS
  3059. qedf_dbg_init("qedf");
  3060. #endif
  3061. qedf_fc_transport_template =
  3062. fc_attach_transport(&qedf_fc_transport_fn);
  3063. if (!qedf_fc_transport_template) {
  3064. QEDF_ERR(NULL, "Could not register with FC transport\n");
  3065. goto err2;
  3066. }
  3067. qedf_fc_vport_transport_template =
  3068. fc_attach_transport(&qedf_fc_vport_transport_fn);
  3069. if (!qedf_fc_vport_transport_template) {
  3070. QEDF_ERR(NULL, "Could not register vport template with FC "
  3071. "transport\n");
  3072. goto err3;
  3073. }
  3074. qedf_io_wq = create_workqueue("qedf_io_wq");
  3075. if (!qedf_io_wq) {
  3076. QEDF_ERR(NULL, "Could not create qedf_io_wq.\n");
  3077. goto err4;
  3078. }
  3079. qedf_cb_ops.get_login_failures = qedf_get_login_failures;
  3080. ret = pci_register_driver(&qedf_pci_driver);
  3081. if (ret) {
  3082. QEDF_ERR(NULL, "Failed to register driver\n");
  3083. goto err5;
  3084. }
  3085. return 0;
  3086. err5:
  3087. destroy_workqueue(qedf_io_wq);
  3088. err4:
  3089. fc_release_transport(qedf_fc_vport_transport_template);
  3090. err3:
  3091. fc_release_transport(qedf_fc_transport_template);
  3092. err2:
  3093. #ifdef CONFIG_DEBUG_FS
  3094. qedf_dbg_exit();
  3095. #endif
  3096. qed_put_fcoe_ops();
  3097. err1:
  3098. return -EINVAL;
  3099. }
  3100. static void __exit qedf_cleanup(void)
  3101. {
  3102. pci_unregister_driver(&qedf_pci_driver);
  3103. destroy_workqueue(qedf_io_wq);
  3104. fc_release_transport(qedf_fc_vport_transport_template);
  3105. fc_release_transport(qedf_fc_transport_template);
  3106. #ifdef CONFIG_DEBUG_FS
  3107. qedf_dbg_exit();
  3108. #endif
  3109. qed_put_fcoe_ops();
  3110. kmem_cache_destroy(qedf_io_work_cache);
  3111. }
  3112. MODULE_LICENSE("GPL");
  3113. MODULE_DESCRIPTION("QLogic QEDF 25/40/50/100Gb FCoE Driver");
  3114. MODULE_AUTHOR("QLogic Corporation");
  3115. MODULE_VERSION(QEDF_VERSION);
  3116. module_init(qedf_init);
  3117. module_exit(qedf_cleanup);