lpfc_scsi.c 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614
  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->status = IOSTAT_SUCCESS;
  568. spin_unlock_irqrestore(
  569. &phba->sli4_hba.abts_scsi_buf_list_lock,
  570. iflag);
  571. lpfc_release_scsi_buf_s4(phba, psb);
  572. return;
  573. }
  574. }
  575. spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
  576. iflag);
  577. }
  578. /**
  579. * lpfc_sli4_repost_scsi_sgl_list - Repsot the Scsi buffers sgl pages as block
  580. * @phba: pointer to lpfc hba data structure.
  581. *
  582. * This routine walks the list of scsi buffers that have been allocated and
  583. * repost them to the HBA by using SGL block post. This is needed after a
  584. * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
  585. * is responsible for moving all scsi buffers on the lpfc_abts_scsi_sgl_list
  586. * to the lpfc_scsi_buf_list. If the repost fails, reject all scsi buffers.
  587. *
  588. * Returns: 0 = success, non-zero failure.
  589. **/
  590. int
  591. lpfc_sli4_repost_scsi_sgl_list(struct lpfc_hba *phba)
  592. {
  593. struct lpfc_scsi_buf *psb;
  594. int index, status, bcnt = 0, rcnt = 0, rc = 0;
  595. LIST_HEAD(sblist);
  596. for (index = 0; index < phba->sli4_hba.scsi_xri_cnt; index++) {
  597. psb = phba->sli4_hba.lpfc_scsi_psb_array[index];
  598. if (psb) {
  599. /* Remove from SCSI buffer list */
  600. list_del(&psb->list);
  601. /* Add it to a local SCSI buffer list */
  602. list_add_tail(&psb->list, &sblist);
  603. if (++rcnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  604. bcnt = rcnt;
  605. rcnt = 0;
  606. }
  607. } else
  608. /* A hole present in the XRI array, need to skip */
  609. bcnt = rcnt;
  610. if (index == phba->sli4_hba.scsi_xri_cnt - 1)
  611. /* End of XRI array for SCSI buffer, complete */
  612. bcnt = rcnt;
  613. /* Continue until collect up to a nembed page worth of sgls */
  614. if (bcnt == 0)
  615. continue;
  616. /* Now, post the SCSI buffer list sgls as a block */
  617. status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
  618. /* Reset SCSI buffer count for next round of posting */
  619. bcnt = 0;
  620. while (!list_empty(&sblist)) {
  621. list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
  622. list);
  623. if (status) {
  624. /* Put this back on the abort scsi list */
  625. psb->status = IOSTAT_LOCAL_REJECT;
  626. psb->result = IOERR_ABORT_REQUESTED;
  627. rc++;
  628. } else
  629. psb->status = IOSTAT_SUCCESS;
  630. /* Put it back into the SCSI buffer list */
  631. lpfc_release_scsi_buf_s4(phba, psb);
  632. }
  633. }
  634. return rc;
  635. }
  636. /**
  637. * lpfc_new_scsi_buf_s4 - Scsi buffer allocator for HBA with SLI4 IF spec
  638. * @vport: The virtual port for which this call being executed.
  639. * @num_to_allocate: The requested number of buffers to allocate.
  640. *
  641. * This routine allocates a scsi buffer for device with SLI-4 interface spec,
  642. * the scsi buffer contains all the necessary information needed to initiate
  643. * a SCSI I/O.
  644. *
  645. * Return codes:
  646. * int - number of scsi buffers that were allocated.
  647. * 0 = failure, less than num_to_alloc is a partial failure.
  648. **/
  649. static int
  650. lpfc_new_scsi_buf_s4(struct lpfc_vport *vport, int num_to_alloc)
  651. {
  652. struct lpfc_hba *phba = vport->phba;
  653. struct lpfc_scsi_buf *psb;
  654. struct sli4_sge *sgl;
  655. IOCB_t *iocb;
  656. dma_addr_t pdma_phys_fcp_cmd;
  657. dma_addr_t pdma_phys_fcp_rsp;
  658. dma_addr_t pdma_phys_bpl, pdma_phys_bpl1;
  659. uint16_t iotag, last_xritag = NO_XRI;
  660. int status = 0, index;
  661. int bcnt;
  662. int non_sequential_xri = 0;
  663. int rc = 0;
  664. LIST_HEAD(sblist);
  665. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  666. psb = kzalloc(sizeof(struct lpfc_scsi_buf), GFP_KERNEL);
  667. if (!psb)
  668. break;
  669. /*
  670. * Get memory from the pci pool to map the virt space to pci bus
  671. * space for an I/O. The DMA buffer includes space for the
  672. * struct fcp_cmnd, struct fcp_rsp and the number of bde's
  673. * necessary to support the sg_tablesize.
  674. */
  675. psb->data = pci_pool_alloc(phba->lpfc_scsi_dma_buf_pool,
  676. GFP_KERNEL, &psb->dma_handle);
  677. if (!psb->data) {
  678. kfree(psb);
  679. break;
  680. }
  681. /* Initialize virtual ptrs to dma_buf region. */
  682. memset(psb->data, 0, phba->cfg_sg_dma_buf_size);
  683. /* Allocate iotag for psb->cur_iocbq. */
  684. iotag = lpfc_sli_next_iotag(phba, &psb->cur_iocbq);
  685. if (iotag == 0) {
  686. kfree(psb);
  687. break;
  688. }
  689. psb->cur_iocbq.sli4_xritag = lpfc_sli4_next_xritag(phba);
  690. if (psb->cur_iocbq.sli4_xritag == NO_XRI) {
  691. pci_pool_free(phba->lpfc_scsi_dma_buf_pool,
  692. psb->data, psb->dma_handle);
  693. kfree(psb);
  694. break;
  695. }
  696. if (last_xritag != NO_XRI
  697. && psb->cur_iocbq.sli4_xritag != (last_xritag+1)) {
  698. non_sequential_xri = 1;
  699. } else
  700. list_add_tail(&psb->list, &sblist);
  701. last_xritag = psb->cur_iocbq.sli4_xritag;
  702. index = phba->sli4_hba.scsi_xri_cnt++;
  703. psb->cur_iocbq.iocb_flag |= LPFC_IO_FCP;
  704. psb->fcp_bpl = psb->data;
  705. psb->fcp_cmnd = (psb->data + phba->cfg_sg_dma_buf_size)
  706. - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  707. psb->fcp_rsp = (struct fcp_rsp *)((uint8_t *)psb->fcp_cmnd +
  708. sizeof(struct fcp_cmnd));
  709. /* Initialize local short-hand pointers. */
  710. sgl = (struct sli4_sge *)psb->fcp_bpl;
  711. pdma_phys_bpl = psb->dma_handle;
  712. pdma_phys_fcp_cmd =
  713. (psb->dma_handle + phba->cfg_sg_dma_buf_size)
  714. - (sizeof(struct fcp_cmnd) + sizeof(struct fcp_rsp));
  715. pdma_phys_fcp_rsp = pdma_phys_fcp_cmd + sizeof(struct fcp_cmnd);
  716. /*
  717. * The first two bdes are the FCP_CMD and FCP_RSP. The balance
  718. * are sg list bdes. Initialize the first two and leave the
  719. * rest for queuecommand.
  720. */
  721. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_cmd));
  722. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_cmd));
  723. bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_cmnd));
  724. bf_set(lpfc_sli4_sge_last, sgl, 0);
  725. sgl->word2 = cpu_to_le32(sgl->word2);
  726. sgl->word3 = cpu_to_le32(sgl->word3);
  727. sgl++;
  728. /* Setup the physical region for the FCP RSP */
  729. sgl->addr_hi = cpu_to_le32(putPaddrHigh(pdma_phys_fcp_rsp));
  730. sgl->addr_lo = cpu_to_le32(putPaddrLow(pdma_phys_fcp_rsp));
  731. bf_set(lpfc_sli4_sge_len, sgl, sizeof(struct fcp_rsp));
  732. bf_set(lpfc_sli4_sge_last, sgl, 1);
  733. sgl->word2 = cpu_to_le32(sgl->word2);
  734. sgl->word3 = cpu_to_le32(sgl->word3);
  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->status = IOSTAT_LOCAL_REJECT;
  765. psb->result = IOERR_ABORT_REQUESTED;
  766. rc++;
  767. } else
  768. psb->status = IOSTAT_SUCCESS;
  769. /* Put it back into the SCSI buffer list */
  770. lpfc_release_scsi_buf_s4(phba, psb);
  771. break;
  772. }
  773. }
  774. if (bcnt) {
  775. status = lpfc_sli4_post_scsi_sgl_block(phba, &sblist, bcnt);
  776. /* Reset SCSI buffer count for next round of posting */
  777. while (!list_empty(&sblist)) {
  778. list_remove_head(&sblist, psb, struct lpfc_scsi_buf,
  779. list);
  780. if (status) {
  781. /* Put this back on the abort scsi list */
  782. psb->status = IOSTAT_LOCAL_REJECT;
  783. psb->result = IOERR_ABORT_REQUESTED;
  784. rc++;
  785. } else
  786. psb->status = IOSTAT_SUCCESS;
  787. /* Put it back into the SCSI buffer list */
  788. lpfc_release_scsi_buf_s4(phba, psb);
  789. }
  790. }
  791. return bcnt + non_sequential_xri - rc;
  792. }
  793. /**
  794. * lpfc_new_scsi_buf - Wrapper funciton for scsi buffer allocator
  795. * @vport: The virtual port for which this call being executed.
  796. * @num_to_allocate: The requested number of buffers to allocate.
  797. *
  798. * This routine wraps the actual SCSI buffer allocator function pointer from
  799. * the lpfc_hba struct.
  800. *
  801. * Return codes:
  802. * int - number of scsi buffers that were allocated.
  803. * 0 = failure, less than num_to_alloc is a partial failure.
  804. **/
  805. static inline int
  806. lpfc_new_scsi_buf(struct lpfc_vport *vport, int num_to_alloc)
  807. {
  808. return vport->phba->lpfc_new_scsi_buf(vport, num_to_alloc);
  809. }
  810. /**
  811. * lpfc_get_scsi_buf - Get a scsi buffer from lpfc_scsi_buf_list of the HBA
  812. * @phba: The HBA for which this call is being executed.
  813. *
  814. * This routine removes a scsi buffer from head of @phba lpfc_scsi_buf_list list
  815. * and returns to caller.
  816. *
  817. * Return codes:
  818. * NULL - Error
  819. * Pointer to lpfc_scsi_buf - Success
  820. **/
  821. static struct lpfc_scsi_buf*
  822. lpfc_get_scsi_buf(struct lpfc_hba * phba)
  823. {
  824. struct lpfc_scsi_buf * lpfc_cmd = NULL;
  825. struct list_head *scsi_buf_list = &phba->lpfc_scsi_buf_list;
  826. unsigned long iflag = 0;
  827. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  828. list_remove_head(scsi_buf_list, lpfc_cmd, struct lpfc_scsi_buf, list);
  829. if (lpfc_cmd) {
  830. lpfc_cmd->seg_cnt = 0;
  831. lpfc_cmd->nonsg_phys = 0;
  832. lpfc_cmd->prot_seg_cnt = 0;
  833. }
  834. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  835. return lpfc_cmd;
  836. }
  837. /**
  838. * lpfc_release_scsi_buf - Return a scsi buffer back to hba scsi buf list
  839. * @phba: The Hba for which this call is being executed.
  840. * @psb: The scsi buffer which is being released.
  841. *
  842. * This routine releases @psb scsi buffer by adding it to tail of @phba
  843. * lpfc_scsi_buf_list list.
  844. **/
  845. static void
  846. lpfc_release_scsi_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  847. {
  848. unsigned long iflag = 0;
  849. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  850. psb->pCmd = NULL;
  851. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  852. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  853. }
  854. /**
  855. * lpfc_release_scsi_buf_s4: Return a scsi buffer back to hba scsi buf list.
  856. * @phba: The Hba for which this call is being executed.
  857. * @psb: The scsi buffer which is being released.
  858. *
  859. * This routine releases @psb scsi buffer by adding it to tail of @phba
  860. * lpfc_scsi_buf_list list. For SLI4 XRI's are tied to the scsi buffer
  861. * and cannot be reused for at least RA_TOV amount of time if it was
  862. * aborted.
  863. **/
  864. static void
  865. lpfc_release_scsi_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  866. {
  867. unsigned long iflag = 0;
  868. if (psb->status == IOSTAT_LOCAL_REJECT
  869. && psb->result == IOERR_ABORT_REQUESTED) {
  870. spin_lock_irqsave(&phba->sli4_hba.abts_scsi_buf_list_lock,
  871. iflag);
  872. psb->pCmd = NULL;
  873. list_add_tail(&psb->list,
  874. &phba->sli4_hba.lpfc_abts_scsi_buf_list);
  875. spin_unlock_irqrestore(&phba->sli4_hba.abts_scsi_buf_list_lock,
  876. iflag);
  877. } else {
  878. spin_lock_irqsave(&phba->scsi_buf_list_lock, iflag);
  879. psb->pCmd = NULL;
  880. list_add_tail(&psb->list, &phba->lpfc_scsi_buf_list);
  881. spin_unlock_irqrestore(&phba->scsi_buf_list_lock, iflag);
  882. }
  883. }
  884. /**
  885. * lpfc_release_scsi_buf: Return a scsi buffer back to hba scsi buf list.
  886. * @phba: The Hba for which this call is being executed.
  887. * @psb: The scsi buffer which is being released.
  888. *
  889. * This routine releases @psb scsi buffer by adding it to tail of @phba
  890. * lpfc_scsi_buf_list list.
  891. **/
  892. static void
  893. lpfc_release_scsi_buf(struct lpfc_hba *phba, struct lpfc_scsi_buf *psb)
  894. {
  895. phba->lpfc_release_scsi_buf(phba, psb);
  896. }
  897. /**
  898. * lpfc_scsi_prep_dma_buf_s3 - DMA mapping for scsi buffer to SLI3 IF spec
  899. * @phba: The Hba for which this call is being executed.
  900. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  901. *
  902. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  903. * field of @lpfc_cmd for device with SLI-3 interface spec. This routine scans
  904. * through sg elements and format the bdea. This routine also initializes all
  905. * IOCB fields which are dependent on scsi command request buffer.
  906. *
  907. * Return codes:
  908. * 1 - Error
  909. * 0 - Success
  910. **/
  911. static int
  912. lpfc_scsi_prep_dma_buf_s3(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  913. {
  914. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  915. struct scatterlist *sgel = NULL;
  916. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  917. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  918. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  919. struct ulp_bde64 *data_bde = iocb_cmd->unsli3.fcp_ext.dbde;
  920. dma_addr_t physaddr;
  921. uint32_t num_bde = 0;
  922. int nseg, datadir = scsi_cmnd->sc_data_direction;
  923. /*
  924. * There are three possibilities here - use scatter-gather segment, use
  925. * the single mapping, or neither. Start the lpfc command prep by
  926. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  927. * data bde entry.
  928. */
  929. bpl += 2;
  930. if (scsi_sg_count(scsi_cmnd)) {
  931. /*
  932. * The driver stores the segment count returned from pci_map_sg
  933. * because this a count of dma-mappings used to map the use_sg
  934. * pages. They are not guaranteed to be the same for those
  935. * architectures that implement an IOMMU.
  936. */
  937. nseg = dma_map_sg(&phba->pcidev->dev, scsi_sglist(scsi_cmnd),
  938. scsi_sg_count(scsi_cmnd), datadir);
  939. if (unlikely(!nseg))
  940. return 1;
  941. lpfc_cmd->seg_cnt = nseg;
  942. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  943. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  944. "9064 BLKGRD: %s: Too many sg segments from "
  945. "dma_map_sg. Config %d, seg_cnt %d\n",
  946. __func__, phba->cfg_sg_seg_cnt,
  947. lpfc_cmd->seg_cnt);
  948. scsi_dma_unmap(scsi_cmnd);
  949. return 1;
  950. }
  951. /*
  952. * The driver established a maximum scatter-gather segment count
  953. * during probe that limits the number of sg elements in any
  954. * single scsi command. Just run through the seg_cnt and format
  955. * the bde's.
  956. * When using SLI-3 the driver will try to fit all the BDEs into
  957. * the IOCB. If it can't then the BDEs get added to a BPL as it
  958. * does for SLI-2 mode.
  959. */
  960. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  961. physaddr = sg_dma_address(sgel);
  962. if (phba->sli_rev == 3 &&
  963. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  964. nseg <= LPFC_EXT_DATA_BDE_COUNT) {
  965. data_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  966. data_bde->tus.f.bdeSize = sg_dma_len(sgel);
  967. data_bde->addrLow = putPaddrLow(physaddr);
  968. data_bde->addrHigh = putPaddrHigh(physaddr);
  969. data_bde++;
  970. } else {
  971. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  972. bpl->tus.f.bdeSize = sg_dma_len(sgel);
  973. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  974. bpl->addrLow =
  975. le32_to_cpu(putPaddrLow(physaddr));
  976. bpl->addrHigh =
  977. le32_to_cpu(putPaddrHigh(physaddr));
  978. bpl++;
  979. }
  980. }
  981. }
  982. /*
  983. * Finish initializing those IOCB fields that are dependent on the
  984. * scsi_cmnd request_buffer. Note that for SLI-2 the bdeSize is
  985. * explicitly reinitialized and for SLI-3 the extended bde count is
  986. * explicitly reinitialized since all iocb memory resources are reused.
  987. */
  988. if (phba->sli_rev == 3 &&
  989. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED)) {
  990. if (num_bde > LPFC_EXT_DATA_BDE_COUNT) {
  991. /*
  992. * The extended IOCB format can only fit 3 BDE or a BPL.
  993. * This I/O has more than 3 BDE so the 1st data bde will
  994. * be a BPL that is filled in here.
  995. */
  996. physaddr = lpfc_cmd->dma_handle;
  997. data_bde->tus.f.bdeFlags = BUFF_TYPE_BLP_64;
  998. data_bde->tus.f.bdeSize = (num_bde *
  999. sizeof(struct ulp_bde64));
  1000. physaddr += (sizeof(struct fcp_cmnd) +
  1001. sizeof(struct fcp_rsp) +
  1002. (2 * sizeof(struct ulp_bde64)));
  1003. data_bde->addrHigh = putPaddrHigh(physaddr);
  1004. data_bde->addrLow = putPaddrLow(physaddr);
  1005. /* ebde count includes the responce bde and data bpl */
  1006. iocb_cmd->unsli3.fcp_ext.ebde_count = 2;
  1007. } else {
  1008. /* ebde count includes the responce bde and data bdes */
  1009. iocb_cmd->unsli3.fcp_ext.ebde_count = (num_bde + 1);
  1010. }
  1011. } else {
  1012. iocb_cmd->un.fcpi64.bdl.bdeSize =
  1013. ((num_bde + 2) * sizeof(struct ulp_bde64));
  1014. }
  1015. fcp_cmnd->fcpDl = cpu_to_be32(scsi_bufflen(scsi_cmnd));
  1016. /*
  1017. * Due to difference in data length between DIF/non-DIF paths,
  1018. * we need to set word 4 of IOCB here
  1019. */
  1020. iocb_cmd->un.fcpi.fcpi_parm = scsi_bufflen(scsi_cmnd);
  1021. return 0;
  1022. }
  1023. /*
  1024. * Given a scsi cmnd, determine the BlockGuard profile to be used
  1025. * with the cmd
  1026. */
  1027. static int
  1028. lpfc_sc_to_sli_prof(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  1029. {
  1030. uint8_t guard_type = scsi_host_get_guard(sc->device->host);
  1031. uint8_t ret_prof = LPFC_PROF_INVALID;
  1032. if (guard_type == SHOST_DIX_GUARD_IP) {
  1033. switch (scsi_get_prot_op(sc)) {
  1034. case SCSI_PROT_READ_INSERT:
  1035. case SCSI_PROT_WRITE_STRIP:
  1036. ret_prof = LPFC_PROF_AST2;
  1037. break;
  1038. case SCSI_PROT_READ_STRIP:
  1039. case SCSI_PROT_WRITE_INSERT:
  1040. ret_prof = LPFC_PROF_A1;
  1041. break;
  1042. case SCSI_PROT_READ_PASS:
  1043. case SCSI_PROT_WRITE_PASS:
  1044. ret_prof = LPFC_PROF_AST1;
  1045. break;
  1046. case SCSI_PROT_NORMAL:
  1047. default:
  1048. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1049. "9063 BLKGRD:Bad op/guard:%d/%d combination\n",
  1050. scsi_get_prot_op(sc), guard_type);
  1051. break;
  1052. }
  1053. } else if (guard_type == SHOST_DIX_GUARD_CRC) {
  1054. switch (scsi_get_prot_op(sc)) {
  1055. case SCSI_PROT_READ_STRIP:
  1056. case SCSI_PROT_WRITE_INSERT:
  1057. ret_prof = LPFC_PROF_A1;
  1058. break;
  1059. case SCSI_PROT_READ_PASS:
  1060. case SCSI_PROT_WRITE_PASS:
  1061. ret_prof = LPFC_PROF_C1;
  1062. break;
  1063. case SCSI_PROT_READ_INSERT:
  1064. case SCSI_PROT_WRITE_STRIP:
  1065. case SCSI_PROT_NORMAL:
  1066. default:
  1067. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1068. "9075 BLKGRD: Bad op/guard:%d/%d combination\n",
  1069. scsi_get_prot_op(sc), guard_type);
  1070. break;
  1071. }
  1072. } else {
  1073. /* unsupported format */
  1074. BUG();
  1075. }
  1076. return ret_prof;
  1077. }
  1078. struct scsi_dif_tuple {
  1079. __be16 guard_tag; /* Checksum */
  1080. __be16 app_tag; /* Opaque storage */
  1081. __be32 ref_tag; /* Target LBA or indirect LBA */
  1082. };
  1083. static inline unsigned
  1084. lpfc_cmd_blksize(struct scsi_cmnd *sc)
  1085. {
  1086. return sc->device->sector_size;
  1087. }
  1088. /**
  1089. * lpfc_get_cmd_dif_parms - Extract DIF parameters from SCSI command
  1090. * @sc: in: SCSI command
  1091. * @apptagmask: out: app tag mask
  1092. * @apptagval: out: app tag value
  1093. * @reftag: out: ref tag (reference tag)
  1094. *
  1095. * Description:
  1096. * Extract DIF parameters from the command if possible. Otherwise,
  1097. * use default parameters.
  1098. *
  1099. **/
  1100. static inline void
  1101. lpfc_get_cmd_dif_parms(struct scsi_cmnd *sc, uint16_t *apptagmask,
  1102. uint16_t *apptagval, uint32_t *reftag)
  1103. {
  1104. struct scsi_dif_tuple *spt;
  1105. unsigned char op = scsi_get_prot_op(sc);
  1106. unsigned int protcnt = scsi_prot_sg_count(sc);
  1107. static int cnt;
  1108. if (protcnt && (op == SCSI_PROT_WRITE_STRIP ||
  1109. op == SCSI_PROT_WRITE_PASS)) {
  1110. cnt++;
  1111. spt = page_address(sg_page(scsi_prot_sglist(sc))) +
  1112. scsi_prot_sglist(sc)[0].offset;
  1113. *apptagmask = 0;
  1114. *apptagval = 0;
  1115. *reftag = cpu_to_be32(spt->ref_tag);
  1116. } else {
  1117. /* SBC defines ref tag to be lower 32bits of LBA */
  1118. *reftag = (uint32_t) (0xffffffff & scsi_get_lba(sc));
  1119. *apptagmask = 0;
  1120. *apptagval = 0;
  1121. }
  1122. }
  1123. /*
  1124. * This function sets up buffer list for protection groups of
  1125. * type LPFC_PG_TYPE_NO_DIF
  1126. *
  1127. * This is usually used when the HBA is instructed to generate
  1128. * DIFs and insert them into data stream (or strip DIF from
  1129. * incoming data stream)
  1130. *
  1131. * The buffer list consists of just one protection group described
  1132. * below:
  1133. * +-------------------------+
  1134. * start of prot group --> | PDE_1 |
  1135. * +-------------------------+
  1136. * | Data BDE |
  1137. * +-------------------------+
  1138. * |more Data BDE's ... (opt)|
  1139. * +-------------------------+
  1140. *
  1141. * @sc: pointer to scsi command we're working on
  1142. * @bpl: pointer to buffer list for protection groups
  1143. * @datacnt: number of segments of data that have been dma mapped
  1144. *
  1145. * Note: Data s/g buffers have been dma mapped
  1146. */
  1147. static int
  1148. lpfc_bg_setup_bpl(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1149. struct ulp_bde64 *bpl, int datasegcnt)
  1150. {
  1151. struct scatterlist *sgde = NULL; /* s/g data entry */
  1152. struct lpfc_pde *pde1 = NULL;
  1153. dma_addr_t physaddr;
  1154. int i = 0, num_bde = 0;
  1155. int datadir = sc->sc_data_direction;
  1156. int prof = LPFC_PROF_INVALID;
  1157. unsigned blksize;
  1158. uint32_t reftag;
  1159. uint16_t apptagmask, apptagval;
  1160. pde1 = (struct lpfc_pde *) bpl;
  1161. prof = lpfc_sc_to_sli_prof(phba, sc);
  1162. if (prof == LPFC_PROF_INVALID)
  1163. goto out;
  1164. /* extract some info from the scsi command for PDE1*/
  1165. blksize = lpfc_cmd_blksize(sc);
  1166. lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
  1167. /* setup PDE1 with what we have */
  1168. lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
  1169. BG_EC_STOP_ERR);
  1170. lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
  1171. num_bde++;
  1172. bpl++;
  1173. /* assumption: caller has already run dma_map_sg on command data */
  1174. scsi_for_each_sg(sc, sgde, datasegcnt, i) {
  1175. physaddr = sg_dma_address(sgde);
  1176. bpl->addrLow = le32_to_cpu(putPaddrLow(physaddr));
  1177. bpl->addrHigh = le32_to_cpu(putPaddrHigh(physaddr));
  1178. bpl->tus.f.bdeSize = sg_dma_len(sgde);
  1179. if (datadir == DMA_TO_DEVICE)
  1180. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1181. else
  1182. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1183. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1184. bpl++;
  1185. num_bde++;
  1186. }
  1187. out:
  1188. return num_bde;
  1189. }
  1190. /*
  1191. * This function sets up buffer list for protection groups of
  1192. * type LPFC_PG_TYPE_DIF_BUF
  1193. *
  1194. * This is usually used when DIFs are in their own buffers,
  1195. * separate from the data. The HBA can then by instructed
  1196. * to place the DIFs in the outgoing stream. For read operations,
  1197. * The HBA could extract the DIFs and place it in DIF buffers.
  1198. *
  1199. * The buffer list for this type consists of one or more of the
  1200. * protection groups described below:
  1201. * +-------------------------+
  1202. * start of first prot group --> | PDE_1 |
  1203. * +-------------------------+
  1204. * | PDE_3 (Prot BDE) |
  1205. * +-------------------------+
  1206. * | Data BDE |
  1207. * +-------------------------+
  1208. * |more Data BDE's ... (opt)|
  1209. * +-------------------------+
  1210. * start of new prot group --> | PDE_1 |
  1211. * +-------------------------+
  1212. * | ... |
  1213. * +-------------------------+
  1214. *
  1215. * @sc: pointer to scsi command we're working on
  1216. * @bpl: pointer to buffer list for protection groups
  1217. * @datacnt: number of segments of data that have been dma mapped
  1218. * @protcnt: number of segment of protection data that have been dma mapped
  1219. *
  1220. * Note: It is assumed that both data and protection s/g buffers have been
  1221. * mapped for DMA
  1222. */
  1223. static int
  1224. lpfc_bg_setup_bpl_prot(struct lpfc_hba *phba, struct scsi_cmnd *sc,
  1225. struct ulp_bde64 *bpl, int datacnt, int protcnt)
  1226. {
  1227. struct scatterlist *sgde = NULL; /* s/g data entry */
  1228. struct scatterlist *sgpe = NULL; /* s/g prot entry */
  1229. struct lpfc_pde *pde1 = NULL;
  1230. struct ulp_bde64 *prot_bde = NULL;
  1231. dma_addr_t dataphysaddr, protphysaddr;
  1232. unsigned short curr_data = 0, curr_prot = 0;
  1233. unsigned int split_offset, protgroup_len;
  1234. unsigned int protgrp_blks, protgrp_bytes;
  1235. unsigned int remainder, subtotal;
  1236. int prof = LPFC_PROF_INVALID;
  1237. int datadir = sc->sc_data_direction;
  1238. unsigned char pgdone = 0, alldone = 0;
  1239. unsigned blksize;
  1240. uint32_t reftag;
  1241. uint16_t apptagmask, apptagval;
  1242. int num_bde = 0;
  1243. sgpe = scsi_prot_sglist(sc);
  1244. sgde = scsi_sglist(sc);
  1245. if (!sgpe || !sgde) {
  1246. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1247. "9020 Invalid s/g entry: data=0x%p prot=0x%p\n",
  1248. sgpe, sgde);
  1249. return 0;
  1250. }
  1251. prof = lpfc_sc_to_sli_prof(phba, sc);
  1252. if (prof == LPFC_PROF_INVALID)
  1253. goto out;
  1254. /* extract some info from the scsi command for PDE1*/
  1255. blksize = lpfc_cmd_blksize(sc);
  1256. lpfc_get_cmd_dif_parms(sc, &apptagmask, &apptagval, &reftag);
  1257. split_offset = 0;
  1258. do {
  1259. /* setup the first PDE_1 */
  1260. pde1 = (struct lpfc_pde *) bpl;
  1261. lpfc_pde_set_bg_parms(pde1, LPFC_PDE1_DESCRIPTOR, prof, blksize,
  1262. BG_EC_STOP_ERR);
  1263. lpfc_pde_set_dif_parms(pde1, apptagmask, apptagval, reftag);
  1264. num_bde++;
  1265. bpl++;
  1266. /* setup the first BDE that points to protection buffer */
  1267. prot_bde = (struct ulp_bde64 *) bpl;
  1268. protphysaddr = sg_dma_address(sgpe);
  1269. prot_bde->addrLow = le32_to_cpu(putPaddrLow(protphysaddr));
  1270. prot_bde->addrHigh = le32_to_cpu(putPaddrHigh(protphysaddr));
  1271. protgroup_len = sg_dma_len(sgpe);
  1272. /* must be integer multiple of the DIF block length */
  1273. BUG_ON(protgroup_len % 8);
  1274. protgrp_blks = protgroup_len / 8;
  1275. protgrp_bytes = protgrp_blks * blksize;
  1276. prot_bde->tus.f.bdeSize = protgroup_len;
  1277. if (datadir == DMA_TO_DEVICE)
  1278. prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1279. else
  1280. prot_bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1281. prot_bde->tus.w = le32_to_cpu(bpl->tus.w);
  1282. curr_prot++;
  1283. num_bde++;
  1284. /* setup BDE's for data blocks associated with DIF data */
  1285. pgdone = 0;
  1286. subtotal = 0; /* total bytes processed for current prot grp */
  1287. while (!pgdone) {
  1288. if (!sgde) {
  1289. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1290. "9065 BLKGRD:%s Invalid data segment\n",
  1291. __func__);
  1292. return 0;
  1293. }
  1294. bpl++;
  1295. dataphysaddr = sg_dma_address(sgde) + split_offset;
  1296. bpl->addrLow = le32_to_cpu(putPaddrLow(dataphysaddr));
  1297. bpl->addrHigh = le32_to_cpu(putPaddrHigh(dataphysaddr));
  1298. remainder = sg_dma_len(sgde) - split_offset;
  1299. if ((subtotal + remainder) <= protgrp_bytes) {
  1300. /* we can use this whole buffer */
  1301. bpl->tus.f.bdeSize = remainder;
  1302. split_offset = 0;
  1303. if ((subtotal + remainder) == protgrp_bytes)
  1304. pgdone = 1;
  1305. } else {
  1306. /* must split this buffer with next prot grp */
  1307. bpl->tus.f.bdeSize = protgrp_bytes - subtotal;
  1308. split_offset += bpl->tus.f.bdeSize;
  1309. }
  1310. subtotal += bpl->tus.f.bdeSize;
  1311. if (datadir == DMA_TO_DEVICE)
  1312. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1313. else
  1314. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  1315. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  1316. num_bde++;
  1317. curr_data++;
  1318. if (split_offset)
  1319. break;
  1320. /* Move to the next s/g segment if possible */
  1321. sgde = sg_next(sgde);
  1322. }
  1323. /* are we done ? */
  1324. if (curr_prot == protcnt) {
  1325. alldone = 1;
  1326. } else if (curr_prot < protcnt) {
  1327. /* advance to next prot buffer */
  1328. sgpe = sg_next(sgpe);
  1329. bpl++;
  1330. /* update the reference tag */
  1331. reftag += protgrp_blks;
  1332. } else {
  1333. /* if we're here, we have a bug */
  1334. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1335. "9054 BLKGRD: bug in %s\n", __func__);
  1336. }
  1337. } while (!alldone);
  1338. out:
  1339. return num_bde;
  1340. }
  1341. /*
  1342. * Given a SCSI command that supports DIF, determine composition of protection
  1343. * groups involved in setting up buffer lists
  1344. *
  1345. * Returns:
  1346. * for DIF (for both read and write)
  1347. * */
  1348. static int
  1349. lpfc_prot_group_type(struct lpfc_hba *phba, struct scsi_cmnd *sc)
  1350. {
  1351. int ret = LPFC_PG_TYPE_INVALID;
  1352. unsigned char op = scsi_get_prot_op(sc);
  1353. switch (op) {
  1354. case SCSI_PROT_READ_STRIP:
  1355. case SCSI_PROT_WRITE_INSERT:
  1356. ret = LPFC_PG_TYPE_NO_DIF;
  1357. break;
  1358. case SCSI_PROT_READ_INSERT:
  1359. case SCSI_PROT_WRITE_STRIP:
  1360. case SCSI_PROT_READ_PASS:
  1361. case SCSI_PROT_WRITE_PASS:
  1362. ret = LPFC_PG_TYPE_DIF_BUF;
  1363. break;
  1364. default:
  1365. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1366. "9021 Unsupported protection op:%d\n", op);
  1367. break;
  1368. }
  1369. return ret;
  1370. }
  1371. /*
  1372. * This is the protection/DIF aware version of
  1373. * lpfc_scsi_prep_dma_buf(). It may be a good idea to combine the
  1374. * two functions eventually, but for now, it's here
  1375. */
  1376. static int
  1377. lpfc_bg_scsi_prep_dma_buf(struct lpfc_hba *phba,
  1378. struct lpfc_scsi_buf *lpfc_cmd)
  1379. {
  1380. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1381. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1382. struct ulp_bde64 *bpl = lpfc_cmd->fcp_bpl;
  1383. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1384. uint32_t num_bde = 0;
  1385. int datasegcnt, protsegcnt, datadir = scsi_cmnd->sc_data_direction;
  1386. int prot_group_type = 0;
  1387. int diflen, fcpdl;
  1388. unsigned blksize;
  1389. /*
  1390. * Start the lpfc command prep by bumping the bpl beyond fcp_cmnd
  1391. * fcp_rsp regions to the first data bde entry
  1392. */
  1393. bpl += 2;
  1394. if (scsi_sg_count(scsi_cmnd)) {
  1395. /*
  1396. * The driver stores the segment count returned from pci_map_sg
  1397. * because this a count of dma-mappings used to map the use_sg
  1398. * pages. They are not guaranteed to be the same for those
  1399. * architectures that implement an IOMMU.
  1400. */
  1401. datasegcnt = dma_map_sg(&phba->pcidev->dev,
  1402. scsi_sglist(scsi_cmnd),
  1403. scsi_sg_count(scsi_cmnd), datadir);
  1404. if (unlikely(!datasegcnt))
  1405. return 1;
  1406. lpfc_cmd->seg_cnt = datasegcnt;
  1407. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1408. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1409. "9067 BLKGRD: %s: Too many sg segments"
  1410. " from dma_map_sg. Config %d, seg_cnt"
  1411. " %d\n",
  1412. __func__, phba->cfg_sg_seg_cnt,
  1413. lpfc_cmd->seg_cnt);
  1414. scsi_dma_unmap(scsi_cmnd);
  1415. return 1;
  1416. }
  1417. prot_group_type = lpfc_prot_group_type(phba, scsi_cmnd);
  1418. switch (prot_group_type) {
  1419. case LPFC_PG_TYPE_NO_DIF:
  1420. num_bde = lpfc_bg_setup_bpl(phba, scsi_cmnd, bpl,
  1421. datasegcnt);
  1422. /* we shoud have 2 or more entries in buffer list */
  1423. if (num_bde < 2)
  1424. goto err;
  1425. break;
  1426. case LPFC_PG_TYPE_DIF_BUF:{
  1427. /*
  1428. * This type indicates that protection buffers are
  1429. * passed to the driver, so that needs to be prepared
  1430. * for DMA
  1431. */
  1432. protsegcnt = dma_map_sg(&phba->pcidev->dev,
  1433. scsi_prot_sglist(scsi_cmnd),
  1434. scsi_prot_sg_count(scsi_cmnd), datadir);
  1435. if (unlikely(!protsegcnt)) {
  1436. scsi_dma_unmap(scsi_cmnd);
  1437. return 1;
  1438. }
  1439. lpfc_cmd->prot_seg_cnt = protsegcnt;
  1440. if (lpfc_cmd->prot_seg_cnt
  1441. > phba->cfg_prot_sg_seg_cnt) {
  1442. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1443. "9068 BLKGRD: %s: Too many prot sg "
  1444. "segments from dma_map_sg. Config %d,"
  1445. "prot_seg_cnt %d\n", __func__,
  1446. phba->cfg_prot_sg_seg_cnt,
  1447. lpfc_cmd->prot_seg_cnt);
  1448. dma_unmap_sg(&phba->pcidev->dev,
  1449. scsi_prot_sglist(scsi_cmnd),
  1450. scsi_prot_sg_count(scsi_cmnd),
  1451. datadir);
  1452. scsi_dma_unmap(scsi_cmnd);
  1453. return 1;
  1454. }
  1455. num_bde = lpfc_bg_setup_bpl_prot(phba, scsi_cmnd, bpl,
  1456. datasegcnt, protsegcnt);
  1457. /* we shoud have 3 or more entries in buffer list */
  1458. if (num_bde < 3)
  1459. goto err;
  1460. break;
  1461. }
  1462. case LPFC_PG_TYPE_INVALID:
  1463. default:
  1464. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1465. "9022 Unexpected protection group %i\n",
  1466. prot_group_type);
  1467. return 1;
  1468. }
  1469. }
  1470. /*
  1471. * Finish initializing those IOCB fields that are dependent on the
  1472. * scsi_cmnd request_buffer. Note that the bdeSize is explicitly
  1473. * reinitialized since all iocb memory resources are used many times
  1474. * for transmit, receive, and continuation bpl's.
  1475. */
  1476. iocb_cmd->un.fcpi64.bdl.bdeSize = (2 * sizeof(struct ulp_bde64));
  1477. iocb_cmd->un.fcpi64.bdl.bdeSize += (num_bde * sizeof(struct ulp_bde64));
  1478. iocb_cmd->ulpBdeCount = 1;
  1479. iocb_cmd->ulpLe = 1;
  1480. fcpdl = scsi_bufflen(scsi_cmnd);
  1481. if (scsi_get_prot_type(scsi_cmnd) == SCSI_PROT_DIF_TYPE1) {
  1482. /*
  1483. * We are in DIF Type 1 mode
  1484. * Every data block has a 8 byte DIF (trailer)
  1485. * attached to it. Must ajust FCP data length
  1486. */
  1487. blksize = lpfc_cmd_blksize(scsi_cmnd);
  1488. diflen = (fcpdl / blksize) * 8;
  1489. fcpdl += diflen;
  1490. }
  1491. fcp_cmnd->fcpDl = be32_to_cpu(fcpdl);
  1492. /*
  1493. * Due to difference in data length between DIF/non-DIF paths,
  1494. * we need to set word 4 of IOCB here
  1495. */
  1496. iocb_cmd->un.fcpi.fcpi_parm = fcpdl;
  1497. return 0;
  1498. err:
  1499. lpfc_printf_log(phba, KERN_ERR, LOG_FCP,
  1500. "9023 Could not setup all needed BDE's"
  1501. "prot_group_type=%d, num_bde=%d\n",
  1502. prot_group_type, num_bde);
  1503. return 1;
  1504. }
  1505. /*
  1506. * This function checks for BlockGuard errors detected by
  1507. * the HBA. In case of errors, the ASC/ASCQ fields in the
  1508. * sense buffer will be set accordingly, paired with
  1509. * ILLEGAL_REQUEST to signal to the kernel that the HBA
  1510. * detected corruption.
  1511. *
  1512. * Returns:
  1513. * 0 - No error found
  1514. * 1 - BlockGuard error found
  1515. * -1 - Internal error (bad profile, ...etc)
  1516. */
  1517. static int
  1518. lpfc_parse_bg_err(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd,
  1519. struct lpfc_iocbq *pIocbOut)
  1520. {
  1521. struct scsi_cmnd *cmd = lpfc_cmd->pCmd;
  1522. struct sli3_bg_fields *bgf = &pIocbOut->iocb.unsli3.sli3_bg;
  1523. int ret = 0;
  1524. uint32_t bghm = bgf->bghm;
  1525. uint32_t bgstat = bgf->bgstat;
  1526. uint64_t failing_sector = 0;
  1527. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9069 BLKGRD: BG ERROR in cmd"
  1528. " 0x%x lba 0x%llx blk cnt 0x%x "
  1529. "bgstat=0x%x bghm=0x%x\n",
  1530. cmd->cmnd[0], (unsigned long long)scsi_get_lba(cmd),
  1531. blk_rq_sectors(cmd->request), bgstat, bghm);
  1532. spin_lock(&_dump_buf_lock);
  1533. if (!_dump_buf_done) {
  1534. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9070 BLKGRD: Saving"
  1535. " Data for %u blocks to debugfs\n",
  1536. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1537. lpfc_debug_save_data(phba, cmd);
  1538. /* If we have a prot sgl, save the DIF buffer */
  1539. if (lpfc_prot_group_type(phba, cmd) ==
  1540. LPFC_PG_TYPE_DIF_BUF) {
  1541. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9071 BLKGRD: "
  1542. "Saving DIF for %u blocks to debugfs\n",
  1543. (cmd->cmnd[7] << 8 | cmd->cmnd[8]));
  1544. lpfc_debug_save_dif(phba, cmd);
  1545. }
  1546. _dump_buf_done = 1;
  1547. }
  1548. spin_unlock(&_dump_buf_lock);
  1549. if (lpfc_bgs_get_invalid_prof(bgstat)) {
  1550. cmd->result = ScsiResult(DID_ERROR, 0);
  1551. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9072 BLKGRD: Invalid"
  1552. " BlockGuard profile. bgstat:0x%x\n",
  1553. bgstat);
  1554. ret = (-1);
  1555. goto out;
  1556. }
  1557. if (lpfc_bgs_get_uninit_dif_block(bgstat)) {
  1558. cmd->result = ScsiResult(DID_ERROR, 0);
  1559. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9073 BLKGRD: "
  1560. "Invalid BlockGuard DIF Block. bgstat:0x%x\n",
  1561. bgstat);
  1562. ret = (-1);
  1563. goto out;
  1564. }
  1565. if (lpfc_bgs_get_guard_err(bgstat)) {
  1566. ret = 1;
  1567. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1568. 0x10, 0x1);
  1569. cmd->result = DRIVER_SENSE << 24
  1570. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1571. phba->bg_guard_err_cnt++;
  1572. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1573. "9055 BLKGRD: guard_tag error\n");
  1574. }
  1575. if (lpfc_bgs_get_reftag_err(bgstat)) {
  1576. ret = 1;
  1577. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1578. 0x10, 0x3);
  1579. cmd->result = DRIVER_SENSE << 24
  1580. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1581. phba->bg_reftag_err_cnt++;
  1582. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1583. "9056 BLKGRD: ref_tag error\n");
  1584. }
  1585. if (lpfc_bgs_get_apptag_err(bgstat)) {
  1586. ret = 1;
  1587. scsi_build_sense_buffer(1, cmd->sense_buffer, ILLEGAL_REQUEST,
  1588. 0x10, 0x2);
  1589. cmd->result = DRIVER_SENSE << 24
  1590. | ScsiResult(DID_ABORT, SAM_STAT_CHECK_CONDITION);
  1591. phba->bg_apptag_err_cnt++;
  1592. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1593. "9061 BLKGRD: app_tag error\n");
  1594. }
  1595. if (lpfc_bgs_get_hi_water_mark_present(bgstat)) {
  1596. /*
  1597. * setup sense data descriptor 0 per SPC-4 as an information
  1598. * field, and put the failing LBA in it
  1599. */
  1600. cmd->sense_buffer[8] = 0; /* Information */
  1601. cmd->sense_buffer[9] = 0xa; /* Add. length */
  1602. bghm /= cmd->device->sector_size;
  1603. failing_sector = scsi_get_lba(cmd);
  1604. failing_sector += bghm;
  1605. put_unaligned_be64(failing_sector, &cmd->sense_buffer[10]);
  1606. }
  1607. if (!ret) {
  1608. /* No error was reported - problem in FW? */
  1609. cmd->result = ScsiResult(DID_ERROR, 0);
  1610. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  1611. "9057 BLKGRD: no errors reported!\n");
  1612. }
  1613. out:
  1614. return ret;
  1615. }
  1616. /**
  1617. * lpfc_scsi_prep_dma_buf_s4 - DMA mapping for scsi buffer to SLI4 IF spec
  1618. * @phba: The Hba for which this call is being executed.
  1619. * @lpfc_cmd: The scsi buffer which is going to be mapped.
  1620. *
  1621. * This routine does the pci dma mapping for scatter-gather list of scsi cmnd
  1622. * field of @lpfc_cmd for device with SLI-4 interface spec.
  1623. *
  1624. * Return codes:
  1625. * 1 - Error
  1626. * 0 - Success
  1627. **/
  1628. static int
  1629. lpfc_scsi_prep_dma_buf_s4(struct lpfc_hba *phba, struct lpfc_scsi_buf *lpfc_cmd)
  1630. {
  1631. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  1632. struct scatterlist *sgel = NULL;
  1633. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  1634. struct sli4_sge *sgl = (struct sli4_sge *)lpfc_cmd->fcp_bpl;
  1635. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  1636. dma_addr_t physaddr;
  1637. uint32_t num_bde = 0;
  1638. uint32_t dma_len;
  1639. uint32_t dma_offset = 0;
  1640. int nseg;
  1641. /*
  1642. * There are three possibilities here - use scatter-gather segment, use
  1643. * the single mapping, or neither. Start the lpfc command prep by
  1644. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  1645. * data bde entry.
  1646. */
  1647. if (scsi_sg_count(scsi_cmnd)) {
  1648. /*
  1649. * The driver stores the segment count returned from pci_map_sg
  1650. * because this a count of dma-mappings used to map the use_sg
  1651. * pages. They are not guaranteed to be the same for those
  1652. * architectures that implement an IOMMU.
  1653. */
  1654. nseg = scsi_dma_map(scsi_cmnd);
  1655. if (unlikely(!nseg))
  1656. return 1;
  1657. sgl += 1;
  1658. /* clear the last flag in the fcp_rsp map entry */
  1659. sgl->word2 = le32_to_cpu(sgl->word2);
  1660. bf_set(lpfc_sli4_sge_last, sgl, 0);
  1661. sgl->word2 = cpu_to_le32(sgl->word2);
  1662. sgl += 1;
  1663. lpfc_cmd->seg_cnt = nseg;
  1664. if (lpfc_cmd->seg_cnt > phba->cfg_sg_seg_cnt) {
  1665. lpfc_printf_log(phba, KERN_ERR, LOG_BG, "9074 BLKGRD:"
  1666. " %s: Too many sg segments from "
  1667. "dma_map_sg. Config %d, seg_cnt %d\n",
  1668. __func__, phba->cfg_sg_seg_cnt,
  1669. lpfc_cmd->seg_cnt);
  1670. scsi_dma_unmap(scsi_cmnd);
  1671. return 1;
  1672. }
  1673. /*
  1674. * The driver established a maximum scatter-gather segment count
  1675. * during probe that limits the number of sg elements in any
  1676. * single scsi command. Just run through the seg_cnt and format
  1677. * the sge's.
  1678. * When using SLI-3 the driver will try to fit all the BDEs into
  1679. * the IOCB. If it can't then the BDEs get added to a BPL as it
  1680. * does for SLI-2 mode.
  1681. */
  1682. scsi_for_each_sg(scsi_cmnd, sgel, nseg, num_bde) {
  1683. physaddr = sg_dma_address(sgel);
  1684. dma_len = sg_dma_len(sgel);
  1685. bf_set(lpfc_sli4_sge_len, sgl, 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->word3 = cpu_to_le32(sgl->word3);
  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. if (pnode && NLP_CHK_NODE_ACT(pnode))
  2016. atomic_dec(&pnode->cmd_pending);
  2017. if (lpfc_cmd->status) {
  2018. if (lpfc_cmd->status == IOSTAT_LOCAL_REJECT &&
  2019. (lpfc_cmd->result & IOERR_DRVR_MASK))
  2020. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  2021. else if (lpfc_cmd->status >= IOSTAT_CNT)
  2022. lpfc_cmd->status = IOSTAT_DEFAULT;
  2023. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2024. "9030 FCP cmd x%x failed <%d/%d> "
  2025. "status: x%x result: x%x Data: x%x x%x\n",
  2026. cmd->cmnd[0],
  2027. cmd->device ? cmd->device->id : 0xffff,
  2028. cmd->device ? cmd->device->lun : 0xffff,
  2029. lpfc_cmd->status, lpfc_cmd->result,
  2030. pIocbOut->iocb.ulpContext,
  2031. lpfc_cmd->cur_iocbq.iocb.ulpIoTag);
  2032. switch (lpfc_cmd->status) {
  2033. case IOSTAT_FCP_RSP_ERROR:
  2034. /* Call FCP RSP handler to determine result */
  2035. lpfc_handle_fcp_err(vport, lpfc_cmd, pIocbOut);
  2036. break;
  2037. case IOSTAT_NPORT_BSY:
  2038. case IOSTAT_FABRIC_BSY:
  2039. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  2040. fast_path_evt = lpfc_alloc_fast_evt(phba);
  2041. if (!fast_path_evt)
  2042. break;
  2043. fast_path_evt->un.fabric_evt.event_type =
  2044. FC_REG_FABRIC_EVENT;
  2045. fast_path_evt->un.fabric_evt.subcategory =
  2046. (lpfc_cmd->status == IOSTAT_NPORT_BSY) ?
  2047. LPFC_EVENT_PORT_BUSY : LPFC_EVENT_FABRIC_BUSY;
  2048. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2049. memcpy(&fast_path_evt->un.fabric_evt.wwpn,
  2050. &pnode->nlp_portname,
  2051. sizeof(struct lpfc_name));
  2052. memcpy(&fast_path_evt->un.fabric_evt.wwnn,
  2053. &pnode->nlp_nodename,
  2054. sizeof(struct lpfc_name));
  2055. }
  2056. fast_path_evt->vport = vport;
  2057. fast_path_evt->work_evt.evt =
  2058. LPFC_EVT_FASTPATH_MGMT_EVT;
  2059. spin_lock_irqsave(&phba->hbalock, flags);
  2060. list_add_tail(&fast_path_evt->work_evt.evt_listp,
  2061. &phba->work_list);
  2062. spin_unlock_irqrestore(&phba->hbalock, flags);
  2063. lpfc_worker_wake_up(phba);
  2064. break;
  2065. case IOSTAT_LOCAL_REJECT:
  2066. if (lpfc_cmd->result == IOERR_INVALID_RPI ||
  2067. lpfc_cmd->result == IOERR_NO_RESOURCES ||
  2068. lpfc_cmd->result == IOERR_ABORT_REQUESTED) {
  2069. cmd->result = ScsiResult(DID_REQUEUE, 0);
  2070. break;
  2071. }
  2072. if ((lpfc_cmd->result == IOERR_RX_DMA_FAILED ||
  2073. lpfc_cmd->result == IOERR_TX_DMA_FAILED) &&
  2074. pIocbOut->iocb.unsli3.sli3_bg.bgstat) {
  2075. if (scsi_get_prot_op(cmd) != SCSI_PROT_NORMAL) {
  2076. /*
  2077. * This is a response for a BG enabled
  2078. * cmd. Parse BG error
  2079. */
  2080. lpfc_parse_bg_err(phba, lpfc_cmd,
  2081. pIocbOut);
  2082. break;
  2083. } else {
  2084. lpfc_printf_vlog(vport, KERN_WARNING,
  2085. LOG_BG,
  2086. "9031 non-zero BGSTAT "
  2087. "on unprotected cmd\n");
  2088. }
  2089. }
  2090. /* else: fall through */
  2091. default:
  2092. cmd->result = ScsiResult(DID_ERROR, 0);
  2093. break;
  2094. }
  2095. if (!pnode || !NLP_CHK_NODE_ACT(pnode)
  2096. || (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  2097. cmd->result = ScsiResult(DID_TRANSPORT_DISRUPTED,
  2098. SAM_STAT_BUSY);
  2099. } else {
  2100. cmd->result = ScsiResult(DID_OK, 0);
  2101. }
  2102. if (cmd->result || lpfc_cmd->fcp_rsp->rspSnsLen) {
  2103. uint32_t *lp = (uint32_t *)cmd->sense_buffer;
  2104. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2105. "0710 Iodone <%d/%d> cmd %p, error "
  2106. "x%x SNS x%x x%x Data: x%x x%x\n",
  2107. cmd->device->id, cmd->device->lun, cmd,
  2108. cmd->result, *lp, *(lp + 3), cmd->retries,
  2109. scsi_get_resid(cmd));
  2110. }
  2111. lpfc_update_stats(phba, lpfc_cmd);
  2112. result = cmd->result;
  2113. if (vport->cfg_max_scsicmpl_time &&
  2114. time_after(jiffies, lpfc_cmd->start_time +
  2115. msecs_to_jiffies(vport->cfg_max_scsicmpl_time))) {
  2116. spin_lock_irqsave(shost->host_lock, flags);
  2117. if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2118. if (pnode->cmd_qdepth >
  2119. atomic_read(&pnode->cmd_pending) &&
  2120. (atomic_read(&pnode->cmd_pending) >
  2121. LPFC_MIN_TGT_QDEPTH) &&
  2122. ((cmd->cmnd[0] == READ_10) ||
  2123. (cmd->cmnd[0] == WRITE_10)))
  2124. pnode->cmd_qdepth =
  2125. atomic_read(&pnode->cmd_pending);
  2126. pnode->last_change_time = jiffies;
  2127. }
  2128. spin_unlock_irqrestore(shost->host_lock, flags);
  2129. } else if (pnode && NLP_CHK_NODE_ACT(pnode)) {
  2130. if ((pnode->cmd_qdepth < LPFC_MAX_TGT_QDEPTH) &&
  2131. time_after(jiffies, pnode->last_change_time +
  2132. msecs_to_jiffies(LPFC_TGTQ_INTERVAL))) {
  2133. spin_lock_irqsave(shost->host_lock, flags);
  2134. pnode->cmd_qdepth += pnode->cmd_qdepth *
  2135. LPFC_TGTQ_RAMPUP_PCENT / 100;
  2136. if (pnode->cmd_qdepth > LPFC_MAX_TGT_QDEPTH)
  2137. pnode->cmd_qdepth = LPFC_MAX_TGT_QDEPTH;
  2138. pnode->last_change_time = jiffies;
  2139. spin_unlock_irqrestore(shost->host_lock, flags);
  2140. }
  2141. }
  2142. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  2143. /* The sdev is not guaranteed to be valid post scsi_done upcall. */
  2144. queue_depth = cmd->device->queue_depth;
  2145. scsi_id = cmd->device->id;
  2146. cmd->scsi_done(cmd);
  2147. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2148. /*
  2149. * If there is a thread waiting for command completion
  2150. * wake up the thread.
  2151. */
  2152. spin_lock_irqsave(shost->host_lock, flags);
  2153. lpfc_cmd->pCmd = NULL;
  2154. if (lpfc_cmd->waitq)
  2155. wake_up(lpfc_cmd->waitq);
  2156. spin_unlock_irqrestore(shost->host_lock, flags);
  2157. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2158. return;
  2159. }
  2160. if (!result)
  2161. lpfc_rampup_queue_depth(vport, queue_depth);
  2162. /*
  2163. * Check for queue full. If the lun is reporting queue full, then
  2164. * back off the lun queue depth to prevent target overloads.
  2165. */
  2166. if (result == SAM_STAT_TASK_SET_FULL && pnode &&
  2167. NLP_CHK_NODE_ACT(pnode)) {
  2168. shost_for_each_device(tmp_sdev, shost) {
  2169. if (tmp_sdev->id != scsi_id)
  2170. continue;
  2171. depth = scsi_track_queue_full(tmp_sdev,
  2172. tmp_sdev->queue_depth-1);
  2173. if (depth <= 0)
  2174. continue;
  2175. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2176. "0711 detected queue full - lun queue "
  2177. "depth adjusted to %d.\n", depth);
  2178. lpfc_send_sdev_queuedepth_change_event(phba, vport,
  2179. pnode,
  2180. tmp_sdev->lun,
  2181. depth+1, depth);
  2182. }
  2183. }
  2184. /*
  2185. * If there is a thread waiting for command completion
  2186. * wake up the thread.
  2187. */
  2188. spin_lock_irqsave(shost->host_lock, flags);
  2189. lpfc_cmd->pCmd = NULL;
  2190. if (lpfc_cmd->waitq)
  2191. wake_up(lpfc_cmd->waitq);
  2192. spin_unlock_irqrestore(shost->host_lock, flags);
  2193. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2194. }
  2195. /**
  2196. * lpfc_fcpcmd_to_iocb - copy the fcp_cmd data into the IOCB
  2197. * @data: A pointer to the immediate command data portion of the IOCB.
  2198. * @fcp_cmnd: The FCP Command that is provided by the SCSI layer.
  2199. *
  2200. * The routine copies the entire FCP command from @fcp_cmnd to @data while
  2201. * byte swapping the data to big endian format for transmission on the wire.
  2202. **/
  2203. static void
  2204. lpfc_fcpcmd_to_iocb(uint8_t *data, struct fcp_cmnd *fcp_cmnd)
  2205. {
  2206. int i, j;
  2207. for (i = 0, j = 0; i < sizeof(struct fcp_cmnd);
  2208. i += sizeof(uint32_t), j++) {
  2209. ((uint32_t *)data)[j] = cpu_to_be32(((uint32_t *)fcp_cmnd)[j]);
  2210. }
  2211. }
  2212. /**
  2213. * lpfc_scsi_prep_cmnd - Wrapper func for convert scsi cmnd to FCP info unit
  2214. * @vport: The virtual port for which this call is being executed.
  2215. * @lpfc_cmd: The scsi command which needs to send.
  2216. * @pnode: Pointer to lpfc_nodelist.
  2217. *
  2218. * This routine initializes fcp_cmnd and iocb data structure from scsi command
  2219. * to transfer for device with SLI3 interface spec.
  2220. **/
  2221. static void
  2222. lpfc_scsi_prep_cmnd(struct lpfc_vport *vport, struct lpfc_scsi_buf *lpfc_cmd,
  2223. struct lpfc_nodelist *pnode)
  2224. {
  2225. struct lpfc_hba *phba = vport->phba;
  2226. struct scsi_cmnd *scsi_cmnd = lpfc_cmd->pCmd;
  2227. struct fcp_cmnd *fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2228. IOCB_t *iocb_cmd = &lpfc_cmd->cur_iocbq.iocb;
  2229. struct lpfc_iocbq *piocbq = &(lpfc_cmd->cur_iocbq);
  2230. int datadir = scsi_cmnd->sc_data_direction;
  2231. char tag[2];
  2232. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2233. return;
  2234. lpfc_cmd->fcp_rsp->rspSnsLen = 0;
  2235. /* clear task management bits */
  2236. lpfc_cmd->fcp_cmnd->fcpCntl2 = 0;
  2237. int_to_scsilun(lpfc_cmd->pCmd->device->lun,
  2238. &lpfc_cmd->fcp_cmnd->fcp_lun);
  2239. memcpy(&fcp_cmnd->fcpCdb[0], scsi_cmnd->cmnd, 16);
  2240. if (scsi_populate_tag_msg(scsi_cmnd, tag)) {
  2241. switch (tag[0]) {
  2242. case HEAD_OF_QUEUE_TAG:
  2243. fcp_cmnd->fcpCntl1 = HEAD_OF_Q;
  2244. break;
  2245. case ORDERED_QUEUE_TAG:
  2246. fcp_cmnd->fcpCntl1 = ORDERED_Q;
  2247. break;
  2248. default:
  2249. fcp_cmnd->fcpCntl1 = SIMPLE_Q;
  2250. break;
  2251. }
  2252. } else
  2253. fcp_cmnd->fcpCntl1 = 0;
  2254. /*
  2255. * There are three possibilities here - use scatter-gather segment, use
  2256. * the single mapping, or neither. Start the lpfc command prep by
  2257. * bumping the bpl beyond the fcp_cmnd and fcp_rsp regions to the first
  2258. * data bde entry.
  2259. */
  2260. if (scsi_sg_count(scsi_cmnd)) {
  2261. if (datadir == DMA_TO_DEVICE) {
  2262. iocb_cmd->ulpCommand = CMD_FCP_IWRITE64_CR;
  2263. if (phba->sli_rev < LPFC_SLI_REV4) {
  2264. iocb_cmd->un.fcpi.fcpi_parm = 0;
  2265. iocb_cmd->ulpPU = 0;
  2266. } else
  2267. iocb_cmd->ulpPU = PARM_READ_CHECK;
  2268. fcp_cmnd->fcpCntl3 = WRITE_DATA;
  2269. phba->fc4OutputRequests++;
  2270. } else {
  2271. iocb_cmd->ulpCommand = CMD_FCP_IREAD64_CR;
  2272. iocb_cmd->ulpPU = PARM_READ_CHECK;
  2273. fcp_cmnd->fcpCntl3 = READ_DATA;
  2274. phba->fc4InputRequests++;
  2275. }
  2276. } else {
  2277. iocb_cmd->ulpCommand = CMD_FCP_ICMND64_CR;
  2278. iocb_cmd->un.fcpi.fcpi_parm = 0;
  2279. iocb_cmd->ulpPU = 0;
  2280. fcp_cmnd->fcpCntl3 = 0;
  2281. phba->fc4ControlRequests++;
  2282. }
  2283. if (phba->sli_rev == 3 &&
  2284. !(phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  2285. lpfc_fcpcmd_to_iocb(iocb_cmd->unsli3.fcp_ext.icd, fcp_cmnd);
  2286. /*
  2287. * Finish initializing those IOCB fields that are independent
  2288. * of the scsi_cmnd request_buffer
  2289. */
  2290. piocbq->iocb.ulpContext = pnode->nlp_rpi;
  2291. if (pnode->nlp_fcp_info & NLP_FCP_2_DEVICE)
  2292. piocbq->iocb.ulpFCP2Rcvy = 1;
  2293. else
  2294. piocbq->iocb.ulpFCP2Rcvy = 0;
  2295. piocbq->iocb.ulpClass = (pnode->nlp_fcp_info & 0x0f);
  2296. piocbq->context1 = lpfc_cmd;
  2297. piocbq->iocb_cmpl = lpfc_scsi_cmd_iocb_cmpl;
  2298. piocbq->iocb.ulpTimeout = lpfc_cmd->timeout;
  2299. piocbq->vport = vport;
  2300. }
  2301. /**
  2302. * lpfc_scsi_prep_task_mgmt_cmnd - Convert SLI3 scsi TM cmd to FCP info unit
  2303. * @vport: The virtual port for which this call is being executed.
  2304. * @lpfc_cmd: Pointer to lpfc_scsi_buf data structure.
  2305. * @lun: Logical unit number.
  2306. * @task_mgmt_cmd: SCSI task management command.
  2307. *
  2308. * This routine creates FCP information unit corresponding to @task_mgmt_cmd
  2309. * for device with SLI-3 interface spec.
  2310. *
  2311. * Return codes:
  2312. * 0 - Error
  2313. * 1 - Success
  2314. **/
  2315. static int
  2316. lpfc_scsi_prep_task_mgmt_cmd(struct lpfc_vport *vport,
  2317. struct lpfc_scsi_buf *lpfc_cmd,
  2318. unsigned int lun,
  2319. uint8_t task_mgmt_cmd)
  2320. {
  2321. struct lpfc_iocbq *piocbq;
  2322. IOCB_t *piocb;
  2323. struct fcp_cmnd *fcp_cmnd;
  2324. struct lpfc_rport_data *rdata = lpfc_cmd->rdata;
  2325. struct lpfc_nodelist *ndlp = rdata->pnode;
  2326. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp) ||
  2327. ndlp->nlp_state != NLP_STE_MAPPED_NODE)
  2328. return 0;
  2329. piocbq = &(lpfc_cmd->cur_iocbq);
  2330. piocbq->vport = vport;
  2331. piocb = &piocbq->iocb;
  2332. fcp_cmnd = lpfc_cmd->fcp_cmnd;
  2333. /* Clear out any old data in the FCP command area */
  2334. memset(fcp_cmnd, 0, sizeof(struct fcp_cmnd));
  2335. int_to_scsilun(lun, &fcp_cmnd->fcp_lun);
  2336. fcp_cmnd->fcpCntl2 = task_mgmt_cmd;
  2337. if (vport->phba->sli_rev == 3 &&
  2338. !(vport->phba->sli3_options & LPFC_SLI3_BG_ENABLED))
  2339. lpfc_fcpcmd_to_iocb(piocb->unsli3.fcp_ext.icd, fcp_cmnd);
  2340. piocb->ulpCommand = CMD_FCP_ICMND64_CR;
  2341. piocb->ulpContext = ndlp->nlp_rpi;
  2342. if (ndlp->nlp_fcp_info & NLP_FCP_2_DEVICE) {
  2343. piocb->ulpFCP2Rcvy = 1;
  2344. }
  2345. piocb->ulpClass = (ndlp->nlp_fcp_info & 0x0f);
  2346. /* ulpTimeout is only one byte */
  2347. if (lpfc_cmd->timeout > 0xff) {
  2348. /*
  2349. * Do not timeout the command at the firmware level.
  2350. * The driver will provide the timeout mechanism.
  2351. */
  2352. piocb->ulpTimeout = 0;
  2353. } else
  2354. piocb->ulpTimeout = lpfc_cmd->timeout;
  2355. if (vport->phba->sli_rev == LPFC_SLI_REV4)
  2356. lpfc_sli4_set_rsp_sgl_last(vport->phba, lpfc_cmd);
  2357. return 1;
  2358. }
  2359. /**
  2360. * lpfc_scsi_api_table_setup - Set up scsi api fucntion jump table
  2361. * @phba: The hba struct for which this call is being executed.
  2362. * @dev_grp: The HBA PCI-Device group number.
  2363. *
  2364. * This routine sets up the SCSI interface API function jump table in @phba
  2365. * struct.
  2366. * Returns: 0 - success, -ENODEV - failure.
  2367. **/
  2368. int
  2369. lpfc_scsi_api_table_setup(struct lpfc_hba *phba, uint8_t dev_grp)
  2370. {
  2371. phba->lpfc_scsi_unprep_dma_buf = lpfc_scsi_unprep_dma_buf;
  2372. phba->lpfc_scsi_prep_cmnd = lpfc_scsi_prep_cmnd;
  2373. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
  2374. switch (dev_grp) {
  2375. case LPFC_PCI_DEV_LP:
  2376. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s3;
  2377. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s3;
  2378. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s3;
  2379. break;
  2380. case LPFC_PCI_DEV_OC:
  2381. phba->lpfc_new_scsi_buf = lpfc_new_scsi_buf_s4;
  2382. phba->lpfc_scsi_prep_dma_buf = lpfc_scsi_prep_dma_buf_s4;
  2383. phba->lpfc_release_scsi_buf = lpfc_release_scsi_buf_s4;
  2384. break;
  2385. default:
  2386. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  2387. "1418 Invalid HBA PCI-device group: 0x%x\n",
  2388. dev_grp);
  2389. return -ENODEV;
  2390. break;
  2391. }
  2392. phba->lpfc_get_scsi_buf = lpfc_get_scsi_buf;
  2393. phba->lpfc_rampdown_queue_depth = lpfc_rampdown_queue_depth;
  2394. return 0;
  2395. }
  2396. /**
  2397. * lpfc_taskmgmt_def_cmpl - IOCB completion routine for task management command
  2398. * @phba: The Hba for which this call is being executed.
  2399. * @cmdiocbq: Pointer to lpfc_iocbq data structure.
  2400. * @rspiocbq: Pointer to lpfc_iocbq data structure.
  2401. *
  2402. * This routine is IOCB completion routine for device reset and target reset
  2403. * routine. This routine release scsi buffer associated with lpfc_cmd.
  2404. **/
  2405. static void
  2406. lpfc_tskmgmt_def_cmpl(struct lpfc_hba *phba,
  2407. struct lpfc_iocbq *cmdiocbq,
  2408. struct lpfc_iocbq *rspiocbq)
  2409. {
  2410. struct lpfc_scsi_buf *lpfc_cmd =
  2411. (struct lpfc_scsi_buf *) cmdiocbq->context1;
  2412. if (lpfc_cmd)
  2413. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2414. return;
  2415. }
  2416. /**
  2417. * lpfc_info - Info entry point of scsi_host_template data structure
  2418. * @host: The scsi host for which this call is being executed.
  2419. *
  2420. * This routine provides module information about hba.
  2421. *
  2422. * Reutrn code:
  2423. * Pointer to char - Success.
  2424. **/
  2425. const char *
  2426. lpfc_info(struct Scsi_Host *host)
  2427. {
  2428. struct lpfc_vport *vport = (struct lpfc_vport *) host->hostdata;
  2429. struct lpfc_hba *phba = vport->phba;
  2430. int len;
  2431. static char lpfcinfobuf[384];
  2432. memset(lpfcinfobuf,0,384);
  2433. if (phba && phba->pcidev){
  2434. strncpy(lpfcinfobuf, phba->ModelDesc, 256);
  2435. len = strlen(lpfcinfobuf);
  2436. snprintf(lpfcinfobuf + len,
  2437. 384-len,
  2438. " on PCI bus %02x device %02x irq %d",
  2439. phba->pcidev->bus->number,
  2440. phba->pcidev->devfn,
  2441. phba->pcidev->irq);
  2442. len = strlen(lpfcinfobuf);
  2443. if (phba->Port[0]) {
  2444. snprintf(lpfcinfobuf + len,
  2445. 384-len,
  2446. " port %s",
  2447. phba->Port);
  2448. }
  2449. }
  2450. return lpfcinfobuf;
  2451. }
  2452. /**
  2453. * lpfc_poll_rearm_time - Routine to modify fcp_poll timer of hba
  2454. * @phba: The Hba for which this call is being executed.
  2455. *
  2456. * This routine modifies fcp_poll_timer field of @phba by cfg_poll_tmo.
  2457. * The default value of cfg_poll_tmo is 10 milliseconds.
  2458. **/
  2459. static __inline__ void lpfc_poll_rearm_timer(struct lpfc_hba * phba)
  2460. {
  2461. unsigned long poll_tmo_expires =
  2462. (jiffies + msecs_to_jiffies(phba->cfg_poll_tmo));
  2463. if (phba->sli.ring[LPFC_FCP_RING].txcmplq_cnt)
  2464. mod_timer(&phba->fcp_poll_timer,
  2465. poll_tmo_expires);
  2466. }
  2467. /**
  2468. * lpfc_poll_start_timer - Routine to start fcp_poll_timer of HBA
  2469. * @phba: The Hba for which this call is being executed.
  2470. *
  2471. * This routine starts the fcp_poll_timer of @phba.
  2472. **/
  2473. void lpfc_poll_start_timer(struct lpfc_hba * phba)
  2474. {
  2475. lpfc_poll_rearm_timer(phba);
  2476. }
  2477. /**
  2478. * lpfc_poll_timeout - Restart polling timer
  2479. * @ptr: Map to lpfc_hba data structure pointer.
  2480. *
  2481. * This routine restarts fcp_poll timer, when FCP ring polling is enable
  2482. * and FCP Ring interrupt is disable.
  2483. **/
  2484. void lpfc_poll_timeout(unsigned long ptr)
  2485. {
  2486. struct lpfc_hba *phba = (struct lpfc_hba *) ptr;
  2487. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2488. lpfc_sli_handle_fast_ring_event(phba,
  2489. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  2490. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2491. lpfc_poll_rearm_timer(phba);
  2492. }
  2493. }
  2494. /**
  2495. * lpfc_queuecommand - scsi_host_template queuecommand entry point
  2496. * @cmnd: Pointer to scsi_cmnd data structure.
  2497. * @done: Pointer to done routine.
  2498. *
  2499. * Driver registers this routine to scsi midlayer to submit a @cmd to process.
  2500. * This routine prepares an IOCB from scsi command and provides to firmware.
  2501. * The @done callback is invoked after driver finished processing the command.
  2502. *
  2503. * Return value :
  2504. * 0 - Success
  2505. * SCSI_MLQUEUE_HOST_BUSY - Block all devices served by this host temporarily.
  2506. **/
  2507. static int
  2508. lpfc_queuecommand(struct scsi_cmnd *cmnd, void (*done) (struct scsi_cmnd *))
  2509. {
  2510. struct Scsi_Host *shost = cmnd->device->host;
  2511. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2512. struct lpfc_hba *phba = vport->phba;
  2513. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2514. struct lpfc_nodelist *ndlp;
  2515. struct lpfc_scsi_buf *lpfc_cmd;
  2516. struct fc_rport *rport = starget_to_rport(scsi_target(cmnd->device));
  2517. int err;
  2518. err = fc_remote_port_chkready(rport);
  2519. if (err) {
  2520. cmnd->result = err;
  2521. goto out_fail_command;
  2522. }
  2523. ndlp = rdata->pnode;
  2524. if (!(phba->sli3_options & LPFC_SLI3_BG_ENABLED) &&
  2525. scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2526. lpfc_printf_log(phba, KERN_ERR, LOG_BG,
  2527. "9058 BLKGRD: ERROR: rcvd protected cmd:%02x"
  2528. " op:%02x str=%s without registering for"
  2529. " BlockGuard - Rejecting command\n",
  2530. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2531. dif_op_str[scsi_get_prot_op(cmnd)]);
  2532. goto out_fail_command;
  2533. }
  2534. /*
  2535. * Catch race where our node has transitioned, but the
  2536. * transport is still transitioning.
  2537. */
  2538. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  2539. cmnd->result = ScsiResult(DID_TRANSPORT_DISRUPTED, 0);
  2540. goto out_fail_command;
  2541. }
  2542. if (vport->cfg_max_scsicmpl_time &&
  2543. (atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth))
  2544. goto out_host_busy;
  2545. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2546. if (lpfc_cmd == NULL) {
  2547. lpfc_rampdown_queue_depth(phba);
  2548. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2549. "0707 driver's buffer pool is empty, "
  2550. "IO busied\n");
  2551. goto out_host_busy;
  2552. }
  2553. /*
  2554. * Store the midlayer's command structure for the completion phase
  2555. * and complete the command initialization.
  2556. */
  2557. lpfc_cmd->pCmd = cmnd;
  2558. lpfc_cmd->rdata = rdata;
  2559. lpfc_cmd->timeout = 0;
  2560. lpfc_cmd->start_time = jiffies;
  2561. cmnd->host_scribble = (unsigned char *)lpfc_cmd;
  2562. cmnd->scsi_done = done;
  2563. if (scsi_get_prot_op(cmnd) != SCSI_PROT_NORMAL) {
  2564. if (vport->phba->cfg_enable_bg) {
  2565. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2566. "9033 BLKGRD: rcvd protected cmd:%02x op:%02x "
  2567. "str=%s\n",
  2568. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2569. dif_op_str[scsi_get_prot_op(cmnd)]);
  2570. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2571. "9034 BLKGRD: CDB: %02x %02x %02x %02x %02x "
  2572. "%02x %02x %02x %02x %02x\n",
  2573. cmnd->cmnd[0], cmnd->cmnd[1], cmnd->cmnd[2],
  2574. cmnd->cmnd[3], cmnd->cmnd[4], cmnd->cmnd[5],
  2575. cmnd->cmnd[6], cmnd->cmnd[7], cmnd->cmnd[8],
  2576. cmnd->cmnd[9]);
  2577. if (cmnd->cmnd[0] == READ_10)
  2578. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2579. "9035 BLKGRD: READ @ sector %llu, "
  2580. "count %u\n",
  2581. (unsigned long long)scsi_get_lba(cmnd),
  2582. blk_rq_sectors(cmnd->request));
  2583. else if (cmnd->cmnd[0] == WRITE_10)
  2584. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2585. "9036 BLKGRD: WRITE @ sector %llu, "
  2586. "count %u cmd=%p\n",
  2587. (unsigned long long)scsi_get_lba(cmnd),
  2588. blk_rq_sectors(cmnd->request),
  2589. cmnd);
  2590. }
  2591. err = lpfc_bg_scsi_prep_dma_buf(phba, lpfc_cmd);
  2592. } else {
  2593. if (vport->phba->cfg_enable_bg) {
  2594. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2595. "9038 BLKGRD: rcvd unprotected cmd:"
  2596. "%02x op:%02x str=%s\n",
  2597. cmnd->cmnd[0], scsi_get_prot_op(cmnd),
  2598. dif_op_str[scsi_get_prot_op(cmnd)]);
  2599. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2600. "9039 BLKGRD: CDB: %02x %02x %02x "
  2601. "%02x %02x %02x %02x %02x %02x %02x\n",
  2602. cmnd->cmnd[0], cmnd->cmnd[1],
  2603. cmnd->cmnd[2], cmnd->cmnd[3],
  2604. cmnd->cmnd[4], cmnd->cmnd[5],
  2605. cmnd->cmnd[6], cmnd->cmnd[7],
  2606. cmnd->cmnd[8], cmnd->cmnd[9]);
  2607. if (cmnd->cmnd[0] == READ_10)
  2608. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2609. "9040 dbg: READ @ sector %llu, "
  2610. "count %u\n",
  2611. (unsigned long long)scsi_get_lba(cmnd),
  2612. blk_rq_sectors(cmnd->request));
  2613. else if (cmnd->cmnd[0] == WRITE_10)
  2614. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2615. "9041 dbg: WRITE @ sector %llu, "
  2616. "count %u cmd=%p\n",
  2617. (unsigned long long)scsi_get_lba(cmnd),
  2618. blk_rq_sectors(cmnd->request), cmnd);
  2619. else
  2620. lpfc_printf_vlog(vport, KERN_WARNING, LOG_BG,
  2621. "9042 dbg: parser not implemented\n");
  2622. }
  2623. err = lpfc_scsi_prep_dma_buf(phba, lpfc_cmd);
  2624. }
  2625. if (err)
  2626. goto out_host_busy_free_buf;
  2627. lpfc_scsi_prep_cmnd(vport, lpfc_cmd, ndlp);
  2628. atomic_inc(&ndlp->cmd_pending);
  2629. err = lpfc_sli_issue_iocb(phba, LPFC_FCP_RING,
  2630. &lpfc_cmd->cur_iocbq, SLI_IOCB_RET_IOCB);
  2631. if (err) {
  2632. atomic_dec(&ndlp->cmd_pending);
  2633. goto out_host_busy_free_buf;
  2634. }
  2635. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  2636. spin_unlock(shost->host_lock);
  2637. lpfc_sli_handle_fast_ring_event(phba,
  2638. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  2639. spin_lock(shost->host_lock);
  2640. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2641. lpfc_poll_rearm_timer(phba);
  2642. }
  2643. return 0;
  2644. out_host_busy_free_buf:
  2645. lpfc_scsi_unprep_dma_buf(phba, lpfc_cmd);
  2646. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2647. out_host_busy:
  2648. return SCSI_MLQUEUE_HOST_BUSY;
  2649. out_fail_command:
  2650. done(cmnd);
  2651. return 0;
  2652. }
  2653. /**
  2654. * lpfc_abort_handler - scsi_host_template eh_abort_handler entry point
  2655. * @cmnd: Pointer to scsi_cmnd data structure.
  2656. *
  2657. * This routine aborts @cmnd pending in base driver.
  2658. *
  2659. * Return code :
  2660. * 0x2003 - Error
  2661. * 0x2002 - Success
  2662. **/
  2663. static int
  2664. lpfc_abort_handler(struct scsi_cmnd *cmnd)
  2665. {
  2666. struct Scsi_Host *shost = cmnd->device->host;
  2667. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2668. struct lpfc_hba *phba = vport->phba;
  2669. struct lpfc_iocbq *iocb;
  2670. struct lpfc_iocbq *abtsiocb;
  2671. struct lpfc_scsi_buf *lpfc_cmd;
  2672. IOCB_t *cmd, *icmd;
  2673. int ret = SUCCESS;
  2674. DECLARE_WAIT_QUEUE_HEAD_ONSTACK(waitq);
  2675. fc_block_scsi_eh(cmnd);
  2676. lpfc_cmd = (struct lpfc_scsi_buf *)cmnd->host_scribble;
  2677. BUG_ON(!lpfc_cmd);
  2678. /*
  2679. * If pCmd field of the corresponding lpfc_scsi_buf structure
  2680. * points to a different SCSI command, then the driver has
  2681. * already completed this command, but the midlayer did not
  2682. * see the completion before the eh fired. Just return
  2683. * SUCCESS.
  2684. */
  2685. iocb = &lpfc_cmd->cur_iocbq;
  2686. if (lpfc_cmd->pCmd != cmnd)
  2687. goto out;
  2688. BUG_ON(iocb->context1 != lpfc_cmd);
  2689. abtsiocb = lpfc_sli_get_iocbq(phba);
  2690. if (abtsiocb == NULL) {
  2691. ret = FAILED;
  2692. goto out;
  2693. }
  2694. /*
  2695. * The scsi command can not be in txq and it is in flight because the
  2696. * pCmd is still pointig at the SCSI command we have to abort. There
  2697. * is no need to search the txcmplq. Just send an abort to the FW.
  2698. */
  2699. cmd = &iocb->iocb;
  2700. icmd = &abtsiocb->iocb;
  2701. icmd->un.acxri.abortType = ABORT_TYPE_ABTS;
  2702. icmd->un.acxri.abortContextTag = cmd->ulpContext;
  2703. if (phba->sli_rev == LPFC_SLI_REV4)
  2704. icmd->un.acxri.abortIoTag = iocb->sli4_xritag;
  2705. else
  2706. icmd->un.acxri.abortIoTag = cmd->ulpIoTag;
  2707. icmd->ulpLe = 1;
  2708. icmd->ulpClass = cmd->ulpClass;
  2709. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  2710. abtsiocb->fcp_wqidx = iocb->fcp_wqidx;
  2711. if (lpfc_is_link_up(phba))
  2712. icmd->ulpCommand = CMD_ABORT_XRI_CN;
  2713. else
  2714. icmd->ulpCommand = CMD_CLOSE_XRI_CN;
  2715. abtsiocb->iocb_cmpl = lpfc_sli_abort_fcp_cmpl;
  2716. abtsiocb->vport = vport;
  2717. if (lpfc_sli_issue_iocb(phba, LPFC_FCP_RING, abtsiocb, 0) ==
  2718. IOCB_ERROR) {
  2719. lpfc_sli_release_iocbq(phba, abtsiocb);
  2720. ret = FAILED;
  2721. goto out;
  2722. }
  2723. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  2724. lpfc_sli_handle_fast_ring_event(phba,
  2725. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  2726. lpfc_cmd->waitq = &waitq;
  2727. /* Wait for abort to complete */
  2728. wait_event_timeout(waitq,
  2729. (lpfc_cmd->pCmd != cmnd),
  2730. (2*vport->cfg_devloss_tmo*HZ));
  2731. spin_lock_irq(shost->host_lock);
  2732. lpfc_cmd->waitq = NULL;
  2733. spin_unlock_irq(shost->host_lock);
  2734. if (lpfc_cmd->pCmd == cmnd) {
  2735. ret = FAILED;
  2736. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2737. "0748 abort handler timed out waiting "
  2738. "for abort to complete: ret %#x, ID %d, "
  2739. "LUN %d, snum %#lx\n",
  2740. ret, cmnd->device->id, cmnd->device->lun,
  2741. cmnd->serial_number);
  2742. }
  2743. out:
  2744. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  2745. "0749 SCSI Layer I/O Abort Request Status x%x ID %d "
  2746. "LUN %d snum %#lx\n", ret, cmnd->device->id,
  2747. cmnd->device->lun, cmnd->serial_number);
  2748. return ret;
  2749. }
  2750. static char *
  2751. lpfc_taskmgmt_name(uint8_t task_mgmt_cmd)
  2752. {
  2753. switch (task_mgmt_cmd) {
  2754. case FCP_ABORT_TASK_SET:
  2755. return "ABORT_TASK_SET";
  2756. case FCP_CLEAR_TASK_SET:
  2757. return "FCP_CLEAR_TASK_SET";
  2758. case FCP_BUS_RESET:
  2759. return "FCP_BUS_RESET";
  2760. case FCP_LUN_RESET:
  2761. return "FCP_LUN_RESET";
  2762. case FCP_TARGET_RESET:
  2763. return "FCP_TARGET_RESET";
  2764. case FCP_CLEAR_ACA:
  2765. return "FCP_CLEAR_ACA";
  2766. case FCP_TERMINATE_TASK:
  2767. return "FCP_TERMINATE_TASK";
  2768. default:
  2769. return "unknown";
  2770. }
  2771. }
  2772. /**
  2773. * lpfc_send_taskmgmt - Generic SCSI Task Mgmt Handler
  2774. * @vport: The virtual port for which this call is being executed.
  2775. * @rdata: Pointer to remote port local data
  2776. * @tgt_id: Target ID of remote device.
  2777. * @lun_id: Lun number for the TMF
  2778. * @task_mgmt_cmd: type of TMF to send
  2779. *
  2780. * This routine builds and sends a TMF (SCSI Task Mgmt Function) to
  2781. * a remote port.
  2782. *
  2783. * Return Code:
  2784. * 0x2003 - Error
  2785. * 0x2002 - Success.
  2786. **/
  2787. static int
  2788. lpfc_send_taskmgmt(struct lpfc_vport *vport, struct lpfc_rport_data *rdata,
  2789. unsigned tgt_id, unsigned int lun_id,
  2790. uint8_t task_mgmt_cmd)
  2791. {
  2792. struct lpfc_hba *phba = vport->phba;
  2793. struct lpfc_scsi_buf *lpfc_cmd;
  2794. struct lpfc_iocbq *iocbq;
  2795. struct lpfc_iocbq *iocbqrsp;
  2796. int ret;
  2797. int status;
  2798. if (!rdata->pnode || !NLP_CHK_NODE_ACT(rdata->pnode))
  2799. return FAILED;
  2800. lpfc_cmd = lpfc_get_scsi_buf(phba);
  2801. if (lpfc_cmd == NULL)
  2802. return FAILED;
  2803. lpfc_cmd->timeout = 60;
  2804. lpfc_cmd->rdata = rdata;
  2805. status = lpfc_scsi_prep_task_mgmt_cmd(vport, lpfc_cmd, lun_id,
  2806. task_mgmt_cmd);
  2807. if (!status) {
  2808. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2809. return FAILED;
  2810. }
  2811. iocbq = &lpfc_cmd->cur_iocbq;
  2812. iocbqrsp = lpfc_sli_get_iocbq(phba);
  2813. if (iocbqrsp == NULL) {
  2814. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2815. return FAILED;
  2816. }
  2817. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2818. "0702 Issue %s to TGT %d LUN %d "
  2819. "rpi x%x nlp_flag x%x\n",
  2820. lpfc_taskmgmt_name(task_mgmt_cmd), tgt_id, lun_id,
  2821. rdata->pnode->nlp_rpi, rdata->pnode->nlp_flag);
  2822. status = lpfc_sli_issue_iocb_wait(phba, LPFC_FCP_RING,
  2823. iocbq, iocbqrsp, lpfc_cmd->timeout);
  2824. if (status != IOCB_SUCCESS) {
  2825. if (status == IOCB_TIMEDOUT) {
  2826. iocbq->iocb_cmpl = lpfc_tskmgmt_def_cmpl;
  2827. ret = TIMEOUT_ERROR;
  2828. } else
  2829. ret = FAILED;
  2830. lpfc_cmd->status = IOSTAT_DRIVER_REJECT;
  2831. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2832. "0727 TMF %s to TGT %d LUN %d failed (%d, %d)\n",
  2833. lpfc_taskmgmt_name(task_mgmt_cmd),
  2834. tgt_id, lun_id, iocbqrsp->iocb.ulpStatus,
  2835. iocbqrsp->iocb.un.ulpWord[4]);
  2836. } else
  2837. ret = SUCCESS;
  2838. lpfc_sli_release_iocbq(phba, iocbqrsp);
  2839. if (ret != TIMEOUT_ERROR)
  2840. lpfc_release_scsi_buf(phba, lpfc_cmd);
  2841. return ret;
  2842. }
  2843. /**
  2844. * lpfc_chk_tgt_mapped -
  2845. * @vport: The virtual port to check on
  2846. * @cmnd: Pointer to scsi_cmnd data structure.
  2847. *
  2848. * This routine delays until the scsi target (aka rport) for the
  2849. * command exists (is present and logged in) or we declare it non-existent.
  2850. *
  2851. * Return code :
  2852. * 0x2003 - Error
  2853. * 0x2002 - Success
  2854. **/
  2855. static int
  2856. lpfc_chk_tgt_mapped(struct lpfc_vport *vport, struct scsi_cmnd *cmnd)
  2857. {
  2858. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2859. struct lpfc_nodelist *pnode;
  2860. unsigned long later;
  2861. if (!rdata) {
  2862. lpfc_printf_vlog(vport, KERN_INFO, LOG_FCP,
  2863. "0797 Tgt Map rport failure: rdata x%p\n", rdata);
  2864. return FAILED;
  2865. }
  2866. pnode = rdata->pnode;
  2867. /*
  2868. * If target is not in a MAPPED state, delay until
  2869. * target is rediscovered or devloss timeout expires.
  2870. */
  2871. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2872. while (time_after(later, jiffies)) {
  2873. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  2874. return FAILED;
  2875. if (pnode->nlp_state == NLP_STE_MAPPED_NODE)
  2876. return SUCCESS;
  2877. schedule_timeout_uninterruptible(msecs_to_jiffies(500));
  2878. rdata = cmnd->device->hostdata;
  2879. if (!rdata)
  2880. return FAILED;
  2881. pnode = rdata->pnode;
  2882. }
  2883. if (!pnode || !NLP_CHK_NODE_ACT(pnode) ||
  2884. (pnode->nlp_state != NLP_STE_MAPPED_NODE))
  2885. return FAILED;
  2886. return SUCCESS;
  2887. }
  2888. /**
  2889. * lpfc_reset_flush_io_context -
  2890. * @vport: The virtual port (scsi_host) for the flush context
  2891. * @tgt_id: If aborting by Target contect - specifies the target id
  2892. * @lun_id: If aborting by Lun context - specifies the lun id
  2893. * @context: specifies the context level to flush at.
  2894. *
  2895. * After a reset condition via TMF, we need to flush orphaned i/o
  2896. * contexts from the adapter. This routine aborts any contexts
  2897. * outstanding, then waits for their completions. The wait is
  2898. * bounded by devloss_tmo though.
  2899. *
  2900. * Return code :
  2901. * 0x2003 - Error
  2902. * 0x2002 - Success
  2903. **/
  2904. static int
  2905. lpfc_reset_flush_io_context(struct lpfc_vport *vport, uint16_t tgt_id,
  2906. uint64_t lun_id, lpfc_ctx_cmd context)
  2907. {
  2908. struct lpfc_hba *phba = vport->phba;
  2909. unsigned long later;
  2910. int cnt;
  2911. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  2912. if (cnt)
  2913. lpfc_sli_abort_iocb(vport, &phba->sli.ring[phba->sli.fcp_ring],
  2914. tgt_id, lun_id, context);
  2915. later = msecs_to_jiffies(2 * vport->cfg_devloss_tmo * 1000) + jiffies;
  2916. while (time_after(later, jiffies) && cnt) {
  2917. schedule_timeout_uninterruptible(msecs_to_jiffies(20));
  2918. cnt = lpfc_sli_sum_iocb(vport, tgt_id, lun_id, context);
  2919. }
  2920. if (cnt) {
  2921. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2922. "0724 I/O flush failure for context %s : cnt x%x\n",
  2923. ((context == LPFC_CTX_LUN) ? "LUN" :
  2924. ((context == LPFC_CTX_TGT) ? "TGT" :
  2925. ((context == LPFC_CTX_HOST) ? "HOST" : "Unknown"))),
  2926. cnt);
  2927. return FAILED;
  2928. }
  2929. return SUCCESS;
  2930. }
  2931. /**
  2932. * lpfc_device_reset_handler - scsi_host_template eh_device_reset entry point
  2933. * @cmnd: Pointer to scsi_cmnd data structure.
  2934. *
  2935. * This routine does a device reset by sending a LUN_RESET task management
  2936. * command.
  2937. *
  2938. * Return code :
  2939. * 0x2003 - Error
  2940. * 0x2002 - Success
  2941. **/
  2942. static int
  2943. lpfc_device_reset_handler(struct scsi_cmnd *cmnd)
  2944. {
  2945. struct Scsi_Host *shost = cmnd->device->host;
  2946. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  2947. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  2948. struct lpfc_nodelist *pnode;
  2949. unsigned tgt_id = cmnd->device->id;
  2950. unsigned int lun_id = cmnd->device->lun;
  2951. struct lpfc_scsi_event_header scsi_event;
  2952. int status;
  2953. if (!rdata) {
  2954. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2955. "0798 Device Reset rport failure: rdata x%p\n", rdata);
  2956. return FAILED;
  2957. }
  2958. pnode = rdata->pnode;
  2959. fc_block_scsi_eh(cmnd);
  2960. status = lpfc_chk_tgt_mapped(vport, cmnd);
  2961. if (status == FAILED) {
  2962. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2963. "0721 Device Reset rport failure: rdata x%p\n", rdata);
  2964. return FAILED;
  2965. }
  2966. scsi_event.event_type = FC_REG_SCSI_EVENT;
  2967. scsi_event.subcategory = LPFC_EVENT_LUNRESET;
  2968. scsi_event.lun = lun_id;
  2969. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  2970. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  2971. fc_host_post_vendor_event(shost, fc_get_event_number(),
  2972. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  2973. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  2974. FCP_LUN_RESET);
  2975. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  2976. "0713 SCSI layer issued Device Reset (%d, %d) "
  2977. "return x%x\n", tgt_id, lun_id, status);
  2978. /*
  2979. * We have to clean up i/o as : they may be orphaned by the TMF;
  2980. * or if the TMF failed, they may be in an indeterminate state.
  2981. * So, continue on.
  2982. * We will report success if all the i/o aborts successfully.
  2983. */
  2984. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  2985. LPFC_CTX_LUN);
  2986. return status;
  2987. }
  2988. /**
  2989. * lpfc_target_reset_handler - scsi_host_template eh_target_reset entry point
  2990. * @cmnd: Pointer to scsi_cmnd data structure.
  2991. *
  2992. * This routine does a target reset by sending a TARGET_RESET task management
  2993. * command.
  2994. *
  2995. * Return code :
  2996. * 0x2003 - Error
  2997. * 0x2002 - Success
  2998. **/
  2999. static int
  3000. lpfc_target_reset_handler(struct scsi_cmnd *cmnd)
  3001. {
  3002. struct Scsi_Host *shost = cmnd->device->host;
  3003. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3004. struct lpfc_rport_data *rdata = cmnd->device->hostdata;
  3005. struct lpfc_nodelist *pnode;
  3006. unsigned tgt_id = cmnd->device->id;
  3007. unsigned int lun_id = cmnd->device->lun;
  3008. struct lpfc_scsi_event_header scsi_event;
  3009. int status;
  3010. if (!rdata) {
  3011. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3012. "0799 Target Reset rport failure: rdata x%p\n", rdata);
  3013. return FAILED;
  3014. }
  3015. pnode = rdata->pnode;
  3016. fc_block_scsi_eh(cmnd);
  3017. status = lpfc_chk_tgt_mapped(vport, cmnd);
  3018. if (status == FAILED) {
  3019. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3020. "0722 Target Reset rport failure: rdata x%p\n", rdata);
  3021. return FAILED;
  3022. }
  3023. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3024. scsi_event.subcategory = LPFC_EVENT_TGTRESET;
  3025. scsi_event.lun = 0;
  3026. memcpy(scsi_event.wwpn, &pnode->nlp_portname, sizeof(struct lpfc_name));
  3027. memcpy(scsi_event.wwnn, &pnode->nlp_nodename, sizeof(struct lpfc_name));
  3028. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3029. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3030. status = lpfc_send_taskmgmt(vport, rdata, tgt_id, lun_id,
  3031. FCP_TARGET_RESET);
  3032. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3033. "0723 SCSI layer issued Target Reset (%d, %d) "
  3034. "return x%x\n", tgt_id, lun_id, status);
  3035. /*
  3036. * We have to clean up i/o as : they may be orphaned by the TMF;
  3037. * or if the TMF failed, they may be in an indeterminate state.
  3038. * So, continue on.
  3039. * We will report success if all the i/o aborts successfully.
  3040. */
  3041. status = lpfc_reset_flush_io_context(vport, tgt_id, lun_id,
  3042. LPFC_CTX_TGT);
  3043. return status;
  3044. }
  3045. /**
  3046. * lpfc_bus_reset_handler - scsi_host_template eh_bus_reset_handler entry point
  3047. * @cmnd: Pointer to scsi_cmnd data structure.
  3048. *
  3049. * This routine does target reset to all targets on @cmnd->device->host.
  3050. * This emulates Parallel SCSI Bus Reset Semantics.
  3051. *
  3052. * Return code :
  3053. * 0x2003 - Error
  3054. * 0x2002 - Success
  3055. **/
  3056. static int
  3057. lpfc_bus_reset_handler(struct scsi_cmnd *cmnd)
  3058. {
  3059. struct Scsi_Host *shost = cmnd->device->host;
  3060. struct lpfc_vport *vport = (struct lpfc_vport *) shost->hostdata;
  3061. struct lpfc_nodelist *ndlp = NULL;
  3062. struct lpfc_scsi_event_header scsi_event;
  3063. int match;
  3064. int ret = SUCCESS, status, i;
  3065. scsi_event.event_type = FC_REG_SCSI_EVENT;
  3066. scsi_event.subcategory = LPFC_EVENT_BUSRESET;
  3067. scsi_event.lun = 0;
  3068. memcpy(scsi_event.wwpn, &vport->fc_portname, sizeof(struct lpfc_name));
  3069. memcpy(scsi_event.wwnn, &vport->fc_nodename, sizeof(struct lpfc_name));
  3070. fc_host_post_vendor_event(shost, fc_get_event_number(),
  3071. sizeof(scsi_event), (char *)&scsi_event, LPFC_NL_VENDOR_ID);
  3072. fc_block_scsi_eh(cmnd);
  3073. /*
  3074. * Since the driver manages a single bus device, reset all
  3075. * targets known to the driver. Should any target reset
  3076. * fail, this routine returns failure to the midlayer.
  3077. */
  3078. for (i = 0; i < LPFC_MAX_TARGET; i++) {
  3079. /* Search for mapped node by target ID */
  3080. match = 0;
  3081. spin_lock_irq(shost->host_lock);
  3082. list_for_each_entry(ndlp, &vport->fc_nodes, nlp_listp) {
  3083. if (!NLP_CHK_NODE_ACT(ndlp))
  3084. continue;
  3085. if (ndlp->nlp_state == NLP_STE_MAPPED_NODE &&
  3086. ndlp->nlp_sid == i &&
  3087. ndlp->rport) {
  3088. match = 1;
  3089. break;
  3090. }
  3091. }
  3092. spin_unlock_irq(shost->host_lock);
  3093. if (!match)
  3094. continue;
  3095. status = lpfc_send_taskmgmt(vport, ndlp->rport->dd_data,
  3096. i, 0, FCP_TARGET_RESET);
  3097. if (status != SUCCESS) {
  3098. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3099. "0700 Bus Reset on target %d failed\n",
  3100. i);
  3101. ret = FAILED;
  3102. }
  3103. }
  3104. /*
  3105. * We have to clean up i/o as : they may be orphaned by the TMFs
  3106. * above; or if any of the TMFs failed, they may be in an
  3107. * indeterminate state.
  3108. * We will report success if all the i/o aborts successfully.
  3109. */
  3110. status = lpfc_reset_flush_io_context(vport, 0, 0, LPFC_CTX_HOST);
  3111. if (status != SUCCESS)
  3112. ret = FAILED;
  3113. lpfc_printf_vlog(vport, KERN_ERR, LOG_FCP,
  3114. "0714 SCSI layer issued Bus Reset Data: x%x\n", ret);
  3115. return ret;
  3116. }
  3117. /**
  3118. * lpfc_slave_alloc - scsi_host_template slave_alloc entry point
  3119. * @sdev: Pointer to scsi_device.
  3120. *
  3121. * This routine populates the cmds_per_lun count + 2 scsi_bufs into this host's
  3122. * globally available list of scsi buffers. This routine also makes sure scsi
  3123. * buffer is not allocated more than HBA limit conveyed to midlayer. This list
  3124. * of scsi buffer exists for the lifetime of the driver.
  3125. *
  3126. * Return codes:
  3127. * non-0 - Error
  3128. * 0 - Success
  3129. **/
  3130. static int
  3131. lpfc_slave_alloc(struct scsi_device *sdev)
  3132. {
  3133. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3134. struct lpfc_hba *phba = vport->phba;
  3135. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  3136. uint32_t total = 0;
  3137. uint32_t num_to_alloc = 0;
  3138. int num_allocated = 0;
  3139. if (!rport || fc_remote_port_chkready(rport))
  3140. return -ENXIO;
  3141. sdev->hostdata = rport->dd_data;
  3142. /*
  3143. * Populate the cmds_per_lun count scsi_bufs into this host's globally
  3144. * available list of scsi buffers. Don't allocate more than the
  3145. * HBA limit conveyed to the midlayer via the host structure. The
  3146. * formula accounts for the lun_queue_depth + error handlers + 1
  3147. * extra. This list of scsi bufs exists for the lifetime of the driver.
  3148. */
  3149. total = phba->total_scsi_bufs;
  3150. num_to_alloc = vport->cfg_lun_queue_depth + 2;
  3151. /* Allow some exchanges to be available always to complete discovery */
  3152. if (total >= phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  3153. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3154. "0704 At limitation of %d preallocated "
  3155. "command buffers\n", total);
  3156. return 0;
  3157. /* Allow some exchanges to be available always to complete discovery */
  3158. } else if (total + num_to_alloc >
  3159. phba->cfg_hba_queue_depth - LPFC_DISC_IOCB_BUFF_COUNT ) {
  3160. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3161. "0705 Allocation request of %d "
  3162. "command buffers will exceed max of %d. "
  3163. "Reducing allocation request to %d.\n",
  3164. num_to_alloc, phba->cfg_hba_queue_depth,
  3165. (phba->cfg_hba_queue_depth - total));
  3166. num_to_alloc = phba->cfg_hba_queue_depth - total;
  3167. }
  3168. num_allocated = lpfc_new_scsi_buf(vport, num_to_alloc);
  3169. if (num_to_alloc != num_allocated) {
  3170. lpfc_printf_vlog(vport, KERN_WARNING, LOG_FCP,
  3171. "0708 Allocation request of %d "
  3172. "command buffers did not succeed. "
  3173. "Allocated %d buffers.\n",
  3174. num_to_alloc, num_allocated);
  3175. }
  3176. if (num_allocated > 0)
  3177. phba->total_scsi_bufs += num_allocated;
  3178. return 0;
  3179. }
  3180. /**
  3181. * lpfc_slave_configure - scsi_host_template slave_configure entry point
  3182. * @sdev: Pointer to scsi_device.
  3183. *
  3184. * This routine configures following items
  3185. * - Tag command queuing support for @sdev if supported.
  3186. * - Dev loss time out value of fc_rport.
  3187. * - Enable SLI polling for fcp ring if ENABLE_FCP_RING_POLLING flag is set.
  3188. *
  3189. * Return codes:
  3190. * 0 - Success
  3191. **/
  3192. static int
  3193. lpfc_slave_configure(struct scsi_device *sdev)
  3194. {
  3195. struct lpfc_vport *vport = (struct lpfc_vport *) sdev->host->hostdata;
  3196. struct lpfc_hba *phba = vport->phba;
  3197. struct fc_rport *rport = starget_to_rport(sdev->sdev_target);
  3198. if (sdev->tagged_supported)
  3199. scsi_activate_tcq(sdev, vport->cfg_lun_queue_depth);
  3200. else
  3201. scsi_deactivate_tcq(sdev, vport->cfg_lun_queue_depth);
  3202. /*
  3203. * Initialize the fc transport attributes for the target
  3204. * containing this scsi device. Also note that the driver's
  3205. * target pointer is stored in the starget_data for the
  3206. * driver's sysfs entry point functions.
  3207. */
  3208. rport->dev_loss_tmo = vport->cfg_devloss_tmo;
  3209. if (phba->cfg_poll & ENABLE_FCP_RING_POLLING) {
  3210. lpfc_sli_handle_fast_ring_event(phba,
  3211. &phba->sli.ring[LPFC_FCP_RING], HA_R0RE_REQ);
  3212. if (phba->cfg_poll & DISABLE_FCP_RING_INT)
  3213. lpfc_poll_rearm_timer(phba);
  3214. }
  3215. return 0;
  3216. }
  3217. /**
  3218. * lpfc_slave_destroy - slave_destroy entry point of SHT data structure
  3219. * @sdev: Pointer to scsi_device.
  3220. *
  3221. * This routine sets @sdev hostatdata filed to null.
  3222. **/
  3223. static void
  3224. lpfc_slave_destroy(struct scsi_device *sdev)
  3225. {
  3226. sdev->hostdata = NULL;
  3227. return;
  3228. }
  3229. struct scsi_host_template lpfc_template = {
  3230. .module = THIS_MODULE,
  3231. .name = LPFC_DRIVER_NAME,
  3232. .info = lpfc_info,
  3233. .queuecommand = lpfc_queuecommand,
  3234. .eh_abort_handler = lpfc_abort_handler,
  3235. .eh_device_reset_handler = lpfc_device_reset_handler,
  3236. .eh_target_reset_handler = lpfc_target_reset_handler,
  3237. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  3238. .slave_alloc = lpfc_slave_alloc,
  3239. .slave_configure = lpfc_slave_configure,
  3240. .slave_destroy = lpfc_slave_destroy,
  3241. .scan_finished = lpfc_scan_finished,
  3242. .this_id = -1,
  3243. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  3244. .cmd_per_lun = LPFC_CMD_PER_LUN,
  3245. .use_clustering = ENABLE_CLUSTERING,
  3246. .shost_attrs = lpfc_hba_attrs,
  3247. .max_sectors = 0xFFFF,
  3248. .vendor_id = LPFC_NL_VENDOR_ID,
  3249. .change_queue_depth = lpfc_change_queue_depth,
  3250. };
  3251. struct scsi_host_template lpfc_vport_template = {
  3252. .module = THIS_MODULE,
  3253. .name = LPFC_DRIVER_NAME,
  3254. .info = lpfc_info,
  3255. .queuecommand = lpfc_queuecommand,
  3256. .eh_abort_handler = lpfc_abort_handler,
  3257. .eh_device_reset_handler = lpfc_device_reset_handler,
  3258. .eh_target_reset_handler = lpfc_target_reset_handler,
  3259. .eh_bus_reset_handler = lpfc_bus_reset_handler,
  3260. .slave_alloc = lpfc_slave_alloc,
  3261. .slave_configure = lpfc_slave_configure,
  3262. .slave_destroy = lpfc_slave_destroy,
  3263. .scan_finished = lpfc_scan_finished,
  3264. .this_id = -1,
  3265. .sg_tablesize = LPFC_DEFAULT_SG_SEG_CNT,
  3266. .cmd_per_lun = LPFC_CMD_PER_LUN,
  3267. .use_clustering = ENABLE_CLUSTERING,
  3268. .shost_attrs = lpfc_vport_attrs,
  3269. .max_sectors = 0xFFFF,
  3270. .change_queue_depth = lpfc_change_queue_depth,
  3271. };