lpfc_scsi.c 110 KB

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