lpfc_scsi.c 113 KB

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