lpfc_scsi.c 109 KB

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