lpfc_scsi.c 89 KB

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