lpfc_scsi.c 112 KB

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