lpfc_nvme.c 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020
  1. /*******************************************************************
  2. * This file is part of the Emulex Linux Device Driver for *
  3. * Fibre Channel Host Bus Adapters. *
  4. * Copyright (C) 2017-2018 Broadcom. All Rights Reserved. The term *
  5. * “Broadcom” refers to Broadcom Inc. and/or its subsidiaries. *
  6. * Copyright (C) 2004-2016 Emulex. All rights reserved. *
  7. * EMULEX and SLI are trademarks of Emulex. *
  8. * www.broadcom.com *
  9. * Portions Copyright (C) 2004-2005 Christoph Hellwig *
  10. * *
  11. * This program is free software; you can redistribute it and/or *
  12. * modify it under the terms of version 2 of the GNU General *
  13. * Public License as published by the Free Software Foundation. *
  14. * This program is distributed in the hope that it will be useful. *
  15. * ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND *
  16. * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, *
  17. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT, ARE *
  18. * DISCLAIMED, EXCEPT TO THE EXTENT THAT SUCH DISCLAIMERS ARE HELD *
  19. * TO BE LEGALLY INVALID. See the GNU General Public License for *
  20. * more details, a copy of which can be found in the file COPYING *
  21. * included with this package. *
  22. ********************************************************************/
  23. #include <linux/pci.h>
  24. #include <linux/slab.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/delay.h>
  27. #include <asm/unaligned.h>
  28. #include <linux/crc-t10dif.h>
  29. #include <net/checksum.h>
  30. #include <scsi/scsi.h>
  31. #include <scsi/scsi_device.h>
  32. #include <scsi/scsi_eh.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi_tcq.h>
  35. #include <scsi/scsi_transport_fc.h>
  36. #include <scsi/fc/fc_fs.h>
  37. #include <linux/nvme.h>
  38. #include <linux/nvme-fc-driver.h>
  39. #include <linux/nvme-fc.h>
  40. #include "lpfc_version.h"
  41. #include "lpfc_hw4.h"
  42. #include "lpfc_hw.h"
  43. #include "lpfc_sli.h"
  44. #include "lpfc_sli4.h"
  45. #include "lpfc_nl.h"
  46. #include "lpfc_disc.h"
  47. #include "lpfc.h"
  48. #include "lpfc_nvme.h"
  49. #include "lpfc_scsi.h"
  50. #include "lpfc_logmsg.h"
  51. #include "lpfc_crtn.h"
  52. #include "lpfc_vport.h"
  53. #include "lpfc_debugfs.h"
  54. /* NVME initiator-based functions */
  55. static struct lpfc_nvme_buf *
  56. lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  57. int expedite);
  58. static void
  59. lpfc_release_nvme_buf(struct lpfc_hba *, struct lpfc_nvme_buf *);
  60. static struct nvme_fc_port_template lpfc_nvme_template;
  61. static union lpfc_wqe128 lpfc_iread_cmd_template;
  62. static union lpfc_wqe128 lpfc_iwrite_cmd_template;
  63. static union lpfc_wqe128 lpfc_icmnd_cmd_template;
  64. /* Setup WQE templates for NVME IOs */
  65. void
  66. lpfc_nvme_cmd_template(void)
  67. {
  68. union lpfc_wqe128 *wqe;
  69. /* IREAD template */
  70. wqe = &lpfc_iread_cmd_template;
  71. memset(wqe, 0, sizeof(union lpfc_wqe128));
  72. /* Word 0, 1, 2 - BDE is variable */
  73. /* Word 3 - cmd_buff_len, payload_offset_len is zero */
  74. /* Word 4 - total_xfer_len is variable */
  75. /* Word 5 - is zero */
  76. /* Word 6 - ctxt_tag, xri_tag is variable */
  77. /* Word 7 */
  78. bf_set(wqe_cmnd, &wqe->fcp_iread.wqe_com, CMD_FCP_IREAD64_WQE);
  79. bf_set(wqe_pu, &wqe->fcp_iread.wqe_com, PARM_READ_CHECK);
  80. bf_set(wqe_class, &wqe->fcp_iread.wqe_com, CLASS3);
  81. bf_set(wqe_ct, &wqe->fcp_iread.wqe_com, SLI4_CT_RPI);
  82. /* Word 8 - abort_tag is variable */
  83. /* Word 9 - reqtag is variable */
  84. /* Word 10 - dbde, wqes is variable */
  85. bf_set(wqe_qosd, &wqe->fcp_iread.wqe_com, 0);
  86. bf_set(wqe_nvme, &wqe->fcp_iread.wqe_com, 1);
  87. bf_set(wqe_iod, &wqe->fcp_iread.wqe_com, LPFC_WQE_IOD_READ);
  88. bf_set(wqe_lenloc, &wqe->fcp_iread.wqe_com, LPFC_WQE_LENLOC_WORD4);
  89. bf_set(wqe_dbde, &wqe->fcp_iread.wqe_com, 0);
  90. bf_set(wqe_wqes, &wqe->fcp_iread.wqe_com, 1);
  91. /* Word 11 - pbde is variable */
  92. bf_set(wqe_cmd_type, &wqe->fcp_iread.wqe_com, NVME_READ_CMD);
  93. bf_set(wqe_cqid, &wqe->fcp_iread.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  94. bf_set(wqe_pbde, &wqe->fcp_iread.wqe_com, 1);
  95. /* Word 12 - is zero */
  96. /* Word 13, 14, 15 - PBDE is variable */
  97. /* IWRITE template */
  98. wqe = &lpfc_iwrite_cmd_template;
  99. memset(wqe, 0, sizeof(union lpfc_wqe128));
  100. /* Word 0, 1, 2 - BDE is variable */
  101. /* Word 3 - cmd_buff_len, payload_offset_len is zero */
  102. /* Word 4 - total_xfer_len is variable */
  103. /* Word 5 - initial_xfer_len is variable */
  104. /* Word 6 - ctxt_tag, xri_tag is variable */
  105. /* Word 7 */
  106. bf_set(wqe_cmnd, &wqe->fcp_iwrite.wqe_com, CMD_FCP_IWRITE64_WQE);
  107. bf_set(wqe_pu, &wqe->fcp_iwrite.wqe_com, PARM_READ_CHECK);
  108. bf_set(wqe_class, &wqe->fcp_iwrite.wqe_com, CLASS3);
  109. bf_set(wqe_ct, &wqe->fcp_iwrite.wqe_com, SLI4_CT_RPI);
  110. /* Word 8 - abort_tag is variable */
  111. /* Word 9 - reqtag is variable */
  112. /* Word 10 - dbde, wqes is variable */
  113. bf_set(wqe_qosd, &wqe->fcp_iwrite.wqe_com, 0);
  114. bf_set(wqe_nvme, &wqe->fcp_iwrite.wqe_com, 1);
  115. bf_set(wqe_iod, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_IOD_WRITE);
  116. bf_set(wqe_lenloc, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_LENLOC_WORD4);
  117. bf_set(wqe_dbde, &wqe->fcp_iwrite.wqe_com, 0);
  118. bf_set(wqe_wqes, &wqe->fcp_iwrite.wqe_com, 1);
  119. /* Word 11 - pbde is variable */
  120. bf_set(wqe_cmd_type, &wqe->fcp_iwrite.wqe_com, NVME_WRITE_CMD);
  121. bf_set(wqe_cqid, &wqe->fcp_iwrite.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  122. bf_set(wqe_pbde, &wqe->fcp_iwrite.wqe_com, 1);
  123. /* Word 12 - is zero */
  124. /* Word 13, 14, 15 - PBDE is variable */
  125. /* ICMND template */
  126. wqe = &lpfc_icmnd_cmd_template;
  127. memset(wqe, 0, sizeof(union lpfc_wqe128));
  128. /* Word 0, 1, 2 - BDE is variable */
  129. /* Word 3 - payload_offset_len is variable */
  130. /* Word 4, 5 - is zero */
  131. /* Word 6 - ctxt_tag, xri_tag is variable */
  132. /* Word 7 */
  133. bf_set(wqe_cmnd, &wqe->fcp_icmd.wqe_com, CMD_FCP_ICMND64_WQE);
  134. bf_set(wqe_pu, &wqe->fcp_icmd.wqe_com, 0);
  135. bf_set(wqe_class, &wqe->fcp_icmd.wqe_com, CLASS3);
  136. bf_set(wqe_ct, &wqe->fcp_icmd.wqe_com, SLI4_CT_RPI);
  137. /* Word 8 - abort_tag is variable */
  138. /* Word 9 - reqtag is variable */
  139. /* Word 10 - dbde, wqes is variable */
  140. bf_set(wqe_qosd, &wqe->fcp_icmd.wqe_com, 1);
  141. bf_set(wqe_nvme, &wqe->fcp_icmd.wqe_com, 1);
  142. bf_set(wqe_iod, &wqe->fcp_icmd.wqe_com, LPFC_WQE_IOD_NONE);
  143. bf_set(wqe_lenloc, &wqe->fcp_icmd.wqe_com, LPFC_WQE_LENLOC_NONE);
  144. bf_set(wqe_dbde, &wqe->fcp_icmd.wqe_com, 0);
  145. bf_set(wqe_wqes, &wqe->fcp_icmd.wqe_com, 1);
  146. /* Word 11 */
  147. bf_set(wqe_cmd_type, &wqe->fcp_icmd.wqe_com, FCP_COMMAND);
  148. bf_set(wqe_cqid, &wqe->fcp_icmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  149. bf_set(wqe_pbde, &wqe->fcp_icmd.wqe_com, 0);
  150. /* Word 12, 13, 14, 15 - is zero */
  151. }
  152. /**
  153. * lpfc_nvme_create_queue -
  154. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  155. * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
  156. * @handle: An opaque driver handle used in follow-up calls.
  157. *
  158. * Driver registers this routine to preallocate and initialize any
  159. * internal data structures to bind the @qidx to its internal IO queues.
  160. * A hardware queue maps (qidx) to a specific driver MSI-X vector/EQ/CQ/WQ.
  161. *
  162. * Return value :
  163. * 0 - Success
  164. * -EINVAL - Unsupported input value.
  165. * -ENOMEM - Could not alloc necessary memory
  166. **/
  167. static int
  168. lpfc_nvme_create_queue(struct nvme_fc_local_port *pnvme_lport,
  169. unsigned int qidx, u16 qsize,
  170. void **handle)
  171. {
  172. struct lpfc_nvme_lport *lport;
  173. struct lpfc_vport *vport;
  174. struct lpfc_nvme_qhandle *qhandle;
  175. char *str;
  176. if (!pnvme_lport->private)
  177. return -ENOMEM;
  178. lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
  179. vport = lport->vport;
  180. qhandle = kzalloc(sizeof(struct lpfc_nvme_qhandle), GFP_KERNEL);
  181. if (qhandle == NULL)
  182. return -ENOMEM;
  183. qhandle->cpu_id = smp_processor_id();
  184. qhandle->qidx = qidx;
  185. /*
  186. * NVME qidx == 0 is the admin queue, so both admin queue
  187. * and first IO queue will use MSI-X vector and associated
  188. * EQ/CQ/WQ at index 0. After that they are sequentially assigned.
  189. */
  190. if (qidx) {
  191. str = "IO "; /* IO queue */
  192. qhandle->index = ((qidx - 1) %
  193. vport->phba->cfg_nvme_io_channel);
  194. } else {
  195. str = "ADM"; /* Admin queue */
  196. qhandle->index = qidx;
  197. }
  198. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
  199. "6073 Binding %s HdwQueue %d (cpu %d) to "
  200. "io_channel %d qhandle %p\n", str,
  201. qidx, qhandle->cpu_id, qhandle->index, qhandle);
  202. *handle = (void *)qhandle;
  203. return 0;
  204. }
  205. /**
  206. * lpfc_nvme_delete_queue -
  207. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  208. * @qidx: An cpu index used to affinitize IO queues and MSIX vectors.
  209. * @handle: An opaque driver handle from lpfc_nvme_create_queue
  210. *
  211. * Driver registers this routine to free
  212. * any internal data structures to bind the @qidx to its internal
  213. * IO queues.
  214. *
  215. * Return value :
  216. * 0 - Success
  217. * TODO: What are the failure codes.
  218. **/
  219. static void
  220. lpfc_nvme_delete_queue(struct nvme_fc_local_port *pnvme_lport,
  221. unsigned int qidx,
  222. void *handle)
  223. {
  224. struct lpfc_nvme_lport *lport;
  225. struct lpfc_vport *vport;
  226. if (!pnvme_lport->private)
  227. return;
  228. lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
  229. vport = lport->vport;
  230. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
  231. "6001 ENTER. lpfc_pnvme %p, qidx x%x qhandle %p\n",
  232. lport, qidx, handle);
  233. kfree(handle);
  234. }
  235. static void
  236. lpfc_nvme_localport_delete(struct nvme_fc_local_port *localport)
  237. {
  238. struct lpfc_nvme_lport *lport = localport->private;
  239. lpfc_printf_vlog(lport->vport, KERN_INFO, LOG_NVME,
  240. "6173 localport %p delete complete\n",
  241. lport);
  242. /* release any threads waiting for the unreg to complete */
  243. if (lport->vport->localport)
  244. complete(lport->lport_unreg_cmp);
  245. }
  246. /* lpfc_nvme_remoteport_delete
  247. *
  248. * @remoteport: Pointer to an nvme transport remoteport instance.
  249. *
  250. * This is a template downcall. NVME transport calls this function
  251. * when it has completed the unregistration of a previously
  252. * registered remoteport.
  253. *
  254. * Return value :
  255. * None
  256. */
  257. void
  258. lpfc_nvme_remoteport_delete(struct nvme_fc_remote_port *remoteport)
  259. {
  260. struct lpfc_nvme_rport *rport = remoteport->private;
  261. struct lpfc_vport *vport;
  262. struct lpfc_nodelist *ndlp;
  263. ndlp = rport->ndlp;
  264. if (!ndlp)
  265. goto rport_err;
  266. vport = ndlp->vport;
  267. if (!vport)
  268. goto rport_err;
  269. /* Remove this rport from the lport's list - memory is owned by the
  270. * transport. Remove the ndlp reference for the NVME transport before
  271. * calling state machine to remove the node.
  272. */
  273. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
  274. "6146 remoteport delete of remoteport %p\n",
  275. remoteport);
  276. spin_lock_irq(&vport->phba->hbalock);
  277. /* The register rebind might have occurred before the delete
  278. * downcall. Guard against this race.
  279. */
  280. if (ndlp->upcall_flags & NLP_WAIT_FOR_UNREG) {
  281. ndlp->nrport = NULL;
  282. ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
  283. spin_unlock_irq(&vport->phba->hbalock);
  284. /* Remove original register reference. The host transport
  285. * won't reference this rport/remoteport any further.
  286. */
  287. lpfc_nlp_put(ndlp);
  288. } else {
  289. spin_unlock_irq(&vport->phba->hbalock);
  290. }
  291. rport_err:
  292. return;
  293. }
  294. static void
  295. lpfc_nvme_cmpl_gen_req(struct lpfc_hba *phba, struct lpfc_iocbq *cmdwqe,
  296. struct lpfc_wcqe_complete *wcqe)
  297. {
  298. struct lpfc_vport *vport = cmdwqe->vport;
  299. struct lpfc_nvme_lport *lport;
  300. uint32_t status;
  301. struct nvmefc_ls_req *pnvme_lsreq;
  302. struct lpfc_dmabuf *buf_ptr;
  303. struct lpfc_nodelist *ndlp;
  304. pnvme_lsreq = (struct nvmefc_ls_req *)cmdwqe->context2;
  305. status = bf_get(lpfc_wcqe_c_status, wcqe) & LPFC_IOCB_STATUS_MASK;
  306. if (vport->localport) {
  307. lport = (struct lpfc_nvme_lport *)vport->localport->private;
  308. if (lport) {
  309. atomic_inc(&lport->fc4NvmeLsCmpls);
  310. if (status) {
  311. if (bf_get(lpfc_wcqe_c_xb, wcqe))
  312. atomic_inc(&lport->cmpl_ls_xb);
  313. atomic_inc(&lport->cmpl_ls_err);
  314. }
  315. }
  316. }
  317. ndlp = (struct lpfc_nodelist *)cmdwqe->context1;
  318. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
  319. "6047 nvme cmpl Enter "
  320. "Data %p DID %x Xri: %x status %x reason x%x cmd:%p "
  321. "lsreg:%p bmp:%p ndlp:%p\n",
  322. pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
  323. cmdwqe->sli4_xritag, status,
  324. (wcqe->parameter & 0xffff),
  325. cmdwqe, pnvme_lsreq, cmdwqe->context3, ndlp);
  326. lpfc_nvmeio_data(phba, "NVME LS CMPL: xri x%x stat x%x parm x%x\n",
  327. cmdwqe->sli4_xritag, status, wcqe->parameter);
  328. if (cmdwqe->context3) {
  329. buf_ptr = (struct lpfc_dmabuf *)cmdwqe->context3;
  330. lpfc_mbuf_free(phba, buf_ptr->virt, buf_ptr->phys);
  331. kfree(buf_ptr);
  332. cmdwqe->context3 = NULL;
  333. }
  334. if (pnvme_lsreq->done)
  335. pnvme_lsreq->done(pnvme_lsreq, status);
  336. else
  337. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
  338. "6046 nvme cmpl without done call back? "
  339. "Data %p DID %x Xri: %x status %x\n",
  340. pnvme_lsreq, ndlp ? ndlp->nlp_DID : 0,
  341. cmdwqe->sli4_xritag, status);
  342. if (ndlp) {
  343. lpfc_nlp_put(ndlp);
  344. cmdwqe->context1 = NULL;
  345. }
  346. lpfc_sli_release_iocbq(phba, cmdwqe);
  347. }
  348. static int
  349. lpfc_nvme_gen_req(struct lpfc_vport *vport, struct lpfc_dmabuf *bmp,
  350. struct lpfc_dmabuf *inp,
  351. struct nvmefc_ls_req *pnvme_lsreq,
  352. void (*cmpl)(struct lpfc_hba *, struct lpfc_iocbq *,
  353. struct lpfc_wcqe_complete *),
  354. struct lpfc_nodelist *ndlp, uint32_t num_entry,
  355. uint32_t tmo, uint8_t retry)
  356. {
  357. struct lpfc_hba *phba = vport->phba;
  358. union lpfc_wqe128 *wqe;
  359. struct lpfc_iocbq *genwqe;
  360. struct ulp_bde64 *bpl;
  361. struct ulp_bde64 bde;
  362. int i, rc, xmit_len, first_len;
  363. /* Allocate buffer for command WQE */
  364. genwqe = lpfc_sli_get_iocbq(phba);
  365. if (genwqe == NULL)
  366. return 1;
  367. wqe = &genwqe->wqe;
  368. memset(wqe, 0, sizeof(union lpfc_wqe));
  369. genwqe->context3 = (uint8_t *)bmp;
  370. genwqe->iocb_flag |= LPFC_IO_NVME_LS;
  371. /* Save for completion so we can release these resources */
  372. genwqe->context1 = lpfc_nlp_get(ndlp);
  373. genwqe->context2 = (uint8_t *)pnvme_lsreq;
  374. /* Fill in payload, bp points to frame payload */
  375. if (!tmo)
  376. /* FC spec states we need 3 * ratov for CT requests */
  377. tmo = (3 * phba->fc_ratov);
  378. /* For this command calculate the xmit length of the request bde. */
  379. xmit_len = 0;
  380. first_len = 0;
  381. bpl = (struct ulp_bde64 *)bmp->virt;
  382. for (i = 0; i < num_entry; i++) {
  383. bde.tus.w = bpl[i].tus.w;
  384. if (bde.tus.f.bdeFlags != BUFF_TYPE_BDE_64)
  385. break;
  386. xmit_len += bde.tus.f.bdeSize;
  387. if (i == 0)
  388. first_len = xmit_len;
  389. }
  390. genwqe->rsvd2 = num_entry;
  391. genwqe->hba_wqidx = 0;
  392. /* Words 0 - 2 */
  393. wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  394. wqe->generic.bde.tus.f.bdeSize = first_len;
  395. wqe->generic.bde.addrLow = bpl[0].addrLow;
  396. wqe->generic.bde.addrHigh = bpl[0].addrHigh;
  397. /* Word 3 */
  398. wqe->gen_req.request_payload_len = first_len;
  399. /* Word 4 */
  400. /* Word 5 */
  401. bf_set(wqe_dfctl, &wqe->gen_req.wge_ctl, 0);
  402. bf_set(wqe_si, &wqe->gen_req.wge_ctl, 1);
  403. bf_set(wqe_la, &wqe->gen_req.wge_ctl, 1);
  404. bf_set(wqe_rctl, &wqe->gen_req.wge_ctl, FC_RCTL_ELS4_REQ);
  405. bf_set(wqe_type, &wqe->gen_req.wge_ctl, FC_TYPE_NVME);
  406. /* Word 6 */
  407. bf_set(wqe_ctxt_tag, &wqe->gen_req.wqe_com,
  408. phba->sli4_hba.rpi_ids[ndlp->nlp_rpi]);
  409. bf_set(wqe_xri_tag, &wqe->gen_req.wqe_com, genwqe->sli4_xritag);
  410. /* Word 7 */
  411. bf_set(wqe_tmo, &wqe->gen_req.wqe_com, (vport->phba->fc_ratov-1));
  412. bf_set(wqe_class, &wqe->gen_req.wqe_com, CLASS3);
  413. bf_set(wqe_cmnd, &wqe->gen_req.wqe_com, CMD_GEN_REQUEST64_WQE);
  414. bf_set(wqe_ct, &wqe->gen_req.wqe_com, SLI4_CT_RPI);
  415. /* Word 8 */
  416. wqe->gen_req.wqe_com.abort_tag = genwqe->iotag;
  417. /* Word 9 */
  418. bf_set(wqe_reqtag, &wqe->gen_req.wqe_com, genwqe->iotag);
  419. /* Word 10 */
  420. bf_set(wqe_dbde, &wqe->gen_req.wqe_com, 1);
  421. bf_set(wqe_iod, &wqe->gen_req.wqe_com, LPFC_WQE_IOD_READ);
  422. bf_set(wqe_qosd, &wqe->gen_req.wqe_com, 1);
  423. bf_set(wqe_lenloc, &wqe->gen_req.wqe_com, LPFC_WQE_LENLOC_NONE);
  424. bf_set(wqe_ebde_cnt, &wqe->gen_req.wqe_com, 0);
  425. /* Word 11 */
  426. bf_set(wqe_cqid, &wqe->gen_req.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  427. bf_set(wqe_cmd_type, &wqe->gen_req.wqe_com, OTHER_COMMAND);
  428. /* Issue GEN REQ WQE for NPORT <did> */
  429. lpfc_printf_vlog(vport, KERN_INFO, LOG_ELS,
  430. "6050 Issue GEN REQ WQE to NPORT x%x "
  431. "Data: x%x x%x wq:%p lsreq:%p bmp:%p xmit:%d 1st:%d\n",
  432. ndlp->nlp_DID, genwqe->iotag,
  433. vport->port_state,
  434. genwqe, pnvme_lsreq, bmp, xmit_len, first_len);
  435. genwqe->wqe_cmpl = cmpl;
  436. genwqe->iocb_cmpl = NULL;
  437. genwqe->drvrTimeout = tmo + LPFC_DRVR_TIMEOUT;
  438. genwqe->vport = vport;
  439. genwqe->retry = retry;
  440. lpfc_nvmeio_data(phba, "NVME LS XMIT: xri x%x iotag x%x to x%06x\n",
  441. genwqe->sli4_xritag, genwqe->iotag, ndlp->nlp_DID);
  442. rc = lpfc_sli4_issue_wqe(phba, LPFC_ELS_RING, genwqe);
  443. if (rc) {
  444. lpfc_printf_vlog(vport, KERN_ERR, LOG_ELS,
  445. "6045 Issue GEN REQ WQE to NPORT x%x "
  446. "Data: x%x x%x\n",
  447. ndlp->nlp_DID, genwqe->iotag,
  448. vport->port_state);
  449. lpfc_sli_release_iocbq(phba, genwqe);
  450. return 1;
  451. }
  452. return 0;
  453. }
  454. /**
  455. * lpfc_nvme_ls_req - Issue an Link Service request
  456. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  457. * @lpfc_nvme_lport: Pointer to the driver's local port data
  458. * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
  459. *
  460. * Driver registers this routine to handle any link service request
  461. * from the nvme_fc transport to a remote nvme-aware port.
  462. *
  463. * Return value :
  464. * 0 - Success
  465. * TODO: What are the failure codes.
  466. **/
  467. static int
  468. lpfc_nvme_ls_req(struct nvme_fc_local_port *pnvme_lport,
  469. struct nvme_fc_remote_port *pnvme_rport,
  470. struct nvmefc_ls_req *pnvme_lsreq)
  471. {
  472. int ret = 0;
  473. struct lpfc_nvme_lport *lport;
  474. struct lpfc_nvme_rport *rport;
  475. struct lpfc_vport *vport;
  476. struct lpfc_nodelist *ndlp;
  477. struct ulp_bde64 *bpl;
  478. struct lpfc_dmabuf *bmp;
  479. uint16_t ntype, nstate;
  480. /* there are two dma buf in the request, actually there is one and
  481. * the second one is just the start address + cmd size.
  482. * Before calling lpfc_nvme_gen_req these buffers need to be wrapped
  483. * in a lpfc_dmabuf struct. When freeing we just free the wrapper
  484. * because the nvem layer owns the data bufs.
  485. * We do not have to break these packets open, we don't care what is in
  486. * them. And we do not have to look at the resonse data, we only care
  487. * that we got a response. All of the caring is going to happen in the
  488. * nvme-fc layer.
  489. */
  490. lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
  491. rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
  492. if (unlikely(!lport) || unlikely(!rport))
  493. return -EINVAL;
  494. vport = lport->vport;
  495. if (vport->load_flag & FC_UNLOADING)
  496. return -ENODEV;
  497. /* Need the ndlp. It is stored in the driver's rport. */
  498. ndlp = rport->ndlp;
  499. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  500. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
  501. "6051 Remoteport %p, rport has invalid ndlp. "
  502. "Failing LS Req\n", pnvme_rport);
  503. return -ENODEV;
  504. }
  505. /* The remote node has to be a mapped nvme target or an
  506. * unmapped nvme initiator or it's an error.
  507. */
  508. ntype = ndlp->nlp_type;
  509. nstate = ndlp->nlp_state;
  510. if ((ntype & NLP_NVME_TARGET && nstate != NLP_STE_MAPPED_NODE) ||
  511. (ntype & NLP_NVME_INITIATOR && nstate != NLP_STE_UNMAPPED_NODE)) {
  512. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
  513. "6088 DID x%06x not ready for "
  514. "IO. State x%x, Type x%x\n",
  515. pnvme_rport->port_id,
  516. ndlp->nlp_state, ndlp->nlp_type);
  517. return -ENODEV;
  518. }
  519. bmp = kmalloc(sizeof(struct lpfc_dmabuf), GFP_KERNEL);
  520. if (!bmp) {
  521. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
  522. "6044 Could not find node for DID %x\n",
  523. pnvme_rport->port_id);
  524. return 2;
  525. }
  526. INIT_LIST_HEAD(&bmp->list);
  527. bmp->virt = lpfc_mbuf_alloc(vport->phba, MEM_PRI, &(bmp->phys));
  528. if (!bmp->virt) {
  529. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
  530. "6042 Could not find node for DID %x\n",
  531. pnvme_rport->port_id);
  532. kfree(bmp);
  533. return 3;
  534. }
  535. bpl = (struct ulp_bde64 *)bmp->virt;
  536. bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rqstdma));
  537. bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rqstdma));
  538. bpl->tus.f.bdeFlags = 0;
  539. bpl->tus.f.bdeSize = pnvme_lsreq->rqstlen;
  540. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  541. bpl++;
  542. bpl->addrHigh = le32_to_cpu(putPaddrHigh(pnvme_lsreq->rspdma));
  543. bpl->addrLow = le32_to_cpu(putPaddrLow(pnvme_lsreq->rspdma));
  544. bpl->tus.f.bdeFlags = BUFF_TYPE_BDE_64I;
  545. bpl->tus.f.bdeSize = pnvme_lsreq->rsplen;
  546. bpl->tus.w = le32_to_cpu(bpl->tus.w);
  547. /* Expand print to include key fields. */
  548. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
  549. "6149 Issue LS Req to DID 0x%06x lport %p, rport %p "
  550. "lsreq%p rqstlen:%d rsplen:%d %pad %pad\n",
  551. ndlp->nlp_DID,
  552. pnvme_lport, pnvme_rport,
  553. pnvme_lsreq, pnvme_lsreq->rqstlen,
  554. pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
  555. &pnvme_lsreq->rspdma);
  556. atomic_inc(&lport->fc4NvmeLsRequests);
  557. /* Hardcode the wait to 30 seconds. Connections are failing otherwise.
  558. * This code allows it all to work.
  559. */
  560. ret = lpfc_nvme_gen_req(vport, bmp, pnvme_lsreq->rqstaddr,
  561. pnvme_lsreq, lpfc_nvme_cmpl_gen_req,
  562. ndlp, 2, 30, 0);
  563. if (ret != WQE_SUCCESS) {
  564. atomic_inc(&lport->xmt_ls_err);
  565. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
  566. "6052 EXIT. issue ls wqe failed lport %p, "
  567. "rport %p lsreq%p Status %x DID %x\n",
  568. pnvme_lport, pnvme_rport, pnvme_lsreq,
  569. ret, ndlp->nlp_DID);
  570. lpfc_mbuf_free(vport->phba, bmp->virt, bmp->phys);
  571. kfree(bmp);
  572. return ret;
  573. }
  574. /* Stub in routine and return 0 for now. */
  575. return ret;
  576. }
  577. /**
  578. * lpfc_nvme_ls_abort - Issue an Link Service request
  579. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  580. * @lpfc_nvme_lport: Pointer to the driver's local port data
  581. * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
  582. *
  583. * Driver registers this routine to handle any link service request
  584. * from the nvme_fc transport to a remote nvme-aware port.
  585. *
  586. * Return value :
  587. * 0 - Success
  588. * TODO: What are the failure codes.
  589. **/
  590. static void
  591. lpfc_nvme_ls_abort(struct nvme_fc_local_port *pnvme_lport,
  592. struct nvme_fc_remote_port *pnvme_rport,
  593. struct nvmefc_ls_req *pnvme_lsreq)
  594. {
  595. struct lpfc_nvme_lport *lport;
  596. struct lpfc_vport *vport;
  597. struct lpfc_hba *phba;
  598. struct lpfc_nodelist *ndlp;
  599. LIST_HEAD(abort_list);
  600. struct lpfc_sli_ring *pring;
  601. struct lpfc_iocbq *wqe, *next_wqe;
  602. lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
  603. if (unlikely(!lport))
  604. return;
  605. vport = lport->vport;
  606. phba = vport->phba;
  607. if (vport->load_flag & FC_UNLOADING)
  608. return;
  609. ndlp = lpfc_findnode_did(vport, pnvme_rport->port_id);
  610. if (!ndlp) {
  611. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  612. "6049 Could not find node for DID %x\n",
  613. pnvme_rport->port_id);
  614. return;
  615. }
  616. /* Expand print to include key fields. */
  617. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
  618. "6040 ENTER. lport %p, rport %p lsreq %p rqstlen:%d "
  619. "rsplen:%d %pad %pad\n",
  620. pnvme_lport, pnvme_rport,
  621. pnvme_lsreq, pnvme_lsreq->rqstlen,
  622. pnvme_lsreq->rsplen, &pnvme_lsreq->rqstdma,
  623. &pnvme_lsreq->rspdma);
  624. /*
  625. * Lock the ELS ring txcmplq and build a local list of all ELS IOs
  626. * that need an ABTS. The IOs need to stay on the txcmplq so that
  627. * the abort operation completes them successfully.
  628. */
  629. pring = phba->sli4_hba.nvmels_wq->pring;
  630. spin_lock_irq(&phba->hbalock);
  631. spin_lock(&pring->ring_lock);
  632. list_for_each_entry_safe(wqe, next_wqe, &pring->txcmplq, list) {
  633. /* Add to abort_list on on NDLP match. */
  634. if (lpfc_check_sli_ndlp(phba, pring, wqe, ndlp)) {
  635. wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
  636. list_add_tail(&wqe->dlist, &abort_list);
  637. }
  638. }
  639. spin_unlock(&pring->ring_lock);
  640. spin_unlock_irq(&phba->hbalock);
  641. /* Abort the targeted IOs and remove them from the abort list. */
  642. list_for_each_entry_safe(wqe, next_wqe, &abort_list, dlist) {
  643. atomic_inc(&lport->xmt_ls_abort);
  644. spin_lock_irq(&phba->hbalock);
  645. list_del_init(&wqe->dlist);
  646. lpfc_sli_issue_abort_iotag(phba, pring, wqe);
  647. spin_unlock_irq(&phba->hbalock);
  648. }
  649. }
  650. /* Fix up the existing sgls for NVME IO. */
  651. static inline void
  652. lpfc_nvme_adj_fcp_sgls(struct lpfc_vport *vport,
  653. struct lpfc_nvme_buf *lpfc_ncmd,
  654. struct nvmefc_fcp_req *nCmd)
  655. {
  656. struct lpfc_hba *phba = vport->phba;
  657. struct sli4_sge *sgl;
  658. union lpfc_wqe128 *wqe;
  659. uint32_t *wptr, *dptr;
  660. /*
  661. * Get a local pointer to the built-in wqe and correct
  662. * the cmd size to match NVME's 96 bytes and fix
  663. * the dma address.
  664. */
  665. wqe = &lpfc_ncmd->cur_iocbq.wqe;
  666. /*
  667. * Adjust the FCP_CMD and FCP_RSP DMA data and sge_len to
  668. * match NVME. NVME sends 96 bytes. Also, use the
  669. * nvme commands command and response dma addresses
  670. * rather than the virtual memory to ease the restore
  671. * operation.
  672. */
  673. sgl = lpfc_ncmd->nvme_sgl;
  674. sgl->sge_len = cpu_to_le32(nCmd->cmdlen);
  675. if (phba->cfg_nvme_embed_cmd) {
  676. sgl->addr_hi = 0;
  677. sgl->addr_lo = 0;
  678. /* Word 0-2 - NVME CMND IU (embedded payload) */
  679. wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_IMMED;
  680. wqe->generic.bde.tus.f.bdeSize = 56;
  681. wqe->generic.bde.addrHigh = 0;
  682. wqe->generic.bde.addrLow = 64; /* Word 16 */
  683. /* Word 10 - dbde is 0, wqes is 1 in template */
  684. /*
  685. * Embed the payload in the last half of the WQE
  686. * WQE words 16-30 get the NVME CMD IU payload
  687. *
  688. * WQE words 16-19 get payload Words 1-4
  689. * WQE words 20-21 get payload Words 6-7
  690. * WQE words 22-29 get payload Words 16-23
  691. */
  692. wptr = &wqe->words[16]; /* WQE ptr */
  693. dptr = (uint32_t *)nCmd->cmdaddr; /* payload ptr */
  694. dptr++; /* Skip Word 0 in payload */
  695. *wptr++ = *dptr++; /* Word 1 */
  696. *wptr++ = *dptr++; /* Word 2 */
  697. *wptr++ = *dptr++; /* Word 3 */
  698. *wptr++ = *dptr++; /* Word 4 */
  699. dptr++; /* Skip Word 5 in payload */
  700. *wptr++ = *dptr++; /* Word 6 */
  701. *wptr++ = *dptr++; /* Word 7 */
  702. dptr += 8; /* Skip Words 8-15 in payload */
  703. *wptr++ = *dptr++; /* Word 16 */
  704. *wptr++ = *dptr++; /* Word 17 */
  705. *wptr++ = *dptr++; /* Word 18 */
  706. *wptr++ = *dptr++; /* Word 19 */
  707. *wptr++ = *dptr++; /* Word 20 */
  708. *wptr++ = *dptr++; /* Word 21 */
  709. *wptr++ = *dptr++; /* Word 22 */
  710. *wptr = *dptr; /* Word 23 */
  711. } else {
  712. sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->cmddma));
  713. sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->cmddma));
  714. /* Word 0-2 - NVME CMND IU Inline BDE */
  715. wqe->generic.bde.tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  716. wqe->generic.bde.tus.f.bdeSize = nCmd->cmdlen;
  717. wqe->generic.bde.addrHigh = sgl->addr_hi;
  718. wqe->generic.bde.addrLow = sgl->addr_lo;
  719. /* Word 10 */
  720. bf_set(wqe_dbde, &wqe->generic.wqe_com, 1);
  721. bf_set(wqe_wqes, &wqe->generic.wqe_com, 0);
  722. }
  723. sgl++;
  724. /* Setup the physical region for the FCP RSP */
  725. sgl->addr_hi = cpu_to_le32(putPaddrHigh(nCmd->rspdma));
  726. sgl->addr_lo = cpu_to_le32(putPaddrLow(nCmd->rspdma));
  727. sgl->word2 = le32_to_cpu(sgl->word2);
  728. if (nCmd->sg_cnt)
  729. bf_set(lpfc_sli4_sge_last, sgl, 0);
  730. else
  731. bf_set(lpfc_sli4_sge_last, sgl, 1);
  732. sgl->word2 = cpu_to_le32(sgl->word2);
  733. sgl->sge_len = cpu_to_le32(nCmd->rsplen);
  734. }
  735. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  736. static void
  737. lpfc_nvme_ktime(struct lpfc_hba *phba,
  738. struct lpfc_nvme_buf *lpfc_ncmd)
  739. {
  740. uint64_t seg1, seg2, seg3, seg4;
  741. uint64_t segsum;
  742. if (!lpfc_ncmd->ts_last_cmd ||
  743. !lpfc_ncmd->ts_cmd_start ||
  744. !lpfc_ncmd->ts_cmd_wqput ||
  745. !lpfc_ncmd->ts_isr_cmpl ||
  746. !lpfc_ncmd->ts_data_nvme)
  747. return;
  748. if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_cmd_start)
  749. return;
  750. if (lpfc_ncmd->ts_cmd_start < lpfc_ncmd->ts_last_cmd)
  751. return;
  752. if (lpfc_ncmd->ts_cmd_wqput < lpfc_ncmd->ts_cmd_start)
  753. return;
  754. if (lpfc_ncmd->ts_isr_cmpl < lpfc_ncmd->ts_cmd_wqput)
  755. return;
  756. if (lpfc_ncmd->ts_data_nvme < lpfc_ncmd->ts_isr_cmpl)
  757. return;
  758. /*
  759. * Segment 1 - Time from Last FCP command cmpl is handed
  760. * off to NVME Layer to start of next command.
  761. * Segment 2 - Time from Driver receives a IO cmd start
  762. * from NVME Layer to WQ put is done on IO cmd.
  763. * Segment 3 - Time from Driver WQ put is done on IO cmd
  764. * to MSI-X ISR for IO cmpl.
  765. * Segment 4 - Time from MSI-X ISR for IO cmpl to when
  766. * cmpl is handled off to the NVME Layer.
  767. */
  768. seg1 = lpfc_ncmd->ts_cmd_start - lpfc_ncmd->ts_last_cmd;
  769. if (seg1 > 5000000) /* 5 ms - for sequential IOs only */
  770. seg1 = 0;
  771. /* Calculate times relative to start of IO */
  772. seg2 = (lpfc_ncmd->ts_cmd_wqput - lpfc_ncmd->ts_cmd_start);
  773. segsum = seg2;
  774. seg3 = lpfc_ncmd->ts_isr_cmpl - lpfc_ncmd->ts_cmd_start;
  775. if (segsum > seg3)
  776. return;
  777. seg3 -= segsum;
  778. segsum += seg3;
  779. seg4 = lpfc_ncmd->ts_data_nvme - lpfc_ncmd->ts_cmd_start;
  780. if (segsum > seg4)
  781. return;
  782. seg4 -= segsum;
  783. phba->ktime_data_samples++;
  784. phba->ktime_seg1_total += seg1;
  785. if (seg1 < phba->ktime_seg1_min)
  786. phba->ktime_seg1_min = seg1;
  787. else if (seg1 > phba->ktime_seg1_max)
  788. phba->ktime_seg1_max = seg1;
  789. phba->ktime_seg2_total += seg2;
  790. if (seg2 < phba->ktime_seg2_min)
  791. phba->ktime_seg2_min = seg2;
  792. else if (seg2 > phba->ktime_seg2_max)
  793. phba->ktime_seg2_max = seg2;
  794. phba->ktime_seg3_total += seg3;
  795. if (seg3 < phba->ktime_seg3_min)
  796. phba->ktime_seg3_min = seg3;
  797. else if (seg3 > phba->ktime_seg3_max)
  798. phba->ktime_seg3_max = seg3;
  799. phba->ktime_seg4_total += seg4;
  800. if (seg4 < phba->ktime_seg4_min)
  801. phba->ktime_seg4_min = seg4;
  802. else if (seg4 > phba->ktime_seg4_max)
  803. phba->ktime_seg4_max = seg4;
  804. lpfc_ncmd->ts_last_cmd = 0;
  805. lpfc_ncmd->ts_cmd_start = 0;
  806. lpfc_ncmd->ts_cmd_wqput = 0;
  807. lpfc_ncmd->ts_isr_cmpl = 0;
  808. lpfc_ncmd->ts_data_nvme = 0;
  809. }
  810. #endif
  811. /**
  812. * lpfc_nvme_io_cmd_wqe_cmpl - Complete an NVME-over-FCP IO
  813. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  814. * @lpfc_nvme_lport: Pointer to the driver's local port data
  815. * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
  816. *
  817. * Driver registers this routine as it io request handler. This
  818. * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
  819. * data structure to the rport indicated in @lpfc_nvme_rport.
  820. *
  821. * Return value :
  822. * 0 - Success
  823. * TODO: What are the failure codes.
  824. **/
  825. static void
  826. lpfc_nvme_io_cmd_wqe_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *pwqeIn,
  827. struct lpfc_wcqe_complete *wcqe)
  828. {
  829. struct lpfc_nvme_buf *lpfc_ncmd =
  830. (struct lpfc_nvme_buf *)pwqeIn->context1;
  831. struct lpfc_vport *vport = pwqeIn->vport;
  832. struct nvmefc_fcp_req *nCmd;
  833. struct nvme_fc_ersp_iu *ep;
  834. struct nvme_fc_cmd_iu *cp;
  835. struct lpfc_nvme_rport *rport;
  836. struct lpfc_nodelist *ndlp;
  837. struct lpfc_nvme_fcpreq_priv *freqpriv;
  838. struct lpfc_nvme_lport *lport;
  839. struct lpfc_nvme_ctrl_stat *cstat;
  840. unsigned long flags;
  841. uint32_t code, status, idx;
  842. uint16_t cid, sqhd, data;
  843. uint32_t *ptr;
  844. /* Sanity check on return of outstanding command */
  845. if (!lpfc_ncmd || !lpfc_ncmd->nvmeCmd || !lpfc_ncmd->nrport) {
  846. if (!lpfc_ncmd) {
  847. lpfc_printf_vlog(vport, KERN_ERR,
  848. LOG_NODE | LOG_NVME_IOERR,
  849. "6071 Null lpfc_ncmd pointer. No "
  850. "release, skip completion\n");
  851. return;
  852. }
  853. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
  854. "6066 Missing cmpl ptrs: lpfc_ncmd %p, "
  855. "nvmeCmd %p nrport %p\n",
  856. lpfc_ncmd, lpfc_ncmd->nvmeCmd,
  857. lpfc_ncmd->nrport);
  858. /* Release the lpfc_ncmd regardless of the missing elements. */
  859. lpfc_release_nvme_buf(phba, lpfc_ncmd);
  860. return;
  861. }
  862. nCmd = lpfc_ncmd->nvmeCmd;
  863. rport = lpfc_ncmd->nrport;
  864. status = bf_get(lpfc_wcqe_c_status, wcqe);
  865. if (vport->localport) {
  866. lport = (struct lpfc_nvme_lport *)vport->localport->private;
  867. if (lport) {
  868. idx = lpfc_ncmd->cur_iocbq.hba_wqidx;
  869. cstat = &lport->cstat[idx];
  870. atomic_inc(&cstat->fc4NvmeIoCmpls);
  871. if (status) {
  872. if (bf_get(lpfc_wcqe_c_xb, wcqe))
  873. atomic_inc(&lport->cmpl_fcp_xb);
  874. atomic_inc(&lport->cmpl_fcp_err);
  875. }
  876. }
  877. }
  878. lpfc_nvmeio_data(phba, "NVME FCP CMPL: xri x%x stat x%x parm x%x\n",
  879. lpfc_ncmd->cur_iocbq.sli4_xritag,
  880. status, wcqe->parameter);
  881. /*
  882. * Catch race where our node has transitioned, but the
  883. * transport is still transitioning.
  884. */
  885. ndlp = rport->ndlp;
  886. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  887. lpfc_printf_vlog(vport, KERN_ERR, LOG_NODE | LOG_NVME_IOERR,
  888. "6061 rport %p, DID x%06x node not ready.\n",
  889. rport, rport->remoteport->port_id);
  890. ndlp = lpfc_findnode_did(vport, rport->remoteport->port_id);
  891. if (!ndlp) {
  892. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
  893. "6062 Ignoring NVME cmpl. No ndlp\n");
  894. goto out_err;
  895. }
  896. }
  897. code = bf_get(lpfc_wcqe_c_code, wcqe);
  898. if (code == CQE_CODE_NVME_ERSP) {
  899. /* For this type of CQE, we need to rebuild the rsp */
  900. ep = (struct nvme_fc_ersp_iu *)nCmd->rspaddr;
  901. /*
  902. * Get Command Id from cmd to plug into response. This
  903. * code is not needed in the next NVME Transport drop.
  904. */
  905. cp = (struct nvme_fc_cmd_iu *)nCmd->cmdaddr;
  906. cid = cp->sqe.common.command_id;
  907. /*
  908. * RSN is in CQE word 2
  909. * SQHD is in CQE Word 3 bits 15:0
  910. * Cmd Specific info is in CQE Word 1
  911. * and in CQE Word 0 bits 15:0
  912. */
  913. sqhd = bf_get(lpfc_wcqe_c_sqhead, wcqe);
  914. /* Now lets build the NVME ERSP IU */
  915. ep->iu_len = cpu_to_be16(8);
  916. ep->rsn = wcqe->parameter;
  917. ep->xfrd_len = cpu_to_be32(nCmd->payload_length);
  918. ep->rsvd12 = 0;
  919. ptr = (uint32_t *)&ep->cqe.result.u64;
  920. *ptr++ = wcqe->total_data_placed;
  921. data = bf_get(lpfc_wcqe_c_ersp0, wcqe);
  922. *ptr = (uint32_t)data;
  923. ep->cqe.sq_head = sqhd;
  924. ep->cqe.sq_id = nCmd->sqid;
  925. ep->cqe.command_id = cid;
  926. ep->cqe.status = 0;
  927. lpfc_ncmd->status = IOSTAT_SUCCESS;
  928. lpfc_ncmd->result = 0;
  929. nCmd->rcv_rsplen = LPFC_NVME_ERSP_LEN;
  930. nCmd->transferred_length = nCmd->payload_length;
  931. } else {
  932. lpfc_ncmd->status = (status & LPFC_IOCB_STATUS_MASK);
  933. lpfc_ncmd->result = (wcqe->parameter & IOERR_PARAM_MASK);
  934. /* For NVME, the only failure path that results in an
  935. * IO error is when the adapter rejects it. All other
  936. * conditions are a success case and resolved by the
  937. * transport.
  938. * IOSTAT_FCP_RSP_ERROR means:
  939. * 1. Length of data received doesn't match total
  940. * transfer length in WQE
  941. * 2. If the RSP payload does NOT match these cases:
  942. * a. RSP length 12/24 bytes and all zeros
  943. * b. NVME ERSP
  944. */
  945. switch (lpfc_ncmd->status) {
  946. case IOSTAT_SUCCESS:
  947. nCmd->transferred_length = wcqe->total_data_placed;
  948. nCmd->rcv_rsplen = 0;
  949. nCmd->status = 0;
  950. break;
  951. case IOSTAT_FCP_RSP_ERROR:
  952. nCmd->transferred_length = wcqe->total_data_placed;
  953. nCmd->rcv_rsplen = wcqe->parameter;
  954. nCmd->status = 0;
  955. /* Sanity check */
  956. if (nCmd->rcv_rsplen == LPFC_NVME_ERSP_LEN)
  957. break;
  958. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
  959. "6081 NVME Completion Protocol Error: "
  960. "xri %x status x%x result x%x "
  961. "placed x%x\n",
  962. lpfc_ncmd->cur_iocbq.sli4_xritag,
  963. lpfc_ncmd->status, lpfc_ncmd->result,
  964. wcqe->total_data_placed);
  965. break;
  966. case IOSTAT_LOCAL_REJECT:
  967. /* Let fall through to set command final state. */
  968. if (lpfc_ncmd->result == IOERR_ABORT_REQUESTED)
  969. lpfc_printf_vlog(vport, KERN_INFO,
  970. LOG_NVME_IOERR,
  971. "6032 Delay Aborted cmd %p "
  972. "nvme cmd %p, xri x%x, "
  973. "xb %d\n",
  974. lpfc_ncmd, nCmd,
  975. lpfc_ncmd->cur_iocbq.sli4_xritag,
  976. bf_get(lpfc_wcqe_c_xb, wcqe));
  977. default:
  978. out_err:
  979. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  980. "6072 NVME Completion Error: xri %x "
  981. "status x%x result x%x placed x%x\n",
  982. lpfc_ncmd->cur_iocbq.sli4_xritag,
  983. lpfc_ncmd->status, lpfc_ncmd->result,
  984. wcqe->total_data_placed);
  985. nCmd->transferred_length = 0;
  986. nCmd->rcv_rsplen = 0;
  987. nCmd->status = NVME_SC_INTERNAL;
  988. }
  989. }
  990. /* pick up SLI4 exhange busy condition */
  991. if (bf_get(lpfc_wcqe_c_xb, wcqe))
  992. lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
  993. else
  994. lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
  995. /* Update stats and complete the IO. There is
  996. * no need for dma unprep because the nvme_transport
  997. * owns the dma address.
  998. */
  999. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1000. if (lpfc_ncmd->ts_cmd_start) {
  1001. lpfc_ncmd->ts_isr_cmpl = pwqeIn->isr_timestamp;
  1002. lpfc_ncmd->ts_data_nvme = ktime_get_ns();
  1003. phba->ktime_last_cmd = lpfc_ncmd->ts_data_nvme;
  1004. lpfc_nvme_ktime(phba, lpfc_ncmd);
  1005. }
  1006. if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
  1007. if (lpfc_ncmd->cpu != smp_processor_id())
  1008. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
  1009. "6701 CPU Check cmpl: "
  1010. "cpu %d expect %d\n",
  1011. smp_processor_id(), lpfc_ncmd->cpu);
  1012. if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
  1013. phba->cpucheck_cmpl_io[lpfc_ncmd->cpu]++;
  1014. }
  1015. #endif
  1016. /* NVME targets need completion held off until the abort exchange
  1017. * completes unless the NVME Rport is getting unregistered.
  1018. */
  1019. if (!(lpfc_ncmd->flags & LPFC_SBUF_XBUSY)) {
  1020. freqpriv = nCmd->private;
  1021. freqpriv->nvme_buf = NULL;
  1022. nCmd->done(nCmd);
  1023. lpfc_ncmd->nvmeCmd = NULL;
  1024. }
  1025. spin_lock_irqsave(&phba->hbalock, flags);
  1026. lpfc_ncmd->nrport = NULL;
  1027. spin_unlock_irqrestore(&phba->hbalock, flags);
  1028. /* Call release with XB=1 to queue the IO into the abort list. */
  1029. lpfc_release_nvme_buf(phba, lpfc_ncmd);
  1030. }
  1031. /**
  1032. * lpfc_nvme_prep_io_cmd - Issue an NVME-over-FCP IO
  1033. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  1034. * @lpfc_nvme_lport: Pointer to the driver's local port data
  1035. * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
  1036. * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
  1037. * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
  1038. *
  1039. * Driver registers this routine as it io request handler. This
  1040. * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
  1041. * data structure to the rport indicated in @lpfc_nvme_rport.
  1042. *
  1043. * Return value :
  1044. * 0 - Success
  1045. * TODO: What are the failure codes.
  1046. **/
  1047. static int
  1048. lpfc_nvme_prep_io_cmd(struct lpfc_vport *vport,
  1049. struct lpfc_nvme_buf *lpfc_ncmd,
  1050. struct lpfc_nodelist *pnode,
  1051. struct lpfc_nvme_ctrl_stat *cstat)
  1052. {
  1053. struct lpfc_hba *phba = vport->phba;
  1054. struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
  1055. struct lpfc_iocbq *pwqeq = &(lpfc_ncmd->cur_iocbq);
  1056. union lpfc_wqe128 *wqe = &pwqeq->wqe;
  1057. uint32_t req_len;
  1058. if (!pnode || !NLP_CHK_NODE_ACT(pnode))
  1059. return -EINVAL;
  1060. /*
  1061. * There are three possibilities here - use scatter-gather segment, use
  1062. * the single mapping, or neither.
  1063. */
  1064. if (nCmd->sg_cnt) {
  1065. if (nCmd->io_dir == NVMEFC_FCP_WRITE) {
  1066. /* From the iwrite template, initialize words 7 - 11 */
  1067. memcpy(&wqe->words[7],
  1068. &lpfc_iwrite_cmd_template.words[7],
  1069. sizeof(uint32_t) * 5);
  1070. /* Word 4 */
  1071. wqe->fcp_iwrite.total_xfer_len = nCmd->payload_length;
  1072. /* Word 5 */
  1073. if ((phba->cfg_nvme_enable_fb) &&
  1074. (pnode->nlp_flag & NLP_FIRSTBURST)) {
  1075. req_len = lpfc_ncmd->nvmeCmd->payload_length;
  1076. if (req_len < pnode->nvme_fb_size)
  1077. wqe->fcp_iwrite.initial_xfer_len =
  1078. req_len;
  1079. else
  1080. wqe->fcp_iwrite.initial_xfer_len =
  1081. pnode->nvme_fb_size;
  1082. } else {
  1083. wqe->fcp_iwrite.initial_xfer_len = 0;
  1084. }
  1085. atomic_inc(&cstat->fc4NvmeOutputRequests);
  1086. } else {
  1087. /* From the iread template, initialize words 7 - 11 */
  1088. memcpy(&wqe->words[7],
  1089. &lpfc_iread_cmd_template.words[7],
  1090. sizeof(uint32_t) * 5);
  1091. /* Word 4 */
  1092. wqe->fcp_iread.total_xfer_len = nCmd->payload_length;
  1093. /* Word 5 */
  1094. wqe->fcp_iread.rsrvd5 = 0;
  1095. atomic_inc(&cstat->fc4NvmeInputRequests);
  1096. }
  1097. } else {
  1098. /* From the icmnd template, initialize words 4 - 11 */
  1099. memcpy(&wqe->words[4], &lpfc_icmnd_cmd_template.words[4],
  1100. sizeof(uint32_t) * 8);
  1101. atomic_inc(&cstat->fc4NvmeControlRequests);
  1102. }
  1103. /*
  1104. * Finish initializing those WQE fields that are independent
  1105. * of the nvme_cmnd request_buffer
  1106. */
  1107. /* Word 3 */
  1108. bf_set(payload_offset_len, &wqe->fcp_icmd,
  1109. (nCmd->rsplen + nCmd->cmdlen));
  1110. /* Word 6 */
  1111. bf_set(wqe_ctxt_tag, &wqe->generic.wqe_com,
  1112. phba->sli4_hba.rpi_ids[pnode->nlp_rpi]);
  1113. bf_set(wqe_xri_tag, &wqe->generic.wqe_com, pwqeq->sli4_xritag);
  1114. /* Word 8 */
  1115. wqe->generic.wqe_com.abort_tag = pwqeq->iotag;
  1116. /* Word 9 */
  1117. bf_set(wqe_reqtag, &wqe->generic.wqe_com, pwqeq->iotag);
  1118. /* Words 13 14 15 are for PBDE support */
  1119. pwqeq->vport = vport;
  1120. return 0;
  1121. }
  1122. /**
  1123. * lpfc_nvme_prep_io_dma - Issue an NVME-over-FCP IO
  1124. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  1125. * @lpfc_nvme_lport: Pointer to the driver's local port data
  1126. * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
  1127. * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
  1128. * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
  1129. *
  1130. * Driver registers this routine as it io request handler. This
  1131. * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
  1132. * data structure to the rport indicated in @lpfc_nvme_rport.
  1133. *
  1134. * Return value :
  1135. * 0 - Success
  1136. * TODO: What are the failure codes.
  1137. **/
  1138. static int
  1139. lpfc_nvme_prep_io_dma(struct lpfc_vport *vport,
  1140. struct lpfc_nvme_buf *lpfc_ncmd)
  1141. {
  1142. struct lpfc_hba *phba = vport->phba;
  1143. struct nvmefc_fcp_req *nCmd = lpfc_ncmd->nvmeCmd;
  1144. union lpfc_wqe128 *wqe = &lpfc_ncmd->cur_iocbq.wqe;
  1145. struct sli4_sge *sgl = lpfc_ncmd->nvme_sgl;
  1146. struct scatterlist *data_sg;
  1147. struct sli4_sge *first_data_sgl;
  1148. struct ulp_bde64 *bde;
  1149. dma_addr_t physaddr;
  1150. uint32_t num_bde = 0;
  1151. uint32_t dma_len;
  1152. uint32_t dma_offset = 0;
  1153. int nseg, i;
  1154. /* Fix up the command and response DMA stuff. */
  1155. lpfc_nvme_adj_fcp_sgls(vport, lpfc_ncmd, nCmd);
  1156. /*
  1157. * There are three possibilities here - use scatter-gather segment, use
  1158. * the single mapping, or neither.
  1159. */
  1160. if (nCmd->sg_cnt) {
  1161. /*
  1162. * Jump over the cmd and rsp SGEs. The fix routine
  1163. * has already adjusted for this.
  1164. */
  1165. sgl += 2;
  1166. first_data_sgl = sgl;
  1167. lpfc_ncmd->seg_cnt = nCmd->sg_cnt;
  1168. if (lpfc_ncmd->seg_cnt > lpfc_nvme_template.max_sgl_segments) {
  1169. lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
  1170. "6058 Too many sg segments from "
  1171. "NVME Transport. Max %d, "
  1172. "nvmeIO sg_cnt %d\n",
  1173. phba->cfg_nvme_seg_cnt + 1,
  1174. lpfc_ncmd->seg_cnt);
  1175. lpfc_ncmd->seg_cnt = 0;
  1176. return 1;
  1177. }
  1178. /*
  1179. * The driver established a maximum scatter-gather segment count
  1180. * during probe that limits the number of sg elements in any
  1181. * single nvme command. Just run through the seg_cnt and format
  1182. * the sge's.
  1183. */
  1184. nseg = nCmd->sg_cnt;
  1185. data_sg = nCmd->first_sgl;
  1186. for (i = 0; i < nseg; i++) {
  1187. if (data_sg == NULL) {
  1188. lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
  1189. "6059 dptr err %d, nseg %d\n",
  1190. i, nseg);
  1191. lpfc_ncmd->seg_cnt = 0;
  1192. return 1;
  1193. }
  1194. physaddr = data_sg->dma_address;
  1195. dma_len = data_sg->length;
  1196. sgl->addr_lo = cpu_to_le32(putPaddrLow(physaddr));
  1197. sgl->addr_hi = cpu_to_le32(putPaddrHigh(physaddr));
  1198. sgl->word2 = le32_to_cpu(sgl->word2);
  1199. if ((num_bde + 1) == nseg)
  1200. bf_set(lpfc_sli4_sge_last, sgl, 1);
  1201. else
  1202. bf_set(lpfc_sli4_sge_last, sgl, 0);
  1203. bf_set(lpfc_sli4_sge_offset, sgl, dma_offset);
  1204. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_DATA);
  1205. sgl->word2 = cpu_to_le32(sgl->word2);
  1206. sgl->sge_len = cpu_to_le32(dma_len);
  1207. dma_offset += dma_len;
  1208. data_sg = sg_next(data_sg);
  1209. sgl++;
  1210. }
  1211. if (phba->cfg_enable_pbde) {
  1212. /* Use PBDE support for first SGL only, offset == 0 */
  1213. /* Words 13-15 */
  1214. bde = (struct ulp_bde64 *)
  1215. &wqe->words[13];
  1216. bde->addrLow = first_data_sgl->addr_lo;
  1217. bde->addrHigh = first_data_sgl->addr_hi;
  1218. bde->tus.f.bdeSize =
  1219. le32_to_cpu(first_data_sgl->sge_len);
  1220. bde->tus.f.bdeFlags = BUFF_TYPE_BDE_64;
  1221. bde->tus.w = cpu_to_le32(bde->tus.w);
  1222. /* wqe_pbde is 1 in template */
  1223. } else {
  1224. memset(&wqe->words[13], 0, (sizeof(uint32_t) * 3));
  1225. bf_set(wqe_pbde, &wqe->generic.wqe_com, 0);
  1226. }
  1227. } else {
  1228. /* For this clause to be valid, the payload_length
  1229. * and sg_cnt must zero.
  1230. */
  1231. if (nCmd->payload_length != 0) {
  1232. lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
  1233. "6063 NVME DMA Prep Err: sg_cnt %d "
  1234. "payload_length x%x\n",
  1235. nCmd->sg_cnt, nCmd->payload_length);
  1236. return 1;
  1237. }
  1238. }
  1239. return 0;
  1240. }
  1241. /**
  1242. * lpfc_nvme_fcp_io_submit - Issue an NVME-over-FCP IO
  1243. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  1244. * @lpfc_nvme_lport: Pointer to the driver's local port data
  1245. * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
  1246. * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
  1247. * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
  1248. *
  1249. * Driver registers this routine as it io request handler. This
  1250. * routine issues an fcp WQE with data from the @lpfc_nvme_fcpreq
  1251. * data structure to the rport
  1252. indicated in @lpfc_nvme_rport.
  1253. *
  1254. * Return value :
  1255. * 0 - Success
  1256. * TODO: What are the failure codes.
  1257. **/
  1258. static int
  1259. lpfc_nvme_fcp_io_submit(struct nvme_fc_local_port *pnvme_lport,
  1260. struct nvme_fc_remote_port *pnvme_rport,
  1261. void *hw_queue_handle,
  1262. struct nvmefc_fcp_req *pnvme_fcreq)
  1263. {
  1264. int ret = 0;
  1265. int expedite = 0;
  1266. int idx;
  1267. struct lpfc_nvme_lport *lport;
  1268. struct lpfc_nvme_ctrl_stat *cstat;
  1269. struct lpfc_vport *vport;
  1270. struct lpfc_hba *phba;
  1271. struct lpfc_nodelist *ndlp;
  1272. struct lpfc_nvme_buf *lpfc_ncmd;
  1273. struct lpfc_nvme_rport *rport;
  1274. struct lpfc_nvme_qhandle *lpfc_queue_info;
  1275. struct lpfc_nvme_fcpreq_priv *freqpriv;
  1276. struct nvme_common_command *sqe;
  1277. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1278. uint64_t start = 0;
  1279. #endif
  1280. /* Validate pointers. LLDD fault handling with transport does
  1281. * have timing races.
  1282. */
  1283. lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
  1284. if (unlikely(!lport)) {
  1285. ret = -EINVAL;
  1286. goto out_fail;
  1287. }
  1288. vport = lport->vport;
  1289. if (unlikely(!hw_queue_handle)) {
  1290. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  1291. "6117 Fail IO, NULL hw_queue_handle\n");
  1292. atomic_inc(&lport->xmt_fcp_err);
  1293. ret = -EBUSY;
  1294. goto out_fail;
  1295. }
  1296. phba = vport->phba;
  1297. if (vport->load_flag & FC_UNLOADING) {
  1298. ret = -ENODEV;
  1299. goto out_fail;
  1300. }
  1301. if (vport->load_flag & FC_UNLOADING) {
  1302. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  1303. "6124 Fail IO, Driver unload\n");
  1304. atomic_inc(&lport->xmt_fcp_err);
  1305. ret = -ENODEV;
  1306. goto out_fail;
  1307. }
  1308. freqpriv = pnvme_fcreq->private;
  1309. if (unlikely(!freqpriv)) {
  1310. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  1311. "6158 Fail IO, NULL request data\n");
  1312. atomic_inc(&lport->xmt_fcp_err);
  1313. ret = -EINVAL;
  1314. goto out_fail;
  1315. }
  1316. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1317. if (phba->ktime_on)
  1318. start = ktime_get_ns();
  1319. #endif
  1320. rport = (struct lpfc_nvme_rport *)pnvme_rport->private;
  1321. lpfc_queue_info = (struct lpfc_nvme_qhandle *)hw_queue_handle;
  1322. /*
  1323. * Catch race where our node has transitioned, but the
  1324. * transport is still transitioning.
  1325. */
  1326. ndlp = rport->ndlp;
  1327. if (!ndlp || !NLP_CHK_NODE_ACT(ndlp)) {
  1328. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
  1329. "6053 Fail IO, ndlp not ready: rport %p "
  1330. "ndlp %p, DID x%06x\n",
  1331. rport, ndlp, pnvme_rport->port_id);
  1332. atomic_inc(&lport->xmt_fcp_err);
  1333. ret = -EBUSY;
  1334. goto out_fail;
  1335. }
  1336. /* The remote node has to be a mapped target or it's an error. */
  1337. if ((ndlp->nlp_type & NLP_NVME_TARGET) &&
  1338. (ndlp->nlp_state != NLP_STE_MAPPED_NODE)) {
  1339. lpfc_printf_vlog(vport, KERN_INFO, LOG_NODE | LOG_NVME_IOERR,
  1340. "6036 Fail IO, DID x%06x not ready for "
  1341. "IO. State x%x, Type x%x Flg x%x\n",
  1342. pnvme_rport->port_id,
  1343. ndlp->nlp_state, ndlp->nlp_type,
  1344. ndlp->upcall_flags);
  1345. atomic_inc(&lport->xmt_fcp_bad_ndlp);
  1346. ret = -EBUSY;
  1347. goto out_fail;
  1348. }
  1349. /* Currently only NVME Keep alive commands should be expedited
  1350. * if the driver runs out of a resource. These should only be
  1351. * issued on the admin queue, qidx 0
  1352. */
  1353. if (!lpfc_queue_info->qidx && !pnvme_fcreq->sg_cnt) {
  1354. sqe = &((struct nvme_fc_cmd_iu *)
  1355. pnvme_fcreq->cmdaddr)->sqe.common;
  1356. if (sqe->opcode == nvme_admin_keep_alive)
  1357. expedite = 1;
  1358. }
  1359. /* The node is shared with FCP IO, make sure the IO pending count does
  1360. * not exceed the programmed depth.
  1361. */
  1362. if (lpfc_ndlp_check_qdepth(phba, ndlp)) {
  1363. if ((atomic_read(&ndlp->cmd_pending) >= ndlp->cmd_qdepth) &&
  1364. !expedite) {
  1365. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  1366. "6174 Fail IO, ndlp qdepth exceeded: "
  1367. "idx %d DID %x pend %d qdepth %d\n",
  1368. lpfc_queue_info->index, ndlp->nlp_DID,
  1369. atomic_read(&ndlp->cmd_pending),
  1370. ndlp->cmd_qdepth);
  1371. atomic_inc(&lport->xmt_fcp_qdepth);
  1372. ret = -EBUSY;
  1373. goto out_fail;
  1374. }
  1375. }
  1376. lpfc_ncmd = lpfc_get_nvme_buf(phba, ndlp, expedite);
  1377. if (lpfc_ncmd == NULL) {
  1378. atomic_inc(&lport->xmt_fcp_noxri);
  1379. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  1380. "6065 Fail IO, driver buffer pool is empty: "
  1381. "idx %d DID %x\n",
  1382. lpfc_queue_info->index, ndlp->nlp_DID);
  1383. ret = -EBUSY;
  1384. goto out_fail;
  1385. }
  1386. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1387. if (start) {
  1388. lpfc_ncmd->ts_cmd_start = start;
  1389. lpfc_ncmd->ts_last_cmd = phba->ktime_last_cmd;
  1390. } else {
  1391. lpfc_ncmd->ts_cmd_start = 0;
  1392. }
  1393. #endif
  1394. /*
  1395. * Store the data needed by the driver to issue, abort, and complete
  1396. * an IO.
  1397. * Do not let the IO hang out forever. There is no midlayer issuing
  1398. * an abort so inform the FW of the maximum IO pending time.
  1399. */
  1400. freqpriv->nvme_buf = lpfc_ncmd;
  1401. lpfc_ncmd->nvmeCmd = pnvme_fcreq;
  1402. lpfc_ncmd->nrport = rport;
  1403. lpfc_ncmd->ndlp = ndlp;
  1404. lpfc_ncmd->start_time = jiffies;
  1405. /*
  1406. * Issue the IO on the WQ indicated by index in the hw_queue_handle.
  1407. * This identfier was create in our hardware queue create callback
  1408. * routine. The driver now is dependent on the IO queue steering from
  1409. * the transport. We are trusting the upper NVME layers know which
  1410. * index to use and that they have affinitized a CPU to this hardware
  1411. * queue. A hardware queue maps to a driver MSI-X vector/EQ/CQ/WQ.
  1412. */
  1413. idx = lpfc_queue_info->index;
  1414. lpfc_ncmd->cur_iocbq.hba_wqidx = idx;
  1415. cstat = &lport->cstat[idx];
  1416. lpfc_nvme_prep_io_cmd(vport, lpfc_ncmd, ndlp, cstat);
  1417. ret = lpfc_nvme_prep_io_dma(vport, lpfc_ncmd);
  1418. if (ret) {
  1419. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  1420. "6175 Fail IO, Prep DMA: "
  1421. "idx %d DID %x\n",
  1422. lpfc_queue_info->index, ndlp->nlp_DID);
  1423. atomic_inc(&lport->xmt_fcp_err);
  1424. ret = -ENOMEM;
  1425. goto out_free_nvme_buf;
  1426. }
  1427. lpfc_nvmeio_data(phba, "NVME FCP XMIT: xri x%x idx %d to %06x\n",
  1428. lpfc_ncmd->cur_iocbq.sli4_xritag,
  1429. lpfc_queue_info->index, ndlp->nlp_DID);
  1430. ret = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, &lpfc_ncmd->cur_iocbq);
  1431. if (ret) {
  1432. atomic_inc(&lport->xmt_fcp_wqerr);
  1433. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  1434. "6113 Fail IO, Could not issue WQE err %x "
  1435. "sid: x%x did: x%x oxid: x%x\n",
  1436. ret, vport->fc_myDID, ndlp->nlp_DID,
  1437. lpfc_ncmd->cur_iocbq.sli4_xritag);
  1438. goto out_free_nvme_buf;
  1439. }
  1440. #ifdef CONFIG_SCSI_LPFC_DEBUG_FS
  1441. if (lpfc_ncmd->ts_cmd_start)
  1442. lpfc_ncmd->ts_cmd_wqput = ktime_get_ns();
  1443. if (phba->cpucheck_on & LPFC_CHECK_NVME_IO) {
  1444. lpfc_ncmd->cpu = smp_processor_id();
  1445. if (lpfc_ncmd->cpu != lpfc_queue_info->index) {
  1446. /* Check for admin queue */
  1447. if (lpfc_queue_info->qidx) {
  1448. lpfc_printf_vlog(vport,
  1449. KERN_ERR, LOG_NVME_IOERR,
  1450. "6702 CPU Check cmd: "
  1451. "cpu %d wq %d\n",
  1452. lpfc_ncmd->cpu,
  1453. lpfc_queue_info->index);
  1454. }
  1455. lpfc_ncmd->cpu = lpfc_queue_info->index;
  1456. }
  1457. if (lpfc_ncmd->cpu < LPFC_CHECK_CPU_CNT)
  1458. phba->cpucheck_xmt_io[lpfc_ncmd->cpu]++;
  1459. }
  1460. #endif
  1461. return 0;
  1462. out_free_nvme_buf:
  1463. if (lpfc_ncmd->nvmeCmd->sg_cnt) {
  1464. if (lpfc_ncmd->nvmeCmd->io_dir == NVMEFC_FCP_WRITE)
  1465. atomic_dec(&cstat->fc4NvmeOutputRequests);
  1466. else
  1467. atomic_dec(&cstat->fc4NvmeInputRequests);
  1468. } else
  1469. atomic_dec(&cstat->fc4NvmeControlRequests);
  1470. lpfc_release_nvme_buf(phba, lpfc_ncmd);
  1471. out_fail:
  1472. return ret;
  1473. }
  1474. /**
  1475. * lpfc_nvme_abort_fcreq_cmpl - Complete an NVME FCP abort request.
  1476. * @phba: Pointer to HBA context object
  1477. * @cmdiocb: Pointer to command iocb object.
  1478. * @rspiocb: Pointer to response iocb object.
  1479. *
  1480. * This is the callback function for any NVME FCP IO that was aborted.
  1481. *
  1482. * Return value:
  1483. * None
  1484. **/
  1485. void
  1486. lpfc_nvme_abort_fcreq_cmpl(struct lpfc_hba *phba, struct lpfc_iocbq *cmdiocb,
  1487. struct lpfc_wcqe_complete *abts_cmpl)
  1488. {
  1489. lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
  1490. "6145 ABORT_XRI_CN completing on rpi x%x "
  1491. "original iotag x%x, abort cmd iotag x%x "
  1492. "req_tag x%x, status x%x, hwstatus x%x\n",
  1493. cmdiocb->iocb.un.acxri.abortContextTag,
  1494. cmdiocb->iocb.un.acxri.abortIoTag,
  1495. cmdiocb->iotag,
  1496. bf_get(lpfc_wcqe_c_request_tag, abts_cmpl),
  1497. bf_get(lpfc_wcqe_c_status, abts_cmpl),
  1498. bf_get(lpfc_wcqe_c_hw_status, abts_cmpl));
  1499. lpfc_sli_release_iocbq(phba, cmdiocb);
  1500. }
  1501. /**
  1502. * lpfc_nvme_fcp_abort - Issue an NVME-over-FCP ABTS
  1503. * @lpfc_pnvme: Pointer to the driver's nvme instance data
  1504. * @lpfc_nvme_lport: Pointer to the driver's local port data
  1505. * @lpfc_nvme_rport: Pointer to the rport getting the @lpfc_nvme_ereq
  1506. * @lpfc_nvme_fcreq: IO request from nvme fc to driver.
  1507. * @hw_queue_handle: Driver-returned handle in lpfc_nvme_create_queue
  1508. *
  1509. * Driver registers this routine as its nvme request io abort handler. This
  1510. * routine issues an fcp Abort WQE with data from the @lpfc_nvme_fcpreq
  1511. * data structure to the rport indicated in @lpfc_nvme_rport. This routine
  1512. * is executed asynchronously - one the target is validated as "MAPPED" and
  1513. * ready for IO, the driver issues the abort request and returns.
  1514. *
  1515. * Return value:
  1516. * None
  1517. **/
  1518. static void
  1519. lpfc_nvme_fcp_abort(struct nvme_fc_local_port *pnvme_lport,
  1520. struct nvme_fc_remote_port *pnvme_rport,
  1521. void *hw_queue_handle,
  1522. struct nvmefc_fcp_req *pnvme_fcreq)
  1523. {
  1524. struct lpfc_nvme_lport *lport;
  1525. struct lpfc_vport *vport;
  1526. struct lpfc_hba *phba;
  1527. struct lpfc_nvme_buf *lpfc_nbuf;
  1528. struct lpfc_iocbq *abts_buf;
  1529. struct lpfc_iocbq *nvmereq_wqe;
  1530. struct lpfc_nvme_fcpreq_priv *freqpriv;
  1531. union lpfc_wqe128 *abts_wqe;
  1532. unsigned long flags;
  1533. int ret_val;
  1534. /* Validate pointers. LLDD fault handling with transport does
  1535. * have timing races.
  1536. */
  1537. lport = (struct lpfc_nvme_lport *)pnvme_lport->private;
  1538. if (unlikely(!lport))
  1539. return;
  1540. vport = lport->vport;
  1541. if (unlikely(!hw_queue_handle)) {
  1542. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
  1543. "6129 Fail Abort, HW Queue Handle NULL.\n");
  1544. return;
  1545. }
  1546. phba = vport->phba;
  1547. freqpriv = pnvme_fcreq->private;
  1548. if (unlikely(!freqpriv))
  1549. return;
  1550. if (vport->load_flag & FC_UNLOADING)
  1551. return;
  1552. /* Announce entry to new IO submit field. */
  1553. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
  1554. "6002 Abort Request to rport DID x%06x "
  1555. "for nvme_fc_req %p\n",
  1556. pnvme_rport->port_id,
  1557. pnvme_fcreq);
  1558. /* If the hba is getting reset, this flag is set. It is
  1559. * cleared when the reset is complete and rings reestablished.
  1560. */
  1561. spin_lock_irqsave(&phba->hbalock, flags);
  1562. /* driver queued commands are in process of being flushed */
  1563. if (phba->hba_flag & HBA_NVME_IOQ_FLUSH) {
  1564. spin_unlock_irqrestore(&phba->hbalock, flags);
  1565. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  1566. "6139 Driver in reset cleanup - flushing "
  1567. "NVME Req now. hba_flag x%x\n",
  1568. phba->hba_flag);
  1569. return;
  1570. }
  1571. lpfc_nbuf = freqpriv->nvme_buf;
  1572. if (!lpfc_nbuf) {
  1573. spin_unlock_irqrestore(&phba->hbalock, flags);
  1574. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  1575. "6140 NVME IO req has no matching lpfc nvme "
  1576. "io buffer. Skipping abort req.\n");
  1577. return;
  1578. } else if (!lpfc_nbuf->nvmeCmd) {
  1579. spin_unlock_irqrestore(&phba->hbalock, flags);
  1580. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  1581. "6141 lpfc NVME IO req has no nvme_fcreq "
  1582. "io buffer. Skipping abort req.\n");
  1583. return;
  1584. }
  1585. nvmereq_wqe = &lpfc_nbuf->cur_iocbq;
  1586. /*
  1587. * The lpfc_nbuf and the mapped nvme_fcreq in the driver's
  1588. * state must match the nvme_fcreq passed by the nvme
  1589. * transport. If they don't match, it is likely the driver
  1590. * has already completed the NVME IO and the nvme transport
  1591. * has not seen it yet.
  1592. */
  1593. if (lpfc_nbuf->nvmeCmd != pnvme_fcreq) {
  1594. spin_unlock_irqrestore(&phba->hbalock, flags);
  1595. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  1596. "6143 NVME req mismatch: "
  1597. "lpfc_nbuf %p nvmeCmd %p, "
  1598. "pnvme_fcreq %p. Skipping Abort xri x%x\n",
  1599. lpfc_nbuf, lpfc_nbuf->nvmeCmd,
  1600. pnvme_fcreq, nvmereq_wqe->sli4_xritag);
  1601. return;
  1602. }
  1603. /* Don't abort IOs no longer on the pending queue. */
  1604. if (!(nvmereq_wqe->iocb_flag & LPFC_IO_ON_TXCMPLQ)) {
  1605. spin_unlock_irqrestore(&phba->hbalock, flags);
  1606. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  1607. "6142 NVME IO req %p not queued - skipping "
  1608. "abort req xri x%x\n",
  1609. pnvme_fcreq, nvmereq_wqe->sli4_xritag);
  1610. return;
  1611. }
  1612. atomic_inc(&lport->xmt_fcp_abort);
  1613. lpfc_nvmeio_data(phba, "NVME FCP ABORT: xri x%x idx %d to %06x\n",
  1614. nvmereq_wqe->sli4_xritag,
  1615. nvmereq_wqe->hba_wqidx, pnvme_rport->port_id);
  1616. /* Outstanding abort is in progress */
  1617. if (nvmereq_wqe->iocb_flag & LPFC_DRIVER_ABORTED) {
  1618. spin_unlock_irqrestore(&phba->hbalock, flags);
  1619. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  1620. "6144 Outstanding NVME I/O Abort Request "
  1621. "still pending on nvme_fcreq %p, "
  1622. "lpfc_ncmd %p xri x%x\n",
  1623. pnvme_fcreq, lpfc_nbuf,
  1624. nvmereq_wqe->sli4_xritag);
  1625. return;
  1626. }
  1627. abts_buf = __lpfc_sli_get_iocbq(phba);
  1628. if (!abts_buf) {
  1629. spin_unlock_irqrestore(&phba->hbalock, flags);
  1630. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  1631. "6136 No available abort wqes. Skipping "
  1632. "Abts req for nvme_fcreq %p xri x%x\n",
  1633. pnvme_fcreq, nvmereq_wqe->sli4_xritag);
  1634. return;
  1635. }
  1636. /* Ready - mark outstanding as aborted by driver. */
  1637. nvmereq_wqe->iocb_flag |= LPFC_DRIVER_ABORTED;
  1638. /* Complete prepping the abort wqe and issue to the FW. */
  1639. abts_wqe = &abts_buf->wqe;
  1640. /* WQEs are reused. Clear stale data and set key fields to
  1641. * zero like ia, iaab, iaar, xri_tag, and ctxt_tag.
  1642. */
  1643. memset(abts_wqe, 0, sizeof(union lpfc_wqe));
  1644. bf_set(abort_cmd_criteria, &abts_wqe->abort_cmd, T_XRI_TAG);
  1645. /* word 7 */
  1646. bf_set(wqe_cmnd, &abts_wqe->abort_cmd.wqe_com, CMD_ABORT_XRI_CX);
  1647. bf_set(wqe_class, &abts_wqe->abort_cmd.wqe_com,
  1648. nvmereq_wqe->iocb.ulpClass);
  1649. /* word 8 - tell the FW to abort the IO associated with this
  1650. * outstanding exchange ID.
  1651. */
  1652. abts_wqe->abort_cmd.wqe_com.abort_tag = nvmereq_wqe->sli4_xritag;
  1653. /* word 9 - this is the iotag for the abts_wqe completion. */
  1654. bf_set(wqe_reqtag, &abts_wqe->abort_cmd.wqe_com,
  1655. abts_buf->iotag);
  1656. /* word 10 */
  1657. bf_set(wqe_qosd, &abts_wqe->abort_cmd.wqe_com, 1);
  1658. bf_set(wqe_lenloc, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_LENLOC_NONE);
  1659. /* word 11 */
  1660. bf_set(wqe_cmd_type, &abts_wqe->abort_cmd.wqe_com, OTHER_COMMAND);
  1661. bf_set(wqe_wqec, &abts_wqe->abort_cmd.wqe_com, 1);
  1662. bf_set(wqe_cqid, &abts_wqe->abort_cmd.wqe_com, LPFC_WQE_CQ_ID_DEFAULT);
  1663. /* ABTS WQE must go to the same WQ as the WQE to be aborted */
  1664. abts_buf->iocb_flag |= LPFC_IO_NVME;
  1665. abts_buf->hba_wqidx = nvmereq_wqe->hba_wqidx;
  1666. abts_buf->vport = vport;
  1667. abts_buf->wqe_cmpl = lpfc_nvme_abort_fcreq_cmpl;
  1668. ret_val = lpfc_sli4_issue_wqe(phba, LPFC_FCP_RING, abts_buf);
  1669. spin_unlock_irqrestore(&phba->hbalock, flags);
  1670. if (ret_val) {
  1671. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_ABTS,
  1672. "6137 Failed abts issue_wqe with status x%x "
  1673. "for nvme_fcreq %p.\n",
  1674. ret_val, pnvme_fcreq);
  1675. lpfc_sli_release_iocbq(phba, abts_buf);
  1676. return;
  1677. }
  1678. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_ABTS,
  1679. "6138 Transport Abort NVME Request Issued for "
  1680. "ox_id x%x on reqtag x%x\n",
  1681. nvmereq_wqe->sli4_xritag,
  1682. abts_buf->iotag);
  1683. }
  1684. /* Declare and initialization an instance of the FC NVME template. */
  1685. static struct nvme_fc_port_template lpfc_nvme_template = {
  1686. /* initiator-based functions */
  1687. .localport_delete = lpfc_nvme_localport_delete,
  1688. .remoteport_delete = lpfc_nvme_remoteport_delete,
  1689. .create_queue = lpfc_nvme_create_queue,
  1690. .delete_queue = lpfc_nvme_delete_queue,
  1691. .ls_req = lpfc_nvme_ls_req,
  1692. .fcp_io = lpfc_nvme_fcp_io_submit,
  1693. .ls_abort = lpfc_nvme_ls_abort,
  1694. .fcp_abort = lpfc_nvme_fcp_abort,
  1695. .max_hw_queues = 1,
  1696. .max_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
  1697. .max_dif_sgl_segments = LPFC_NVME_DEFAULT_SEGS,
  1698. .dma_boundary = 0xFFFFFFFF,
  1699. /* Sizes of additional private data for data structures.
  1700. * No use for the last two sizes at this time.
  1701. */
  1702. .local_priv_sz = sizeof(struct lpfc_nvme_lport),
  1703. .remote_priv_sz = sizeof(struct lpfc_nvme_rport),
  1704. .lsrqst_priv_sz = 0,
  1705. .fcprqst_priv_sz = sizeof(struct lpfc_nvme_fcpreq_priv),
  1706. };
  1707. /**
  1708. * lpfc_sli4_post_nvme_sgl_block - post a block of nvme sgl list to firmware
  1709. * @phba: pointer to lpfc hba data structure.
  1710. * @nblist: pointer to nvme buffer list.
  1711. * @count: number of scsi buffers on the list.
  1712. *
  1713. * This routine is invoked to post a block of @count scsi sgl pages from a
  1714. * SCSI buffer list @nblist to the HBA using non-embedded mailbox command.
  1715. * No Lock is held.
  1716. *
  1717. **/
  1718. static int
  1719. lpfc_sli4_post_nvme_sgl_block(struct lpfc_hba *phba,
  1720. struct list_head *nblist,
  1721. int count)
  1722. {
  1723. struct lpfc_nvme_buf *lpfc_ncmd;
  1724. struct lpfc_mbx_post_uembed_sgl_page1 *sgl;
  1725. struct sgl_page_pairs *sgl_pg_pairs;
  1726. void *viraddr;
  1727. LPFC_MBOXQ_t *mbox;
  1728. uint32_t reqlen, alloclen, pg_pairs;
  1729. uint32_t mbox_tmo;
  1730. uint16_t xritag_start = 0;
  1731. int rc = 0;
  1732. uint32_t shdr_status, shdr_add_status;
  1733. dma_addr_t pdma_phys_bpl1;
  1734. union lpfc_sli4_cfg_shdr *shdr;
  1735. /* Calculate the requested length of the dma memory */
  1736. reqlen = count * sizeof(struct sgl_page_pairs) +
  1737. sizeof(union lpfc_sli4_cfg_shdr) + sizeof(uint32_t);
  1738. if (reqlen > SLI4_PAGE_SIZE) {
  1739. lpfc_printf_log(phba, KERN_WARNING, LOG_INIT,
  1740. "6118 Block sgl registration required DMA "
  1741. "size (%d) great than a page\n", reqlen);
  1742. return -ENOMEM;
  1743. }
  1744. mbox = mempool_alloc(phba->mbox_mem_pool, GFP_KERNEL);
  1745. if (!mbox) {
  1746. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1747. "6119 Failed to allocate mbox cmd memory\n");
  1748. return -ENOMEM;
  1749. }
  1750. /* Allocate DMA memory and set up the non-embedded mailbox command */
  1751. alloclen = lpfc_sli4_config(phba, mbox, LPFC_MBOX_SUBSYSTEM_FCOE,
  1752. LPFC_MBOX_OPCODE_FCOE_POST_SGL_PAGES, reqlen,
  1753. LPFC_SLI4_MBX_NEMBED);
  1754. if (alloclen < reqlen) {
  1755. lpfc_printf_log(phba, KERN_ERR, LOG_INIT,
  1756. "6120 Allocated DMA memory size (%d) is "
  1757. "less than the requested DMA memory "
  1758. "size (%d)\n", alloclen, reqlen);
  1759. lpfc_sli4_mbox_cmd_free(phba, mbox);
  1760. return -ENOMEM;
  1761. }
  1762. /* Get the first SGE entry from the non-embedded DMA memory */
  1763. viraddr = mbox->sge_array->addr[0];
  1764. /* Set up the SGL pages in the non-embedded DMA pages */
  1765. sgl = (struct lpfc_mbx_post_uembed_sgl_page1 *)viraddr;
  1766. sgl_pg_pairs = &sgl->sgl_pg_pairs;
  1767. pg_pairs = 0;
  1768. list_for_each_entry(lpfc_ncmd, nblist, list) {
  1769. /* Set up the sge entry */
  1770. sgl_pg_pairs->sgl_pg0_addr_lo =
  1771. cpu_to_le32(putPaddrLow(lpfc_ncmd->dma_phys_sgl));
  1772. sgl_pg_pairs->sgl_pg0_addr_hi =
  1773. cpu_to_le32(putPaddrHigh(lpfc_ncmd->dma_phys_sgl));
  1774. if (phba->cfg_sg_dma_buf_size > SGL_PAGE_SIZE)
  1775. pdma_phys_bpl1 = lpfc_ncmd->dma_phys_sgl +
  1776. SGL_PAGE_SIZE;
  1777. else
  1778. pdma_phys_bpl1 = 0;
  1779. sgl_pg_pairs->sgl_pg1_addr_lo =
  1780. cpu_to_le32(putPaddrLow(pdma_phys_bpl1));
  1781. sgl_pg_pairs->sgl_pg1_addr_hi =
  1782. cpu_to_le32(putPaddrHigh(pdma_phys_bpl1));
  1783. /* Keep the first xritag on the list */
  1784. if (pg_pairs == 0)
  1785. xritag_start = lpfc_ncmd->cur_iocbq.sli4_xritag;
  1786. sgl_pg_pairs++;
  1787. pg_pairs++;
  1788. }
  1789. bf_set(lpfc_post_sgl_pages_xri, sgl, xritag_start);
  1790. bf_set(lpfc_post_sgl_pages_xricnt, sgl, pg_pairs);
  1791. /* Perform endian conversion if necessary */
  1792. sgl->word0 = cpu_to_le32(sgl->word0);
  1793. if (!phba->sli4_hba.intr_enable)
  1794. rc = lpfc_sli_issue_mbox(phba, mbox, MBX_POLL);
  1795. else {
  1796. mbox_tmo = lpfc_mbox_tmo_val(phba, mbox);
  1797. rc = lpfc_sli_issue_mbox_wait(phba, mbox, mbox_tmo);
  1798. }
  1799. shdr = (union lpfc_sli4_cfg_shdr *)&sgl->cfg_shdr;
  1800. shdr_status = bf_get(lpfc_mbox_hdr_status, &shdr->response);
  1801. shdr_add_status = bf_get(lpfc_mbox_hdr_add_status, &shdr->response);
  1802. if (rc != MBX_TIMEOUT)
  1803. lpfc_sli4_mbox_cmd_free(phba, mbox);
  1804. if (shdr_status || shdr_add_status || rc) {
  1805. lpfc_printf_log(phba, KERN_ERR, LOG_SLI,
  1806. "6125 POST_SGL_BLOCK mailbox command failed "
  1807. "status x%x add_status x%x mbx status x%x\n",
  1808. shdr_status, shdr_add_status, rc);
  1809. rc = -ENXIO;
  1810. }
  1811. return rc;
  1812. }
  1813. /**
  1814. * lpfc_post_nvme_sgl_list - Post blocks of nvme buffer sgls from a list
  1815. * @phba: pointer to lpfc hba data structure.
  1816. * @post_nblist: pointer to the nvme buffer list.
  1817. *
  1818. * This routine walks a list of nvme buffers that was passed in. It attempts
  1819. * to construct blocks of nvme buffer sgls which contains contiguous xris and
  1820. * uses the non-embedded SGL block post mailbox commands to post to the port.
  1821. * For single NVME buffer sgl with non-contiguous xri, if any, it shall use
  1822. * embedded SGL post mailbox command for posting. The @post_nblist passed in
  1823. * must be local list, thus no lock is needed when manipulate the list.
  1824. *
  1825. * Returns: 0 = failure, non-zero number of successfully posted buffers.
  1826. **/
  1827. static int
  1828. lpfc_post_nvme_sgl_list(struct lpfc_hba *phba,
  1829. struct list_head *post_nblist, int sb_count)
  1830. {
  1831. struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
  1832. int status, sgl_size;
  1833. int post_cnt = 0, block_cnt = 0, num_posting = 0, num_posted = 0;
  1834. dma_addr_t pdma_phys_sgl1;
  1835. int last_xritag = NO_XRI;
  1836. int cur_xritag;
  1837. LIST_HEAD(prep_nblist);
  1838. LIST_HEAD(blck_nblist);
  1839. LIST_HEAD(nvme_nblist);
  1840. /* sanity check */
  1841. if (sb_count <= 0)
  1842. return -EINVAL;
  1843. sgl_size = phba->cfg_sg_dma_buf_size;
  1844. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next, post_nblist, list) {
  1845. list_del_init(&lpfc_ncmd->list);
  1846. block_cnt++;
  1847. if ((last_xritag != NO_XRI) &&
  1848. (lpfc_ncmd->cur_iocbq.sli4_xritag != last_xritag + 1)) {
  1849. /* a hole in xri block, form a sgl posting block */
  1850. list_splice_init(&prep_nblist, &blck_nblist);
  1851. post_cnt = block_cnt - 1;
  1852. /* prepare list for next posting block */
  1853. list_add_tail(&lpfc_ncmd->list, &prep_nblist);
  1854. block_cnt = 1;
  1855. } else {
  1856. /* prepare list for next posting block */
  1857. list_add_tail(&lpfc_ncmd->list, &prep_nblist);
  1858. /* enough sgls for non-embed sgl mbox command */
  1859. if (block_cnt == LPFC_NEMBED_MBOX_SGL_CNT) {
  1860. list_splice_init(&prep_nblist, &blck_nblist);
  1861. post_cnt = block_cnt;
  1862. block_cnt = 0;
  1863. }
  1864. }
  1865. num_posting++;
  1866. last_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
  1867. /* end of repost sgl list condition for NVME buffers */
  1868. if (num_posting == sb_count) {
  1869. if (post_cnt == 0) {
  1870. /* last sgl posting block */
  1871. list_splice_init(&prep_nblist, &blck_nblist);
  1872. post_cnt = block_cnt;
  1873. } else if (block_cnt == 1) {
  1874. /* last single sgl with non-contiguous xri */
  1875. if (sgl_size > SGL_PAGE_SIZE)
  1876. pdma_phys_sgl1 =
  1877. lpfc_ncmd->dma_phys_sgl +
  1878. SGL_PAGE_SIZE;
  1879. else
  1880. pdma_phys_sgl1 = 0;
  1881. cur_xritag = lpfc_ncmd->cur_iocbq.sli4_xritag;
  1882. status = lpfc_sli4_post_sgl(phba,
  1883. lpfc_ncmd->dma_phys_sgl,
  1884. pdma_phys_sgl1, cur_xritag);
  1885. if (status) {
  1886. /* failure, put on abort nvme list */
  1887. lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
  1888. } else {
  1889. /* success, put on NVME buffer list */
  1890. lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
  1891. lpfc_ncmd->status = IOSTAT_SUCCESS;
  1892. num_posted++;
  1893. }
  1894. /* success, put on NVME buffer sgl list */
  1895. list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
  1896. }
  1897. }
  1898. /* continue until a nembed page worth of sgls */
  1899. if (post_cnt == 0)
  1900. continue;
  1901. /* post block of NVME buffer list sgls */
  1902. status = lpfc_sli4_post_nvme_sgl_block(phba, &blck_nblist,
  1903. post_cnt);
  1904. /* don't reset xirtag due to hole in xri block */
  1905. if (block_cnt == 0)
  1906. last_xritag = NO_XRI;
  1907. /* reset NVME buffer post count for next round of posting */
  1908. post_cnt = 0;
  1909. /* put posted NVME buffer-sgl posted on NVME buffer sgl list */
  1910. while (!list_empty(&blck_nblist)) {
  1911. list_remove_head(&blck_nblist, lpfc_ncmd,
  1912. struct lpfc_nvme_buf, list);
  1913. if (status) {
  1914. /* failure, put on abort nvme list */
  1915. lpfc_ncmd->flags |= LPFC_SBUF_XBUSY;
  1916. } else {
  1917. /* success, put on NVME buffer list */
  1918. lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
  1919. lpfc_ncmd->status = IOSTAT_SUCCESS;
  1920. num_posted++;
  1921. }
  1922. list_add_tail(&lpfc_ncmd->list, &nvme_nblist);
  1923. }
  1924. }
  1925. /* Push NVME buffers with sgl posted to the available list */
  1926. while (!list_empty(&nvme_nblist)) {
  1927. list_remove_head(&nvme_nblist, lpfc_ncmd,
  1928. struct lpfc_nvme_buf, list);
  1929. lpfc_release_nvme_buf(phba, lpfc_ncmd);
  1930. }
  1931. return num_posted;
  1932. }
  1933. /**
  1934. * lpfc_repost_nvme_sgl_list - Repost all the allocated nvme buffer sgls
  1935. * @phba: pointer to lpfc hba data structure.
  1936. *
  1937. * This routine walks the list of nvme buffers that have been allocated and
  1938. * repost them to the port by using SGL block post. This is needed after a
  1939. * pci_function_reset/warm_start or start. The lpfc_hba_down_post_s4 routine
  1940. * is responsible for moving all nvme buffers on the lpfc_abts_nvme_sgl_list
  1941. * to the lpfc_nvme_buf_list. If the repost fails, reject all nvme buffers.
  1942. *
  1943. * Returns: 0 = success, non-zero failure.
  1944. **/
  1945. int
  1946. lpfc_repost_nvme_sgl_list(struct lpfc_hba *phba)
  1947. {
  1948. LIST_HEAD(post_nblist);
  1949. int num_posted, rc = 0;
  1950. /* get all NVME buffers need to repost to a local list */
  1951. spin_lock_irq(&phba->nvme_buf_list_get_lock);
  1952. spin_lock(&phba->nvme_buf_list_put_lock);
  1953. list_splice_init(&phba->lpfc_nvme_buf_list_get, &post_nblist);
  1954. list_splice(&phba->lpfc_nvme_buf_list_put, &post_nblist);
  1955. phba->get_nvme_bufs = 0;
  1956. phba->put_nvme_bufs = 0;
  1957. spin_unlock(&phba->nvme_buf_list_put_lock);
  1958. spin_unlock_irq(&phba->nvme_buf_list_get_lock);
  1959. /* post the list of nvme buffer sgls to port if available */
  1960. if (!list_empty(&post_nblist)) {
  1961. num_posted = lpfc_post_nvme_sgl_list(phba, &post_nblist,
  1962. phba->sli4_hba.nvme_xri_cnt);
  1963. /* failed to post any nvme buffer, return error */
  1964. if (num_posted == 0)
  1965. rc = -EIO;
  1966. }
  1967. return rc;
  1968. }
  1969. /**
  1970. * lpfc_new_nvme_buf - Scsi buffer allocator for HBA with SLI4 IF spec
  1971. * @vport: The virtual port for which this call being executed.
  1972. * @num_to_allocate: The requested number of buffers to allocate.
  1973. *
  1974. * This routine allocates nvme buffers for device with SLI-4 interface spec,
  1975. * the nvme buffer contains all the necessary information needed to initiate
  1976. * a NVME I/O. After allocating up to @num_to_allocate NVME buffers and put
  1977. * them on a list, it post them to the port by using SGL block post.
  1978. *
  1979. * Return codes:
  1980. * int - number of nvme buffers that were allocated and posted.
  1981. * 0 = failure, less than num_to_alloc is a partial failure.
  1982. **/
  1983. static int
  1984. lpfc_new_nvme_buf(struct lpfc_vport *vport, int num_to_alloc)
  1985. {
  1986. struct lpfc_hba *phba = vport->phba;
  1987. struct lpfc_nvme_buf *lpfc_ncmd;
  1988. struct lpfc_iocbq *pwqeq;
  1989. union lpfc_wqe128 *wqe;
  1990. struct sli4_sge *sgl;
  1991. dma_addr_t pdma_phys_sgl;
  1992. uint16_t iotag, lxri = 0;
  1993. int bcnt, num_posted, sgl_size;
  1994. LIST_HEAD(prep_nblist);
  1995. LIST_HEAD(post_nblist);
  1996. LIST_HEAD(nvme_nblist);
  1997. sgl_size = phba->cfg_sg_dma_buf_size;
  1998. for (bcnt = 0; bcnt < num_to_alloc; bcnt++) {
  1999. lpfc_ncmd = kzalloc(sizeof(struct lpfc_nvme_buf), GFP_KERNEL);
  2000. if (!lpfc_ncmd)
  2001. break;
  2002. /*
  2003. * Get memory from the pci pool to map the virt space to
  2004. * pci bus space for an I/O. The DMA buffer includes the
  2005. * number of SGE's necessary to support the sg_tablesize.
  2006. */
  2007. lpfc_ncmd->data = dma_pool_zalloc(phba->lpfc_sg_dma_buf_pool,
  2008. GFP_KERNEL,
  2009. &lpfc_ncmd->dma_handle);
  2010. if (!lpfc_ncmd->data) {
  2011. kfree(lpfc_ncmd);
  2012. break;
  2013. }
  2014. lxri = lpfc_sli4_next_xritag(phba);
  2015. if (lxri == NO_XRI) {
  2016. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  2017. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  2018. kfree(lpfc_ncmd);
  2019. break;
  2020. }
  2021. pwqeq = &(lpfc_ncmd->cur_iocbq);
  2022. wqe = &pwqeq->wqe;
  2023. /* Allocate iotag for lpfc_ncmd->cur_iocbq. */
  2024. iotag = lpfc_sli_next_iotag(phba, pwqeq);
  2025. if (iotag == 0) {
  2026. dma_pool_free(phba->lpfc_sg_dma_buf_pool,
  2027. lpfc_ncmd->data, lpfc_ncmd->dma_handle);
  2028. kfree(lpfc_ncmd);
  2029. lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
  2030. "6121 Failed to allocated IOTAG for"
  2031. " XRI:0x%x\n", lxri);
  2032. lpfc_sli4_free_xri(phba, lxri);
  2033. break;
  2034. }
  2035. pwqeq->sli4_lxritag = lxri;
  2036. pwqeq->sli4_xritag = phba->sli4_hba.xri_ids[lxri];
  2037. pwqeq->iocb_flag |= LPFC_IO_NVME;
  2038. pwqeq->context1 = lpfc_ncmd;
  2039. pwqeq->wqe_cmpl = lpfc_nvme_io_cmd_wqe_cmpl;
  2040. /* Initialize local short-hand pointers. */
  2041. lpfc_ncmd->nvme_sgl = lpfc_ncmd->data;
  2042. sgl = lpfc_ncmd->nvme_sgl;
  2043. pdma_phys_sgl = lpfc_ncmd->dma_handle;
  2044. lpfc_ncmd->dma_phys_sgl = pdma_phys_sgl;
  2045. /* Rsp SGE will be filled in when we rcv an IO
  2046. * from the NVME Layer to be sent.
  2047. * The cmd is going to be embedded so we need a SKIP SGE.
  2048. */
  2049. bf_set(lpfc_sli4_sge_type, sgl, LPFC_SGE_TYPE_SKIP);
  2050. bf_set(lpfc_sli4_sge_last, sgl, 0);
  2051. sgl->word2 = cpu_to_le32(sgl->word2);
  2052. /* Fill in word 3 / sgl_len during cmd submission */
  2053. lpfc_ncmd->cur_iocbq.context1 = lpfc_ncmd;
  2054. /* Initialize WQE */
  2055. memset(wqe, 0, sizeof(union lpfc_wqe));
  2056. /* add the nvme buffer to a post list */
  2057. list_add_tail(&lpfc_ncmd->list, &post_nblist);
  2058. spin_lock_irq(&phba->nvme_buf_list_get_lock);
  2059. phba->sli4_hba.nvme_xri_cnt++;
  2060. spin_unlock_irq(&phba->nvme_buf_list_get_lock);
  2061. }
  2062. lpfc_printf_log(phba, KERN_INFO, LOG_NVME,
  2063. "6114 Allocate %d out of %d requested new NVME "
  2064. "buffers\n", bcnt, num_to_alloc);
  2065. /* post the list of nvme buffer sgls to port if available */
  2066. if (!list_empty(&post_nblist))
  2067. num_posted = lpfc_post_nvme_sgl_list(phba,
  2068. &post_nblist, bcnt);
  2069. else
  2070. num_posted = 0;
  2071. return num_posted;
  2072. }
  2073. static inline struct lpfc_nvme_buf *
  2074. lpfc_nvme_buf(struct lpfc_hba *phba)
  2075. {
  2076. struct lpfc_nvme_buf *lpfc_ncmd, *lpfc_ncmd_next;
  2077. list_for_each_entry_safe(lpfc_ncmd, lpfc_ncmd_next,
  2078. &phba->lpfc_nvme_buf_list_get, list) {
  2079. list_del_init(&lpfc_ncmd->list);
  2080. phba->get_nvme_bufs--;
  2081. return lpfc_ncmd;
  2082. }
  2083. return NULL;
  2084. }
  2085. /**
  2086. * lpfc_get_nvme_buf - Get a nvme buffer from lpfc_nvme_buf_list of the HBA
  2087. * @phba: The HBA for which this call is being executed.
  2088. *
  2089. * This routine removes a nvme buffer from head of @phba lpfc_nvme_buf_list list
  2090. * and returns to caller.
  2091. *
  2092. * Return codes:
  2093. * NULL - Error
  2094. * Pointer to lpfc_nvme_buf - Success
  2095. **/
  2096. static struct lpfc_nvme_buf *
  2097. lpfc_get_nvme_buf(struct lpfc_hba *phba, struct lpfc_nodelist *ndlp,
  2098. int expedite)
  2099. {
  2100. struct lpfc_nvme_buf *lpfc_ncmd = NULL;
  2101. unsigned long iflag = 0;
  2102. spin_lock_irqsave(&phba->nvme_buf_list_get_lock, iflag);
  2103. if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
  2104. lpfc_ncmd = lpfc_nvme_buf(phba);
  2105. if (!lpfc_ncmd) {
  2106. spin_lock(&phba->nvme_buf_list_put_lock);
  2107. list_splice(&phba->lpfc_nvme_buf_list_put,
  2108. &phba->lpfc_nvme_buf_list_get);
  2109. phba->get_nvme_bufs += phba->put_nvme_bufs;
  2110. INIT_LIST_HEAD(&phba->lpfc_nvme_buf_list_put);
  2111. phba->put_nvme_bufs = 0;
  2112. spin_unlock(&phba->nvme_buf_list_put_lock);
  2113. if (phba->get_nvme_bufs > LPFC_NVME_EXPEDITE_XRICNT || expedite)
  2114. lpfc_ncmd = lpfc_nvme_buf(phba);
  2115. }
  2116. spin_unlock_irqrestore(&phba->nvme_buf_list_get_lock, iflag);
  2117. if (lpfc_ndlp_check_qdepth(phba, ndlp) && lpfc_ncmd) {
  2118. atomic_inc(&ndlp->cmd_pending);
  2119. lpfc_ncmd->flags |= LPFC_BUMP_QDEPTH;
  2120. }
  2121. return lpfc_ncmd;
  2122. }
  2123. /**
  2124. * lpfc_release_nvme_buf: Return a nvme buffer back to hba nvme buf list.
  2125. * @phba: The Hba for which this call is being executed.
  2126. * @lpfc_ncmd: The nvme buffer which is being released.
  2127. *
  2128. * This routine releases @lpfc_ncmd nvme buffer by adding it to tail of @phba
  2129. * lpfc_nvme_buf_list list. For SLI4 XRI's are tied to the nvme buffer
  2130. * and cannot be reused for at least RA_TOV amount of time if it was
  2131. * aborted.
  2132. **/
  2133. static void
  2134. lpfc_release_nvme_buf(struct lpfc_hba *phba, struct lpfc_nvme_buf *lpfc_ncmd)
  2135. {
  2136. unsigned long iflag = 0;
  2137. if ((lpfc_ncmd->flags & LPFC_BUMP_QDEPTH) && lpfc_ncmd->ndlp)
  2138. atomic_dec(&lpfc_ncmd->ndlp->cmd_pending);
  2139. lpfc_ncmd->nonsg_phys = 0;
  2140. lpfc_ncmd->ndlp = NULL;
  2141. lpfc_ncmd->flags &= ~LPFC_BUMP_QDEPTH;
  2142. if (lpfc_ncmd->flags & LPFC_SBUF_XBUSY) {
  2143. lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
  2144. "6310 XB release deferred for "
  2145. "ox_id x%x on reqtag x%x\n",
  2146. lpfc_ncmd->cur_iocbq.sli4_xritag,
  2147. lpfc_ncmd->cur_iocbq.iotag);
  2148. spin_lock_irqsave(&phba->sli4_hba.abts_nvme_buf_list_lock,
  2149. iflag);
  2150. list_add_tail(&lpfc_ncmd->list,
  2151. &phba->sli4_hba.lpfc_abts_nvme_buf_list);
  2152. spin_unlock_irqrestore(&phba->sli4_hba.abts_nvme_buf_list_lock,
  2153. iflag);
  2154. } else {
  2155. lpfc_ncmd->nvmeCmd = NULL;
  2156. lpfc_ncmd->cur_iocbq.iocb_flag = LPFC_IO_NVME;
  2157. spin_lock_irqsave(&phba->nvme_buf_list_put_lock, iflag);
  2158. list_add_tail(&lpfc_ncmd->list, &phba->lpfc_nvme_buf_list_put);
  2159. phba->put_nvme_bufs++;
  2160. spin_unlock_irqrestore(&phba->nvme_buf_list_put_lock, iflag);
  2161. }
  2162. }
  2163. /**
  2164. * lpfc_nvme_create_localport - Create/Bind an nvme localport instance.
  2165. * @pvport - the lpfc_vport instance requesting a localport.
  2166. *
  2167. * This routine is invoked to create an nvme localport instance to bind
  2168. * to the nvme_fc_transport. It is called once during driver load
  2169. * like lpfc_create_shost after all other services are initialized.
  2170. * It requires a vport, vpi, and wwns at call time. Other localport
  2171. * parameters are modified as the driver's FCID and the Fabric WWN
  2172. * are established.
  2173. *
  2174. * Return codes
  2175. * 0 - successful
  2176. * -ENOMEM - no heap memory available
  2177. * other values - from nvme registration upcall
  2178. **/
  2179. int
  2180. lpfc_nvme_create_localport(struct lpfc_vport *vport)
  2181. {
  2182. int ret = 0;
  2183. struct lpfc_hba *phba = vport->phba;
  2184. struct nvme_fc_port_info nfcp_info;
  2185. struct nvme_fc_local_port *localport;
  2186. struct lpfc_nvme_lport *lport;
  2187. struct lpfc_nvme_ctrl_stat *cstat;
  2188. int len, i;
  2189. /* Initialize this localport instance. The vport wwn usage ensures
  2190. * that NPIV is accounted for.
  2191. */
  2192. memset(&nfcp_info, 0, sizeof(struct nvme_fc_port_info));
  2193. nfcp_info.port_role = FC_PORT_ROLE_NVME_INITIATOR;
  2194. nfcp_info.node_name = wwn_to_u64(vport->fc_nodename.u.wwn);
  2195. nfcp_info.port_name = wwn_to_u64(vport->fc_portname.u.wwn);
  2196. /* Limit to LPFC_MAX_NVME_SEG_CNT.
  2197. * For now need + 1 to get around NVME transport logic.
  2198. */
  2199. if (phba->cfg_sg_seg_cnt > LPFC_MAX_NVME_SEG_CNT) {
  2200. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_INIT,
  2201. "6300 Reducing sg segment cnt to %d\n",
  2202. LPFC_MAX_NVME_SEG_CNT);
  2203. phba->cfg_nvme_seg_cnt = LPFC_MAX_NVME_SEG_CNT;
  2204. } else {
  2205. phba->cfg_nvme_seg_cnt = phba->cfg_sg_seg_cnt;
  2206. }
  2207. lpfc_nvme_template.max_sgl_segments = phba->cfg_nvme_seg_cnt + 1;
  2208. lpfc_nvme_template.max_hw_queues = phba->cfg_nvme_io_channel;
  2209. if (!IS_ENABLED(CONFIG_NVME_FC))
  2210. return ret;
  2211. cstat = kmalloc((sizeof(struct lpfc_nvme_ctrl_stat) *
  2212. phba->cfg_nvme_io_channel), GFP_KERNEL);
  2213. if (!cstat)
  2214. return -ENOMEM;
  2215. /* localport is allocated from the stack, but the registration
  2216. * call allocates heap memory as well as the private area.
  2217. */
  2218. ret = nvme_fc_register_localport(&nfcp_info, &lpfc_nvme_template,
  2219. &vport->phba->pcidev->dev, &localport);
  2220. if (!ret) {
  2221. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME | LOG_NVME_DISC,
  2222. "6005 Successfully registered local "
  2223. "NVME port num %d, localP %p, private %p, "
  2224. "sg_seg %d\n",
  2225. localport->port_num, localport,
  2226. localport->private,
  2227. lpfc_nvme_template.max_sgl_segments);
  2228. /* Private is our lport size declared in the template. */
  2229. lport = (struct lpfc_nvme_lport *)localport->private;
  2230. vport->localport = localport;
  2231. lport->vport = vport;
  2232. lport->cstat = cstat;
  2233. vport->nvmei_support = 1;
  2234. atomic_set(&lport->xmt_fcp_noxri, 0);
  2235. atomic_set(&lport->xmt_fcp_bad_ndlp, 0);
  2236. atomic_set(&lport->xmt_fcp_qdepth, 0);
  2237. atomic_set(&lport->xmt_fcp_err, 0);
  2238. atomic_set(&lport->xmt_fcp_wqerr, 0);
  2239. atomic_set(&lport->xmt_fcp_abort, 0);
  2240. atomic_set(&lport->xmt_ls_abort, 0);
  2241. atomic_set(&lport->xmt_ls_err, 0);
  2242. atomic_set(&lport->cmpl_fcp_xb, 0);
  2243. atomic_set(&lport->cmpl_fcp_err, 0);
  2244. atomic_set(&lport->cmpl_ls_xb, 0);
  2245. atomic_set(&lport->cmpl_ls_err, 0);
  2246. atomic_set(&lport->fc4NvmeLsRequests, 0);
  2247. atomic_set(&lport->fc4NvmeLsCmpls, 0);
  2248. for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
  2249. cstat = &lport->cstat[i];
  2250. atomic_set(&cstat->fc4NvmeInputRequests, 0);
  2251. atomic_set(&cstat->fc4NvmeOutputRequests, 0);
  2252. atomic_set(&cstat->fc4NvmeControlRequests, 0);
  2253. atomic_set(&cstat->fc4NvmeIoCmpls, 0);
  2254. }
  2255. /* Don't post more new bufs if repost already recovered
  2256. * the nvme sgls.
  2257. */
  2258. if (phba->sli4_hba.nvme_xri_cnt == 0) {
  2259. len = lpfc_new_nvme_buf(vport,
  2260. phba->sli4_hba.nvme_xri_max);
  2261. vport->phba->total_nvme_bufs += len;
  2262. }
  2263. } else {
  2264. kfree(cstat);
  2265. }
  2266. return ret;
  2267. }
  2268. /* lpfc_nvme_lport_unreg_wait - Wait for the host to complete an lport unreg.
  2269. *
  2270. * The driver has to wait for the host nvme transport to callback
  2271. * indicating the localport has successfully unregistered all
  2272. * resources. Since this is an uninterruptible wait, loop every ten
  2273. * seconds and print a message indicating no progress.
  2274. *
  2275. * An uninterruptible wait is used because of the risk of transport-to-
  2276. * driver state mismatch.
  2277. */
  2278. void
  2279. lpfc_nvme_lport_unreg_wait(struct lpfc_vport *vport,
  2280. struct lpfc_nvme_lport *lport,
  2281. struct completion *lport_unreg_cmp)
  2282. {
  2283. #if (IS_ENABLED(CONFIG_NVME_FC))
  2284. u32 wait_tmo;
  2285. int ret;
  2286. /* Host transport has to clean up and confirm requiring an indefinite
  2287. * wait. Print a message if a 10 second wait expires and renew the
  2288. * wait. This is unexpected.
  2289. */
  2290. wait_tmo = msecs_to_jiffies(LPFC_NVME_WAIT_TMO * 1000);
  2291. while (true) {
  2292. ret = wait_for_completion_timeout(lport_unreg_cmp, wait_tmo);
  2293. if (unlikely(!ret)) {
  2294. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_IOERR,
  2295. "6176 Lport %p Localport %p wait "
  2296. "timed out. Renewing.\n",
  2297. lport, vport->localport);
  2298. continue;
  2299. }
  2300. break;
  2301. }
  2302. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_IOERR,
  2303. "6177 Lport %p Localport %p Complete Success\n",
  2304. lport, vport->localport);
  2305. #endif
  2306. }
  2307. /**
  2308. * lpfc_nvme_destroy_localport - Destroy lpfc_nvme bound to nvme transport.
  2309. * @pnvme: pointer to lpfc nvme data structure.
  2310. *
  2311. * This routine is invoked to destroy all lports bound to the phba.
  2312. * The lport memory was allocated by the nvme fc transport and is
  2313. * released there. This routine ensures all rports bound to the
  2314. * lport have been disconnected.
  2315. *
  2316. **/
  2317. void
  2318. lpfc_nvme_destroy_localport(struct lpfc_vport *vport)
  2319. {
  2320. #if (IS_ENABLED(CONFIG_NVME_FC))
  2321. struct nvme_fc_local_port *localport;
  2322. struct lpfc_nvme_lport *lport;
  2323. struct lpfc_nvme_ctrl_stat *cstat;
  2324. int ret;
  2325. DECLARE_COMPLETION_ONSTACK(lport_unreg_cmp);
  2326. if (vport->nvmei_support == 0)
  2327. return;
  2328. localport = vport->localport;
  2329. lport = (struct lpfc_nvme_lport *)localport->private;
  2330. cstat = lport->cstat;
  2331. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
  2332. "6011 Destroying NVME localport %p\n",
  2333. localport);
  2334. /* lport's rport list is clear. Unregister
  2335. * lport and release resources.
  2336. */
  2337. lport->lport_unreg_cmp = &lport_unreg_cmp;
  2338. ret = nvme_fc_unregister_localport(localport);
  2339. /* Wait for completion. This either blocks
  2340. * indefinitely or succeeds
  2341. */
  2342. lpfc_nvme_lport_unreg_wait(vport, lport, &lport_unreg_cmp);
  2343. vport->localport = NULL;
  2344. kfree(cstat);
  2345. /* Regardless of the unregister upcall response, clear
  2346. * nvmei_support. All rports are unregistered and the
  2347. * driver will clean up.
  2348. */
  2349. vport->nvmei_support = 0;
  2350. if (ret == 0) {
  2351. lpfc_printf_vlog(vport,
  2352. KERN_INFO, LOG_NVME_DISC,
  2353. "6009 Unregistered lport Success\n");
  2354. } else {
  2355. lpfc_printf_vlog(vport,
  2356. KERN_INFO, LOG_NVME_DISC,
  2357. "6010 Unregistered lport "
  2358. "Failed, status x%x\n",
  2359. ret);
  2360. }
  2361. #endif
  2362. }
  2363. void
  2364. lpfc_nvme_update_localport(struct lpfc_vport *vport)
  2365. {
  2366. #if (IS_ENABLED(CONFIG_NVME_FC))
  2367. struct nvme_fc_local_port *localport;
  2368. struct lpfc_nvme_lport *lport;
  2369. localport = vport->localport;
  2370. if (!localport) {
  2371. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
  2372. "6710 Update NVME fail. No localport\n");
  2373. return;
  2374. }
  2375. lport = (struct lpfc_nvme_lport *)localport->private;
  2376. if (!lport) {
  2377. lpfc_printf_vlog(vport, KERN_WARNING, LOG_NVME,
  2378. "6171 Update NVME fail. localP %p, No lport\n",
  2379. localport);
  2380. return;
  2381. }
  2382. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME,
  2383. "6012 Update NVME lport %p did x%x\n",
  2384. localport, vport->fc_myDID);
  2385. localport->port_id = vport->fc_myDID;
  2386. if (localport->port_id == 0)
  2387. localport->port_role = FC_PORT_ROLE_NVME_DISCOVERY;
  2388. else
  2389. localport->port_role = FC_PORT_ROLE_NVME_INITIATOR;
  2390. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
  2391. "6030 bound lport %p to DID x%06x\n",
  2392. lport, localport->port_id);
  2393. #endif
  2394. }
  2395. int
  2396. lpfc_nvme_register_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2397. {
  2398. #if (IS_ENABLED(CONFIG_NVME_FC))
  2399. int ret = 0;
  2400. struct nvme_fc_local_port *localport;
  2401. struct lpfc_nvme_lport *lport;
  2402. struct lpfc_nvme_rport *rport;
  2403. struct lpfc_nvme_rport *oldrport;
  2404. struct nvme_fc_remote_port *remote_port;
  2405. struct nvme_fc_port_info rpinfo;
  2406. struct lpfc_nodelist *prev_ndlp = NULL;
  2407. lpfc_printf_vlog(ndlp->vport, KERN_INFO, LOG_NVME_DISC,
  2408. "6006 Register NVME PORT. DID x%06x nlptype x%x\n",
  2409. ndlp->nlp_DID, ndlp->nlp_type);
  2410. localport = vport->localport;
  2411. if (!localport)
  2412. return 0;
  2413. lport = (struct lpfc_nvme_lport *)localport->private;
  2414. /* NVME rports are not preserved across devloss.
  2415. * Just register this instance. Note, rpinfo->dev_loss_tmo
  2416. * is left 0 to indicate accept transport defaults. The
  2417. * driver communicates port role capabilities consistent
  2418. * with the PRLI response data.
  2419. */
  2420. memset(&rpinfo, 0, sizeof(struct nvme_fc_port_info));
  2421. rpinfo.port_id = ndlp->nlp_DID;
  2422. if (ndlp->nlp_type & NLP_NVME_TARGET)
  2423. rpinfo.port_role |= FC_PORT_ROLE_NVME_TARGET;
  2424. if (ndlp->nlp_type & NLP_NVME_INITIATOR)
  2425. rpinfo.port_role |= FC_PORT_ROLE_NVME_INITIATOR;
  2426. if (ndlp->nlp_type & NLP_NVME_DISCOVERY)
  2427. rpinfo.port_role |= FC_PORT_ROLE_NVME_DISCOVERY;
  2428. rpinfo.port_name = wwn_to_u64(ndlp->nlp_portname.u.wwn);
  2429. rpinfo.node_name = wwn_to_u64(ndlp->nlp_nodename.u.wwn);
  2430. spin_lock_irq(&vport->phba->hbalock);
  2431. oldrport = lpfc_ndlp_get_nrport(ndlp);
  2432. spin_unlock_irq(&vport->phba->hbalock);
  2433. if (!oldrport)
  2434. lpfc_nlp_get(ndlp);
  2435. ret = nvme_fc_register_remoteport(localport, &rpinfo, &remote_port);
  2436. if (!ret) {
  2437. /* If the ndlp already has an nrport, this is just
  2438. * a resume of the existing rport. Else this is a
  2439. * new rport.
  2440. */
  2441. /* Guard against an unregister/reregister
  2442. * race that leaves the WAIT flag set.
  2443. */
  2444. spin_lock_irq(&vport->phba->hbalock);
  2445. ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
  2446. spin_unlock_irq(&vport->phba->hbalock);
  2447. rport = remote_port->private;
  2448. if (oldrport) {
  2449. /* New remoteport record does not guarantee valid
  2450. * host private memory area.
  2451. */
  2452. prev_ndlp = oldrport->ndlp;
  2453. if (oldrport == remote_port->private) {
  2454. /* Same remoteport - ndlp should match.
  2455. * Just reuse.
  2456. */
  2457. lpfc_printf_vlog(ndlp->vport, KERN_INFO,
  2458. LOG_NVME_DISC,
  2459. "6014 Rebinding lport to "
  2460. "remoteport %p wwpn 0x%llx, "
  2461. "Data: x%x x%x %p %p x%x x%06x\n",
  2462. remote_port,
  2463. remote_port->port_name,
  2464. remote_port->port_id,
  2465. remote_port->port_role,
  2466. prev_ndlp,
  2467. ndlp,
  2468. ndlp->nlp_type,
  2469. ndlp->nlp_DID);
  2470. return 0;
  2471. }
  2472. /* Sever the ndlp<->rport association
  2473. * before dropping the ndlp ref from
  2474. * register.
  2475. */
  2476. spin_lock_irq(&vport->phba->hbalock);
  2477. ndlp->nrport = NULL;
  2478. ndlp->upcall_flags &= ~NLP_WAIT_FOR_UNREG;
  2479. spin_unlock_irq(&vport->phba->hbalock);
  2480. rport->ndlp = NULL;
  2481. rport->remoteport = NULL;
  2482. /* Reference only removed if previous NDLP is no longer
  2483. * active. It might be just a swap and removing the
  2484. * reference would cause a premature cleanup.
  2485. */
  2486. if (prev_ndlp && prev_ndlp != ndlp) {
  2487. if ((!NLP_CHK_NODE_ACT(prev_ndlp)) ||
  2488. (!prev_ndlp->nrport))
  2489. lpfc_nlp_put(prev_ndlp);
  2490. }
  2491. }
  2492. /* Clean bind the rport to the ndlp. */
  2493. rport->remoteport = remote_port;
  2494. rport->lport = lport;
  2495. rport->ndlp = ndlp;
  2496. spin_lock_irq(&vport->phba->hbalock);
  2497. ndlp->nrport = rport;
  2498. spin_unlock_irq(&vport->phba->hbalock);
  2499. lpfc_printf_vlog(vport, KERN_INFO,
  2500. LOG_NVME_DISC | LOG_NODE,
  2501. "6022 Binding new rport to "
  2502. "lport %p Remoteport %p rport %p WWNN 0x%llx, "
  2503. "Rport WWPN 0x%llx DID "
  2504. "x%06x Role x%x, ndlp %p prev_ndlp %p\n",
  2505. lport, remote_port, rport,
  2506. rpinfo.node_name, rpinfo.port_name,
  2507. rpinfo.port_id, rpinfo.port_role,
  2508. ndlp, prev_ndlp);
  2509. } else {
  2510. lpfc_printf_vlog(vport, KERN_ERR,
  2511. LOG_NVME_DISC | LOG_NODE,
  2512. "6031 RemotePort Registration failed "
  2513. "err: %d, DID x%06x\n",
  2514. ret, ndlp->nlp_DID);
  2515. }
  2516. return ret;
  2517. #else
  2518. return 0;
  2519. #endif
  2520. }
  2521. /* lpfc_nvme_unregister_port - unbind the DID and port_role from this rport.
  2522. *
  2523. * There is no notion of Devloss or rport recovery from the current
  2524. * nvme_transport perspective. Loss of an rport just means IO cannot
  2525. * be sent and recovery is completely up to the initator.
  2526. * For now, the driver just unbinds the DID and port_role so that
  2527. * no further IO can be issued. Changes are planned for later.
  2528. *
  2529. * Notes - the ndlp reference count is not decremented here since
  2530. * since there is no nvme_transport api for devloss. Node ref count
  2531. * is only adjusted in driver unload.
  2532. */
  2533. void
  2534. lpfc_nvme_unregister_port(struct lpfc_vport *vport, struct lpfc_nodelist *ndlp)
  2535. {
  2536. #if (IS_ENABLED(CONFIG_NVME_FC))
  2537. int ret;
  2538. struct nvme_fc_local_port *localport;
  2539. struct lpfc_nvme_lport *lport;
  2540. struct lpfc_nvme_rport *rport;
  2541. struct nvme_fc_remote_port *remoteport = NULL;
  2542. localport = vport->localport;
  2543. /* This is fundamental error. The localport is always
  2544. * available until driver unload. Just exit.
  2545. */
  2546. if (!localport)
  2547. return;
  2548. lport = (struct lpfc_nvme_lport *)localport->private;
  2549. if (!lport)
  2550. goto input_err;
  2551. spin_lock_irq(&vport->phba->hbalock);
  2552. rport = lpfc_ndlp_get_nrport(ndlp);
  2553. if (rport)
  2554. remoteport = rport->remoteport;
  2555. spin_unlock_irq(&vport->phba->hbalock);
  2556. if (!remoteport)
  2557. goto input_err;
  2558. lpfc_printf_vlog(vport, KERN_INFO, LOG_NVME_DISC,
  2559. "6033 Unreg nvme remoteport %p, portname x%llx, "
  2560. "port_id x%06x, portstate x%x port type x%x\n",
  2561. remoteport, remoteport->port_name,
  2562. remoteport->port_id, remoteport->port_state,
  2563. ndlp->nlp_type);
  2564. /* Sanity check ndlp type. Only call for NVME ports. Don't
  2565. * clear any rport state until the transport calls back.
  2566. */
  2567. if (ndlp->nlp_type & NLP_NVME_TARGET) {
  2568. /* No concern about the role change on the nvme remoteport.
  2569. * The transport will update it.
  2570. */
  2571. ndlp->upcall_flags |= NLP_WAIT_FOR_UNREG;
  2572. /* Don't let the host nvme transport keep sending keep-alives
  2573. * on this remoteport. Vport is unloading, no recovery. The
  2574. * return values is ignored. The upcall is a courtesy to the
  2575. * transport.
  2576. */
  2577. if (vport->load_flag & FC_UNLOADING)
  2578. (void)nvme_fc_set_remoteport_devloss(remoteport, 0);
  2579. ret = nvme_fc_unregister_remoteport(remoteport);
  2580. if (ret != 0) {
  2581. lpfc_nlp_put(ndlp);
  2582. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
  2583. "6167 NVME unregister failed %d "
  2584. "port_state x%x\n",
  2585. ret, remoteport->port_state);
  2586. }
  2587. }
  2588. return;
  2589. input_err:
  2590. #endif
  2591. lpfc_printf_vlog(vport, KERN_ERR, LOG_NVME_DISC,
  2592. "6168 State error: lport %p, rport%p FCID x%06x\n",
  2593. vport->localport, ndlp->rport, ndlp->nlp_DID);
  2594. }
  2595. /**
  2596. * lpfc_sli4_nvme_xri_aborted - Fast-path process of NVME xri abort
  2597. * @phba: pointer to lpfc hba data structure.
  2598. * @axri: pointer to the fcp xri abort wcqe structure.
  2599. *
  2600. * This routine is invoked by the worker thread to process a SLI4 fast-path
  2601. * NVME aborted xri. Aborted NVME IO commands are completed to the transport
  2602. * here.
  2603. **/
  2604. void
  2605. lpfc_sli4_nvme_xri_aborted(struct lpfc_hba *phba,
  2606. struct sli4_wcqe_xri_aborted *axri)
  2607. {
  2608. uint16_t xri = bf_get(lpfc_wcqe_xa_xri, axri);
  2609. struct lpfc_nvme_buf *lpfc_ncmd, *next_lpfc_ncmd;
  2610. struct nvmefc_fcp_req *nvme_cmd = NULL;
  2611. struct lpfc_nodelist *ndlp;
  2612. unsigned long iflag = 0;
  2613. if (!(phba->cfg_enable_fc4_type & LPFC_ENABLE_NVME))
  2614. return;
  2615. spin_lock_irqsave(&phba->hbalock, iflag);
  2616. spin_lock(&phba->sli4_hba.abts_nvme_buf_list_lock);
  2617. list_for_each_entry_safe(lpfc_ncmd, next_lpfc_ncmd,
  2618. &phba->sli4_hba.lpfc_abts_nvme_buf_list,
  2619. list) {
  2620. if (lpfc_ncmd->cur_iocbq.sli4_xritag == xri) {
  2621. list_del_init(&lpfc_ncmd->list);
  2622. lpfc_ncmd->flags &= ~LPFC_SBUF_XBUSY;
  2623. lpfc_ncmd->status = IOSTAT_SUCCESS;
  2624. spin_unlock(
  2625. &phba->sli4_hba.abts_nvme_buf_list_lock);
  2626. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2627. ndlp = lpfc_ncmd->ndlp;
  2628. if (ndlp)
  2629. lpfc_sli4_abts_err_handler(phba, ndlp, axri);
  2630. lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
  2631. "6311 nvme_cmd %p xri x%x tag x%x "
  2632. "abort complete and xri released\n",
  2633. lpfc_ncmd->nvmeCmd, xri,
  2634. lpfc_ncmd->cur_iocbq.iotag);
  2635. /* Aborted NVME commands are required to not complete
  2636. * before the abort exchange command fully completes.
  2637. * Once completed, it is available via the put list.
  2638. */
  2639. if (lpfc_ncmd->nvmeCmd) {
  2640. nvme_cmd = lpfc_ncmd->nvmeCmd;
  2641. nvme_cmd->done(nvme_cmd);
  2642. lpfc_ncmd->nvmeCmd = NULL;
  2643. }
  2644. lpfc_release_nvme_buf(phba, lpfc_ncmd);
  2645. return;
  2646. }
  2647. }
  2648. spin_unlock(&phba->sli4_hba.abts_nvme_buf_list_lock);
  2649. spin_unlock_irqrestore(&phba->hbalock, iflag);
  2650. lpfc_printf_log(phba, KERN_INFO, LOG_NVME_ABTS,
  2651. "6312 XRI Aborted xri x%x not found\n", xri);
  2652. }
  2653. /**
  2654. * lpfc_nvme_wait_for_io_drain - Wait for all NVME wqes to complete
  2655. * @phba: Pointer to HBA context object.
  2656. *
  2657. * This function flushes all wqes in the nvme rings and frees all resources
  2658. * in the txcmplq. This function does not issue abort wqes for the IO
  2659. * commands in txcmplq, they will just be returned with
  2660. * IOERR_SLI_DOWN. This function is invoked with EEH when device's PCI
  2661. * slot has been permanently disabled.
  2662. **/
  2663. void
  2664. lpfc_nvme_wait_for_io_drain(struct lpfc_hba *phba)
  2665. {
  2666. struct lpfc_sli_ring *pring;
  2667. u32 i, wait_cnt = 0;
  2668. if (phba->sli_rev < LPFC_SLI_REV4 || !phba->sli4_hba.nvme_wq)
  2669. return;
  2670. /* Cycle through all NVME rings and make sure all outstanding
  2671. * WQEs have been removed from the txcmplqs.
  2672. */
  2673. for (i = 0; i < phba->cfg_nvme_io_channel; i++) {
  2674. pring = phba->sli4_hba.nvme_wq[i]->pring;
  2675. if (!pring)
  2676. continue;
  2677. /* Retrieve everything on the txcmplq */
  2678. while (!list_empty(&pring->txcmplq)) {
  2679. msleep(LPFC_XRI_EXCH_BUSY_WAIT_T1);
  2680. wait_cnt++;
  2681. /* The sleep is 10mS. Every ten seconds,
  2682. * dump a message. Something is wrong.
  2683. */
  2684. if ((wait_cnt % 1000) == 0) {
  2685. lpfc_printf_log(phba, KERN_ERR, LOG_NVME_IOERR,
  2686. "6178 NVME IO not empty, "
  2687. "cnt %d\n", wait_cnt);
  2688. }
  2689. }
  2690. }
  2691. }