lpfc_scsi.c 115 KB

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