lpfc_scsi.c 108 KB

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