lpfc_scsi.c 108 KB

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