lpfc_scsi.c 122 KB

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