lpfc_scsi.c 109 KB

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