lpfc_scsi.c 123 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2004-2011 Emulex. All rights reserved. *
  5. * EMULEX and SLI are trademarks of Emulex. *
  6. * www.emulex.com *
  7. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  8. * *
  9. * This program is free software; you can redistribute it and/or *
  10. * modify it under the terms of version 2 of the GNU General *
  11. * Public License as published by the Free Software Foundation. *
  12. * This program is distributed in the hope that it will be useful. *
  13. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  14. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  15. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  16. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  17. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  18. * more details, a copy of which can be found in the file COPYING *
  19. * included with this package. *
  20. *******************************************************************/
  21. #include <linux/pci.h>
  22. #include <linux/slab.h>
  23. #include <linux/interrupt.h>
  24. #include <linux/export.h>
  25. #include <linux/delay.h>
  26. #include <asm/unaligned.h>
  27. #include <scsi/scsi.h>
  28. #include <scsi/scsi_device.h>
  29. #include <scsi/scsi_eh.h>
  30. #include <scsi/scsi_host.h>
  31. #include <scsi/scsi_tcq.h>
  32. #include <scsi/scsi_transport_fc.h>
  33. #include "lpfc_version.h"
  34. #include "lpfc_hw4.h"
  35. #include "lpfc_hw.h"
  36. #include "lpfc_sli.h"
  37. #include "lpfc_sli4.h"
  38. #include "lpfc_nl.h"
  39. #include "lpfc_disc.h"
  40. #include "lpfc_scsi.h"
  41. #include "lpfc.h"
  42. #include "lpfc_logmsg.h"
  43. #include "lpfc_crtn.h"
  44. #include "lpfc_vport.h"
  45. #define LPFC_RESET_WAIT 2
  46. #define LPFC_ABORT_WAIT 2
  47. int _dump_buf_done;
  48. static char *dif_op_str[] = {
  49. "SCSI_PROT_NORMAL",
  50. "SCSI_PROT_READ_INSERT",
  51. "SCSI_PROT_WRITE_STRIP",
  52. "SCSI_PROT_READ_STRIP",
  53. "SCSI_PROT_WRITE_INSERT",
  54. "SCSI_PROT_READ_PASS",
  55. "SCSI_PROT_WRITE_PASS",
  56. };
  57. struct scsi_dif_tuple {
  58. __be16 guard_tag; /* Checksum */
  59. __be16 app_tag; /* Opaque storage */
  60. __be32 ref_tag; /* Target LBA or indirect LBA */
  61. };
  62. static void
  63. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  64. static void
  65. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb);
  66. static void
  67. lpfc_debug_save_data(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  68. {
  69. void *src, *dst;
  70. struct scatterlist *sgde = scsi_sglist(cmnd);
  71. if (!_dump_buf_data) {
  72. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  73. "9050 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  74. __func__);
  75. return;
  76. }
  77. if (!sgde) {
  78. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  79. "9051 BLKGRD: ERROR: data scatterlist is null\n");
  80. return;
  81. }
  82. dst = (void *) _dump_buf_data;
  83. while (sgde) {
  84. src = sg_virt(sgde);
  85. memcpy(dst, src, sgde->length);
  86. dst += sgde->length;
  87. sgde = sg_next(sgde);
  88. }
  89. }
  90. static void
  91. lpfc_debug_save_dif(struct lpfc_hba *phba, struct scsi_cmnd *cmnd)
  92. {
  93. void *src, *dst;
  94. struct scatterlist *sgde = scsi_prot_sglist(cmnd);
  95. if (!_dump_buf_dif) {
  96. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  97. "9052 BLKGRD: ERROR %s _dump_buf_data is NULL\n",
  98. __func__);
  99. return;
  100. }
  101. if (!sgde) {
  102. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  103. "9053 BLKGRD: ERROR: prot scatterlist is null\n");
  104. return;
  105. }
  106. dst = _dump_buf_dif;
  107. while (sgde) {
  108. src = sg_virt(sgde);
  109. memcpy(dst, src, sgde->length);
  110. dst += sgde->length;
  111. sgde = sg_next(sgde);
  112. }
  113. }
  114. /**
  115. * lpfc_sli4_set_rsp_sgl_last - Set the last bit in the response sge.
  116. * @phba: Pointer to HBA object.
  117. * @lpfc_cmd: lpfc scsi command object pointer.
  118. *
  119. * This function is called from the lpfc_prep_task_mgmt_cmd function to
  120. * set the last bit in the response sge entry.
  121. **/
  122. static void
  123. lpfc_sli4_set_rsp_sgl_last(struct lpfc_hba *phba,
  124. struct lpfc_scsi_buf *lpfc_cmd)
  125. {
  126. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  127. if (sgl) {
  128. sgl += 1;
  129. sgl->word2 = le32_to_cpu(sgl->word2);
  130. bf_set(lpfc_sli4_sge_last, sgl, 1);
  131. sgl->word2 = cpu_to_le32(sgl->word2);
  132. }
  133. }
  134. /**
  135. * lpfc_update_stats - Update statistical data for the command completion
  136. * @phba: Pointer to HBA object.
  137. * @lpfc_cmd: lpfc scsi command object pointer.
  138. *
  139. * This function is called when there is a command completion and this
  140. * function updates the statistical data for the command completion.
  141. **/
  142. static void
  143. lpfc_update_stats(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  144. {
  145. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  146. struct lpfc_nodelist *pnode = rdata->pnode;
  147. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  148. unsigned long flags;
  149. struct Scsi_Host *shost = cmd->device->host;
  150. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  151. unsigned long latency;
  152. int i;
  153. if (cmd->result)
  154. return;
  155. latency = jiffies_to_msecs((long)jiffies - (long)lpfc_cmd->start_time);
  156. spin_lock_irqsave(shost->host_lock, flags);
  157. if (!vport->stat_data_enabled ||
  158. vport->stat_data_blocked ||
  159. !pnode ||
  160. !pnode->lat_data ||
  161. (phba->bucket_type == LPFC_NO_BUCKET)) {
  162. spin_unlock_irqrestore(shost->host_lock, flags);
  163. return;
  164. }
  165. if (phba->bucket_type == LPFC_LINEAR_BUCKET) {
  166. i = (latency + phba->bucket_step - 1 - phba->bucket_base)/
  167. phba->bucket_step;
  168. /* check array subscript bounds */
  169. if (i < 0)
  170. i = 0;
  171. else if (i >= LPFC_MAX_BUCKET_COUNT)
  172. i = LPFC_MAX_BUCKET_COUNT - 1;
  173. } else {
  174. for (i = 0; i < LPFC_MAX_BUCKET_COUNT-1; i++)
  175. if (latency <= (phba->bucket_base +
  176. ((1<<i)*phba->bucket_step)))
  177. break;
  178. }
  179. pnode->lat_data[i].cmd_count++;
  180. spin_unlock_irqrestore(shost->host_lock, flags);
  181. }
  182. /**
  183. * lpfc_send_sdev_queuedepth_change_event - Posts a queuedepth change event
  184. * @phba: Pointer to HBA context object.
  185. * @vport: Pointer to vport object.
  186. * @ndlp: Pointer to FC node associated with the target.
  187. * @lun: Lun number of the scsi device.
  188. * @old_val: Old value of the queue depth.
  189. * @new_val: New value of the queue depth.
  190. *
  191. * This function sends an event to the mgmt application indicating
  192. * there is a change in the scsi device queue depth.
  193. **/
  194. static void
  195. lpfc_send_sdev_queuedepth_change_event(struct lpfc_hba *phba,
  196. struct lpfc_vport *vport,
  197. struct lpfc_nodelist *ndlp,
  198. uint32_t lun,
  199. uint32_t old_val,
  200. uint32_t new_val)
  201. {
  202. struct lpfc_fast_path_event *fast_path_evt;
  203. unsigned long flags;
  204. fast_path_evt = lpfc_alloc_fast_evt(phba);
  205. if (!fast_path_evt)
  206. return;
  207. fast_path_evt->un.queue_depth_evt.scsi_event.event_type =
  208. FC_REG_SCSI_EVENT;
  209. fast_path_evt->un.queue_depth_evt.scsi_event.subcategory =
  210. LPFC_EVENT_VARQUEDEPTH;
  211. /* Report all luns with change in queue depth */
  212. fast_path_evt->un.queue_depth_evt.scsi_event.lun = lun;
  213. if (ndlp && NLP_CHK_NODE_ACT(ndlp)) {
  214. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwpn,
  215. &ndlp->nlp_portname, sizeof(struct lpfc_name));
  216. memcpy(&fast_path_evt->un.queue_depth_evt.scsi_event.wwnn,
  217. &ndlp->nlp_nodename, sizeof(struct lpfc_name));
  218. }
  219. fast_path_evt->un.queue_depth_evt.oldval = old_val;
  220. fast_path_evt->un.queue_depth_evt.newval = new_val;
  221. fast_path_evt->vport = vport;
  222. fast_path_evt->work_evt.evt = LPFC_EVT_FASTPATH_MGMT_EVT;
  223. spin_lock_irqsave(&phba->hbalock, flags);
  224. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  225. spin_unlock_irqrestore(&phba->hbalock, flags);
  226. lpfc_worker_wake_up(phba);
  227. return;
  228. }
  229. /**
  230. * lpfc_change_queue_depth - Alter scsi device queue depth
  231. * @sdev: Pointer the scsi device on which to change the queue depth.
  232. * @qdepth: New queue depth to set the sdev to.
  233. * @reason: The reason for the queue depth change.
  234. *
  235. * This function is called by the midlayer and the LLD to alter the queue
  236. * depth for a scsi device. This function sets the queue depth to the new
  237. * value and sends an event out to log the queue depth change.
  238. **/
  239. int
  240. lpfc_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  241. {
  242. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  243. struct lpfc_hba *phba = vport->phba;
  244. struct lpfc_rport_data *rdata;
  245. unsigned long new_queue_depth, old_queue_depth;
  246. old_queue_depth = sdev->queue_depth;
  247. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  248. new_queue_depth = sdev->queue_depth;
  249. rdata = sdev->hostdata;
  250. if (rdata)
  251. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  252. rdata->pnode, sdev->lun,
  253. old_queue_depth,
  254. new_queue_depth);
  255. return sdev->queue_depth;
  256. }
  257. /**
  258. * lpfc_rampdown_queue_depth - Post RAMP_DOWN_QUEUE event to worker thread
  259. * @phba: The Hba for which this call is being executed.
  260. *
  261. * This routine is called when there is resource error in driver or firmware.
  262. * This routine posts WORKER_RAMP_DOWN_QUEUE event for @phba. This routine
  263. * posts at most 1 event each second. This routine wakes up worker thread of
  264. * @phba to process WORKER_RAM_DOWN_EVENT event.
  265. *
  266. * This routine should be called with no lock held.
  267. **/
  268. void
  269. lpfc_rampdown_queue_depth(struct lpfc_hba *phba)
  270. {
  271. unsigned long flags;
  272. uint32_t evt_posted;
  273. spin_lock_irqsave(&phba->hbalock, flags);
  274. atomic_inc(&phba->num_rsrc_err);
  275. phba->last_rsrc_error_time = jiffies;
  276. if ((phba->last_ramp_down_time + QUEUE_RAMP_DOWN_INTERVAL) > jiffies) {
  277. spin_unlock_irqrestore(&phba->hbalock, flags);
  278. return;
  279. }
  280. phba->last_ramp_down_time = jiffies;
  281. spin_unlock_irqrestore(&phba->hbalock, flags);
  282. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  283. evt_posted = phba->pport->work_port_events & WORKER_RAMP_DOWN_QUEUE;
  284. if (!evt_posted)
  285. phba->pport->work_port_events |= WORKER_RAMP_DOWN_QUEUE;
  286. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  287. if (!evt_posted)
  288. lpfc_worker_wake_up(phba);
  289. return;
  290. }
  291. /**
  292. * lpfc_rampup_queue_depth - Post RAMP_UP_QUEUE event for worker thread
  293. * @phba: The Hba for which this call is being executed.
  294. *
  295. * This routine post WORKER_RAMP_UP_QUEUE event for @phba vport. This routine
  296. * post at most 1 event every 5 minute after last_ramp_up_time or
  297. * last_rsrc_error_time. This routine wakes up worker thread of @phba
  298. * to process WORKER_RAM_DOWN_EVENT event.
  299. *
  300. * This routine should be called with no lock held.
  301. **/
  302. static inline void
  303. lpfc_rampup_queue_depth(struct lpfc_vport *vport,
  304. uint32_t queue_depth)
  305. {
  306. unsigned long flags;
  307. struct lpfc_hba *phba = vport->phba;
  308. uint32_t evt_posted;
  309. atomic_inc(&phba->num_cmd_success);
  310. if (vport->cfg_lun_queue_depth <= queue_depth)
  311. return;
  312. spin_lock_irqsave(&phba->hbalock, flags);
  313. if (time_before(jiffies,
  314. phba->last_ramp_up_time + QUEUE_RAMP_UP_INTERVAL) ||
  315. time_before(jiffies,
  316. phba->last_rsrc_error_time + QUEUE_RAMP_UP_INTERVAL)) {
  317. spin_unlock_irqrestore(&phba->hbalock, flags);
  318. return;
  319. }
  320. phba->last_ramp_up_time = jiffies;
  321. spin_unlock_irqrestore(&phba->hbalock, flags);
  322. spin_lock_irqsave(&phba->pport->work_port_lock, flags);
  323. evt_posted = phba->pport->work_port_events & WORKER_RAMP_UP_QUEUE;
  324. if (!evt_posted)
  325. phba->pport->work_port_events |= WORKER_RAMP_UP_QUEUE;
  326. spin_unlock_irqrestore(&phba->pport->work_port_lock, flags);
  327. if (!evt_posted)
  328. lpfc_worker_wake_up(phba);
  329. return;
  330. }
  331. /**
  332. * lpfc_ramp_down_queue_handler - WORKER_RAMP_DOWN_QUEUE event handler
  333. * @phba: The Hba for which this call is being executed.
  334. *
  335. * This routine is called to process WORKER_RAMP_DOWN_QUEUE event for worker
  336. * thread.This routine reduces queue depth for all scsi device on each vport
  337. * associated with @phba.
  338. **/
  339. void
  340. lpfc_ramp_down_queue_handler(struct lpfc_hba *phba)
  341. {
  342. struct lpfc_vport **vports;
  343. struct Scsi_Host *shost;
  344. struct scsi_device *sdev;
  345. unsigned long new_queue_depth;
  346. unsigned long num_rsrc_err, num_cmd_success;
  347. int i;
  348. num_rsrc_err = atomic_read(&phba->num_rsrc_err);
  349. num_cmd_success = atomic_read(&phba->num_cmd_success);
  350. vports = lpfc_create_vport_work_array(phba);
  351. if (vports != NULL)
  352. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  353. shost = lpfc_shost_from_vport(vports[i]);
  354. shost_for_each_device(sdev, shost) {
  355. new_queue_depth =
  356. sdev->queue_depth * num_rsrc_err /
  357. (num_rsrc_err + num_cmd_success);
  358. if (!new_queue_depth)
  359. new_queue_depth = sdev->queue_depth - 1;
  360. else
  361. new_queue_depth = sdev->queue_depth -
  362. new_queue_depth;
  363. lpfc_change_queue_depth(sdev, new_queue_depth,
  364. SCSI_QDEPTH_DEFAULT);
  365. }
  366. }
  367. lpfc_destroy_vport_work_array(phba, vports);
  368. atomic_set(&phba->num_rsrc_err, 0);
  369. atomic_set(&phba->num_cmd_success, 0);
  370. }
  371. /**
  372. * lpfc_ramp_up_queue_handler - WORKER_RAMP_UP_QUEUE event handler
  373. * @phba: The Hba for which this call is being executed.
  374. *
  375. * This routine is called to process WORKER_RAMP_UP_QUEUE event for worker
  376. * thread.This routine increases queue depth for all scsi device on each vport
  377. * associated with @phba by 1. This routine also sets @phba num_rsrc_err and
  378. * num_cmd_success to zero.
  379. **/
  380. void
  381. lpfc_ramp_up_queue_handler(struct lpfc_hba *phba)
  382. {
  383. struct lpfc_vport **vports;
  384. struct Scsi_Host *shost;
  385. struct scsi_device *sdev;
  386. int i;
  387. vports = lpfc_create_vport_work_array(phba);
  388. if (vports != NULL)
  389. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  390. shost = lpfc_shost_from_vport(vports[i]);
  391. shost_for_each_device(sdev, shost) {
  392. if (vports[i]->cfg_lun_queue_depth <=
  393. sdev->queue_depth)
  394. continue;
  395. lpfc_change_queue_depth(sdev,
  396. sdev->queue_depth+1,
  397. SCSI_QDEPTH_RAMP_UP);
  398. }
  399. }
  400. lpfc_destroy_vport_work_array(phba, vports);
  401. atomic_set(&phba->num_rsrc_err, 0);
  402. atomic_set(&phba->num_cmd_success, 0);
  403. }
  404. /**
  405. * lpfc_scsi_dev_block - set all scsi hosts to block state
  406. * @phba: Pointer to HBA context object.
  407. *
  408. * This function walks vport list and set each SCSI host to block state
  409. * by invoking fc_remote_port_delete() routine. This function is invoked
  410. * with EEH when device's PCI slot has been permanently disabled.
  411. **/
  412. void
  413. lpfc_scsi_dev_block(struct lpfc_hba *phba)
  414. {
  415. struct lpfc_vport **vports;
  416. struct Scsi_Host *shost;
  417. struct scsi_device *sdev;
  418. struct fc_rport *rport;
  419. int i;
  420. vports = lpfc_create_vport_work_array(phba);
  421. if (vports != NULL)
  422. for (i = 0; i <= phba->max_vports && vports[i] != NULL; i++) {
  423. shost = lpfc_shost_from_vport(vports[i]);
  424. shost_for_each_device(sdev, shost) {
  425. rport = starget_to_rport(scsi_target(sdev));
  426. fc_remote_port_delete(rport);
  427. }
  428. }
  429. lpfc_destroy_vport_work_array(phba, vports);
  430. }
  431. /**
  432. * lpfc_new_scsi_buf_s3 - Scsi buffer allocator for HBA with SLI3 IF spec
  433. * @vport: The virtual port for which this call being executed.
  434. * @num_to_allocate: The requested number of buffers to allocate.
  435. *
  436. * This routine allocates a scsi buffer for device with SLI-3 interface spec,
  437. * the scsi buffer contains all the necessary information needed to initiate
  438. * a SCSI I/O. The non-DMAable buffer region contains information to build
  439. * the IOCB. The DMAable region contains memory for the FCP CMND, FCP RSP,
  440. * and the initial BPL. In addition to allocating memory, the FCP CMND and
  441. * FCP RSP BDEs are setup in the BPL and the BPL BDE is setup in the IOCB.
  442. *
  443. * Return codes:
  444. * int - number of scsi buffers that were allocated.
  445. * 0 = failure, less than num_to_alloc is a partial failure.
  446. **/
  447. static int
  448. lpfc_new_scsi_buf_s3(struct lpfc_vport *vport, int num_to_alloc)
  449. {
  450. struct lpfc_hba *phba = vport->phba;
  451. struct lpfc_scsi_buf *psb;
  452. struct ulp_bde64 *bpl;
  453. IOCB_t *iocb;
  454. dma_addr_t pdma_phys_fcp_cmd;
  455. dma_addr_t pdma_phys_fcp_rsp;
  456. dma_addr_t pdma_phys_bpl;
  457. uint16_t iotag;
  458. int bcnt;
  459. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  460. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  461. if (!psb)
  462. break;
  463. /*
  464. * Get memory from the pci pool to map the virt space to pci
  465. * bus space for an I/O. The DMA buffer includes space for the
  466. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  467. * necessary to support the sg_tablesize.
  468. */
  469. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  470. GFP_KERNEL, &psb->dma_handle);
  471. if (!psb->data) {
  472. kfree(psb);
  473. break;
  474. }
  475. /* Initialize virtual ptrs to dma_buf region. */
  476. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  477. /* Allocate iotag for psb->cur_iocbq. */
  478. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  479. if (iotag == 0) {
  480. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  481. psb->data, psb->dma_handle);
  482. kfree(psb);
  483. break;
  484. }
  485. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  486. psb->fcp_cmnd = psb->data;
  487. psb->fcp_rsp = psb->data + sizeof(struct fcp_cmnd);
  488. psb->fcp_bpl = psb->data + sizeof(struct fcp_cmnd) +
  489. sizeof(struct fcp_rsp);
  490. /* Initialize local short-hand pointers. */
  491. bpl = psb->fcp_bpl;
  492. pdma_phys_fcp_cmd = psb->dma_handle;
  493. pdma_phys_fcp_rsp = psb->dma_handle + sizeof(struct fcp_cmnd);
  494. pdma_phys_bpl = psb->dma_handle + sizeof(struct fcp_cmnd) +
  495. sizeof(struct fcp_rsp);
  496. /*
  497. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  498. * are sg list bdes. Initialize the first two and leave the
  499. * rest for queuecommand.
  500. */
  501. bpl[0].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_cmd));
  502. bpl[0].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_cmd));
  503. bpl[0].tus.f.bdeSize = sizeof(struct fcp_cmnd);
  504. bpl[0].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  505. bpl[0].tus.w = le32_to_cpu(bpl[0].tus.w);
  506. /* Setup the physical region for the FCP RSP */
  507. bpl[1].addrHigh = le32_to_cpu(putPaddrHigh(pdma_phys_fcp_rsp));
  508. bpl[1].addrLow = le32_to_cpu(putPaddrLow(pdma_phys_fcp_rsp));
  509. bpl[1].tus.f.bdeSize = sizeof(struct fcp_rsp);
  510. bpl[1].tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  511. bpl[1].tus.w = le32_to_cpu(bpl[1].tus.w);
  512. /*
  513. * Since the IOCB for the FCP I/O is built into this
  514. * lpfc_scsi_buf, initialize it with all known data now.
  515. */
  516. iocb = &psb->cur_iocbq.iocb;
  517. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  518. if ((phba->sli_rev == 3) &&
  519. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  520. /* fill in immediate fcp command BDE */
  521. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_IMMED;
  522. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  523. iocb->un.fcpi64.bdl.addrLow = offsetof(IOCB_t,
  524. unsli3.fcp_ext.icd);
  525. iocb->un.fcpi64.bdl.addrHigh = 0;
  526. iocb->ulpBdeCount = 0;
  527. iocb->ulpLe = 0;
  528. /* fill in response BDE */
  529. iocb->unsli3.fcp_ext.rbde.tus.f.bdeFlags =
  530. BUFF_TYPE_BDE_64;
  531. iocb->unsli3.fcp_ext.rbde.tus.f.bdeSize =
  532. sizeof(struct fcp_rsp);
  533. iocb->unsli3.fcp_ext.rbde.addrLow =
  534. putPaddrLow(pdma_phys_fcp_rsp);
  535. iocb->unsli3.fcp_ext.rbde.addrHigh =
  536. putPaddrHigh(pdma_phys_fcp_rsp);
  537. } else {
  538. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BLP_64;
  539. iocb->un.fcpi64.bdl.bdeSize =
  540. (2 * sizeof(struct ulp_bde64));
  541. iocb->un.fcpi64.bdl.addrLow =
  542. putPaddrLow(pdma_phys_bpl);
  543. iocb->un.fcpi64.bdl.addrHigh =
  544. putPaddrHigh(pdma_phys_bpl);
  545. iocb->ulpBdeCount = 1;
  546. iocb->ulpLe = 1;
  547. }
  548. iocb->ulpClass = CLASS3;
  549. psb->status = IOSTAT_SUCCESS;
  550. /* Put it back into the SCSI buffer list */
  551. psb->cur_iocbq.context1 = psb;
  552. lpfc_release_scsi_buf_s3(phba, psb);
  553. }
  554. return bcnt;
  555. }
  556. /**
  557. * lpfc_sli4_vport_delete_fcp_xri_aborted -Remove all ndlp references for vport
  558. * @vport: pointer to lpfc vport data structure.
  559. *
  560. * This routine is invoked by the vport cleanup for deletions and the cleanup
  561. * for an ndlp on removal.
  562. **/
  563. void
  564. lpfc_sli4_vport_delete_fcp_xri_aborted(struct lpfc_vport *vport)
  565. {
  566. struct lpfc_hba *phba = vport->phba;
  567. struct lpfc_scsi_buf *psb, *next_psb;
  568. unsigned long iflag = 0;
  569. spin_lock_irqsave(&phba->hbalock, iflag);
  570. spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  571. list_for_each_entry_safe(psb, next_psb,
  572. &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
  573. if (psb->rdata && psb->rdata->pnode
  574. && psb->rdata->pnode->vport == vport)
  575. psb->rdata = NULL;
  576. }
  577. spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  578. spin_unlock_irqrestore(&phba->hbalock, iflag);
  579. }
  580. /**
  581. * lpfc_sli4_fcp_xri_aborted - Fast-path process of fcp xri abort
  582. * @phba: pointer to lpfc hba data structure.
  583. * @axri: pointer to the fcp xri abort wcqe structure.
  584. *
  585. * This routine is invoked by the worker thread to process a SLI4 fast-path
  586. * FCP aborted xri.
  587. **/
  588. void
  589. lpfc_sli4_fcp_xri_aborted(struct lpfc_hba *phba,
  590. struct sli4_wcqe_xri_aborted *axri)
  591. {
  592. uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
  593. uint16_t rxid = bf_get(lpfc_wcqe_xa_remote_xid, axri);
  594. struct lpfc_scsi_buf *psb, *next_psb;
  595. unsigned long iflag = 0;
  596. struct lpfc_iocbq *iocbq;
  597. int i;
  598. struct lpfc_nodelist *ndlp;
  599. int rrq_empty = 0;
  600. struct lpfc_sli_ring *pring = &phba->sli.ring[LPFC_ELS_RING];
  601. spin_lock_irqsave(&phba->hbalock, iflag);
  602. spin_lock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  603. list_for_each_entry_safe(psb, next_psb,
  604. &phba->sli4_hba.lpfc_abts_scsi_buf_list, list) {
  605. if (psb->cur_iocbq.sli4_xritag == xri) {
  606. list_del(&psb->list);
  607. psb->exch_busy = 0;
  608. psb->status = IOSTAT_SUCCESS;
  609. spin_unlock(
  610. &phba->sli4_hba.abts_scsi_buf_list_lock);
  611. if (psb->rdata && psb->rdata->pnode)
  612. ndlp = psb->rdata->pnode;
  613. else
  614. ndlp = NULL;
  615. rrq_empty = list_empty(&phba->active_rrq_list);
  616. spin_unlock_irqrestore(&phba->hbalock, iflag);
  617. if (ndlp) {
  618. lpfc_set_rrq_active(phba, ndlp, xri, rxid, 1);
  619. lpfc_sli4_abts_err_handler(phba, ndlp, axri);
  620. }
  621. lpfc_release_scsi_buf_s4(phba, psb);
  622. if (rrq_empty)
  623. lpfc_worker_wake_up(phba);
  624. return;
  625. }
  626. }
  627. spin_unlock(&phba->sli4_hba.abts_scsi_buf_list_lock);
  628. for (i = 1; i <= phba->sli.last_iotag; i++) {
  629. iocbq = phba->sli.iocbq_lookup[i];
  630. if (!(iocbq->iocb_flag & LPFC_IO_FCP) ||
  631. (iocbq->iocb_flag & LPFC_IO_LIBDFC))
  632. continue;
  633. if (iocbq->sli4_xritag != xri)
  634. continue;
  635. psb = container_of(iocbq, struct lpfc_scsi_buf, cur_iocbq);
  636. psb->exch_busy = 0;
  637. spin_unlock_irqrestore(&phba->hbalock, iflag);
  638. if (pring->txq_cnt)
  639. lpfc_worker_wake_up(phba);
  640. return;
  641. }
  642. spin_unlock_irqrestore(&phba->hbalock, iflag);
  643. }
  644. /**
  645. * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
  646. * @phba: pointer to lpfc hba data structure.
  647. *
  648. * This routine walks the list of scsi buffers that have been allocated and
  649. * repost them to the HBA by using SGL block post. This is needed after a
  650. * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
  651. * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
  652. * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
  653. *
  654. * Returns: 0 = success, non-zero failure.
  655. **/
  656. int
  657. lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
  658. {
  659. struct lpfc_scsi_buf *psb;
  660. int index, status, bcnt = 0, rcnt = 0, rc = 0;
  661. LIST_HEAD(sblist);
  662. for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
  663. psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
  664. if (psb) {
  665. /* Remove from SCSI buffer list */
  666. list_del(&psb->list);
  667. /* Add it to a local SCSI buffer list */
  668. list_add_tail(&psb->list, &sblist);
  669. if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  670. bcnt = rcnt;
  671. rcnt = 0;
  672. }
  673. } else
  674. /* A hole present in the XRI array, need to skip */
  675. bcnt = rcnt;
  676. if (index == phba->sli4_hba.scsi_xri_cnt - 1)
  677. /* End of XRI array for SCSI buffer, complete */
  678. bcnt = rcnt;
  679. /* Continue until collect up to a nembed page worth of sgls */
  680. if (bcnt == 0)
  681. continue;
  682. /* Now, post the SCSI buffer list sgls as a block */
  683. if (!phba->sli4_hba.extents_in_use)
  684. status = lpfc_sli4_post_scsi_sgl_block(phba,
  685. &sblist,
  686. bcnt);
  687. else
  688. status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
  689. &sblist,
  690. bcnt);
  691. /* Reset SCSI buffer count for next round of posting */
  692. bcnt = 0;
  693. while (!list_empty(&sblist)) {
  694. list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
  695. list);
  696. if (status) {
  697. /* Put this back on the abort scsi list */
  698. psb->exch_busy = 1;
  699. rc++;
  700. } else {
  701. psb->exch_busy = 0;
  702. psb->status = IOSTAT_SUCCESS;
  703. }
  704. /* Put it back into the SCSI buffer list */
  705. lpfc_release_scsi_buf_s4(phba, psb);
  706. }
  707. }
  708. return rc;
  709. }
  710. /**
  711. * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
  712. * @vport: The virtual port for which this call being executed.
  713. * @num_to_allocate: The requested number of buffers to allocate.
  714. *
  715. * This routine allocates a scsi buffer for device with SLI-4 interface spec,
  716. * the scsi buffer contains all the necessary information needed to initiate
  717. * a SCSI I/O.
  718. *
  719. * Return codes:
  720. * int - number of scsi buffers that were allocated.
  721. * 0 = failure, less than num_to_alloc is a partial failure.
  722. **/
  723. static int
  724. lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
  725. {
  726. struct lpfc_hba *phba = vport->phba;
  727. struct lpfc_scsi_buf *psb;
  728. struct sli4_sge *sgl;
  729. IOCB_t *iocb;
  730. dma_addr_t pdma_phys_fcp_cmd;
  731. dma_addr_t pdma_phys_fcp_rsp;
  732. dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
  733. uint16_t iotag, last_xritag = NO_XRI, lxri = 0;
  734. int status = 0, index;
  735. int bcnt;
  736. int non_sequential_xri = 0;
  737. LIST_HEAD(sblist);
  738. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  739. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  740. if (!psb)
  741. break;
  742. /*
  743. * Get memory from the pci pool to map the virt space to pci bus
  744. * space for an I/O. The DMA buffer includes space for the
  745. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  746. * necessary to support the sg_tablesize.
  747. */
  748. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  749. GFP_KERNEL, &psb->dma_handle);
  750. if (!psb->data) {
  751. kfree(psb);
  752. break;
  753. }
  754. /* Initialize virtual ptrs to dma_buf region. */
  755. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  756. /* Allocate iotag for psb->cur_iocbq. */
  757. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  758. if (iotag == 0) {
  759. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  760. psb->data, psb->dma_handle);
  761. kfree(psb);
  762. break;
  763. }
  764. lxri = lpfc_sli4_next_xritag(phba);
  765. if (lxri == NO_XRI) {
  766. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  767. psb->data, psb->dma_handle);
  768. kfree(psb);
  769. break;
  770. }
  771. psb->cur_iocbq.sli4_lxritag = lxri;
  772. psb->cur_iocbq.sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  773. if (last_xritag != NO_XRI
  774. && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
  775. non_sequential_xri = 1;
  776. } else
  777. list_add_tail(&psb->list, &sblist);
  778. last_xritag = psb->cur_iocbq.sli4_xritag;
  779. index = phba->sli4_hba.scsi_xri_cnt++;
  780. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  781. psb->fcp_bpl = psb->data;
  782. psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
  783. - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  784. psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
  785. sizeof(struct fcp_cmnd));
  786. /* Initialize local short-hand pointers. */
  787. sgl = (struct sli4_sge *)psb->fcp_bpl;
  788. pdma_phys_bpl = psb->dma_handle;
  789. pdma_phys_fcp_cmd =
  790. (psb->dma_handle + phba->cfg_sg_dma_buf_size)
  791. - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  792. pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
  793. /*
  794. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  795. * are sg list bdes. Initialize the first two and leave the
  796. * rest for queuecommand.
  797. */
  798. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
  799. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
  800. sgl->word2 = le32_to_cpu(sgl->word2);
  801. bf_set(lpfc_sli4_sge_last, sgl, 0);
  802. sgl->word2 = cpu_to_le32(sgl->word2);
  803. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_cmnd));
  804. sgl++;
  805. /* Setup the physical region for the FCP RSP */
  806. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
  807. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
  808. sgl->word2 = le32_to_cpu(sgl->word2);
  809. bf_set(lpfc_sli4_sge_last, sgl, 1);
  810. sgl->word2 = cpu_to_le32(sgl->word2);
  811. sgl->sge_len = cpu_to_le32(sizeof(struct fcp_rsp));
  812. /*
  813. * Since the IOCB for the FCP I/O is built into this
  814. * lpfc_scsi_buf, initialize it with all known data now.
  815. */
  816. iocb = &psb->cur_iocbq.iocb;
  817. iocb->un.fcpi64.bdl.ulpIoTag32 = 0;
  818. iocb->un.fcpi64.bdl.bdeFlags = BUFF_TYPE_BDE_64;
  819. /* setting the BLP size to 2 * sizeof BDE may not be correct.
  820. * We are setting the bpl to point to out sgl. An sgl's
  821. * entries are 16 bytes, a bpl entries are 12 bytes.
  822. */
  823. iocb->un.fcpi64.bdl.bdeSize = sizeof(struct fcp_cmnd);
  824. iocb->un.fcpi64.bdl.addrLow = putPaddrLow(pdma_phys_fcp_cmd);
  825. iocb->un.fcpi64.bdl.addrHigh = putPaddrHigh(pdma_phys_fcp_cmd);
  826. iocb->ulpBdeCount = 1;
  827. iocb->ulpLe = 1;
  828. iocb->ulpClass = CLASS3;
  829. psb->cur_iocbq.context1 = psb;
  830. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  831. pdma_phys_bpl1 = pdma_phys_bpl + SGL_PAGE_SIZE;
  832. else
  833. pdma_phys_bpl1 = 0;
  834. psb->dma_phys_bpl = pdma_phys_bpl;
  835. phba->sli4_hba.lpfc_scsi_psb_array[index] = psb;
  836. if (non_sequential_xri) {
  837. status = lpfc_sli4_post_sgl(phba, pdma_phys_bpl,
  838. pdma_phys_bpl1,
  839. psb->cur_iocbq.sli4_xritag);
  840. if (status) {
  841. /* Put this back on the abort scsi list */
  842. psb->exch_busy = 1;
  843. } else {
  844. psb->exch_busy = 0;
  845. psb->status = IOSTAT_SUCCESS;
  846. }
  847. /* Put it back into the SCSI buffer list */
  848. lpfc_release_scsi_buf_s4(phba, psb);
  849. break;
  850. }
  851. }
  852. if (bcnt) {
  853. if (!phba->sli4_hba.extents_in_use)
  854. status = lpfc_sli4_post_scsi_sgl_block(phba,
  855. &sblist,
  856. bcnt);
  857. else
  858. status = lpfc_sli4_post_scsi_sgl_blk_ext(phba,
  859. &sblist,
  860. bcnt);
  861. if (status) {
  862. lpfc_printf_log(phba, KERN_ERR, LOG_MBOX | LOG_SLI,
  863. "3021 SCSI SGL post error %d\n",
  864. status);
  865. bcnt = 0;
  866. }
  867. /* Reset SCSI buffer count for next round of posting */
  868. while (!list_empty(&sblist)) {
  869. list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
  870. list);
  871. if (status) {
  872. /* Put this back on the abort scsi list */
  873. psb->exch_busy = 1;
  874. } else {
  875. psb->exch_busy = 0;
  876. psb->status = IOSTAT_SUCCESS;
  877. }
  878. /* Put it back into the SCSI buffer list */
  879. lpfc_release_scsi_buf_s4(phba, psb);
  880. }
  881. }
  882. return bcnt + non_sequential_xri;
  883. }
  884. /**
  885. * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
  886. * @vport: The virtual port for which this call being executed.
  887. * @num_to_allocate: The requested number of buffers to allocate.
  888. *
  889. * This routine wraps the actual SCSI buffer allocator function pointer from
  890. * the lpfc_hba struct.
  891. *
  892. * Return codes:
  893. * int - number of scsi buffers that were allocated.
  894. * 0 = failure, less than num_to_alloc is a partial failure.
  895. **/
  896. static inline int
  897. lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
  898. {
  899. return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
  900. }
  901. /**
  902. * lpfc_get_scsi_buf_s3 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  903. * @phba: The HBA for which this call is being executed.
  904. *
  905. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  906. * and returns to caller.
  907. *
  908. * Return codes:
  909. * NULL - Error
  910. * Pointer to lpfc_scsi_buf - Success
  911. **/
  912. static struct lpfc_scsi_buf*
  913. lpfc_get_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  914. {
  915. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  916. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  917. unsigned long iflag = 0;
  918. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  919. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  920. if (lpfc_cmd) {
  921. lpfc_cmd->seg_cnt = 0;
  922. lpfc_cmd->nonsg_phys = 0;
  923. lpfc_cmd->prot_seg_cnt = 0;
  924. }
  925. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  926. return lpfc_cmd;
  927. }
  928. /**
  929. * lpfc_get_scsi_buf_s4 - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  930. * @phba: The HBA for which this call is being executed.
  931. *
  932. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  933. * and returns to caller.
  934. *
  935. * Return codes:
  936. * NULL - Error
  937. * Pointer to lpfc_scsi_buf - Success
  938. **/
  939. static struct lpfc_scsi_buf*
  940. lpfc_get_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  941. {
  942. struct lpfc_scsi_buf *lpfc_cmd ;
  943. unsigned long iflag = 0;
  944. int found = 0;
  945. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  946. list_for_each_entry(lpfc_cmd, &phba->lpfc_scsi_buf_list,
  947. list) {
  948. if (lpfc_test_rrq_active(phba, ndlp,
  949. lpfc_cmd->cur_iocbq.sli4_xritag))
  950. continue;
  951. list_del(&lpfc_cmd->list);
  952. found = 1;
  953. lpfc_cmd->seg_cnt = 0;
  954. lpfc_cmd->nonsg_phys = 0;
  955. lpfc_cmd->prot_seg_cnt = 0;
  956. break;
  957. }
  958. spin_unlock_irqrestore(&phba->scsi_buf_list_lock,
  959. iflag);
  960. if (!found)
  961. return NULL;
  962. else
  963. return lpfc_cmd;
  964. }
  965. /**
  966. * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  967. * @phba: The HBA for which this call is being executed.
  968. *
  969. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  970. * and returns to caller.
  971. *
  972. * Return codes:
  973. * NULL - Error
  974. * Pointer to lpfc_scsi_buf - Success
  975. **/
  976. static struct lpfc_scsi_buf*
  977. lpfc_get_scsi_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp)
  978. {
  979. return phba->lpfc_get_scsi_buf(phba, ndlp);
  980. }
  981. /**
  982. * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
  983. * @phba: The Hba for which this call is being executed.
  984. * @psb: The scsi buffer which is being released.
  985. *
  986. * This routine releases @psb scsi buffer by adding it to tail of @phba
  987. * lpfc_scsi_buf_list list.
  988. **/
  989. static void
  990. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  991. {
  992. unsigned long iflag = 0;
  993. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  994. psb->pCmd = NULL;
  995. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  996. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  997. }
  998. /**
  999. * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
  1000. * @phba: The Hba for which this call is being executed.
  1001. * @psb: The scsi buffer which is being released.
  1002. *
  1003. * This routine releases @psb scsi buffer by adding it to tail of @phba
  1004. * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
  1005. * and cannot be reused for at least RA_TOV amount of time if it was
  1006. * aborted.
  1007. **/
  1008. static void
  1009. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  1010. {
  1011. unsigned long iflag = 0;
  1012. if (psb->exch_busy) {
  1013. spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
  1014. iflag);
  1015. psb->pCmd = NULL;
  1016. list_add_tail(&psb->list,
  1017. &phba->sli4_hba.lpfc_abts_scsi_buf_list);
  1018. spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
  1019. iflag);
  1020. } else {
  1021. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  1022. psb->pCmd = NULL;
  1023. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  1024. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  1025. }
  1026. }
  1027. /**
  1028. * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
  1029. * @phba: The Hba for which this call is being executed.
  1030. * @psb: The scsi buffer which is being released.
  1031. *
  1032. * This routine releases @psb scsi buffer by adding it to tail of @phba
  1033. * lpfc_scsi_buf_list list.
  1034. **/
  1035. static void
  1036. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  1037. {
  1038. phba->lpfc_release_scsi_buf(phba, psb);
  1039. }
  1040. /**
  1041. * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
  1042. * @phba: The Hba for which this call is being executed.
  1043. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  1044. *
  1045. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  1046. * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
  1047. * through sg elements and format the bdea. This routine also initializes all
  1048. * IOCB fields which are dependent on scsi command request buffer.
  1049. *
  1050. * Return codes:
  1051. * 1 - Error
  1052. * 0 - Success
  1053. **/
  1054. static int
  1055. lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  1056. {
  1057. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1058. struct scatterlist *sgel = NULL;
  1059. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1060. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1061. struct lpfc_iocbq *iocbq = &lpfc_cmd->cur_iocbq;
  1062. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1063. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  1064. dma_addr_t physaddr;
  1065. uint32_t num_bde = 0;
  1066. int nseg, datadir = scsi_cmnd->sc_data_direction;
  1067. /*
  1068. * There are three possibilities here - use scatter-gather segment, use
  1069. * the single mapping, or neither. Start the lpfc command prep by
  1070. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1071. * data bde entry.
  1072. */
  1073. bpl += 2;
  1074. if (scsi_sg_count(scsi_cmnd)) {
  1075. /*
  1076. * The driver stores the segment count returned from pci_map_sg
  1077. * because this a count of dma-mappings used to map the use_sg
  1078. * pages. They are not guaranteed to be the same for those
  1079. * architectures that implement an IOMMU.
  1080. */
  1081. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  1082. scsi_sg_count(scsi_cmnd), datadir);
  1083. if (unlikely(!nseg))
  1084. return 1;
  1085. lpfc_cmd->seg_cnt = nseg;
  1086. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1087. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1088. "9064 BLKGRD: %s: Too many sg segments from "
  1089. "dma_map_sg. Config %d, seg_cnt %d\n",
  1090. __func__, phba->cfg_sg_seg_cnt,
  1091. lpfc_cmd->seg_cnt);
  1092. scsi_dma_unmap(scsi_cmnd);
  1093. return 1;
  1094. }
  1095. /*
  1096. * The driver established a maximum scatter-gather segment count
  1097. * during probe that limits the number of sg elements in any
  1098. * single scsi command. Just run through the seg_cnt and format
  1099. * the bde's.
  1100. * When using SLI-3 the driver will try to fit all the BDEs into
  1101. * the IOCB. If it can't then the BDEs get added to a BPL as it
  1102. * does for SLI-2 mode.
  1103. */
  1104. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  1105. physaddr = sg_dma_address(sgel);
  1106. if (phba->sli_rev == 3 &&
  1107. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  1108. !(iocbq->iocb_flag & DSS_SECURITY_OP) &&
  1109. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  1110. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1111. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  1112. data_bde->addrLow = putPaddrLow(physaddr);
  1113. data_bde->addrHigh = putPaddrHigh(physaddr);
  1114. data_bde++;
  1115. } else {
  1116. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1117. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  1118. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1119. bpl->addrLow =
  1120. le32_to_cpu(putPaddrLow(physaddr));
  1121. bpl->addrHigh =
  1122. le32_to_cpu(putPaddrHigh(physaddr));
  1123. bpl++;
  1124. }
  1125. }
  1126. }
  1127. /*
  1128. * Finish initializing those IOCB fields that are dependent on the
  1129. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  1130. * explicitly reinitialized and for SLI-3 the extended bde count is
  1131. * explicitly reinitialized since all iocb memory resources are reused.
  1132. */
  1133. if (phba->sli_rev == 3 &&
  1134. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  1135. !(iocbq->iocb_flag & DSS_SECURITY_OP)) {
  1136. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  1137. /*
  1138. * The extended IOCB format can only fit 3 BDE or a BPL.
  1139. * This I/O has more than 3 BDE so the 1st data bde will
  1140. * be a BPL that is filled in here.
  1141. */
  1142. physaddr = lpfc_cmd->dma_handle;
  1143. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  1144. data_bde->tus.f.bdeSize = (num_bde *
  1145. sizeof(struct ulp_bde64));
  1146. physaddr += (sizeof(struct fcp_cmnd) +
  1147. sizeof(struct fcp_rsp) +
  1148. (2 * sizeof(struct ulp_bde64)));
  1149. data_bde->addrHigh = putPaddrHigh(physaddr);
  1150. data_bde->addrLow = putPaddrLow(physaddr);
  1151. /* ebde count includes the response bde and data bpl */
  1152. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  1153. } else {
  1154. /* ebde count includes the response bde and data bdes */
  1155. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1156. }
  1157. } else {
  1158. iocb_cmd->un.fcpi64.bdl.bdeSize =
  1159. ((num_bde + 2) * sizeof(struct ulp_bde64));
  1160. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1161. }
  1162. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  1163. /*
  1164. * Due to difference in data length between DIF/non-DIF paths,
  1165. * we need to set word 4 of IOCB here
  1166. */
  1167. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  1168. return 0;
  1169. }
  1170. static inline unsigned
  1171. lpfc_cmd_blksize(struct scsi_cmnd *sc)
  1172. {
  1173. return sc->device->sector_size;
  1174. }
  1175. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1176. /*
  1177. * Given a scsi cmnd, determine the BlockGuard tags to be used with it
  1178. * @sc: The SCSI command to examine
  1179. * @reftag: (out) BlockGuard reference tag for transmitted data
  1180. * @apptag: (out) BlockGuard application tag for transmitted data
  1181. * @new_guard (in) Value to replace CRC with if needed
  1182. *
  1183. * Returns (1) if error injection was performed, (0) otherwise
  1184. */
  1185. static int
  1186. lpfc_bg_err_inject(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1187. uint32_t *reftag, uint16_t *apptag, uint32_t new_guard)
  1188. {
  1189. struct scatterlist *sgpe; /* s/g prot entry */
  1190. struct scatterlist *sgde; /* s/g data entry */
  1191. struct scsi_dif_tuple *src;
  1192. uint32_t op = scsi_get_prot_op(sc);
  1193. uint32_t blksize;
  1194. uint32_t numblks;
  1195. sector_t lba;
  1196. int rc = 0;
  1197. if (op == SCSI_PROT_NORMAL)
  1198. return 0;
  1199. lba = scsi_get_lba(sc);
  1200. if (phba->lpfc_injerr_lba != LPFC_INJERR_LBA_OFF) {
  1201. blksize = lpfc_cmd_blksize(sc);
  1202. numblks = (scsi_bufflen(sc) + blksize - 1) / blksize;
  1203. /* Make sure we have the right LBA if one is specified */
  1204. if ((phba->lpfc_injerr_lba < lba) ||
  1205. (phba->lpfc_injerr_lba >= (lba + numblks)))
  1206. return 0;
  1207. }
  1208. sgpe = scsi_prot_sglist(sc);
  1209. sgde = scsi_sglist(sc);
  1210. /* Should we change the Reference Tag */
  1211. if (reftag) {
  1212. /*
  1213. * If we are SCSI_PROT_WRITE_STRIP, the protection data is
  1214. * being stripped from the wire, thus it doesn't matter.
  1215. */
  1216. if ((op == SCSI_PROT_WRITE_PASS) ||
  1217. (op == SCSI_PROT_WRITE_INSERT)) {
  1218. if (phba->lpfc_injerr_wref_cnt) {
  1219. /* DEADBEEF will be the reftag on the wire */
  1220. *reftag = 0xDEADBEEF;
  1221. phba->lpfc_injerr_wref_cnt--;
  1222. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1223. rc = 1;
  1224. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1225. "9081 BLKGRD: Injecting reftag error: "
  1226. "write lba x%lx\n", (unsigned long)lba);
  1227. }
  1228. } else {
  1229. if (phba->lpfc_injerr_rref_cnt) {
  1230. *reftag = 0xDEADBEEF;
  1231. phba->lpfc_injerr_rref_cnt--;
  1232. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1233. rc = 1;
  1234. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1235. "9076 BLKGRD: Injecting reftag error: "
  1236. "read lba x%lx\n", (unsigned long)lba);
  1237. }
  1238. }
  1239. }
  1240. /* Should we change the Application Tag */
  1241. if (apptag) {
  1242. /*
  1243. * If we are SCSI_PROT_WRITE_STRIP, the protection data is
  1244. * being stripped from the wire, thus it doesn't matter.
  1245. */
  1246. if ((op == SCSI_PROT_WRITE_PASS) ||
  1247. (op == SCSI_PROT_WRITE_INSERT)) {
  1248. if (phba->lpfc_injerr_wapp_cnt) {
  1249. /* DEAD will be the apptag on the wire */
  1250. *apptag = 0xDEAD;
  1251. phba->lpfc_injerr_wapp_cnt--;
  1252. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1253. rc = 1;
  1254. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1255. "9077 BLKGRD: Injecting apptag error: "
  1256. "write lba x%lx\n", (unsigned long)lba);
  1257. }
  1258. } else {
  1259. if (phba->lpfc_injerr_rapp_cnt) {
  1260. *apptag = 0xDEAD;
  1261. phba->lpfc_injerr_rapp_cnt--;
  1262. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1263. rc = 1;
  1264. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1265. "9078 BLKGRD: Injecting apptag error: "
  1266. "read lba x%lx\n", (unsigned long)lba);
  1267. }
  1268. }
  1269. }
  1270. /* Should we change the Guard Tag */
  1271. /*
  1272. * If we are SCSI_PROT_WRITE_INSERT, the protection data is
  1273. * being on the wire is being fully generated on the HBA.
  1274. * The host cannot change it or force an error.
  1275. */
  1276. if (((op == SCSI_PROT_WRITE_STRIP) ||
  1277. (op == SCSI_PROT_WRITE_PASS)) &&
  1278. phba->lpfc_injerr_wgrd_cnt) {
  1279. if (sgpe) {
  1280. src = (struct scsi_dif_tuple *)sg_virt(sgpe);
  1281. /*
  1282. * Just inject an error in the first
  1283. * prot block.
  1284. */
  1285. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1286. "9079 BLKGRD: Injecting guard error: "
  1287. "write lba x%lx oldGuard x%x refTag x%x\n",
  1288. (unsigned long)lba, src->guard_tag,
  1289. src->ref_tag);
  1290. src->guard_tag = (uint16_t)new_guard;
  1291. phba->lpfc_injerr_wgrd_cnt--;
  1292. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1293. rc = 1;
  1294. } else {
  1295. blksize = lpfc_cmd_blksize(sc);
  1296. /*
  1297. * Jump past the first data block
  1298. * and inject an error in the
  1299. * prot data. The prot data is already
  1300. * embedded after the regular data.
  1301. */
  1302. src = (struct scsi_dif_tuple *)
  1303. (sg_virt(sgde) + blksize);
  1304. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1305. "9080 BLKGRD: Injecting guard error: "
  1306. "write lba x%lx oldGuard x%x refTag x%x\n",
  1307. (unsigned long)lba, src->guard_tag,
  1308. src->ref_tag);
  1309. src->guard_tag = (uint16_t)new_guard;
  1310. phba->lpfc_injerr_wgrd_cnt--;
  1311. phba->lpfc_injerr_lba = LPFC_INJERR_LBA_OFF;
  1312. rc = 1;
  1313. }
  1314. }
  1315. return rc;
  1316. }
  1317. #endif
  1318. /*
  1319. * Given a scsi cmnd, determine the BlockGuard opcodes to be used with it
  1320. * @sc: The SCSI command to examine
  1321. * @txopt: (out) BlockGuard operation for transmitted data
  1322. * @rxopt: (out) BlockGuard operation for received data
  1323. *
  1324. * Returns: zero on success; non-zero if tx and/or rx op cannot be determined
  1325. *
  1326. */
  1327. static int
  1328. lpfc_sc_to_bg_opcodes(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1329. uint8_t *txop, uint8_t *rxop)
  1330. {
  1331. uint8_t guard_type = scsi_host_get_guard(sc->device->host);
  1332. uint8_t ret = 0;
  1333. if (guard_type == SHOST_DIX_GUARD_IP) {
  1334. switch (scsi_get_prot_op(sc)) {
  1335. case SCSI_PROT_READ_INSERT:
  1336. case SCSI_PROT_WRITE_STRIP:
  1337. *txop = BG_OP_IN_CSUM_OUT_NODIF;
  1338. *rxop = BG_OP_IN_NODIF_OUT_CSUM;
  1339. break;
  1340. case SCSI_PROT_READ_STRIP:
  1341. case SCSI_PROT_WRITE_INSERT:
  1342. *txop = BG_OP_IN_NODIF_OUT_CRC;
  1343. *rxop = BG_OP_IN_CRC_OUT_NODIF;
  1344. break;
  1345. case SCSI_PROT_READ_PASS:
  1346. case SCSI_PROT_WRITE_PASS:
  1347. *txop = BG_OP_IN_CSUM_OUT_CRC;
  1348. *rxop = BG_OP_IN_CRC_OUT_CSUM;
  1349. break;
  1350. case SCSI_PROT_NORMAL:
  1351. default:
  1352. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1353. "9063 BLKGRD: Bad op/guard:%d/IP combination\n",
  1354. scsi_get_prot_op(sc));
  1355. ret = 1;
  1356. break;
  1357. }
  1358. } else {
  1359. switch (scsi_get_prot_op(sc)) {
  1360. case SCSI_PROT_READ_STRIP:
  1361. case SCSI_PROT_WRITE_INSERT:
  1362. *txop = BG_OP_IN_NODIF_OUT_CRC;
  1363. *rxop = BG_OP_IN_CRC_OUT_NODIF;
  1364. break;
  1365. case SCSI_PROT_READ_PASS:
  1366. case SCSI_PROT_WRITE_PASS:
  1367. *txop = BG_OP_IN_CRC_OUT_CRC;
  1368. *rxop = BG_OP_IN_CRC_OUT_CRC;
  1369. break;
  1370. case SCSI_PROT_READ_INSERT:
  1371. case SCSI_PROT_WRITE_STRIP:
  1372. *txop = BG_OP_IN_CRC_OUT_NODIF;
  1373. *rxop = BG_OP_IN_NODIF_OUT_CRC;
  1374. break;
  1375. case SCSI_PROT_NORMAL:
  1376. default:
  1377. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1378. "9075 BLKGRD: Bad op/guard:%d/CRC combination\n",
  1379. scsi_get_prot_op(sc));
  1380. ret = 1;
  1381. break;
  1382. }
  1383. }
  1384. return ret;
  1385. }
  1386. /*
  1387. * This function sets up buffer list for protection groups of
  1388. * type LPFC_PG_TYPE_NO_DIF
  1389. *
  1390. * This is usually used when the HBA is instructed to generate
  1391. * DIFs and insert them into data stream (or strip DIF from
  1392. * incoming data stream)
  1393. *
  1394. * The buffer list consists of just one protection group described
  1395. * below:
  1396. * +-------------------------+
  1397. * start of prot group --> | PDE_5 |
  1398. * +-------------------------+
  1399. * | PDE_6 |
  1400. * +-------------------------+
  1401. * | Data BDE |
  1402. * +-------------------------+
  1403. * |more Data BDE's ... (opt)|
  1404. * +-------------------------+
  1405. *
  1406. * @sc: pointer to scsi command we're working on
  1407. * @bpl: pointer to buffer list for protection groups
  1408. * @datacnt: number of segments of data that have been dma mapped
  1409. *
  1410. * Note: Data s/g buffers have been dma mapped
  1411. */
  1412. static int
  1413. lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1414. struct ulp_bde64 *bpl, int datasegcnt)
  1415. {
  1416. struct scatterlist *sgde = NULL; /* s/g data entry */
  1417. struct lpfc_pde5 *pde5 = NULL;
  1418. struct lpfc_pde6 *pde6 = NULL;
  1419. dma_addr_t physaddr;
  1420. int i = 0, num_bde = 0, status;
  1421. int datadir = sc->sc_data_direction;
  1422. uint32_t reftag;
  1423. unsigned blksize;
  1424. uint8_t txop, rxop;
  1425. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1426. if (status)
  1427. goto out;
  1428. /* extract some info from the scsi command for pde*/
  1429. blksize = lpfc_cmd_blksize(sc);
  1430. reftag = scsi_get_lba(sc) & 0xffffffff;
  1431. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1432. /* reftag is the only error we can inject here */
  1433. lpfc_bg_err_inject(phba, sc, &reftag, 0, 0);
  1434. #endif
  1435. /* setup PDE5 with what we have */
  1436. pde5 = (struct lpfc_pde5 *) bpl;
  1437. memset(pde5, 0, sizeof(struct lpfc_pde5));
  1438. bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
  1439. /* Endianness conversion if necessary for PDE5 */
  1440. pde5->word0 = cpu_to_le32(pde5->word0);
  1441. pde5->reftag = cpu_to_le32(reftag);
  1442. /* advance bpl and increment bde count */
  1443. num_bde++;
  1444. bpl++;
  1445. pde6 = (struct lpfc_pde6 *) bpl;
  1446. /* setup PDE6 with the rest of the info */
  1447. memset(pde6, 0, sizeof(struct lpfc_pde6));
  1448. bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
  1449. bf_set(pde6_optx, pde6, txop);
  1450. bf_set(pde6_oprx, pde6, rxop);
  1451. if (datadir == DMA_FROM_DEVICE) {
  1452. bf_set(pde6_ce, pde6, 1);
  1453. bf_set(pde6_re, pde6, 1);
  1454. }
  1455. bf_set(pde6_ai, pde6, 1);
  1456. bf_set(pde6_ae, pde6, 0);
  1457. bf_set(pde6_apptagval, pde6, 0);
  1458. /* Endianness conversion if necessary for PDE6 */
  1459. pde6->word0 = cpu_to_le32(pde6->word0);
  1460. pde6->word1 = cpu_to_le32(pde6->word1);
  1461. pde6->word2 = cpu_to_le32(pde6->word2);
  1462. /* advance bpl and increment bde count */
  1463. num_bde++;
  1464. bpl++;
  1465. /* assumption: caller has already run dma_map_sg on command data */
  1466. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  1467. physaddr = sg_dma_address(sgde);
  1468. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1469. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1470. bpl->tus.f.bdeSize = sg_dma_len(sgde);
  1471. if (datadir == DMA_TO_DEVICE)
  1472. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1473. else
  1474. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1475. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1476. bpl++;
  1477. num_bde++;
  1478. }
  1479. out:
  1480. return num_bde;
  1481. }
  1482. /*
  1483. * This function sets up buffer list for protection groups of
  1484. * type LPFC_PG_TYPE_DIF_BUF
  1485. *
  1486. * This is usually used when DIFs are in their own buffers,
  1487. * separate from the data. The HBA can then by instructed
  1488. * to place the DIFs in the outgoing stream. For read operations,
  1489. * The HBA could extract the DIFs and place it in DIF buffers.
  1490. *
  1491. * The buffer list for this type consists of one or more of the
  1492. * protection groups described below:
  1493. * +-------------------------+
  1494. * start of first prot group --> | PDE_5 |
  1495. * +-------------------------+
  1496. * | PDE_6 |
  1497. * +-------------------------+
  1498. * | PDE_7 (Prot BDE) |
  1499. * +-------------------------+
  1500. * | Data BDE |
  1501. * +-------------------------+
  1502. * |more Data BDE's ... (opt)|
  1503. * +-------------------------+
  1504. * start of new prot group --> | PDE_5 |
  1505. * +-------------------------+
  1506. * | ... |
  1507. * +-------------------------+
  1508. *
  1509. * @sc: pointer to scsi command we're working on
  1510. * @bpl: pointer to buffer list for protection groups
  1511. * @datacnt: number of segments of data that have been dma mapped
  1512. * @protcnt: number of segment of protection data that have been dma mapped
  1513. *
  1514. * Note: It is assumed that both data and protection s/g buffers have been
  1515. * mapped for DMA
  1516. */
  1517. static int
  1518. lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1519. struct ulp_bde64 *bpl, int datacnt, int protcnt)
  1520. {
  1521. struct scatterlist *sgde = NULL; /* s/g data entry */
  1522. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  1523. struct lpfc_pde5 *pde5 = NULL;
  1524. struct lpfc_pde6 *pde6 = NULL;
  1525. struct lpfc_pde7 *pde7 = NULL;
  1526. dma_addr_t dataphysaddr, protphysaddr;
  1527. unsigned short curr_data = 0, curr_prot = 0;
  1528. unsigned int split_offset;
  1529. unsigned int protgroup_len, protgroup_offset = 0, protgroup_remainder;
  1530. unsigned int protgrp_blks, protgrp_bytes;
  1531. unsigned int remainder, subtotal;
  1532. int status;
  1533. int datadir = sc->sc_data_direction;
  1534. unsigned char pgdone = 0, alldone = 0;
  1535. unsigned blksize;
  1536. uint32_t reftag;
  1537. uint8_t txop, rxop;
  1538. int num_bde = 0;
  1539. sgpe = scsi_prot_sglist(sc);
  1540. sgde = scsi_sglist(sc);
  1541. if (!sgpe || !sgde) {
  1542. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1543. "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
  1544. sgpe, sgde);
  1545. return 0;
  1546. }
  1547. status = lpfc_sc_to_bg_opcodes(phba, sc, &txop, &rxop);
  1548. if (status)
  1549. goto out;
  1550. /* extract some info from the scsi command */
  1551. blksize = lpfc_cmd_blksize(sc);
  1552. reftag = scsi_get_lba(sc) & 0xffffffff;
  1553. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1554. /* reftag / guard tag are the only errors we can inject here */
  1555. lpfc_bg_err_inject(phba, sc, &reftag, 0, 0xDEAD);
  1556. #endif
  1557. split_offset = 0;
  1558. do {
  1559. /* setup PDE5 with what we have */
  1560. pde5 = (struct lpfc_pde5 *) bpl;
  1561. memset(pde5, 0, sizeof(struct lpfc_pde5));
  1562. bf_set(pde5_type, pde5, LPFC_PDE5_DESCRIPTOR);
  1563. /* Endianness conversion if necessary for PDE5 */
  1564. pde5->word0 = cpu_to_le32(pde5->word0);
  1565. pde5->reftag = cpu_to_le32(reftag);
  1566. /* advance bpl and increment bde count */
  1567. num_bde++;
  1568. bpl++;
  1569. pde6 = (struct lpfc_pde6 *) bpl;
  1570. /* setup PDE6 with the rest of the info */
  1571. memset(pde6, 0, sizeof(struct lpfc_pde6));
  1572. bf_set(pde6_type, pde6, LPFC_PDE6_DESCRIPTOR);
  1573. bf_set(pde6_optx, pde6, txop);
  1574. bf_set(pde6_oprx, pde6, rxop);
  1575. bf_set(pde6_ce, pde6, 1);
  1576. bf_set(pde6_re, pde6, 1);
  1577. bf_set(pde6_ai, pde6, 1);
  1578. bf_set(pde6_ae, pde6, 0);
  1579. bf_set(pde6_apptagval, pde6, 0);
  1580. /* Endianness conversion if necessary for PDE6 */
  1581. pde6->word0 = cpu_to_le32(pde6->word0);
  1582. pde6->word1 = cpu_to_le32(pde6->word1);
  1583. pde6->word2 = cpu_to_le32(pde6->word2);
  1584. /* advance bpl and increment bde count */
  1585. num_bde++;
  1586. bpl++;
  1587. /* setup the first BDE that points to protection buffer */
  1588. protphysaddr = sg_dma_address(sgpe) + protgroup_offset;
  1589. protgroup_len = sg_dma_len(sgpe) - protgroup_offset;
  1590. /* must be integer multiple of the DIF block length */
  1591. BUG_ON(protgroup_len % 8);
  1592. pde7 = (struct lpfc_pde7 *) bpl;
  1593. memset(pde7, 0, sizeof(struct lpfc_pde7));
  1594. bf_set(pde7_type, pde7, LPFC_PDE7_DESCRIPTOR);
  1595. pde7->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
  1596. pde7->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
  1597. protgrp_blks = protgroup_len / 8;
  1598. protgrp_bytes = protgrp_blks * blksize;
  1599. /* check if this pde is crossing the 4K boundary; if so split */
  1600. if ((pde7->addrLow & 0xfff) + protgroup_len > 0x1000) {
  1601. protgroup_remainder = 0x1000 - (pde7->addrLow & 0xfff);
  1602. protgroup_offset += protgroup_remainder;
  1603. protgrp_blks = protgroup_remainder / 8;
  1604. protgrp_bytes = protgrp_blks * blksize;
  1605. } else {
  1606. protgroup_offset = 0;
  1607. curr_prot++;
  1608. }
  1609. num_bde++;
  1610. /* setup BDE's for data blocks associated with DIF data */
  1611. pgdone = 0;
  1612. subtotal = 0; /* total bytes processed for current prot grp */
  1613. while (!pgdone) {
  1614. if (!sgde) {
  1615. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1616. "9065 BLKGRD:%s Invalid data segment\n",
  1617. __func__);
  1618. return 0;
  1619. }
  1620. bpl++;
  1621. dataphysaddr = sg_dma_address(sgde) + split_offset;
  1622. bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
  1623. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
  1624. remainder = sg_dma_len(sgde) - split_offset;
  1625. if ((subtotal + remainder) <= protgrp_bytes) {
  1626. /* we can use this whole buffer */
  1627. bpl->tus.f.bdeSize = remainder;
  1628. split_offset = 0;
  1629. if ((subtotal + remainder) == protgrp_bytes)
  1630. pgdone = 1;
  1631. } else {
  1632. /* must split this buffer with next prot grp */
  1633. bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
  1634. split_offset += bpl->tus.f.bdeSize;
  1635. }
  1636. subtotal += bpl->tus.f.bdeSize;
  1637. if (datadir == DMA_TO_DEVICE)
  1638. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1639. else
  1640. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1641. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1642. num_bde++;
  1643. curr_data++;
  1644. if (split_offset)
  1645. break;
  1646. /* Move to the next s/g segment if possible */
  1647. sgde = sg_next(sgde);
  1648. }
  1649. if (protgroup_offset) {
  1650. /* update the reference tag */
  1651. reftag += protgrp_blks;
  1652. bpl++;
  1653. continue;
  1654. }
  1655. /* are we done ? */
  1656. if (curr_prot == protcnt) {
  1657. alldone = 1;
  1658. } else if (curr_prot < protcnt) {
  1659. /* advance to next prot buffer */
  1660. sgpe = sg_next(sgpe);
  1661. bpl++;
  1662. /* update the reference tag */
  1663. reftag += protgrp_blks;
  1664. } else {
  1665. /* if we're here, we have a bug */
  1666. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1667. "9054 BLKGRD: bug in %s\n", __func__);
  1668. }
  1669. } while (!alldone);
  1670. out:
  1671. return num_bde;
  1672. }
  1673. /*
  1674. * Given a SCSI command that supports DIF, determine composition of protection
  1675. * groups involved in setting up buffer lists
  1676. *
  1677. * Returns:
  1678. * for DIF (for both read and write)
  1679. * */
  1680. static int
  1681. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  1682. {
  1683. int ret = LPFC_PG_TYPE_INVALID;
  1684. unsigned char op = scsi_get_prot_op(sc);
  1685. switch (op) {
  1686. case SCSI_PROT_READ_STRIP:
  1687. case SCSI_PROT_WRITE_INSERT:
  1688. ret = LPFC_PG_TYPE_NO_DIF;
  1689. break;
  1690. case SCSI_PROT_READ_INSERT:
  1691. case SCSI_PROT_WRITE_STRIP:
  1692. case SCSI_PROT_READ_PASS:
  1693. case SCSI_PROT_WRITE_PASS:
  1694. ret = LPFC_PG_TYPE_DIF_BUF;
  1695. break;
  1696. default:
  1697. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1698. "9021 Unsupported protection op:%d\n", op);
  1699. break;
  1700. }
  1701. return ret;
  1702. }
  1703. /*
  1704. * This is the protection/DIF aware version of
  1705. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  1706. * two functions eventually, but for now, it's here
  1707. */
  1708. static int
  1709. lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
  1710. struct lpfc_scsi_buf *lpfc_cmd)
  1711. {
  1712. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1713. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1714. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1715. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1716. uint32_t num_bde = 0;
  1717. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  1718. int prot_group_type = 0;
  1719. int diflen, fcpdl;
  1720. unsigned blksize;
  1721. /*
  1722. * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
  1723. * fcp_rsp regions to the first data bde entry
  1724. */
  1725. bpl += 2;
  1726. if (scsi_sg_count(scsi_cmnd)) {
  1727. /*
  1728. * The driver stores the segment count returned from pci_map_sg
  1729. * because this a count of dma-mappings used to map the use_sg
  1730. * pages. They are not guaranteed to be the same for those
  1731. * architectures that implement an IOMMU.
  1732. */
  1733. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  1734. scsi_sglist(scsi_cmnd),
  1735. scsi_sg_count(scsi_cmnd), datadir);
  1736. if (unlikely(!datasegcnt))
  1737. return 1;
  1738. lpfc_cmd->seg_cnt = datasegcnt;
  1739. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1740. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1741. "9067 BLKGRD: %s: Too many sg segments"
  1742. " from dma_map_sg. Config %d, seg_cnt"
  1743. " %d\n",
  1744. __func__, phba->cfg_sg_seg_cnt,
  1745. lpfc_cmd->seg_cnt);
  1746. scsi_dma_unmap(scsi_cmnd);
  1747. return 1;
  1748. }
  1749. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  1750. switch (prot_group_type) {
  1751. case LPFC_PG_TYPE_NO_DIF:
  1752. num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
  1753. datasegcnt);
  1754. /* we should have 2 or more entries in buffer list */
  1755. if (num_bde < 2)
  1756. goto err;
  1757. break;
  1758. case LPFC_PG_TYPE_DIF_BUF:{
  1759. /*
  1760. * This type indicates that protection buffers are
  1761. * passed to the driver, so that needs to be prepared
  1762. * for DMA
  1763. */
  1764. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  1765. scsi_prot_sglist(scsi_cmnd),
  1766. scsi_prot_sg_count(scsi_cmnd), datadir);
  1767. if (unlikely(!protsegcnt)) {
  1768. scsi_dma_unmap(scsi_cmnd);
  1769. return 1;
  1770. }
  1771. lpfc_cmd->prot_seg_cnt = protsegcnt;
  1772. if (lpfc_cmd->prot_seg_cnt
  1773. > phba->cfg_prot_sg_seg_cnt) {
  1774. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1775. "9068 BLKGRD: %s: Too many prot sg "
  1776. "segments from dma_map_sg. Config %d,"
  1777. "prot_seg_cnt %d\n", __func__,
  1778. phba->cfg_prot_sg_seg_cnt,
  1779. lpfc_cmd->prot_seg_cnt);
  1780. dma_unmap_sg(&phba->pcidev->dev,
  1781. scsi_prot_sglist(scsi_cmnd),
  1782. scsi_prot_sg_count(scsi_cmnd),
  1783. datadir);
  1784. scsi_dma_unmap(scsi_cmnd);
  1785. return 1;
  1786. }
  1787. num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
  1788. datasegcnt, protsegcnt);
  1789. /* we should have 3 or more entries in buffer list */
  1790. if (num_bde < 3)
  1791. goto err;
  1792. break;
  1793. }
  1794. case LPFC_PG_TYPE_INVALID:
  1795. default:
  1796. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1797. "9022 Unexpected protection group %i\n",
  1798. prot_group_type);
  1799. return 1;
  1800. }
  1801. }
  1802. /*
  1803. * Finish initializing those IOCB fields that are dependent on the
  1804. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  1805. * reinitialized since all iocb memory resources are used many times
  1806. * for transmit, receive, and continuation bpl's.
  1807. */
  1808. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  1809. iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
  1810. iocb_cmd->ulpBdeCount = 1;
  1811. iocb_cmd->ulpLe = 1;
  1812. fcpdl = scsi_bufflen(scsi_cmnd);
  1813. if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
  1814. /*
  1815. * We are in DIF Type 1 mode
  1816. * Every data block has a 8 byte DIF (trailer)
  1817. * attached to it. Must ajust FCP data length
  1818. */
  1819. blksize = lpfc_cmd_blksize(scsi_cmnd);
  1820. diflen = (fcpdl / blksize) * 8;
  1821. fcpdl += diflen;
  1822. }
  1823. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  1824. /*
  1825. * Due to difference in data length between DIF/non-DIF paths,
  1826. * we need to set word 4 of IOCB here
  1827. */
  1828. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  1829. return 0;
  1830. err:
  1831. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1832. "9023 Could not setup all needed BDE's"
  1833. "prot_group_type=%d, num_bde=%d\n",
  1834. prot_group_type, num_bde);
  1835. return 1;
  1836. }
  1837. /*
  1838. * This function checks for BlockGuard errors detected by
  1839. * the HBA. In case of errors, the ASC/ASCQ fields in the
  1840. * sense buffer will be set accordingly, paired with
  1841. * ILLEGAL_REQUEST to signal to the kernel that the HBA
  1842. * detected corruption.
  1843. *
  1844. * Returns:
  1845. * 0 - No error found
  1846. * 1 - BlockGuard error found
  1847. * -1 - Internal error (bad profile, ...etc)
  1848. */
  1849. static int
  1850. lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
  1851. struct lpfc_iocbq *pIocbOut)
  1852. {
  1853. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  1854. struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
  1855. int ret = 0;
  1856. uint32_t bghm = bgf->bghm;
  1857. uint32_t bgstat = bgf->bgstat;
  1858. uint64_t failing_sector = 0;
  1859. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
  1860. " 0x%x lba 0x%llx blk cnt 0x%x "
  1861. "bgstat=0x%x bghm=0x%x\n",
  1862. cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
  1863. blk_rq_sectors(cmd->request), bgstat, bghm);
  1864. spin_lock(&_dump_buf_lock);
  1865. if (!_dump_buf_done) {
  1866. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
  1867. " Data for %u blocks to debugfs\n",
  1868. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1869. lpfc_debug_save_data(phba, cmd);
  1870. /* If we have a prot sgl, save the DIF buffer */
  1871. if (lpfc_prot_group_type(phba, cmd) ==
  1872. LPFC_PG_TYPE_DIF_BUF) {
  1873. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
  1874. "Saving DIF for %u blocks to debugfs\n",
  1875. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1876. lpfc_debug_save_dif(phba, cmd);
  1877. }
  1878. _dump_buf_done = 1;
  1879. }
  1880. spin_unlock(&_dump_buf_lock);
  1881. if (lpfc_bgs_get_invalid_prof(bgstat)) {
  1882. cmd->result = ScsiResult(DID_ERROR, 0);
  1883. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
  1884. " BlockGuard profile. bgstat:0x%x\n",
  1885. bgstat);
  1886. ret = (-1);
  1887. goto out;
  1888. }
  1889. if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
  1890. cmd->result = ScsiResult(DID_ERROR, 0);
  1891. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
  1892. "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
  1893. bgstat);
  1894. ret = (-1);
  1895. goto out;
  1896. }
  1897. if (lpfc_bgs_get_guard_err(bgstat)) {
  1898. ret = 1;
  1899. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1900. 0x10, 0x1);
  1901. cmd->result = DRIVER_SENSE << 24
  1902. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1903. phba->bg_guard_err_cnt++;
  1904. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1905. "9055 BLKGRD: guard_tag error\n");
  1906. }
  1907. if (lpfc_bgs_get_reftag_err(bgstat)) {
  1908. ret = 1;
  1909. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1910. 0x10, 0x3);
  1911. cmd->result = DRIVER_SENSE << 24
  1912. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1913. phba->bg_reftag_err_cnt++;
  1914. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1915. "9056 BLKGRD: ref_tag error\n");
  1916. }
  1917. if (lpfc_bgs_get_apptag_err(bgstat)) {
  1918. ret = 1;
  1919. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1920. 0x10, 0x2);
  1921. cmd->result = DRIVER_SENSE << 24
  1922. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1923. phba->bg_apptag_err_cnt++;
  1924. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1925. "9061 BLKGRD: app_tag error\n");
  1926. }
  1927. if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
  1928. /*
  1929. * setup sense data descriptor 0 per SPC-4 as an information
  1930. * field, and put the failing LBA in it.
  1931. * This code assumes there was also a guard/app/ref tag error
  1932. * indication.
  1933. */
  1934. cmd->sense_buffer[7] = 0xc; /* Additional sense length */
  1935. cmd->sense_buffer[8] = 0; /* Information descriptor type */
  1936. cmd->sense_buffer[9] = 0xa; /* Additional descriptor length */
  1937. cmd->sense_buffer[10] = 0x80; /* Validity bit */
  1938. bghm /= cmd->device->sector_size;
  1939. failing_sector = scsi_get_lba(cmd);
  1940. failing_sector += bghm;
  1941. /* Descriptor Information */
  1942. put_unaligned_be64(failing_sector, &cmd->sense_buffer[12]);
  1943. }
  1944. if (!ret) {
  1945. /* No error was reported - problem in FW? */
  1946. cmd->result = ScsiResult(DID_ERROR, 0);
  1947. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1948. "9057 BLKGRD: no errors reported!\n");
  1949. }
  1950. out:
  1951. return ret;
  1952. }
  1953. /**
  1954. * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
  1955. * @phba: The Hba for which this call is being executed.
  1956. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  1957. *
  1958. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  1959. * field of @lpfc_cmd for device with SLI-4 interface spec.
  1960. *
  1961. * Return codes:
  1962. * 1 - Error
  1963. * 0 - Success
  1964. **/
  1965. static int
  1966. lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  1967. {
  1968. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1969. struct scatterlist *sgel = NULL;
  1970. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1971. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  1972. struct sli4_sge *first_data_sgl;
  1973. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1974. dma_addr_t physaddr;
  1975. uint32_t num_bde = 0;
  1976. uint32_t dma_len;
  1977. uint32_t dma_offset = 0;
  1978. int nseg;
  1979. struct ulp_bde64 *bde;
  1980. /*
  1981. * There are three possibilities here - use scatter-gather segment, use
  1982. * the single mapping, or neither. Start the lpfc command prep by
  1983. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1984. * data bde entry.
  1985. */
  1986. if (scsi_sg_count(scsi_cmnd)) {
  1987. /*
  1988. * The driver stores the segment count returned from pci_map_sg
  1989. * because this a count of dma-mappings used to map the use_sg
  1990. * pages. They are not guaranteed to be the same for those
  1991. * architectures that implement an IOMMU.
  1992. */
  1993. nseg = scsi_dma_map(scsi_cmnd);
  1994. if (unlikely(!nseg))
  1995. return 1;
  1996. sgl += 1;
  1997. /* clear the last flag in the fcp_rsp map entry */
  1998. sgl->word2 = le32_to_cpu(sgl->word2);
  1999. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2000. sgl->word2 = cpu_to_le32(sgl->word2);
  2001. sgl += 1;
  2002. first_data_sgl = sgl;
  2003. lpfc_cmd->seg_cnt = nseg;
  2004. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  2005. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
  2006. " %s: Too many sg segments from "
  2007. "dma_map_sg. Config %d, seg_cnt %d\n",
  2008. __func__, phba->cfg_sg_seg_cnt,
  2009. lpfc_cmd->seg_cnt);
  2010. scsi_dma_unmap(scsi_cmnd);
  2011. return 1;
  2012. }
  2013. /*
  2014. * The driver established a maximum scatter-gather segment count
  2015. * during probe that limits the number of sg elements in any
  2016. * single scsi command. Just run through the seg_cnt and format
  2017. * the sge's.
  2018. * When using SLI-3 the driver will try to fit all the BDEs into
  2019. * the IOCB. If it can't then the BDEs get added to a BPL as it
  2020. * does for SLI-2 mode.
  2021. */
  2022. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  2023. physaddr = sg_dma_address(sgel);
  2024. dma_len = sg_dma_len(sgel);
  2025. sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
  2026. sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
  2027. sgl->word2 = le32_to_cpu(sgl->word2);
  2028. if ((num_bde + 1) == nseg)
  2029. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2030. else
  2031. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2032. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  2033. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
  2034. sgl->word2 = cpu_to_le32(sgl->word2);
  2035. sgl->sge_len = cpu_to_le32(dma_len);
  2036. dma_offset += dma_len;
  2037. sgl++;
  2038. }
  2039. /* setup the performance hint (first data BDE) if enabled */
  2040. if (phba->sli3_options & LPFC_SLI4_PERFH_ENABLED) {
  2041. bde = (struct ulp_bde64 *)
  2042. &(iocb_cmd->unsli3.sli3Words[5]);
  2043. bde->addrLow = first_data_sgl->addr_lo;
  2044. bde->addrHigh = first_data_sgl->addr_hi;
  2045. bde->tus.f.bdeSize =
  2046. le32_to_cpu(first_data_sgl->sge_len);
  2047. bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  2048. bde->tus.w = cpu_to_le32(bde->tus.w);
  2049. }
  2050. } else {
  2051. sgl += 1;
  2052. /* clear the last flag in the fcp_rsp map entry */
  2053. sgl->word2 = le32_to_cpu(sgl->word2);
  2054. bf_set(lpfc_sli4_sge_last, sgl, 1);
  2055. sgl->word2 = cpu_to_le32(sgl->word2);
  2056. }
  2057. /*
  2058. * Finish initializing those IOCB fields that are dependent on the
  2059. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  2060. * explicitly reinitialized.
  2061. * all iocb memory resources are reused.
  2062. */
  2063. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  2064. /*
  2065. * Due to difference in data length between DIF/non-DIF paths,
  2066. * we need to set word 4 of IOCB here
  2067. */
  2068. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  2069. return 0;
  2070. }
  2071. /**
  2072. * lpfc_scsi_prep_dma_buf - Wrapper function for DMA mapping of scsi buffer
  2073. * @phba: The Hba for which this call is being executed.
  2074. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  2075. *
  2076. * This routine wraps the actual DMA mapping function pointer from the
  2077. * lpfc_hba struct.
  2078. *
  2079. * Return codes:
  2080. * 1 - Error
  2081. * 0 - Success
  2082. **/
  2083. static inline int
  2084. lpfc_scsi_prep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  2085. {
  2086. return phba->lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  2087. }
  2088. /**
  2089. * lpfc_send_scsi_error_event - Posts an event when there is SCSI error
  2090. * @phba: Pointer to hba context object.
  2091. * @vport: Pointer to vport object.
  2092. * @lpfc_cmd: Pointer to lpfc scsi command which reported the error.
  2093. * @rsp_iocb: Pointer to response iocb object which reported error.
  2094. *
  2095. * This function posts an event when there is a SCSI command reporting
  2096. * error from the scsi device.
  2097. **/
  2098. static void
  2099. lpfc_send_scsi_error_event(struct lpfc_hba *phba, struct lpfc_vport *vport,
  2100. struct lpfc_scsi_buf *lpfc_cmd, struct lpfc_iocbq *rsp_iocb) {
  2101. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  2102. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  2103. uint32_t resp_info = fcprsp->rspStatus2;
  2104. uint32_t scsi_status = fcprsp->rspStatus3;
  2105. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  2106. struct lpfc_fast_path_event *fast_path_evt = NULL;
  2107. struct lpfc_nodelist *pnode = lpfc_cmd->rdata->pnode;
  2108. unsigned long flags;
  2109. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2110. return;
  2111. /* If there is queuefull or busy condition send a scsi event */
  2112. if ((cmnd->result == SAM_STAT_TASK_SET_FULL) ||
  2113. (cmnd->result == SAM_STAT_BUSY)) {
  2114. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2115. if (!fast_path_evt)
  2116. return;
  2117. fast_path_evt->un.scsi_evt.event_type =
  2118. FC_REG_SCSI_EVENT;
  2119. fast_path_evt->un.scsi_evt.subcategory =
  2120. (cmnd->result == SAM_STAT_TASK_SET_FULL) ?
  2121. LPFC_EVENT_QFULL : LPFC_EVENT_DEVBSY;
  2122. fast_path_evt->un.scsi_evt.lun = cmnd->device->lun;
  2123. memcpy(&fast_path_evt->un.scsi_evt.wwpn,
  2124. &pnode->nlp_portname, sizeof(struct lpfc_name));
  2125. memcpy(&fast_path_evt->un.scsi_evt.wwnn,
  2126. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2127. } else if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen &&
  2128. ((cmnd->cmnd[0] == READ_10) || (cmnd->cmnd[0] == WRITE_10))) {
  2129. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2130. if (!fast_path_evt)
  2131. return;
  2132. fast_path_evt->un.check_cond_evt.scsi_event.event_type =
  2133. FC_REG_SCSI_EVENT;
  2134. fast_path_evt->un.check_cond_evt.scsi_event.subcategory =
  2135. LPFC_EVENT_CHECK_COND;
  2136. fast_path_evt->un.check_cond_evt.scsi_event.lun =
  2137. cmnd->device->lun;
  2138. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwpn,
  2139. &pnode->nlp_portname, sizeof(struct lpfc_name));
  2140. memcpy(&fast_path_evt->un.check_cond_evt.scsi_event.wwnn,
  2141. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2142. fast_path_evt->un.check_cond_evt.sense_key =
  2143. cmnd->sense_buffer[2] & 0xf;
  2144. fast_path_evt->un.check_cond_evt.asc = cmnd->sense_buffer[12];
  2145. fast_path_evt->un.check_cond_evt.ascq = cmnd->sense_buffer[13];
  2146. } else if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  2147. fcpi_parm &&
  2148. ((be32_to_cpu(fcprsp->rspResId) != fcpi_parm) ||
  2149. ((scsi_status == SAM_STAT_GOOD) &&
  2150. !(resp_info & (RESID_UNDER | RESID_OVER))))) {
  2151. /*
  2152. * If status is good or resid does not match with fcp_param and
  2153. * there is valid fcpi_parm, then there is a read_check error
  2154. */
  2155. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2156. if (!fast_path_evt)
  2157. return;
  2158. fast_path_evt->un.read_check_error.header.event_type =
  2159. FC_REG_FABRIC_EVENT;
  2160. fast_path_evt->un.read_check_error.header.subcategory =
  2161. LPFC_EVENT_FCPRDCHKERR;
  2162. memcpy(&fast_path_evt->un.read_check_error.header.wwpn,
  2163. &pnode->nlp_portname, sizeof(struct lpfc_name));
  2164. memcpy(&fast_path_evt->un.read_check_error.header.wwnn,
  2165. &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2166. fast_path_evt->un.read_check_error.lun = cmnd->device->lun;
  2167. fast_path_evt->un.read_check_error.opcode = cmnd->cmnd[0];
  2168. fast_path_evt->un.read_check_error.fcpiparam =
  2169. fcpi_parm;
  2170. } else
  2171. return;
  2172. fast_path_evt->vport = vport;
  2173. spin_lock_irqsave(&phba->hbalock, flags);
  2174. list_add_tail(&fast_path_evt->work_evt.evt_listp, &phba->work_list);
  2175. spin_unlock_irqrestore(&phba->hbalock, flags);
  2176. lpfc_worker_wake_up(phba);
  2177. return;
  2178. }
  2179. /**
  2180. * lpfc_scsi_unprep_dma_buf - Un-map DMA mapping of SG-list for dev
  2181. * @phba: The HBA for which this call is being executed.
  2182. * @psb: The scsi buffer which is going to be un-mapped.
  2183. *
  2184. * This routine does DMA un-mapping of scatter gather list of scsi command
  2185. * field of @lpfc_cmd for device with SLI-3 interface spec.
  2186. **/
  2187. static void
  2188. lpfc_scsi_unprep_dma_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  2189. {
  2190. /*
  2191. * There are only two special cases to consider. (1) the scsi command
  2192. * requested scatter-gather usage or (2) the scsi command allocated
  2193. * a request buffer, but did not request use_sg. There is a third
  2194. * case, but it does not require resource deallocation.
  2195. */
  2196. if (psb->seg_cnt > 0)
  2197. scsi_dma_unmap(psb->pCmd);
  2198. if (psb->prot_seg_cnt > 0)
  2199. dma_unmap_sg(&phba->pcidev->dev, scsi_prot_sglist(psb->pCmd),
  2200. scsi_prot_sg_count(psb->pCmd),
  2201. psb->pCmd->sc_data_direction);
  2202. }
  2203. /**
  2204. * lpfc_handler_fcp_err - FCP response handler
  2205. * @vport: The virtual port for which this call is being executed.
  2206. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  2207. * @rsp_iocb: The response IOCB which contains FCP error.
  2208. *
  2209. * This routine is called to process response IOCB with status field
  2210. * IOSTAT_FCP_RSP_ERROR. This routine sets result field of scsi command
  2211. * based upon SCSI and FCP error.
  2212. **/
  2213. static void
  2214. lpfc_handle_fcp_err(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  2215. struct lpfc_iocbq *rsp_iocb)
  2216. {
  2217. struct scsi_cmnd *cmnd = lpfc_cmd->pCmd;
  2218. struct fcp_cmnd *fcpcmd = lpfc_cmd->fcp_cmnd;
  2219. struct fcp_rsp *fcprsp = lpfc_cmd->fcp_rsp;
  2220. uint32_t fcpi_parm = rsp_iocb->iocb.un.fcpi.fcpi_parm;
  2221. uint32_t resp_info = fcprsp->rspStatus2;
  2222. uint32_t scsi_status = fcprsp->rspStatus3;
  2223. uint32_t *lp;
  2224. uint32_t host_status = DID_OK;
  2225. uint32_t rsplen = 0;
  2226. uint32_t logit = LOG_FCP | LOG_FCP_ERROR;
  2227. /*
  2228. * If this is a task management command, there is no
  2229. * scsi packet associated with this lpfc_cmd. The driver
  2230. * consumes it.
  2231. */
  2232. if (fcpcmd->fcpCntl2) {
  2233. scsi_status = 0;
  2234. goto out;
  2235. }
  2236. if (resp_info & RSP_LEN_VALID) {
  2237. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  2238. if (rsplen != 0 && rsplen != 4 && rsplen != 8) {
  2239. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2240. "2719 Invalid response length: "
  2241. "tgt x%x lun x%x cmnd x%x rsplen x%x\n",
  2242. cmnd->device->id,
  2243. cmnd->device->lun, cmnd->cmnd[0],
  2244. rsplen);
  2245. host_status = DID_ERROR;
  2246. goto out;
  2247. }
  2248. if (fcprsp->rspInfo3 != RSP_NO_FAILURE) {
  2249. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2250. "2757 Protocol failure detected during "
  2251. "processing of FCP I/O op: "
  2252. "tgt x%x lun x%x cmnd x%x rspInfo3 x%x\n",
  2253. cmnd->device->id,
  2254. cmnd->device->lun, cmnd->cmnd[0],
  2255. fcprsp->rspInfo3);
  2256. host_status = DID_ERROR;
  2257. goto out;
  2258. }
  2259. }
  2260. if ((resp_info & SNS_LEN_VALID) && fcprsp->rspSnsLen) {
  2261. uint32_t snslen = be32_to_cpu(fcprsp->rspSnsLen);
  2262. if (snslen > SCSI_SENSE_BUFFERSIZE)
  2263. snslen = SCSI_SENSE_BUFFERSIZE;
  2264. if (resp_info & RSP_LEN_VALID)
  2265. rsplen = be32_to_cpu(fcprsp->rspRspLen);
  2266. memcpy(cmnd->sense_buffer, &fcprsp->rspInfo0 + rsplen, snslen);
  2267. }
  2268. lp = (uint32_t *)cmnd->sense_buffer;
  2269. if (!scsi_status && (resp_info & RESID_UNDER) &&
  2270. vport->cfg_log_verbose & LOG_FCP_UNDER)
  2271. logit = LOG_FCP_UNDER;
  2272. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  2273. "9024 FCP command x%x failed: x%x SNS x%x x%x "
  2274. "Data: x%x x%x x%x x%x x%x\n",
  2275. cmnd->cmnd[0], scsi_status,
  2276. be32_to_cpu(*lp), be32_to_cpu(*(lp + 3)), resp_info,
  2277. be32_to_cpu(fcprsp->rspResId),
  2278. be32_to_cpu(fcprsp->rspSnsLen),
  2279. be32_to_cpu(fcprsp->rspRspLen),
  2280. fcprsp->rspInfo3);
  2281. scsi_set_resid(cmnd, 0);
  2282. if (resp_info & RESID_UNDER) {
  2283. scsi_set_resid(cmnd, be32_to_cpu(fcprsp->rspResId));
  2284. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP_UNDER,
  2285. "9025 FCP Read Underrun, expected %d, "
  2286. "residual %d Data: x%x x%x x%x\n",
  2287. be32_to_cpu(fcpcmd->fcpDl),
  2288. scsi_get_resid(cmnd), fcpi_parm, cmnd->cmnd[0],
  2289. cmnd->underflow);
  2290. /*
  2291. * If there is an under run check if under run reported by
  2292. * storage array is same as the under run reported by HBA.
  2293. * If this is not same, there is a dropped frame.
  2294. */
  2295. if ((cmnd->sc_data_direction == DMA_FROM_DEVICE) &&
  2296. fcpi_parm &&
  2297. (scsi_get_resid(cmnd) != fcpi_parm)) {
  2298. lpfc_printf_vlog(vport, KERN_WARNING,
  2299. LOG_FCP | LOG_FCP_ERROR,
  2300. "9026 FCP Read Check Error "
  2301. "and Underrun Data: x%x x%x x%x x%x\n",
  2302. be32_to_cpu(fcpcmd->fcpDl),
  2303. scsi_get_resid(cmnd), fcpi_parm,
  2304. cmnd->cmnd[0]);
  2305. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  2306. host_status = DID_ERROR;
  2307. }
  2308. /*
  2309. * The cmnd->underflow is the minimum number of bytes that must
  2310. * be transferred for this command. Provided a sense condition
  2311. * is not present, make sure the actual amount transferred is at
  2312. * least the underflow value or fail.
  2313. */
  2314. if (!(resp_info & SNS_LEN_VALID) &&
  2315. (scsi_status == SAM_STAT_GOOD) &&
  2316. (scsi_bufflen(cmnd) - scsi_get_resid(cmnd)
  2317. < cmnd->underflow)) {
  2318. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2319. "9027 FCP command x%x residual "
  2320. "underrun converted to error "
  2321. "Data: x%x x%x x%x\n",
  2322. cmnd->cmnd[0], scsi_bufflen(cmnd),
  2323. scsi_get_resid(cmnd), cmnd->underflow);
  2324. host_status = DID_ERROR;
  2325. }
  2326. } else if (resp_info & RESID_OVER) {
  2327. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2328. "9028 FCP command x%x residual overrun error. "
  2329. "Data: x%x x%x\n", cmnd->cmnd[0],
  2330. scsi_bufflen(cmnd), scsi_get_resid(cmnd));
  2331. host_status = DID_ERROR;
  2332. /*
  2333. * Check SLI validation that all the transfer was actually done
  2334. * (fcpi_parm should be zero). Apply check only to reads.
  2335. */
  2336. } else if (fcpi_parm && (cmnd->sc_data_direction == DMA_FROM_DEVICE)) {
  2337. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP | LOG_FCP_ERROR,
  2338. "9029 FCP Read Check Error Data: "
  2339. "x%x x%x x%x x%x x%x\n",
  2340. be32_to_cpu(fcpcmd->fcpDl),
  2341. be32_to_cpu(fcprsp->rspResId),
  2342. fcpi_parm, cmnd->cmnd[0], scsi_status);
  2343. switch (scsi_status) {
  2344. case SAM_STAT_GOOD:
  2345. case SAM_STAT_CHECK_CONDITION:
  2346. /* Fabric dropped a data frame. Fail any successful
  2347. * command in which we detected dropped frames.
  2348. * A status of good or some check conditions could
  2349. * be considered a successful command.
  2350. */
  2351. host_status = DID_ERROR;
  2352. break;
  2353. }
  2354. scsi_set_resid(cmnd, scsi_bufflen(cmnd));
  2355. }
  2356. out:
  2357. cmnd->result = ScsiResult(host_status, scsi_status);
  2358. lpfc_send_scsi_error_event(vport->phba, vport, lpfc_cmd, rsp_iocb);
  2359. }
  2360. /**
  2361. * lpfc_scsi_cmd_iocb_cmpl - Scsi cmnd IOCB completion routine
  2362. * @phba: The Hba for which this call is being executed.
  2363. * @pIocbIn: The command IOCBQ for the scsi cmnd.
  2364. * @pIocbOut: The response IOCBQ for the scsi cmnd.
  2365. *
  2366. * This routine assigns scsi command result by looking into response IOCB
  2367. * status field appropriately. This routine handles QUEUE FULL condition as
  2368. * well by ramping down device queue depth.
  2369. **/
  2370. static void
  2371. lpfc_scsi_cmd_iocb_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pIocbIn,
  2372. struct lpfc_iocbq *pIocbOut)
  2373. {
  2374. struct lpfc_scsi_buf *lpfc_cmd =
  2375. (struct lpfc_scsi_buf *) pIocbIn->context1;
  2376. struct lpfc_vport *vport = pIocbIn->vport;
  2377. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  2378. struct lpfc_nodelist *pnode = rdata->pnode;
  2379. struct scsi_cmnd *cmd;
  2380. int result;
  2381. struct scsi_device *tmp_sdev;
  2382. int depth;
  2383. unsigned long flags;
  2384. struct lpfc_fast_path_event *fast_path_evt;
  2385. struct Scsi_Host *shost;
  2386. uint32_t queue_depth, scsi_id;
  2387. uint32_t logit = LOG_FCP;
  2388. /* Sanity check on return of outstanding command */
  2389. if (!(lpfc_cmd->pCmd))
  2390. return;
  2391. cmd = lpfc_cmd->pCmd;
  2392. shost = cmd->device->host;
  2393. lpfc_cmd->result = pIocbOut->iocb.un.ulpWord[4];
  2394. lpfc_cmd->status = pIocbOut->iocb.ulpStatus;
  2395. /* pick up SLI4 exhange busy status from HBA */
  2396. lpfc_cmd->exch_busy = pIocbOut->iocb_flag & LPFC_EXCHANGE_BUSY;
  2397. if (pnode && NLP_CHK_NODE_ACT(pnode))
  2398. atomic_dec(&pnode->cmd_pending);
  2399. if (lpfc_cmd->status) {
  2400. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  2401. (lpfc_cmd->result & IOERR_DRVR_MASK))
  2402. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  2403. else if (lpfc_cmd->status >= IOSTAT_CNT)
  2404. lpfc_cmd->status = IOSTAT_DEFAULT;
  2405. if (lpfc_cmd->status == IOSTAT_FCP_RSP_ERROR
  2406. && !lpfc_cmd->fcp_rsp->rspStatus3
  2407. && (lpfc_cmd->fcp_rsp->rspStatus2 & RESID_UNDER)
  2408. && !(phba->cfg_log_verbose & LOG_FCP_UNDER))
  2409. logit = 0;
  2410. else
  2411. logit = LOG_FCP | LOG_FCP_UNDER;
  2412. lpfc_printf_vlog(vport, KERN_WARNING, logit,
  2413. "9030 FCP cmd x%x failed <%d/%d> "
  2414. "status: x%x result: x%x Data: x%x x%x\n",
  2415. cmd->cmnd[0],
  2416. cmd->device ? cmd->device->id : 0xffff,
  2417. cmd->device ? cmd->device->lun : 0xffff,
  2418. lpfc_cmd->status, lpfc_cmd->result,
  2419. pIocbOut->iocb.ulpContext,
  2420. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  2421. switch (lpfc_cmd->status) {
  2422. case IOSTAT_FCP_RSP_ERROR:
  2423. /* Call FCP RSP handler to determine result */
  2424. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  2425. break;
  2426. case IOSTAT_NPORT_BSY:
  2427. case IOSTAT_FABRIC_BSY:
  2428. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  2429. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2430. if (!fast_path_evt)
  2431. break;
  2432. fast_path_evt->un.fabric_evt.event_type =
  2433. FC_REG_FABRIC_EVENT;
  2434. fast_path_evt->un.fabric_evt.subcategory =
  2435. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  2436. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  2437. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2438. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  2439. &pnode->nlp_portname,
  2440. sizeof(struct lpfc_name));
  2441. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  2442. &pnode->nlp_nodename,
  2443. sizeof(struct lpfc_name));
  2444. }
  2445. fast_path_evt->vport = vport;
  2446. fast_path_evt->work_evt.evt =
  2447. LPFC_EVT_FASTPATH_MGMT_EVT;
  2448. spin_lock_irqsave(&phba->hbalock, flags);
  2449. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  2450. &phba->work_list);
  2451. spin_unlock_irqrestore(&phba->hbalock, flags);
  2452. lpfc_worker_wake_up(phba);
  2453. break;
  2454. case IOSTAT_LOCAL_REJECT:
  2455. case IOSTAT_REMOTE_STOP:
  2456. if (lpfc_cmd->result == IOERR_ELXSEC_KEY_UNWRAP_ERROR ||
  2457. lpfc_cmd->result ==
  2458. IOERR_ELXSEC_KEY_UNWRAP_COMPARE_ERROR ||
  2459. lpfc_cmd->result == IOERR_ELXSEC_CRYPTO_ERROR ||
  2460. lpfc_cmd->result ==
  2461. IOERR_ELXSEC_CRYPTO_COMPARE_ERROR) {
  2462. cmd->result = ScsiResult(DID_NO_CONNECT, 0);
  2463. break;
  2464. }
  2465. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  2466. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  2467. lpfc_cmd->result == IOERR_ABORT_REQUESTED ||
  2468. lpfc_cmd->result == IOERR_SLER_CMD_RCV_FAILURE) {
  2469. cmd->result = ScsiResult(DID_REQUEUE, 0);
  2470. break;
  2471. }
  2472. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  2473. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  2474. pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
  2475. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  2476. /*
  2477. * This is a response for a BG enabled
  2478. * cmd. Parse BG error
  2479. */
  2480. lpfc_parse_bg_err(phba, lpfc_cmd,
  2481. pIocbOut);
  2482. break;
  2483. } else {
  2484. lpfc_printf_vlog(vport, KERN_WARNING,
  2485. LOG_BG,
  2486. "9031 non-zero BGSTAT "
  2487. "on unprotected cmd\n");
  2488. }
  2489. }
  2490. if ((lpfc_cmd->status == IOSTAT_REMOTE_STOP)
  2491. && (phba->sli_rev == LPFC_SLI_REV4)
  2492. && (pnode && NLP_CHK_NODE_ACT(pnode))) {
  2493. /* This IO was aborted by the target, we don't
  2494. * know the rxid and because we did not send the
  2495. * ABTS we cannot generate and RRQ.
  2496. */
  2497. lpfc_set_rrq_active(phba, pnode,
  2498. lpfc_cmd->cur_iocbq.sli4_xritag,
  2499. 0, 0);
  2500. }
  2501. /* else: fall through */
  2502. default:
  2503. cmd->result = ScsiResult(DID_ERROR, 0);
  2504. break;
  2505. }
  2506. if (!pnode || !NLP_CHK_NODE_ACT(pnode)
  2507. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  2508. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
  2509. SAM_STAT_BUSY);
  2510. } else
  2511. cmd->result = ScsiResult(DID_OK, 0);
  2512. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  2513. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  2514. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2515. "0710 Iodone <%d/%d> cmd %p, error "
  2516. "x%x SNS x%x x%x Data: x%x x%x\n",
  2517. cmd->device->id, cmd->device->lun, cmd,
  2518. cmd->result, *lp, *(lp + 3), cmd->retries,
  2519. scsi_get_resid(cmd));
  2520. }
  2521. lpfc_update_stats(phba, lpfc_cmd);
  2522. result = cmd->result;
  2523. if (vport->cfg_max_scsicmpl_time &&
  2524. time_after(jiffies, lpfc_cmd->start_time +
  2525. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  2526. spin_lock_irqsave(shost->host_lock, flags);
  2527. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2528. if (pnode->cmd_qdepth >
  2529. atomic_read(&pnode->cmd_pending) &&
  2530. (atomic_read(&pnode->cmd_pending) >
  2531. LPFC_MIN_TGT_QDEPTH) &&
  2532. ((cmd->cmnd[0] == READ_10) ||
  2533. (cmd->cmnd[0] == WRITE_10)))
  2534. pnode->cmd_qdepth =
  2535. atomic_read(&pnode->cmd_pending);
  2536. pnode->last_change_time = jiffies;
  2537. }
  2538. spin_unlock_irqrestore(shost->host_lock, flags);
  2539. } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2540. if ((pnode->cmd_qdepth < vport->cfg_tgt_queue_depth) &&
  2541. time_after(jiffies, pnode->last_change_time +
  2542. msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
  2543. spin_lock_irqsave(shost->host_lock, flags);
  2544. depth = pnode->cmd_qdepth * LPFC_TGTQ_RAMPUP_PCENT
  2545. / 100;
  2546. depth = depth ? depth : 1;
  2547. pnode->cmd_qdepth += depth;
  2548. if (pnode->cmd_qdepth > vport->cfg_tgt_queue_depth)
  2549. pnode->cmd_qdepth = vport->cfg_tgt_queue_depth;
  2550. pnode->last_change_time = jiffies;
  2551. spin_unlock_irqrestore(shost->host_lock, flags);
  2552. }
  2553. }
  2554. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  2555. /* The sdev is not guaranteed to be valid post scsi_done upcall. */
  2556. queue_depth = cmd->device->queue_depth;
  2557. scsi_id = cmd->device->id;
  2558. cmd->scsi_done(cmd);
  2559. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2560. /*
  2561. * If there is a thread waiting for command completion
  2562. * wake up the thread.
  2563. */
  2564. spin_lock_irqsave(shost->host_lock, flags);
  2565. lpfc_cmd->pCmd = NULL;
  2566. if (lpfc_cmd->waitq)
  2567. wake_up(lpfc_cmd->waitq);
  2568. spin_unlock_irqrestore(shost->host_lock, flags);
  2569. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2570. return;
  2571. }
  2572. if (!result)
  2573. lpfc_rampup_queue_depth(vport, queue_depth);
  2574. /*
  2575. * Check for queue full. If the lun is reporting queue full, then
  2576. * back off the lun queue depth to prevent target overloads.
  2577. */
  2578. if (result == SAM_STAT_TASK_SET_FULL && pnode &&
  2579. NLP_CHK_NODE_ACT(pnode)) {
  2580. shost_for_each_device(tmp_sdev, shost) {
  2581. if (tmp_sdev->id != scsi_id)
  2582. continue;
  2583. depth = scsi_track_queue_full(tmp_sdev,
  2584. tmp_sdev->queue_depth-1);
  2585. if (depth <= 0)
  2586. continue;
  2587. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2588. "0711 detected queue full - lun queue "
  2589. "depth adjusted to %d.\n", depth);
  2590. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  2591. pnode,
  2592. tmp_sdev->lun,
  2593. depth+1, depth);
  2594. }
  2595. }
  2596. /*
  2597. * If there is a thread waiting for command completion
  2598. * wake up the thread.
  2599. */
  2600. spin_lock_irqsave(shost->host_lock, flags);
  2601. lpfc_cmd->pCmd = NULL;
  2602. if (lpfc_cmd->waitq)
  2603. wake_up(lpfc_cmd->waitq);
  2604. spin_unlock_irqrestore(shost->host_lock, flags);
  2605. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2606. }
  2607. /**
  2608. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
  2609. * @data: A pointer to the immediate command data portion of the IOCB.
  2610. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  2611. *
  2612. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  2613. * byte swapping the data to big endian format for transmission on the wire.
  2614. **/
  2615. static void
  2616. lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
  2617. {
  2618. int i, j;
  2619. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  2620. i += sizeof(uint32_t), j++) {
  2621. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  2622. }
  2623. }
  2624. /**
  2625. * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
  2626. * @vport: The virtual port for which this call is being executed.
  2627. * @lpfc_cmd: The scsi command which needs to send.
  2628. * @pnode: Pointer to lpfc_nodelist.
  2629. *
  2630. * This routine initializes fcp_cmnd and iocb data structure from scsi command
  2631. * to transfer for device with SLI3 interface spec.
  2632. **/
  2633. static void
  2634. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  2635. struct lpfc_nodelist *pnode)
  2636. {
  2637. struct lpfc_hba *phba = vport->phba;
  2638. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2639. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2640. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  2641. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  2642. int datadir = scsi_cmnd->sc_data_direction;
  2643. char tag[2];
  2644. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2645. return;
  2646. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  2647. /* clear task management bits */
  2648. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  2649. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  2650. &lpfc_cmd->fcp_cmnd->fcp_lun);
  2651. memset(&fcp_cmnd->fcpCdb[0], 0, LPFC_FCP_CDB_LEN);
  2652. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, scsi_cmnd->cmd_len);
  2653. if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
  2654. switch (tag[0]) {
  2655. case HEAD_OF_QUEUE_TAG:
  2656. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  2657. break;
  2658. case ORDERED_QUEUE_TAG:
  2659. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  2660. break;
  2661. default:
  2662. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  2663. break;
  2664. }
  2665. } else
  2666. fcp_cmnd->fcpCntl1 = 0;
  2667. /*
  2668. * There are three possibilities here - use scatter-gather segment, use
  2669. * the single mapping, or neither. Start the lpfc command prep by
  2670. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  2671. * data bde entry.
  2672. */
  2673. if (scsi_sg_count(scsi_cmnd)) {
  2674. if (datadir == DMA_TO_DEVICE) {
  2675. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  2676. if (phba->sli_rev < LPFC_SLI_REV4) {
  2677. iocb_cmd->un.fcpi.fcpi_parm = 0;
  2678. iocb_cmd->ulpPU = 0;
  2679. } else
  2680. iocb_cmd->ulpPU = PARM_READ_CHECK;
  2681. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  2682. phba->fc4OutputRequests++;
  2683. } else {
  2684. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  2685. iocb_cmd->ulpPU = PARM_READ_CHECK;
  2686. fcp_cmnd->fcpCntl3 = READ_DATA;
  2687. phba->fc4InputRequests++;
  2688. }
  2689. } else {
  2690. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  2691. iocb_cmd->un.fcpi.fcpi_parm = 0;
  2692. iocb_cmd->ulpPU = 0;
  2693. fcp_cmnd->fcpCntl3 = 0;
  2694. phba->fc4ControlRequests++;
  2695. }
  2696. if (phba->sli_rev == 3 &&
  2697. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  2698. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  2699. /*
  2700. * Finish initializing those IOCB fields that are independent
  2701. * of the scsi_cmnd request_buffer
  2702. */
  2703. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  2704. if (phba->sli_rev == LPFC_SLI_REV4)
  2705. piocbq->iocb.ulpContext =
  2706. phba->sli4_hba.rpi_ids[pnode->nlp_rpi];
  2707. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  2708. piocbq->iocb.ulpFCP2Rcvy = 1;
  2709. else
  2710. piocbq->iocb.ulpFCP2Rcvy = 0;
  2711. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  2712. piocbq->context1 = lpfc_cmd;
  2713. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  2714. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  2715. piocbq->vport = vport;
  2716. }
  2717. /**
  2718. * lpfc_scsi_prep_task_mgmt_cmd - Convert SLI3 scsi TM cmd to FCP info unit
  2719. * @vport: The virtual port for which this call is being executed.
  2720. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  2721. * @lun: Logical unit number.
  2722. * @task_mgmt_cmd: SCSI task management command.
  2723. *
  2724. * This routine creates FCP information unit corresponding to @task_mgmt_cmd
  2725. * for device with SLI-3 interface spec.
  2726. *
  2727. * Return codes:
  2728. * 0 - Error
  2729. * 1 - Success
  2730. **/
  2731. static int
  2732. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  2733. struct lpfc_scsi_buf *lpfc_cmd,
  2734. unsigned int lun,
  2735. uint8_t task_mgmt_cmd)
  2736. {
  2737. struct lpfc_iocbq *piocbq;
  2738. IOCB_t *piocb;
  2739. struct fcp_cmnd *fcp_cmnd;
  2740. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  2741. struct lpfc_nodelist *ndlp = rdata->pnode;
  2742. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  2743. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  2744. return 0;
  2745. piocbq = &(lpfc_cmd->cur_iocbq);
  2746. piocbq->vport = vport;
  2747. piocb = &piocbq->iocb;
  2748. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2749. /* Clear out any old data in the FCP command area */
  2750. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  2751. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  2752. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  2753. if (vport->phba->sli_rev == 3 &&
  2754. !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  2755. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  2756. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  2757. piocb->ulpContext = ndlp->nlp_rpi;
  2758. if (vport->phba->sli_rev == LPFC_SLI_REV4) {
  2759. piocb->ulpContext =
  2760. vport->phba->sli4_hba.rpi_ids[ndlp->nlp_rpi];
  2761. }
  2762. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  2763. piocb->ulpFCP2Rcvy = 1;
  2764. }
  2765. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  2766. /* ulpTimeout is only one byte */
  2767. if (lpfc_cmd->timeout > 0xff) {
  2768. /*
  2769. * Do not timeout the command at the firmware level.
  2770. * The driver will provide the timeout mechanism.
  2771. */
  2772. piocb->ulpTimeout = 0;
  2773. } else
  2774. piocb->ulpTimeout = lpfc_cmd->timeout;
  2775. if (vport->phba->sli_rev == LPFC_SLI_REV4)
  2776. lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
  2777. return 1;
  2778. }
  2779. /**
  2780. * lpfc_scsi_api_table_setup - Set up scsi api function jump table
  2781. * @phba: The hba struct for which this call is being executed.
  2782. * @dev_grp: The HBA PCI-Device group number.
  2783. *
  2784. * This routine sets up the SCSI interface API function jump table in @phba
  2785. * struct.
  2786. * Returns: 0 - success, -ENODEV - failure.
  2787. **/
  2788. int
  2789. lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  2790. {
  2791. phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
  2792. phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
  2793. switch (dev_grp) {
  2794. case LPFC_PCI_DEV_LP:
  2795. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
  2796. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
  2797. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
  2798. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s3;
  2799. break;
  2800. case LPFC_PCI_DEV_OC:
  2801. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
  2802. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
  2803. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
  2804. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf_s4;
  2805. break;
  2806. default:
  2807. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2808. "1418 Invalid HBA PCI-device group: 0x%x\n",
  2809. dev_grp);
  2810. return -ENODEV;
  2811. break;
  2812. }
  2813. phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
  2814. phba->lpfc_scsi_cmd_iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  2815. return 0;
  2816. }
  2817. /**
  2818. * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
  2819. * @phba: The Hba for which this call is being executed.
  2820. * @cmdiocbq: Pointer to lpfc_iocbq data structure.
  2821. * @rspiocbq: Pointer to lpfc_iocbq data structure.
  2822. *
  2823. * This routine is IOCB completion routine for device reset and target reset
  2824. * routine. This routine release scsi buffer associated with lpfc_cmd.
  2825. **/
  2826. static void
  2827. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  2828. struct lpfc_iocbq *cmdiocbq,
  2829. struct lpfc_iocbq *rspiocbq)
  2830. {
  2831. struct lpfc_scsi_buf *lpfc_cmd =
  2832. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  2833. if (lpfc_cmd)
  2834. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2835. return;
  2836. }
  2837. /**
  2838. * lpfc_info - Info entry point of scsi_host_template data structure
  2839. * @host: The scsi host for which this call is being executed.
  2840. *
  2841. * This routine provides module information about hba.
  2842. *
  2843. * Reutrn code:
  2844. * Pointer to char - Success.
  2845. **/
  2846. const char *
  2847. lpfc_info(struct Scsi_Host *host)
  2848. {
  2849. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  2850. struct lpfc_hba *phba = vport->phba;
  2851. int len;
  2852. static char lpfcinfobuf[384];
  2853. memset(lpfcinfobuf,0,384);
  2854. if (phba && phba->pcidev){
  2855. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  2856. len = strlen(lpfcinfobuf);
  2857. snprintf(lpfcinfobuf + len,
  2858. 384-len,
  2859. " on PCI bus %02x device %02x irq %d",
  2860. phba->pcidev->bus->number,
  2861. phba->pcidev->devfn,
  2862. phba->pcidev->irq);
  2863. len = strlen(lpfcinfobuf);
  2864. if (phba->Port[0]) {
  2865. snprintf(lpfcinfobuf + len,
  2866. 384-len,
  2867. " port %s",
  2868. phba->Port);
  2869. }
  2870. len = strlen(lpfcinfobuf);
  2871. if (phba->sli4_hba.link_state.logical_speed) {
  2872. snprintf(lpfcinfobuf + len,
  2873. 384-len,
  2874. " Logical Link Speed: %d Mbps",
  2875. phba->sli4_hba.link_state.logical_speed * 10);
  2876. }
  2877. }
  2878. return lpfcinfobuf;
  2879. }
  2880. /**
  2881. * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
  2882. * @phba: The Hba for which this call is being executed.
  2883. *
  2884. * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
  2885. * The default value of cfg_poll_tmo is 10 milliseconds.
  2886. **/
  2887. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  2888. {
  2889. unsigned long poll_tmo_expires =
  2890. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  2891. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  2892. mod_timer(&phba->fcp_poll_timer,
  2893. poll_tmo_expires);
  2894. }
  2895. /**
  2896. * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
  2897. * @phba: The Hba for which this call is being executed.
  2898. *
  2899. * This routine starts the fcp_poll_timer of @phba.
  2900. **/
  2901. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  2902. {
  2903. lpfc_poll_rearm_timer(phba);
  2904. }
  2905. /**
  2906. * lpfc_poll_timeout - Restart polling timer
  2907. * @ptr: Map to lpfc_hba data structure pointer.
  2908. *
  2909. * This routine restarts fcp_poll timer, when FCP ring polling is enable
  2910. * and FCP Ring interrupt is disable.
  2911. **/
  2912. void lpfc_poll_timeout(unsigned long ptr)
  2913. {
  2914. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2915. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2916. lpfc_sli_handle_fast_ring_event(phba,
  2917. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  2918. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2919. lpfc_poll_rearm_timer(phba);
  2920. }
  2921. }
  2922. /**
  2923. * lpfc_queuecommand - scsi_host_template queuecommand entry point
  2924. * @cmnd: Pointer to scsi_cmnd data structure.
  2925. * @done: Pointer to done routine.
  2926. *
  2927. * Driver registers this routine to scsi midlayer to submit a @cmd to process.
  2928. * This routine prepares an IOCB from scsi command and provides to firmware.
  2929. * The @done callback is invoked after driver finished processing the command.
  2930. *
  2931. * Return value :
  2932. * 0 - Success
  2933. * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
  2934. **/
  2935. static int
  2936. lpfc_queuecommand_lck(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  2937. {
  2938. struct Scsi_Host *shost = cmnd->device->host;
  2939. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2940. struct lpfc_hba *phba = vport->phba;
  2941. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2942. struct lpfc_nodelist *ndlp;
  2943. struct lpfc_scsi_buf *lpfc_cmd;
  2944. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2945. int err;
  2946. err = fc_remote_port_chkready(rport);
  2947. if (err) {
  2948. cmnd->result = err;
  2949. goto out_fail_command;
  2950. }
  2951. /*
  2952. * Do not let the mid-layer retry I/O too fast. If an I/O is retried
  2953. * without waiting a bit then indicate that the device is busy.
  2954. */
  2955. if (cmnd->retries &&
  2956. time_before(jiffies, (cmnd->jiffies_at_alloc +
  2957. msecs_to_jiffies(LPFC_RETRY_PAUSE *
  2958. cmnd->retries))))
  2959. return SCSI_MLQUEUE_DEVICE_BUSY;
  2960. ndlp = rdata->pnode;
  2961. if ((scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) &&
  2962. (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) ||
  2963. (phba->sli_rev == LPFC_SLI_REV4))) {
  2964. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  2965. "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
  2966. " op:%02x str=%s without registering for"
  2967. " BlockGuard - Rejecting command\n",
  2968. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2969. dif_op_str[scsi_get_prot_op(cmnd)]);
  2970. goto out_fail_command;
  2971. }
  2972. /*
  2973. * Catch race where our node has transitioned, but the
  2974. * transport is still transitioning.
  2975. */
  2976. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  2977. cmnd->result = ScsiResult(DID_IMM_RETRY, 0);
  2978. goto out_fail_command;
  2979. }
  2980. if (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth)
  2981. goto out_tgt_busy;
  2982. lpfc_cmd = lpfc_get_scsi_buf(phba, ndlp);
  2983. if (lpfc_cmd == NULL) {
  2984. lpfc_rampdown_queue_depth(phba);
  2985. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2986. "0707 driver's buffer pool is empty, "
  2987. "IO busied\n");
  2988. goto out_host_busy;
  2989. }
  2990. /*
  2991. * Store the midlayer's command structure for the completion phase
  2992. * and complete the command initialization.
  2993. */
  2994. lpfc_cmd->pCmd = cmnd;
  2995. lpfc_cmd->rdata = rdata;
  2996. lpfc_cmd->timeout = 0;
  2997. lpfc_cmd->start_time = jiffies;
  2998. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  2999. cmnd->scsi_done = done;
  3000. if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  3001. if (vport->phba->cfg_enable_bg) {
  3002. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3003. "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
  3004. "str=%s\n",
  3005. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  3006. dif_op_str[scsi_get_prot_op(cmnd)]);
  3007. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3008. "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
  3009. "%02x %02x %02x %02x %02x\n",
  3010. cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
  3011. cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
  3012. cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
  3013. cmnd->cmnd[9]);
  3014. if (cmnd->cmnd[0] == READ_10)
  3015. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3016. "9035 BLKGRD: READ @ sector %llu, "
  3017. "count %u\n",
  3018. (unsigned long long)scsi_get_lba(cmnd),
  3019. blk_rq_sectors(cmnd->request));
  3020. else if (cmnd->cmnd[0] == WRITE_10)
  3021. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3022. "9036 BLKGRD: WRITE @ sector %llu, "
  3023. "count %u cmd=%p\n",
  3024. (unsigned long long)scsi_get_lba(cmnd),
  3025. blk_rq_sectors(cmnd->request),
  3026. cmnd);
  3027. }
  3028. err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  3029. } else {
  3030. if (vport->phba->cfg_enable_bg) {
  3031. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3032. "9038 BLKGRD: rcvd unprotected cmd:"
  3033. "%02x op:%02x str=%s\n",
  3034. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  3035. dif_op_str[scsi_get_prot_op(cmnd)]);
  3036. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3037. "9039 BLKGRD: CDB: %02x %02x %02x "
  3038. "%02x %02x %02x %02x %02x %02x %02x\n",
  3039. cmnd->cmnd[0], cmnd->cmnd[1],
  3040. cmnd->cmnd[2], cmnd->cmnd[3],
  3041. cmnd->cmnd[4], cmnd->cmnd[5],
  3042. cmnd->cmnd[6], cmnd->cmnd[7],
  3043. cmnd->cmnd[8], cmnd->cmnd[9]);
  3044. if (cmnd->cmnd[0] == READ_10)
  3045. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3046. "9040 dbg: READ @ sector %llu, "
  3047. "count %u\n",
  3048. (unsigned long long)scsi_get_lba(cmnd),
  3049. blk_rq_sectors(cmnd->request));
  3050. else if (cmnd->cmnd[0] == WRITE_10)
  3051. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3052. "9041 dbg: WRITE @ sector %llu, "
  3053. "count %u cmd=%p\n",
  3054. (unsigned long long)scsi_get_lba(cmnd),
  3055. blk_rq_sectors(cmnd->request), cmnd);
  3056. else
  3057. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  3058. "9042 dbg: parser not implemented\n");
  3059. }
  3060. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  3061. }
  3062. if (err)
  3063. goto out_host_busy_free_buf;
  3064. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  3065. atomic_inc(&ndlp->cmd_pending);
  3066. err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
  3067. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  3068. if (err) {
  3069. atomic_dec(&ndlp->cmd_pending);
  3070. goto out_host_busy_free_buf;
  3071. }
  3072. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  3073. spin_unlock(shost->host_lock);
  3074. lpfc_sli_handle_fast_ring_event(phba,
  3075. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  3076. spin_lock(shost->host_lock);
  3077. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  3078. lpfc_poll_rearm_timer(phba);
  3079. }
  3080. return 0;
  3081. out_host_busy_free_buf:
  3082. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  3083. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3084. out_host_busy:
  3085. return SCSI_MLQUEUE_HOST_BUSY;
  3086. out_tgt_busy:
  3087. return SCSI_MLQUEUE_TARGET_BUSY;
  3088. out_fail_command:
  3089. done(cmnd);
  3090. return 0;
  3091. }
  3092. static DEF_SCSI_QCMD(lpfc_queuecommand)
  3093. /**
  3094. * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
  3095. * @cmnd: Pointer to scsi_cmnd data structure.
  3096. *
  3097. * This routine aborts @cmnd pending in base driver.
  3098. *
  3099. * Return code :
  3100. * 0x2003 - Error
  3101. * 0x2002 - Success
  3102. **/
  3103. static int
  3104. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  3105. {
  3106. struct Scsi_Host *shost = cmnd->device->host;
  3107. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3108. struct lpfc_hba *phba = vport->phba;
  3109. struct lpfc_iocbq *iocb;
  3110. struct lpfc_iocbq *abtsiocb;
  3111. struct lpfc_scsi_buf *lpfc_cmd;
  3112. IOCB_t *cmd, *icmd;
  3113. int ret = SUCCESS;
  3114. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  3115. ret = fc_block_scsi_eh(cmnd);
  3116. if (ret)
  3117. return ret;
  3118. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  3119. if (!lpfc_cmd) {
  3120. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3121. "2873 SCSI Layer I/O Abort Request IO CMPL Status "
  3122. "x%x ID %d LUN %d\n",
  3123. ret, cmnd->device->id, cmnd->device->lun);
  3124. return SUCCESS;
  3125. }
  3126. /*
  3127. * If pCmd field of the corresponding lpfc_scsi_buf structure
  3128. * points to a different SCSI command, then the driver has
  3129. * already completed this command, but the midlayer did not
  3130. * see the completion before the eh fired. Just return
  3131. * SUCCESS.
  3132. */
  3133. iocb = &lpfc_cmd->cur_iocbq;
  3134. if (lpfc_cmd->pCmd != cmnd)
  3135. goto out;
  3136. BUG_ON(iocb->context1 != lpfc_cmd);
  3137. abtsiocb = lpfc_sli_get_iocbq(phba);
  3138. if (abtsiocb == NULL) {
  3139. ret = FAILED;
  3140. goto out;
  3141. }
  3142. /*
  3143. * The scsi command can not be in txq and it is in flight because the
  3144. * pCmd is still pointig at the SCSI command we have to abort. There
  3145. * is no need to search the txcmplq. Just send an abort to the FW.
  3146. */
  3147. cmd = &iocb->iocb;
  3148. icmd = &abtsiocb->iocb;
  3149. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  3150. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  3151. if (phba->sli_rev == LPFC_SLI_REV4)
  3152. icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
  3153. else
  3154. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  3155. icmd->ulpLe = 1;
  3156. icmd->ulpClass = cmd->ulpClass;
  3157. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  3158. abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
  3159. abtsiocb->iocb_flag |= LPFC_USE_FCPWQIDX;
  3160. if (lpfc_is_link_up(phba))
  3161. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  3162. else
  3163. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  3164. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  3165. abtsiocb->vport = vport;
  3166. if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
  3167. IOCB_ERROR) {
  3168. lpfc_sli_release_iocbq(phba, abtsiocb);
  3169. ret = FAILED;
  3170. goto out;
  3171. }
  3172. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  3173. lpfc_sli_handle_fast_ring_event(phba,
  3174. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  3175. lpfc_cmd->waitq = &waitq;
  3176. /* Wait for abort to complete */
  3177. wait_event_timeout(waitq,
  3178. (lpfc_cmd->pCmd != cmnd),
  3179. (2*vport->cfg_devloss_tmo*HZ));
  3180. spin_lock_irq(shost->host_lock);
  3181. lpfc_cmd->waitq = NULL;
  3182. spin_unlock_irq(shost->host_lock);
  3183. if (lpfc_cmd->pCmd == cmnd) {
  3184. ret = FAILED;
  3185. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3186. "0748 abort handler timed out waiting "
  3187. "for abort to complete: ret %#x, ID %d, "
  3188. "LUN %d\n",
  3189. ret, cmnd->device->id, cmnd->device->lun);
  3190. }
  3191. out:
  3192. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3193. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  3194. "LUN %d\n", ret, cmnd->device->id,
  3195. cmnd->device->lun);
  3196. return ret;
  3197. }
  3198. static char *
  3199. lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
  3200. {
  3201. switch (task_mgmt_cmd) {
  3202. case FCP_ABORT_TASK_SET:
  3203. return "ABORT_TASK_SET";
  3204. case FCP_CLEAR_TASK_SET:
  3205. return "FCP_CLEAR_TASK_SET";
  3206. case FCP_BUS_RESET:
  3207. return "FCP_BUS_RESET";
  3208. case FCP_LUN_RESET:
  3209. return "FCP_LUN_RESET";
  3210. case FCP_TARGET_RESET:
  3211. return "FCP_TARGET_RESET";
  3212. case FCP_CLEAR_ACA:
  3213. return "FCP_CLEAR_ACA";
  3214. case FCP_TERMINATE_TASK:
  3215. return "FCP_TERMINATE_TASK";
  3216. default:
  3217. return "unknown";
  3218. }
  3219. }
  3220. /**
  3221. * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
  3222. * @vport: The virtual port for which this call is being executed.
  3223. * @rdata: Pointer to remote port local data
  3224. * @tgt_id: Target ID of remote device.
  3225. * @lun_id: Lun number for the TMF
  3226. * @task_mgmt_cmd: type of TMF to send
  3227. *
  3228. * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
  3229. * a remote port.
  3230. *
  3231. * Return Code:
  3232. * 0x2003 - Error
  3233. * 0x2002 - Success.
  3234. **/
  3235. static int
  3236. lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
  3237. unsigned tgt_id, unsigned int lun_id,
  3238. uint8_t task_mgmt_cmd)
  3239. {
  3240. struct lpfc_hba *phba = vport->phba;
  3241. struct lpfc_scsi_buf *lpfc_cmd;
  3242. struct lpfc_iocbq *iocbq;
  3243. struct lpfc_iocbq *iocbqrsp;
  3244. struct lpfc_nodelist *pnode = rdata->pnode;
  3245. int ret;
  3246. int status;
  3247. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  3248. return FAILED;
  3249. lpfc_cmd = lpfc_get_scsi_buf(phba, rdata->pnode);
  3250. if (lpfc_cmd == NULL)
  3251. return FAILED;
  3252. lpfc_cmd->timeout = 60;
  3253. lpfc_cmd->rdata = rdata;
  3254. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
  3255. task_mgmt_cmd);
  3256. if (!status) {
  3257. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3258. return FAILED;
  3259. }
  3260. iocbq = &lpfc_cmd->cur_iocbq;
  3261. iocbqrsp = lpfc_sli_get_iocbq(phba);
  3262. if (iocbqrsp == NULL) {
  3263. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3264. return FAILED;
  3265. }
  3266. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  3267. "0702 Issue %s to TGT %d LUN %d "
  3268. "rpi x%x nlp_flag x%x Data: x%x x%x\n",
  3269. lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
  3270. pnode->nlp_rpi, pnode->nlp_flag, iocbq->sli4_xritag,
  3271. iocbq->iocb_flag);
  3272. status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
  3273. iocbq, iocbqrsp, lpfc_cmd->timeout);
  3274. if (status != IOCB_SUCCESS) {
  3275. if (status == IOCB_TIMEDOUT) {
  3276. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  3277. ret = TIMEOUT_ERROR;
  3278. } else
  3279. ret = FAILED;
  3280. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  3281. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3282. "0727 TMF %s to TGT %d LUN %d failed (%d, %d) "
  3283. "iocb_flag x%x\n",
  3284. lpfc_taskmgmt_name(task_mgmt_cmd),
  3285. tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
  3286. iocbqrsp->iocb.un.ulpWord[4],
  3287. iocbq->iocb_flag);
  3288. } else if (status == IOCB_BUSY)
  3289. ret = FAILED;
  3290. else
  3291. ret = SUCCESS;
  3292. lpfc_sli_release_iocbq(phba, iocbqrsp);
  3293. if (ret != TIMEOUT_ERROR)
  3294. lpfc_release_scsi_buf(phba, lpfc_cmd);
  3295. return ret;
  3296. }
  3297. /**
  3298. * lpfc_chk_tgt_mapped -
  3299. * @vport: The virtual port to check on
  3300. * @cmnd: Pointer to scsi_cmnd data structure.
  3301. *
  3302. * This routine delays until the scsi target (aka rport) for the
  3303. * command exists (is present and logged in) or we declare it non-existent.
  3304. *
  3305. * Return code :
  3306. * 0x2003 - Error
  3307. * 0x2002 - Success
  3308. **/
  3309. static int
  3310. lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
  3311. {
  3312. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  3313. struct lpfc_nodelist *pnode;
  3314. unsigned long later;
  3315. if (!rdata) {
  3316. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  3317. "0797 Tgt Map rport failure: rdata x%p\n", rdata);
  3318. return FAILED;
  3319. }
  3320. pnode = rdata->pnode;
  3321. /*
  3322. * If target is not in a MAPPED state, delay until
  3323. * target is rediscovered or devloss timeout expires.
  3324. */
  3325. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  3326. while (time_after(later, jiffies)) {
  3327. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  3328. return FAILED;
  3329. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  3330. return SUCCESS;
  3331. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  3332. rdata = cmnd->device->hostdata;
  3333. if (!rdata)
  3334. return FAILED;
  3335. pnode = rdata->pnode;
  3336. }
  3337. if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
  3338. (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  3339. return FAILED;
  3340. return SUCCESS;
  3341. }
  3342. /**
  3343. * lpfc_reset_flush_io_context -
  3344. * @vport: The virtual port (scsi_host) for the flush context
  3345. * @tgt_id: If aborting by Target contect - specifies the target id
  3346. * @lun_id: If aborting by Lun context - specifies the lun id
  3347. * @context: specifies the context level to flush at.
  3348. *
  3349. * After a reset condition via TMF, we need to flush orphaned i/o
  3350. * contexts from the adapter. This routine aborts any contexts
  3351. * outstanding, then waits for their completions. The wait is
  3352. * bounded by devloss_tmo though.
  3353. *
  3354. * Return code :
  3355. * 0x2003 - Error
  3356. * 0x2002 - Success
  3357. **/
  3358. static int
  3359. lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
  3360. uint64_t lun_id, lpfc_ctx_cmd context)
  3361. {
  3362. struct lpfc_hba *phba = vport->phba;
  3363. unsigned long later;
  3364. int cnt;
  3365. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  3366. if (cnt)
  3367. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  3368. tgt_id, lun_id, context);
  3369. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  3370. while (time_after(later, jiffies) && cnt) {
  3371. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  3372. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  3373. }
  3374. if (cnt) {
  3375. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3376. "0724 I/O flush failure for context %s : cnt x%x\n",
  3377. ((context == LPFC_CTX_LUN) ? "LUN" :
  3378. ((context == LPFC_CTX_TGT) ? "TGT" :
  3379. ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
  3380. cnt);
  3381. return FAILED;
  3382. }
  3383. return SUCCESS;
  3384. }
  3385. /**
  3386. * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
  3387. * @cmnd: Pointer to scsi_cmnd data structure.
  3388. *
  3389. * This routine does a device reset by sending a LUN_RESET task management
  3390. * command.
  3391. *
  3392. * Return code :
  3393. * 0x2003 - Error
  3394. * 0x2002 - Success
  3395. **/
  3396. static int
  3397. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  3398. {
  3399. struct Scsi_Host *shost = cmnd->device->host;
  3400. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3401. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  3402. struct lpfc_nodelist *pnode;
  3403. unsigned tgt_id = cmnd->device->id;
  3404. unsigned int lun_id = cmnd->device->lun;
  3405. struct lpfc_scsi_event_header scsi_event;
  3406. int status;
  3407. if (!rdata) {
  3408. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3409. "0798 Device Reset rport failure: rdata x%p\n", rdata);
  3410. return FAILED;
  3411. }
  3412. pnode = rdata->pnode;
  3413. status = fc_block_scsi_eh(cmnd);
  3414. if (status)
  3415. return status;
  3416. status = lpfc_chk_tgt_mapped(vport, cmnd);
  3417. if (status == FAILED) {
  3418. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3419. "0721 Device Reset rport failure: rdata x%p\n", rdata);
  3420. return FAILED;
  3421. }
  3422. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3423. scsi_event.subcategory = LPFC_EVENT_LUNRESET;
  3424. scsi_event.lun = lun_id;
  3425. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  3426. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3427. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3428. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3429. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  3430. FCP_LUN_RESET);
  3431. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3432. "0713 SCSI layer issued Device Reset (%d, %d) "
  3433. "return x%x\n", tgt_id, lun_id, status);
  3434. /*
  3435. * We have to clean up i/o as : they may be orphaned by the TMF;
  3436. * or if the TMF failed, they may be in an indeterminate state.
  3437. * So, continue on.
  3438. * We will report success if all the i/o aborts successfully.
  3439. */
  3440. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  3441. LPFC_CTX_LUN);
  3442. return status;
  3443. }
  3444. /**
  3445. * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
  3446. * @cmnd: Pointer to scsi_cmnd data structure.
  3447. *
  3448. * This routine does a target reset by sending a TARGET_RESET task management
  3449. * command.
  3450. *
  3451. * Return code :
  3452. * 0x2003 - Error
  3453. * 0x2002 - Success
  3454. **/
  3455. static int
  3456. lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
  3457. {
  3458. struct Scsi_Host *shost = cmnd->device->host;
  3459. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3460. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  3461. struct lpfc_nodelist *pnode;
  3462. unsigned tgt_id = cmnd->device->id;
  3463. unsigned int lun_id = cmnd->device->lun;
  3464. struct lpfc_scsi_event_header scsi_event;
  3465. int status;
  3466. if (!rdata) {
  3467. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3468. "0799 Target Reset rport failure: rdata x%p\n", rdata);
  3469. return FAILED;
  3470. }
  3471. pnode = rdata->pnode;
  3472. status = fc_block_scsi_eh(cmnd);
  3473. if (status)
  3474. return status;
  3475. status = lpfc_chk_tgt_mapped(vport, cmnd);
  3476. if (status == FAILED) {
  3477. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3478. "0722 Target Reset rport failure: rdata x%p\n", rdata);
  3479. return FAILED;
  3480. }
  3481. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3482. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  3483. scsi_event.lun = 0;
  3484. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  3485. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3486. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3487. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3488. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  3489. FCP_TARGET_RESET);
  3490. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3491. "0723 SCSI layer issued Target Reset (%d, %d) "
  3492. "return x%x\n", tgt_id, lun_id, status);
  3493. /*
  3494. * We have to clean up i/o as : they may be orphaned by the TMF;
  3495. * or if the TMF failed, they may be in an indeterminate state.
  3496. * So, continue on.
  3497. * We will report success if all the i/o aborts successfully.
  3498. */
  3499. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  3500. LPFC_CTX_TGT);
  3501. return status;
  3502. }
  3503. /**
  3504. * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
  3505. * @cmnd: Pointer to scsi_cmnd data structure.
  3506. *
  3507. * This routine does target reset to all targets on @cmnd->device->host.
  3508. * This emulates Parallel SCSI Bus Reset Semantics.
  3509. *
  3510. * Return code :
  3511. * 0x2003 - Error
  3512. * 0x2002 - Success
  3513. **/
  3514. static int
  3515. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  3516. {
  3517. struct Scsi_Host *shost = cmnd->device->host;
  3518. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3519. struct lpfc_nodelist *ndlp = NULL;
  3520. struct lpfc_scsi_event_header scsi_event;
  3521. int match;
  3522. int ret = SUCCESS, status, i;
  3523. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3524. scsi_event.subcategory = LPFC_EVENT_BUSRESET;
  3525. scsi_event.lun = 0;
  3526. memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
  3527. memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
  3528. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3529. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3530. status = fc_block_scsi_eh(cmnd);
  3531. if (status)
  3532. return status;
  3533. /*
  3534. * Since the driver manages a single bus device, reset all
  3535. * targets known to the driver. Should any target reset
  3536. * fail, this routine returns failure to the midlayer.
  3537. */
  3538. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  3539. /* Search for mapped node by target ID */
  3540. match = 0;
  3541. spin_lock_irq(shost->host_lock);
  3542. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  3543. if (!NLP_CHK_NODE_ACT(ndlp))
  3544. continue;
  3545. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  3546. ndlp->nlp_sid == i &&
  3547. ndlp->rport) {
  3548. match = 1;
  3549. break;
  3550. }
  3551. }
  3552. spin_unlock_irq(shost->host_lock);
  3553. if (!match)
  3554. continue;
  3555. status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
  3556. i, 0, FCP_TARGET_RESET);
  3557. if (status != SUCCESS) {
  3558. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3559. "0700 Bus Reset on target %d failed\n",
  3560. i);
  3561. ret = FAILED;
  3562. }
  3563. }
  3564. /*
  3565. * We have to clean up i/o as : they may be orphaned by the TMFs
  3566. * above; or if any of the TMFs failed, they may be in an
  3567. * indeterminate state.
  3568. * We will report success if all the i/o aborts successfully.
  3569. */
  3570. status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
  3571. if (status != SUCCESS)
  3572. ret = FAILED;
  3573. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3574. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  3575. return ret;
  3576. }
  3577. /**
  3578. * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
  3579. * @sdev: Pointer to scsi_device.
  3580. *
  3581. * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
  3582. * globally available list of scsi buffers. This routine also makes sure scsi
  3583. * buffer is not allocated more than HBA limit conveyed to midlayer. This list
  3584. * of scsi buffer exists for the lifetime of the driver.
  3585. *
  3586. * Return codes:
  3587. * non-0 - Error
  3588. * 0 - Success
  3589. **/
  3590. static int
  3591. lpfc_slave_alloc(struct scsi_device *sdev)
  3592. {
  3593. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3594. struct lpfc_hba *phba = vport->phba;
  3595. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  3596. uint32_t total = 0;
  3597. uint32_t num_to_alloc = 0;
  3598. int num_allocated = 0;
  3599. uint32_t sdev_cnt;
  3600. if (!rport || fc_remote_port_chkready(rport))
  3601. return -ENXIO;
  3602. sdev->hostdata = rport->dd_data;
  3603. sdev_cnt = atomic_inc_return(&phba->sdev_cnt);
  3604. /*
  3605. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  3606. * available list of scsi buffers. Don't allocate more than the
  3607. * HBA limit conveyed to the midlayer via the host structure. The
  3608. * formula accounts for the lun_queue_depth + error handlers + 1
  3609. * extra. This list of scsi bufs exists for the lifetime of the driver.
  3610. */
  3611. total = phba->total_scsi_bufs;
  3612. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  3613. /* If allocated buffers are enough do nothing */
  3614. if ((sdev_cnt * (vport->cfg_lun_queue_depth + 2)) < total)
  3615. return 0;
  3616. /* Allow some exchanges to be available always to complete discovery */
  3617. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  3618. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3619. "0704 At limitation of %d preallocated "
  3620. "command buffers\n", total);
  3621. return 0;
  3622. /* Allow some exchanges to be available always to complete discovery */
  3623. } else if (total + num_to_alloc >
  3624. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  3625. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3626. "0705 Allocation request of %d "
  3627. "command buffers will exceed max of %d. "
  3628. "Reducing allocation request to %d.\n",
  3629. num_to_alloc, phba->cfg_hba_queue_depth,
  3630. (phba->cfg_hba_queue_depth - total));
  3631. num_to_alloc = phba->cfg_hba_queue_depth - total;
  3632. }
  3633. num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
  3634. if (num_to_alloc != num_allocated) {
  3635. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3636. "0708 Allocation request of %d "
  3637. "command buffers did not succeed. "
  3638. "Allocated %d buffers.\n",
  3639. num_to_alloc, num_allocated);
  3640. }
  3641. if (num_allocated > 0)
  3642. phba->total_scsi_bufs += num_allocated;
  3643. return 0;
  3644. }
  3645. /**
  3646. * lpfc_slave_configure - scsi_host_template slave_configure entry point
  3647. * @sdev: Pointer to scsi_device.
  3648. *
  3649. * This routine configures following items
  3650. * - Tag command queuing support for @sdev if supported.
  3651. * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
  3652. *
  3653. * Return codes:
  3654. * 0 - Success
  3655. **/
  3656. static int
  3657. lpfc_slave_configure(struct scsi_device *sdev)
  3658. {
  3659. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3660. struct lpfc_hba *phba = vport->phba;
  3661. if (sdev->tagged_supported)
  3662. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  3663. else
  3664. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  3665. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  3666. lpfc_sli_handle_fast_ring_event(phba,
  3667. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  3668. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  3669. lpfc_poll_rearm_timer(phba);
  3670. }
  3671. return 0;
  3672. }
  3673. /**
  3674. * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
  3675. * @sdev: Pointer to scsi_device.
  3676. *
  3677. * This routine sets @sdev hostatdata filed to null.
  3678. **/
  3679. static void
  3680. lpfc_slave_destroy(struct scsi_device *sdev)
  3681. {
  3682. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3683. struct lpfc_hba *phba = vport->phba;
  3684. atomic_dec(&phba->sdev_cnt);
  3685. sdev->hostdata = NULL;
  3686. return;
  3687. }
  3688. struct scsi_host_template lpfc_template = {
  3689. .module = THIS_MODULE,
  3690. .name = LPFC_DRIVER_NAME,
  3691. .info = lpfc_info,
  3692. .queuecommand = lpfc_queuecommand,
  3693. .eh_abort_handler = lpfc_abort_handler,
  3694. .eh_device_reset_handler = lpfc_device_reset_handler,
  3695. .eh_target_reset_handler = lpfc_target_reset_handler,
  3696. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  3697. .slave_alloc = lpfc_slave_alloc,
  3698. .slave_configure = lpfc_slave_configure,
  3699. .slave_destroy = lpfc_slave_destroy,
  3700. .scan_finished = lpfc_scan_finished,
  3701. .this_id = -1,
  3702. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  3703. .cmd_per_lun = LPFC_CMD_PER_LUN,
  3704. .use_clustering = ENABLE_CLUSTERING,
  3705. .shost_attrs = lpfc_hba_attrs,
  3706. .max_sectors = 0xFFFF,
  3707. .vendor_id = LPFC_NL_VENDOR_ID,
  3708. .change_queue_depth = lpfc_change_queue_depth,
  3709. };
  3710. struct scsi_host_template lpfc_vport_template = {
  3711. .module = THIS_MODULE,
  3712. .name = LPFC_DRIVER_NAME,
  3713. .info = lpfc_info,
  3714. .queuecommand = lpfc_queuecommand,
  3715. .eh_abort_handler = lpfc_abort_handler,
  3716. .eh_device_reset_handler = lpfc_device_reset_handler,
  3717. .eh_target_reset_handler = lpfc_target_reset_handler,
  3718. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  3719. .slave_alloc = lpfc_slave_alloc,
  3720. .slave_configure = lpfc_slave_configure,
  3721. .slave_destroy = lpfc_slave_destroy,
  3722. .scan_finished = lpfc_scan_finished,
  3723. .this_id = -1,
  3724. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  3725. .cmd_per_lun = LPFC_CMD_PER_LUN,
  3726. .use_clustering = ENABLE_CLUSTERING,
  3727. .shost_attrs = lpfc_vport_attrs,
  3728. .max_sectors = 0xFFFF,
  3729. .change_queue_depth = lpfc_change_queue_depth,
  3730. };