lpfc_scsi.c 117 KB

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