lpfc_scsi.c 113 KB

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