lpfc_scsi.c 107 KB

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