lpfc_scsi.c 108 KB

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