ibmvscsi_tgt.c 107 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*******************************************************************************
  3. * IBM Virtual SCSI Target Driver
  4. * Copyright (C) 2003-2005 Dave Boutcher ([email protected]) IBM Corp.
  5. * Santiago Leon ([email protected]) IBM Corp.
  6. * Linda Xie ([email protected]) IBM Corp.
  7. *
  8. * Copyright (C) 2005-2011 FUJITA Tomonori <[email protected]>
  9. * Copyright (C) 2010 Nicholas A. Bellinger <[email protected]>
  10. *
  11. * Authors: Bryant G. Ly <[email protected]>
  12. * Authors: Michael Cyr <[email protected]>
  13. *
  14. ****************************************************************************/
  15. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  16. #include <linux/module.h>
  17. #include <linux/kernel.h>
  18. #include <linux/slab.h>
  19. #include <linux/types.h>
  20. #include <linux/list.h>
  21. #include <linux/string.h>
  22. #include <linux/delay.h>
  23. #include <linux/of.h>
  24. #include <target/target_core_base.h>
  25. #include <target/target_core_fabric.h>
  26. #include <asm/hvcall.h>
  27. #include <asm/vio.h>
  28. #include <scsi/viosrp.h>
  29. #include "ibmvscsi_tgt.h"
  30. #define IBMVSCSIS_VERSION "v0.2"
  31. #define INITIAL_SRP_LIMIT 1024
  32. #define DEFAULT_MAX_SECTORS 256
  33. #define MAX_TXU 1024 * 1024
  34. static uint max_vdma_size = MAX_H_COPY_RDMA;
  35. static char system_id[SYS_ID_NAME_LEN] = "";
  36. static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
  37. static uint partition_number = -1;
  38. /* Adapter list and lock to control it */
  39. static DEFINE_SPINLOCK(ibmvscsis_dev_lock);
  40. static LIST_HEAD(ibmvscsis_dev_list);
  41. static long ibmvscsis_parse_command(struct scsi_info *vscsi,
  42. struct viosrp_crq *crq);
  43. static void ibmvscsis_adapter_idle(struct scsi_info *vscsi);
  44. static void ibmvscsis_determine_resid(struct se_cmd *se_cmd,
  45. struct srp_rsp *rsp)
  46. {
  47. u32 residual_count = se_cmd->residual_count;
  48. if (!residual_count)
  49. return;
  50. if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
  51. if (se_cmd->data_direction == DMA_TO_DEVICE) {
  52. /* residual data from an underflow write */
  53. rsp->flags = SRP_RSP_FLAG_DOUNDER;
  54. rsp->data_out_res_cnt = cpu_to_be32(residual_count);
  55. } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  56. /* residual data from an underflow read */
  57. rsp->flags = SRP_RSP_FLAG_DIUNDER;
  58. rsp->data_in_res_cnt = cpu_to_be32(residual_count);
  59. }
  60. } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
  61. if (se_cmd->data_direction == DMA_TO_DEVICE) {
  62. /* residual data from an overflow write */
  63. rsp->flags = SRP_RSP_FLAG_DOOVER;
  64. rsp->data_out_res_cnt = cpu_to_be32(residual_count);
  65. } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
  66. /* residual data from an overflow read */
  67. rsp->flags = SRP_RSP_FLAG_DIOVER;
  68. rsp->data_in_res_cnt = cpu_to_be32(residual_count);
  69. }
  70. }
  71. }
  72. /**
  73. * connection_broken() - Determine if the connection to the client is good
  74. * @vscsi: Pointer to our adapter structure
  75. *
  76. * This function attempts to send a ping MAD to the client. If the call to
  77. * queue the request returns H_CLOSED then the connection has been broken
  78. * and the function returns TRUE.
  79. *
  80. * EXECUTION ENVIRONMENT:
  81. * Interrupt or Process environment
  82. */
  83. static bool connection_broken(struct scsi_info *vscsi)
  84. {
  85. struct viosrp_crq *crq;
  86. u64 buffer[2] = { 0, 0 };
  87. long h_return_code;
  88. bool rc = false;
  89. /* create a PING crq */
  90. crq = (struct viosrp_crq *)&buffer;
  91. crq->valid = VALID_CMD_RESP_EL;
  92. crq->format = MESSAGE_IN_CRQ;
  93. crq->status = PING;
  94. h_return_code = h_send_crq(vscsi->dds.unit_id,
  95. cpu_to_be64(buffer[MSG_HI]),
  96. cpu_to_be64(buffer[MSG_LOW]));
  97. dev_dbg(&vscsi->dev, "Connection_broken: rc %ld\n", h_return_code);
  98. if (h_return_code == H_CLOSED)
  99. rc = true;
  100. return rc;
  101. }
  102. /**
  103. * ibmvscsis_unregister_command_q() - Helper Function-Unregister Command Queue
  104. * @vscsi: Pointer to our adapter structure
  105. *
  106. * This function calls h_free_q then frees the interrupt bit etc.
  107. * It must release the lock before doing so because of the time it can take
  108. * for h_free_crq in PHYP
  109. * NOTE: * the caller must make sure that state and or flags will prevent
  110. * interrupt handler from scheduling work.
  111. * * anyone calling this function may need to set the CRQ_CLOSED flag
  112. * we can't do it here, because we don't have the lock
  113. *
  114. * EXECUTION ENVIRONMENT:
  115. * Process level
  116. */
  117. static long ibmvscsis_unregister_command_q(struct scsi_info *vscsi)
  118. {
  119. long qrc;
  120. long rc = ADAPT_SUCCESS;
  121. int ticks = 0;
  122. do {
  123. qrc = h_free_crq(vscsi->dds.unit_id);
  124. switch (qrc) {
  125. case H_SUCCESS:
  126. spin_lock_bh(&vscsi->intr_lock);
  127. vscsi->flags &= ~PREP_FOR_SUSPEND_FLAGS;
  128. spin_unlock_bh(&vscsi->intr_lock);
  129. break;
  130. case H_HARDWARE:
  131. case H_PARAMETER:
  132. dev_err(&vscsi->dev, "unregister_command_q: error from h_free_crq %ld\n",
  133. qrc);
  134. rc = ERROR;
  135. break;
  136. case H_BUSY:
  137. case H_LONG_BUSY_ORDER_1_MSEC:
  138. /* msleep not good for small values */
  139. usleep_range(1000, 2000);
  140. ticks += 1;
  141. break;
  142. case H_LONG_BUSY_ORDER_10_MSEC:
  143. usleep_range(10000, 20000);
  144. ticks += 10;
  145. break;
  146. case H_LONG_BUSY_ORDER_100_MSEC:
  147. msleep(100);
  148. ticks += 100;
  149. break;
  150. case H_LONG_BUSY_ORDER_1_SEC:
  151. ssleep(1);
  152. ticks += 1000;
  153. break;
  154. case H_LONG_BUSY_ORDER_10_SEC:
  155. ssleep(10);
  156. ticks += 10000;
  157. break;
  158. case H_LONG_BUSY_ORDER_100_SEC:
  159. ssleep(100);
  160. ticks += 100000;
  161. break;
  162. default:
  163. dev_err(&vscsi->dev, "unregister_command_q: unknown error %ld from h_free_crq\n",
  164. qrc);
  165. rc = ERROR;
  166. break;
  167. }
  168. /*
  169. * dont wait more then 300 seconds
  170. * ticks are in milliseconds more or less
  171. */
  172. if (ticks > 300000 && qrc != H_SUCCESS) {
  173. rc = ERROR;
  174. dev_err(&vscsi->dev, "Excessive wait for h_free_crq\n");
  175. }
  176. } while (qrc != H_SUCCESS && rc == ADAPT_SUCCESS);
  177. dev_dbg(&vscsi->dev, "Freeing CRQ: phyp rc %ld, rc %ld\n", qrc, rc);
  178. return rc;
  179. }
  180. /**
  181. * ibmvscsis_delete_client_info() - Helper function to Delete Client Info
  182. * @vscsi: Pointer to our adapter structure
  183. * @client_closed: True if client closed its queue
  184. *
  185. * Deletes information specific to the client when the client goes away
  186. *
  187. * EXECUTION ENVIRONMENT:
  188. * Interrupt or Process
  189. */
  190. static void ibmvscsis_delete_client_info(struct scsi_info *vscsi,
  191. bool client_closed)
  192. {
  193. vscsi->client_cap = 0;
  194. /*
  195. * Some things we don't want to clear if we're closing the queue,
  196. * because some clients don't resend the host handshake when they
  197. * get a transport event.
  198. */
  199. if (client_closed)
  200. vscsi->client_data.os_type = 0;
  201. }
  202. /**
  203. * ibmvscsis_free_command_q() - Free Command Queue
  204. * @vscsi: Pointer to our adapter structure
  205. *
  206. * This function calls unregister_command_q, then clears interrupts and
  207. * any pending interrupt acknowledgments associated with the command q.
  208. * It also clears memory if there is no error.
  209. *
  210. * PHYP did not meet the PAPR architecture so that we must give up the
  211. * lock. This causes a timing hole regarding state change. To close the
  212. * hole this routine does accounting on any change that occurred during
  213. * the time the lock is not held.
  214. * NOTE: must give up and then acquire the interrupt lock, the caller must
  215. * make sure that state and or flags will prevent interrupt handler from
  216. * scheduling work.
  217. *
  218. * EXECUTION ENVIRONMENT:
  219. * Process level, interrupt lock is held
  220. */
  221. static long ibmvscsis_free_command_q(struct scsi_info *vscsi)
  222. {
  223. int bytes;
  224. u32 flags_under_lock;
  225. u16 state_under_lock;
  226. long rc = ADAPT_SUCCESS;
  227. if (!(vscsi->flags & CRQ_CLOSED)) {
  228. vio_disable_interrupts(vscsi->dma_dev);
  229. state_under_lock = vscsi->new_state;
  230. flags_under_lock = vscsi->flags;
  231. vscsi->phyp_acr_state = 0;
  232. vscsi->phyp_acr_flags = 0;
  233. spin_unlock_bh(&vscsi->intr_lock);
  234. rc = ibmvscsis_unregister_command_q(vscsi);
  235. spin_lock_bh(&vscsi->intr_lock);
  236. if (state_under_lock != vscsi->new_state)
  237. vscsi->phyp_acr_state = vscsi->new_state;
  238. vscsi->phyp_acr_flags = ((~flags_under_lock) & vscsi->flags);
  239. if (rc == ADAPT_SUCCESS) {
  240. bytes = vscsi->cmd_q.size * PAGE_SIZE;
  241. memset(vscsi->cmd_q.base_addr, 0, bytes);
  242. vscsi->cmd_q.index = 0;
  243. vscsi->flags |= CRQ_CLOSED;
  244. ibmvscsis_delete_client_info(vscsi, false);
  245. }
  246. dev_dbg(&vscsi->dev, "free_command_q: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
  247. vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
  248. vscsi->phyp_acr_state);
  249. }
  250. return rc;
  251. }
  252. /**
  253. * ibmvscsis_cmd_q_dequeue() - Get valid Command element
  254. * @mask: Mask to use in case index wraps
  255. * @current_index: Current index into command queue
  256. * @base_addr: Pointer to start of command queue
  257. *
  258. * Returns a pointer to a valid command element or NULL, if the command
  259. * queue is empty
  260. *
  261. * EXECUTION ENVIRONMENT:
  262. * Interrupt environment, interrupt lock held
  263. */
  264. static struct viosrp_crq *ibmvscsis_cmd_q_dequeue(uint mask,
  265. uint *current_index,
  266. struct viosrp_crq *base_addr)
  267. {
  268. struct viosrp_crq *ptr;
  269. ptr = base_addr + *current_index;
  270. if (ptr->valid) {
  271. *current_index = (*current_index + 1) & mask;
  272. dma_rmb();
  273. } else {
  274. ptr = NULL;
  275. }
  276. return ptr;
  277. }
  278. /**
  279. * ibmvscsis_send_init_message() - send initialize message to the client
  280. * @vscsi: Pointer to our adapter structure
  281. * @format: Which Init Message format to send
  282. *
  283. * EXECUTION ENVIRONMENT:
  284. * Interrupt environment interrupt lock held
  285. */
  286. static long ibmvscsis_send_init_message(struct scsi_info *vscsi, u8 format)
  287. {
  288. struct viosrp_crq *crq;
  289. u64 buffer[2] = { 0, 0 };
  290. long rc;
  291. crq = (struct viosrp_crq *)&buffer;
  292. crq->valid = VALID_INIT_MSG;
  293. crq->format = format;
  294. rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
  295. cpu_to_be64(buffer[MSG_LOW]));
  296. return rc;
  297. }
  298. /**
  299. * ibmvscsis_check_init_msg() - Check init message valid
  300. * @vscsi: Pointer to our adapter structure
  301. * @format: Pointer to return format of Init Message, if any.
  302. * Set to UNUSED_FORMAT if no Init Message in queue.
  303. *
  304. * Checks if an initialize message was queued by the initiatior
  305. * after the queue was created and before the interrupt was enabled.
  306. *
  307. * EXECUTION ENVIRONMENT:
  308. * Process level only, interrupt lock held
  309. */
  310. static long ibmvscsis_check_init_msg(struct scsi_info *vscsi, uint *format)
  311. {
  312. struct viosrp_crq *crq;
  313. long rc = ADAPT_SUCCESS;
  314. crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask, &vscsi->cmd_q.index,
  315. vscsi->cmd_q.base_addr);
  316. if (!crq) {
  317. *format = (uint)UNUSED_FORMAT;
  318. } else if (crq->valid == VALID_INIT_MSG && crq->format == INIT_MSG) {
  319. *format = (uint)INIT_MSG;
  320. crq->valid = INVALIDATE_CMD_RESP_EL;
  321. dma_rmb();
  322. /*
  323. * the caller has ensured no initialize message was
  324. * sent after the queue was
  325. * created so there should be no other message on the queue.
  326. */
  327. crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask,
  328. &vscsi->cmd_q.index,
  329. vscsi->cmd_q.base_addr);
  330. if (crq) {
  331. *format = (uint)(crq->format);
  332. rc = ERROR;
  333. crq->valid = INVALIDATE_CMD_RESP_EL;
  334. dma_rmb();
  335. }
  336. } else {
  337. *format = (uint)(crq->format);
  338. rc = ERROR;
  339. crq->valid = INVALIDATE_CMD_RESP_EL;
  340. dma_rmb();
  341. }
  342. return rc;
  343. }
  344. /**
  345. * ibmvscsis_disconnect() - Helper function to disconnect
  346. * @work: Pointer to work_struct, gives access to our adapter structure
  347. *
  348. * An error has occurred or the driver received a Transport event,
  349. * and the driver is requesting that the command queue be de-registered
  350. * in a safe manner. If there is no outstanding I/O then we can stop the
  351. * queue. If we are restarting the queue it will be reflected in the
  352. * the state of the adapter.
  353. *
  354. * EXECUTION ENVIRONMENT:
  355. * Process environment
  356. */
  357. static void ibmvscsis_disconnect(struct work_struct *work)
  358. {
  359. struct scsi_info *vscsi = container_of(work, struct scsi_info,
  360. proc_work);
  361. u16 new_state;
  362. bool wait_idle = false;
  363. spin_lock_bh(&vscsi->intr_lock);
  364. new_state = vscsi->new_state;
  365. vscsi->new_state = 0;
  366. vscsi->flags |= DISCONNECT_SCHEDULED;
  367. vscsi->flags &= ~SCHEDULE_DISCONNECT;
  368. dev_dbg(&vscsi->dev, "disconnect: flags 0x%x, state 0x%hx\n",
  369. vscsi->flags, vscsi->state);
  370. /*
  371. * check which state we are in and see if we
  372. * should transitition to the new state
  373. */
  374. switch (vscsi->state) {
  375. /* Should never be called while in this state. */
  376. case NO_QUEUE:
  377. /*
  378. * Can never transition from this state;
  379. * igonore errors and logout.
  380. */
  381. case UNCONFIGURING:
  382. break;
  383. /* can transition from this state to UNCONFIGURING */
  384. case ERR_DISCONNECT:
  385. if (new_state == UNCONFIGURING)
  386. vscsi->state = new_state;
  387. break;
  388. /*
  389. * Can transition from this state to unconfiguring
  390. * or err disconnect.
  391. */
  392. case ERR_DISCONNECT_RECONNECT:
  393. switch (new_state) {
  394. case UNCONFIGURING:
  395. case ERR_DISCONNECT:
  396. vscsi->state = new_state;
  397. break;
  398. case WAIT_IDLE:
  399. break;
  400. default:
  401. break;
  402. }
  403. break;
  404. /* can transition from this state to UNCONFIGURING */
  405. case ERR_DISCONNECTED:
  406. if (new_state == UNCONFIGURING)
  407. vscsi->state = new_state;
  408. break;
  409. case WAIT_ENABLED:
  410. switch (new_state) {
  411. case UNCONFIGURING:
  412. vscsi->state = new_state;
  413. vscsi->flags |= RESPONSE_Q_DOWN;
  414. vscsi->flags &= ~(SCHEDULE_DISCONNECT |
  415. DISCONNECT_SCHEDULED);
  416. dma_rmb();
  417. if (vscsi->flags & CFG_SLEEPING) {
  418. vscsi->flags &= ~CFG_SLEEPING;
  419. complete(&vscsi->unconfig);
  420. }
  421. break;
  422. /* should never happen */
  423. case ERR_DISCONNECT:
  424. case ERR_DISCONNECT_RECONNECT:
  425. case WAIT_IDLE:
  426. dev_err(&vscsi->dev, "disconnect: invalid state %d for WAIT_IDLE\n",
  427. vscsi->state);
  428. break;
  429. }
  430. break;
  431. case WAIT_IDLE:
  432. switch (new_state) {
  433. case UNCONFIGURING:
  434. vscsi->flags |= RESPONSE_Q_DOWN;
  435. vscsi->state = new_state;
  436. vscsi->flags &= ~(SCHEDULE_DISCONNECT |
  437. DISCONNECT_SCHEDULED);
  438. ibmvscsis_free_command_q(vscsi);
  439. break;
  440. case ERR_DISCONNECT:
  441. case ERR_DISCONNECT_RECONNECT:
  442. vscsi->state = new_state;
  443. break;
  444. }
  445. break;
  446. /*
  447. * Initiator has not done a successful srp login
  448. * or has done a successful srp logout ( adapter was not
  449. * busy). In the first case there can be responses queued
  450. * waiting for space on the initiators response queue (MAD)
  451. * The second case the adapter is idle. Assume the worse case,
  452. * i.e. the second case.
  453. */
  454. case WAIT_CONNECTION:
  455. case CONNECTED:
  456. case SRP_PROCESSING:
  457. wait_idle = true;
  458. vscsi->state = new_state;
  459. break;
  460. /* can transition from this state to UNCONFIGURING */
  461. case UNDEFINED:
  462. if (new_state == UNCONFIGURING)
  463. vscsi->state = new_state;
  464. break;
  465. default:
  466. break;
  467. }
  468. if (wait_idle) {
  469. dev_dbg(&vscsi->dev, "disconnect start wait, active %d, sched %d\n",
  470. (int)list_empty(&vscsi->active_q),
  471. (int)list_empty(&vscsi->schedule_q));
  472. if (!list_empty(&vscsi->active_q) ||
  473. !list_empty(&vscsi->schedule_q)) {
  474. vscsi->flags |= WAIT_FOR_IDLE;
  475. dev_dbg(&vscsi->dev, "disconnect flags 0x%x\n",
  476. vscsi->flags);
  477. /*
  478. * This routine is can not be called with the interrupt
  479. * lock held.
  480. */
  481. spin_unlock_bh(&vscsi->intr_lock);
  482. wait_for_completion(&vscsi->wait_idle);
  483. spin_lock_bh(&vscsi->intr_lock);
  484. }
  485. dev_dbg(&vscsi->dev, "disconnect stop wait\n");
  486. ibmvscsis_adapter_idle(vscsi);
  487. }
  488. spin_unlock_bh(&vscsi->intr_lock);
  489. }
  490. /**
  491. * ibmvscsis_post_disconnect() - Schedule the disconnect
  492. * @vscsi: Pointer to our adapter structure
  493. * @new_state: State to move to after disconnecting
  494. * @flag_bits: Flags to turn on in adapter structure
  495. *
  496. * If it's already been scheduled, then see if we need to "upgrade"
  497. * the new state (if the one passed in is more "severe" than the
  498. * previous one).
  499. *
  500. * PRECONDITION:
  501. * interrupt lock is held
  502. */
  503. static void ibmvscsis_post_disconnect(struct scsi_info *vscsi, uint new_state,
  504. uint flag_bits)
  505. {
  506. uint state;
  507. /* check the validity of the new state */
  508. switch (new_state) {
  509. case UNCONFIGURING:
  510. case ERR_DISCONNECT:
  511. case ERR_DISCONNECT_RECONNECT:
  512. case WAIT_IDLE:
  513. break;
  514. default:
  515. dev_err(&vscsi->dev, "post_disconnect: Invalid new state %d\n",
  516. new_state);
  517. return;
  518. }
  519. vscsi->flags |= flag_bits;
  520. dev_dbg(&vscsi->dev, "post_disconnect: new_state 0x%x, flag_bits 0x%x, vscsi->flags 0x%x, state %hx\n",
  521. new_state, flag_bits, vscsi->flags, vscsi->state);
  522. if (!(vscsi->flags & (DISCONNECT_SCHEDULED | SCHEDULE_DISCONNECT))) {
  523. vscsi->flags |= SCHEDULE_DISCONNECT;
  524. vscsi->new_state = new_state;
  525. INIT_WORK(&vscsi->proc_work, ibmvscsis_disconnect);
  526. (void)queue_work(vscsi->work_q, &vscsi->proc_work);
  527. } else {
  528. if (vscsi->new_state)
  529. state = vscsi->new_state;
  530. else
  531. state = vscsi->state;
  532. switch (state) {
  533. case NO_QUEUE:
  534. case UNCONFIGURING:
  535. break;
  536. case ERR_DISCONNECTED:
  537. case ERR_DISCONNECT:
  538. case UNDEFINED:
  539. if (new_state == UNCONFIGURING)
  540. vscsi->new_state = new_state;
  541. break;
  542. case ERR_DISCONNECT_RECONNECT:
  543. switch (new_state) {
  544. case UNCONFIGURING:
  545. case ERR_DISCONNECT:
  546. vscsi->new_state = new_state;
  547. break;
  548. default:
  549. break;
  550. }
  551. break;
  552. case WAIT_ENABLED:
  553. case WAIT_IDLE:
  554. case WAIT_CONNECTION:
  555. case CONNECTED:
  556. case SRP_PROCESSING:
  557. vscsi->new_state = new_state;
  558. break;
  559. default:
  560. break;
  561. }
  562. }
  563. dev_dbg(&vscsi->dev, "Leaving post_disconnect: flags 0x%x, new_state 0x%x\n",
  564. vscsi->flags, vscsi->new_state);
  565. }
  566. /**
  567. * ibmvscsis_handle_init_compl_msg() - Respond to an Init Complete Message
  568. * @vscsi: Pointer to our adapter structure
  569. *
  570. * Must be called with interrupt lock held.
  571. */
  572. static long ibmvscsis_handle_init_compl_msg(struct scsi_info *vscsi)
  573. {
  574. long rc = ADAPT_SUCCESS;
  575. switch (vscsi->state) {
  576. case NO_QUEUE:
  577. case ERR_DISCONNECT:
  578. case ERR_DISCONNECT_RECONNECT:
  579. case ERR_DISCONNECTED:
  580. case UNCONFIGURING:
  581. case UNDEFINED:
  582. rc = ERROR;
  583. break;
  584. case WAIT_CONNECTION:
  585. vscsi->state = CONNECTED;
  586. break;
  587. case WAIT_IDLE:
  588. case SRP_PROCESSING:
  589. case CONNECTED:
  590. case WAIT_ENABLED:
  591. default:
  592. rc = ERROR;
  593. dev_err(&vscsi->dev, "init_msg: invalid state %d to get init compl msg\n",
  594. vscsi->state);
  595. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  596. break;
  597. }
  598. return rc;
  599. }
  600. /**
  601. * ibmvscsis_handle_init_msg() - Respond to an Init Message
  602. * @vscsi: Pointer to our adapter structure
  603. *
  604. * Must be called with interrupt lock held.
  605. */
  606. static long ibmvscsis_handle_init_msg(struct scsi_info *vscsi)
  607. {
  608. long rc = ADAPT_SUCCESS;
  609. switch (vscsi->state) {
  610. case WAIT_CONNECTION:
  611. rc = ibmvscsis_send_init_message(vscsi, INIT_COMPLETE_MSG);
  612. switch (rc) {
  613. case H_SUCCESS:
  614. vscsi->state = CONNECTED;
  615. break;
  616. case H_PARAMETER:
  617. dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
  618. rc);
  619. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
  620. break;
  621. case H_DROPPED:
  622. dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
  623. rc);
  624. rc = ERROR;
  625. ibmvscsis_post_disconnect(vscsi,
  626. ERR_DISCONNECT_RECONNECT, 0);
  627. break;
  628. case H_CLOSED:
  629. dev_warn(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
  630. rc);
  631. rc = 0;
  632. break;
  633. }
  634. break;
  635. case UNDEFINED:
  636. rc = ERROR;
  637. break;
  638. case UNCONFIGURING:
  639. break;
  640. case WAIT_ENABLED:
  641. case CONNECTED:
  642. case SRP_PROCESSING:
  643. case WAIT_IDLE:
  644. case NO_QUEUE:
  645. case ERR_DISCONNECT:
  646. case ERR_DISCONNECT_RECONNECT:
  647. case ERR_DISCONNECTED:
  648. default:
  649. rc = ERROR;
  650. dev_err(&vscsi->dev, "init_msg: invalid state %d to get init msg\n",
  651. vscsi->state);
  652. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  653. break;
  654. }
  655. return rc;
  656. }
  657. /**
  658. * ibmvscsis_init_msg() - Respond to an init message
  659. * @vscsi: Pointer to our adapter structure
  660. * @crq: Pointer to CRQ element containing the Init Message
  661. *
  662. * EXECUTION ENVIRONMENT:
  663. * Interrupt, interrupt lock held
  664. */
  665. static long ibmvscsis_init_msg(struct scsi_info *vscsi, struct viosrp_crq *crq)
  666. {
  667. long rc = ADAPT_SUCCESS;
  668. dev_dbg(&vscsi->dev, "init_msg: state 0x%hx\n", vscsi->state);
  669. rc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
  670. (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
  671. 0);
  672. if (rc == H_SUCCESS) {
  673. vscsi->client_data.partition_number =
  674. be64_to_cpu(*(u64 *)vscsi->map_buf);
  675. dev_dbg(&vscsi->dev, "init_msg, part num %d\n",
  676. vscsi->client_data.partition_number);
  677. } else {
  678. dev_dbg(&vscsi->dev, "init_msg h_vioctl rc %ld\n", rc);
  679. rc = ADAPT_SUCCESS;
  680. }
  681. if (crq->format == INIT_MSG) {
  682. rc = ibmvscsis_handle_init_msg(vscsi);
  683. } else if (crq->format == INIT_COMPLETE_MSG) {
  684. rc = ibmvscsis_handle_init_compl_msg(vscsi);
  685. } else {
  686. rc = ERROR;
  687. dev_err(&vscsi->dev, "init_msg: invalid format %d\n",
  688. (uint)crq->format);
  689. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  690. }
  691. return rc;
  692. }
  693. /**
  694. * ibmvscsis_establish_new_q() - Establish new CRQ queue
  695. * @vscsi: Pointer to our adapter structure
  696. *
  697. * Must be called with interrupt lock held.
  698. */
  699. static long ibmvscsis_establish_new_q(struct scsi_info *vscsi)
  700. {
  701. long rc = ADAPT_SUCCESS;
  702. uint format;
  703. rc = h_vioctl(vscsi->dds.unit_id, H_ENABLE_PREPARE_FOR_SUSPEND, 30000,
  704. 0, 0, 0, 0);
  705. if (rc == H_SUCCESS)
  706. vscsi->flags |= PREP_FOR_SUSPEND_ENABLED;
  707. else if (rc != H_NOT_FOUND)
  708. dev_err(&vscsi->dev, "Error from Enable Prepare for Suspend: %ld\n",
  709. rc);
  710. vscsi->flags &= PRESERVE_FLAG_FIELDS;
  711. vscsi->rsp_q_timer.timer_pops = 0;
  712. vscsi->debit = 0;
  713. vscsi->credit = 0;
  714. rc = vio_enable_interrupts(vscsi->dma_dev);
  715. if (rc) {
  716. dev_warn(&vscsi->dev, "establish_new_q: failed to enable interrupts, rc %ld\n",
  717. rc);
  718. return rc;
  719. }
  720. rc = ibmvscsis_check_init_msg(vscsi, &format);
  721. if (rc) {
  722. dev_err(&vscsi->dev, "establish_new_q: check_init_msg failed, rc %ld\n",
  723. rc);
  724. return rc;
  725. }
  726. if (format == UNUSED_FORMAT) {
  727. rc = ibmvscsis_send_init_message(vscsi, INIT_MSG);
  728. switch (rc) {
  729. case H_SUCCESS:
  730. case H_DROPPED:
  731. case H_CLOSED:
  732. rc = ADAPT_SUCCESS;
  733. break;
  734. case H_PARAMETER:
  735. case H_HARDWARE:
  736. break;
  737. default:
  738. vscsi->state = UNDEFINED;
  739. rc = H_HARDWARE;
  740. break;
  741. }
  742. } else if (format == INIT_MSG) {
  743. rc = ibmvscsis_handle_init_msg(vscsi);
  744. }
  745. return rc;
  746. }
  747. /**
  748. * ibmvscsis_reset_queue() - Reset CRQ Queue
  749. * @vscsi: Pointer to our adapter structure
  750. *
  751. * This function calls h_free_q and then calls h_reg_q and does all
  752. * of the bookkeeping to get us back to where we can communicate.
  753. *
  754. * Actually, we don't always call h_free_crq. A problem was discovered
  755. * where one partition would close and reopen his queue, which would
  756. * cause his partner to get a transport event, which would cause him to
  757. * close and reopen his queue, which would cause the original partition
  758. * to get a transport event, etc., etc. To prevent this, we don't
  759. * actually close our queue if the client initiated the reset, (i.e.
  760. * either we got a transport event or we have detected that the client's
  761. * queue is gone)
  762. *
  763. * EXECUTION ENVIRONMENT:
  764. * Process environment, called with interrupt lock held
  765. */
  766. static void ibmvscsis_reset_queue(struct scsi_info *vscsi)
  767. {
  768. int bytes;
  769. long rc = ADAPT_SUCCESS;
  770. dev_dbg(&vscsi->dev, "reset_queue: flags 0x%x\n", vscsi->flags);
  771. /* don't reset, the client did it for us */
  772. if (vscsi->flags & (CLIENT_FAILED | TRANS_EVENT)) {
  773. vscsi->flags &= PRESERVE_FLAG_FIELDS;
  774. vscsi->rsp_q_timer.timer_pops = 0;
  775. vscsi->debit = 0;
  776. vscsi->credit = 0;
  777. vscsi->state = WAIT_CONNECTION;
  778. vio_enable_interrupts(vscsi->dma_dev);
  779. } else {
  780. rc = ibmvscsis_free_command_q(vscsi);
  781. if (rc == ADAPT_SUCCESS) {
  782. vscsi->state = WAIT_CONNECTION;
  783. bytes = vscsi->cmd_q.size * PAGE_SIZE;
  784. rc = h_reg_crq(vscsi->dds.unit_id,
  785. vscsi->cmd_q.crq_token, bytes);
  786. if (rc == H_CLOSED || rc == H_SUCCESS) {
  787. rc = ibmvscsis_establish_new_q(vscsi);
  788. }
  789. if (rc != ADAPT_SUCCESS) {
  790. dev_dbg(&vscsi->dev, "reset_queue: reg_crq rc %ld\n",
  791. rc);
  792. vscsi->state = ERR_DISCONNECTED;
  793. vscsi->flags |= RESPONSE_Q_DOWN;
  794. ibmvscsis_free_command_q(vscsi);
  795. }
  796. } else {
  797. vscsi->state = ERR_DISCONNECTED;
  798. vscsi->flags |= RESPONSE_Q_DOWN;
  799. }
  800. }
  801. }
  802. /**
  803. * ibmvscsis_free_cmd_resources() - Free command resources
  804. * @vscsi: Pointer to our adapter structure
  805. * @cmd: Command which is not longer in use
  806. *
  807. * Must be called with interrupt lock held.
  808. */
  809. static void ibmvscsis_free_cmd_resources(struct scsi_info *vscsi,
  810. struct ibmvscsis_cmd *cmd)
  811. {
  812. struct iu_entry *iue = cmd->iue;
  813. switch (cmd->type) {
  814. case TASK_MANAGEMENT:
  815. case SCSI_CDB:
  816. /*
  817. * When the queue goes down this value is cleared, so it
  818. * cannot be cleared in this general purpose function.
  819. */
  820. if (vscsi->debit)
  821. vscsi->debit -= 1;
  822. break;
  823. case ADAPTER_MAD:
  824. vscsi->flags &= ~PROCESSING_MAD;
  825. break;
  826. case UNSET_TYPE:
  827. break;
  828. default:
  829. dev_err(&vscsi->dev, "free_cmd_resources unknown type %d\n",
  830. cmd->type);
  831. break;
  832. }
  833. cmd->iue = NULL;
  834. list_add_tail(&cmd->list, &vscsi->free_cmd);
  835. srp_iu_put(iue);
  836. if (list_empty(&vscsi->active_q) && list_empty(&vscsi->schedule_q) &&
  837. list_empty(&vscsi->waiting_rsp) && (vscsi->flags & WAIT_FOR_IDLE)) {
  838. vscsi->flags &= ~WAIT_FOR_IDLE;
  839. complete(&vscsi->wait_idle);
  840. }
  841. }
  842. /**
  843. * ibmvscsis_ready_for_suspend() - Helper function to call VIOCTL
  844. * @vscsi: Pointer to our adapter structure
  845. * @idle: Indicates whether we were called from adapter_idle. This
  846. * is important to know if we need to do a disconnect, since if
  847. * we're called from adapter_idle, we're still processing the
  848. * current disconnect, so we can't just call post_disconnect.
  849. *
  850. * This function is called when the adapter is idle when phyp has sent
  851. * us a Prepare for Suspend Transport Event.
  852. *
  853. * EXECUTION ENVIRONMENT:
  854. * Process or interrupt environment called with interrupt lock held
  855. */
  856. static long ibmvscsis_ready_for_suspend(struct scsi_info *vscsi, bool idle)
  857. {
  858. long rc = 0;
  859. struct viosrp_crq *crq;
  860. /* See if there is a Resume event in the queue */
  861. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  862. dev_dbg(&vscsi->dev, "ready_suspend: flags 0x%x, state 0x%hx crq_valid:%x\n",
  863. vscsi->flags, vscsi->state, (int)crq->valid);
  864. if (!(vscsi->flags & PREP_FOR_SUSPEND_ABORTED) && !(crq->valid)) {
  865. rc = h_vioctl(vscsi->dds.unit_id, H_READY_FOR_SUSPEND, 0, 0, 0,
  866. 0, 0);
  867. if (rc) {
  868. dev_err(&vscsi->dev, "Ready for Suspend Vioctl failed: %ld\n",
  869. rc);
  870. rc = 0;
  871. }
  872. } else if (((vscsi->flags & PREP_FOR_SUSPEND_OVERWRITE) &&
  873. (vscsi->flags & PREP_FOR_SUSPEND_ABORTED)) ||
  874. ((crq->valid) && ((crq->valid != VALID_TRANS_EVENT) ||
  875. (crq->format != RESUME_FROM_SUSP)))) {
  876. if (idle) {
  877. vscsi->state = ERR_DISCONNECT_RECONNECT;
  878. ibmvscsis_reset_queue(vscsi);
  879. rc = -1;
  880. } else if (vscsi->state == CONNECTED) {
  881. ibmvscsis_post_disconnect(vscsi,
  882. ERR_DISCONNECT_RECONNECT, 0);
  883. }
  884. vscsi->flags &= ~PREP_FOR_SUSPEND_OVERWRITE;
  885. if ((crq->valid) && ((crq->valid != VALID_TRANS_EVENT) ||
  886. (crq->format != RESUME_FROM_SUSP)))
  887. dev_err(&vscsi->dev, "Invalid element in CRQ after Prepare for Suspend");
  888. }
  889. vscsi->flags &= ~(PREP_FOR_SUSPEND_PENDING | PREP_FOR_SUSPEND_ABORTED);
  890. return rc;
  891. }
  892. /**
  893. * ibmvscsis_trans_event() - Handle a Transport Event
  894. * @vscsi: Pointer to our adapter structure
  895. * @crq: Pointer to CRQ entry containing the Transport Event
  896. *
  897. * Do the logic to close the I_T nexus. This function may not
  898. * behave to specification.
  899. *
  900. * EXECUTION ENVIRONMENT:
  901. * Interrupt, interrupt lock held
  902. */
  903. static long ibmvscsis_trans_event(struct scsi_info *vscsi,
  904. struct viosrp_crq *crq)
  905. {
  906. long rc = ADAPT_SUCCESS;
  907. dev_dbg(&vscsi->dev, "trans_event: format %d, flags 0x%x, state 0x%hx\n",
  908. (int)crq->format, vscsi->flags, vscsi->state);
  909. switch (crq->format) {
  910. case MIGRATED:
  911. case PARTNER_FAILED:
  912. case PARTNER_DEREGISTER:
  913. ibmvscsis_delete_client_info(vscsi, true);
  914. if (crq->format == MIGRATED)
  915. vscsi->flags &= ~PREP_FOR_SUSPEND_OVERWRITE;
  916. switch (vscsi->state) {
  917. case NO_QUEUE:
  918. case ERR_DISCONNECTED:
  919. case UNDEFINED:
  920. break;
  921. case UNCONFIGURING:
  922. vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
  923. break;
  924. case WAIT_ENABLED:
  925. break;
  926. case WAIT_CONNECTION:
  927. break;
  928. case CONNECTED:
  929. ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
  930. (RESPONSE_Q_DOWN |
  931. TRANS_EVENT));
  932. break;
  933. case SRP_PROCESSING:
  934. if ((vscsi->debit > 0) ||
  935. !list_empty(&vscsi->schedule_q) ||
  936. !list_empty(&vscsi->waiting_rsp) ||
  937. !list_empty(&vscsi->active_q)) {
  938. dev_dbg(&vscsi->dev, "debit %d, sched %d, wait %d, active %d\n",
  939. vscsi->debit,
  940. (int)list_empty(&vscsi->schedule_q),
  941. (int)list_empty(&vscsi->waiting_rsp),
  942. (int)list_empty(&vscsi->active_q));
  943. dev_warn(&vscsi->dev, "connection lost with outstanding work\n");
  944. } else {
  945. dev_dbg(&vscsi->dev, "trans_event: SRP Processing, but no outstanding work\n");
  946. }
  947. ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
  948. (RESPONSE_Q_DOWN |
  949. TRANS_EVENT));
  950. break;
  951. case ERR_DISCONNECT:
  952. case ERR_DISCONNECT_RECONNECT:
  953. case WAIT_IDLE:
  954. vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
  955. break;
  956. }
  957. break;
  958. case PREPARE_FOR_SUSPEND:
  959. dev_dbg(&vscsi->dev, "Prep for Suspend, crq status = 0x%x\n",
  960. (int)crq->status);
  961. switch (vscsi->state) {
  962. case ERR_DISCONNECTED:
  963. case WAIT_CONNECTION:
  964. case CONNECTED:
  965. ibmvscsis_ready_for_suspend(vscsi, false);
  966. break;
  967. case SRP_PROCESSING:
  968. vscsi->resume_state = vscsi->state;
  969. vscsi->flags |= PREP_FOR_SUSPEND_PENDING;
  970. if (crq->status == CRQ_ENTRY_OVERWRITTEN)
  971. vscsi->flags |= PREP_FOR_SUSPEND_OVERWRITE;
  972. ibmvscsis_post_disconnect(vscsi, WAIT_IDLE, 0);
  973. break;
  974. case NO_QUEUE:
  975. case UNDEFINED:
  976. case UNCONFIGURING:
  977. case WAIT_ENABLED:
  978. case ERR_DISCONNECT:
  979. case ERR_DISCONNECT_RECONNECT:
  980. case WAIT_IDLE:
  981. dev_err(&vscsi->dev, "Invalid state for Prepare for Suspend Trans Event: 0x%x\n",
  982. vscsi->state);
  983. break;
  984. }
  985. break;
  986. case RESUME_FROM_SUSP:
  987. dev_dbg(&vscsi->dev, "Resume from Suspend, crq status = 0x%x\n",
  988. (int)crq->status);
  989. if (vscsi->flags & PREP_FOR_SUSPEND_PENDING) {
  990. vscsi->flags |= PREP_FOR_SUSPEND_ABORTED;
  991. } else {
  992. if ((crq->status == CRQ_ENTRY_OVERWRITTEN) ||
  993. (vscsi->flags & PREP_FOR_SUSPEND_OVERWRITE)) {
  994. ibmvscsis_post_disconnect(vscsi,
  995. ERR_DISCONNECT_RECONNECT,
  996. 0);
  997. vscsi->flags &= ~PREP_FOR_SUSPEND_OVERWRITE;
  998. }
  999. }
  1000. break;
  1001. default:
  1002. rc = ERROR;
  1003. dev_err(&vscsi->dev, "trans_event: invalid format %d\n",
  1004. (uint)crq->format);
  1005. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT,
  1006. RESPONSE_Q_DOWN);
  1007. break;
  1008. }
  1009. rc = vscsi->flags & SCHEDULE_DISCONNECT;
  1010. dev_dbg(&vscsi->dev, "Leaving trans_event: flags 0x%x, state 0x%hx, rc %ld\n",
  1011. vscsi->flags, vscsi->state, rc);
  1012. return rc;
  1013. }
  1014. /**
  1015. * ibmvscsis_poll_cmd_q() - Poll Command Queue
  1016. * @vscsi: Pointer to our adapter structure
  1017. *
  1018. * Called to handle command elements that may have arrived while
  1019. * interrupts were disabled.
  1020. *
  1021. * EXECUTION ENVIRONMENT:
  1022. * intr_lock must be held
  1023. */
  1024. static void ibmvscsis_poll_cmd_q(struct scsi_info *vscsi)
  1025. {
  1026. struct viosrp_crq *crq;
  1027. long rc;
  1028. bool ack = true;
  1029. volatile u8 valid;
  1030. dev_dbg(&vscsi->dev, "poll_cmd_q: flags 0x%x, state 0x%hx, q index %ud\n",
  1031. vscsi->flags, vscsi->state, vscsi->cmd_q.index);
  1032. rc = vscsi->flags & SCHEDULE_DISCONNECT;
  1033. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  1034. valid = crq->valid;
  1035. dma_rmb();
  1036. while (valid) {
  1037. poll_work:
  1038. vscsi->cmd_q.index =
  1039. (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
  1040. if (!rc) {
  1041. rc = ibmvscsis_parse_command(vscsi, crq);
  1042. } else {
  1043. if ((uint)crq->valid == VALID_TRANS_EVENT) {
  1044. /*
  1045. * must service the transport layer events even
  1046. * in an error state, dont break out until all
  1047. * the consecutive transport events have been
  1048. * processed
  1049. */
  1050. rc = ibmvscsis_trans_event(vscsi, crq);
  1051. } else if (vscsi->flags & TRANS_EVENT) {
  1052. /*
  1053. * if a tranport event has occurred leave
  1054. * everything but transport events on the queue
  1055. */
  1056. dev_dbg(&vscsi->dev, "poll_cmd_q, ignoring\n");
  1057. /*
  1058. * need to decrement the queue index so we can
  1059. * look at the elment again
  1060. */
  1061. if (vscsi->cmd_q.index)
  1062. vscsi->cmd_q.index -= 1;
  1063. else
  1064. /*
  1065. * index is at 0 it just wrapped.
  1066. * have it index last element in q
  1067. */
  1068. vscsi->cmd_q.index = vscsi->cmd_q.mask;
  1069. break;
  1070. }
  1071. }
  1072. crq->valid = INVALIDATE_CMD_RESP_EL;
  1073. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  1074. valid = crq->valid;
  1075. dma_rmb();
  1076. }
  1077. if (!rc) {
  1078. if (ack) {
  1079. vio_enable_interrupts(vscsi->dma_dev);
  1080. ack = false;
  1081. dev_dbg(&vscsi->dev, "poll_cmd_q, reenabling interrupts\n");
  1082. }
  1083. valid = crq->valid;
  1084. dma_rmb();
  1085. if (valid)
  1086. goto poll_work;
  1087. }
  1088. dev_dbg(&vscsi->dev, "Leaving poll_cmd_q: rc %ld\n", rc);
  1089. }
  1090. /**
  1091. * ibmvscsis_free_cmd_qs() - Free elements in queue
  1092. * @vscsi: Pointer to our adapter structure
  1093. *
  1094. * Free all of the elements on all queues that are waiting for
  1095. * whatever reason.
  1096. *
  1097. * PRECONDITION:
  1098. * Called with interrupt lock held
  1099. */
  1100. static void ibmvscsis_free_cmd_qs(struct scsi_info *vscsi)
  1101. {
  1102. struct ibmvscsis_cmd *cmd, *nxt;
  1103. dev_dbg(&vscsi->dev, "free_cmd_qs: waiting_rsp empty %d, timer starter %d\n",
  1104. (int)list_empty(&vscsi->waiting_rsp),
  1105. vscsi->rsp_q_timer.started);
  1106. list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
  1107. list_del(&cmd->list);
  1108. ibmvscsis_free_cmd_resources(vscsi, cmd);
  1109. }
  1110. }
  1111. /**
  1112. * ibmvscsis_get_free_cmd() - Get free command from list
  1113. * @vscsi: Pointer to our adapter structure
  1114. *
  1115. * Must be called with interrupt lock held.
  1116. */
  1117. static struct ibmvscsis_cmd *ibmvscsis_get_free_cmd(struct scsi_info *vscsi)
  1118. {
  1119. struct ibmvscsis_cmd *cmd = NULL;
  1120. struct iu_entry *iue;
  1121. iue = srp_iu_get(&vscsi->target);
  1122. if (iue) {
  1123. cmd = list_first_entry_or_null(&vscsi->free_cmd,
  1124. struct ibmvscsis_cmd, list);
  1125. if (cmd) {
  1126. if (cmd->abort_cmd)
  1127. cmd->abort_cmd = NULL;
  1128. cmd->flags &= ~(DELAY_SEND);
  1129. list_del(&cmd->list);
  1130. cmd->iue = iue;
  1131. cmd->type = UNSET_TYPE;
  1132. memset(&cmd->se_cmd, 0, sizeof(cmd->se_cmd));
  1133. } else {
  1134. srp_iu_put(iue);
  1135. }
  1136. }
  1137. return cmd;
  1138. }
  1139. /**
  1140. * ibmvscsis_adapter_idle() - Helper function to handle idle adapter
  1141. * @vscsi: Pointer to our adapter structure
  1142. *
  1143. * This function is called when the adapter is idle when the driver
  1144. * is attempting to clear an error condition.
  1145. * The adapter is considered busy if any of its cmd queues
  1146. * are non-empty. This function can be invoked
  1147. * from the off level disconnect function.
  1148. *
  1149. * EXECUTION ENVIRONMENT:
  1150. * Process environment called with interrupt lock held
  1151. */
  1152. static void ibmvscsis_adapter_idle(struct scsi_info *vscsi)
  1153. {
  1154. int free_qs = false;
  1155. long rc = 0;
  1156. dev_dbg(&vscsi->dev, "adapter_idle: flags 0x%x, state 0x%hx\n",
  1157. vscsi->flags, vscsi->state);
  1158. /* Only need to free qs if we're disconnecting from client */
  1159. if (vscsi->state != WAIT_CONNECTION || vscsi->flags & TRANS_EVENT)
  1160. free_qs = true;
  1161. switch (vscsi->state) {
  1162. case UNCONFIGURING:
  1163. ibmvscsis_free_command_q(vscsi);
  1164. dma_rmb();
  1165. isync();
  1166. if (vscsi->flags & CFG_SLEEPING) {
  1167. vscsi->flags &= ~CFG_SLEEPING;
  1168. complete(&vscsi->unconfig);
  1169. }
  1170. break;
  1171. case ERR_DISCONNECT_RECONNECT:
  1172. ibmvscsis_reset_queue(vscsi);
  1173. dev_dbg(&vscsi->dev, "adapter_idle, disc_rec: flags 0x%x\n",
  1174. vscsi->flags);
  1175. break;
  1176. case ERR_DISCONNECT:
  1177. ibmvscsis_free_command_q(vscsi);
  1178. vscsi->flags &= ~(SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED);
  1179. vscsi->flags |= RESPONSE_Q_DOWN;
  1180. if (vscsi->tport.enabled)
  1181. vscsi->state = ERR_DISCONNECTED;
  1182. else
  1183. vscsi->state = WAIT_ENABLED;
  1184. dev_dbg(&vscsi->dev, "adapter_idle, disc: flags 0x%x, state 0x%hx\n",
  1185. vscsi->flags, vscsi->state);
  1186. break;
  1187. case WAIT_IDLE:
  1188. vscsi->rsp_q_timer.timer_pops = 0;
  1189. vscsi->debit = 0;
  1190. vscsi->credit = 0;
  1191. if (vscsi->flags & PREP_FOR_SUSPEND_PENDING) {
  1192. vscsi->state = vscsi->resume_state;
  1193. vscsi->resume_state = 0;
  1194. rc = ibmvscsis_ready_for_suspend(vscsi, true);
  1195. vscsi->flags &= ~DISCONNECT_SCHEDULED;
  1196. if (rc)
  1197. break;
  1198. } else if (vscsi->flags & TRANS_EVENT) {
  1199. vscsi->state = WAIT_CONNECTION;
  1200. vscsi->flags &= PRESERVE_FLAG_FIELDS;
  1201. } else {
  1202. vscsi->state = CONNECTED;
  1203. vscsi->flags &= ~DISCONNECT_SCHEDULED;
  1204. }
  1205. dev_dbg(&vscsi->dev, "adapter_idle, wait: flags 0x%x, state 0x%hx\n",
  1206. vscsi->flags, vscsi->state);
  1207. ibmvscsis_poll_cmd_q(vscsi);
  1208. break;
  1209. case ERR_DISCONNECTED:
  1210. vscsi->flags &= ~DISCONNECT_SCHEDULED;
  1211. dev_dbg(&vscsi->dev, "adapter_idle, disconnected: flags 0x%x, state 0x%hx\n",
  1212. vscsi->flags, vscsi->state);
  1213. break;
  1214. default:
  1215. dev_err(&vscsi->dev, "adapter_idle: in invalid state %d\n",
  1216. vscsi->state);
  1217. break;
  1218. }
  1219. if (free_qs)
  1220. ibmvscsis_free_cmd_qs(vscsi);
  1221. /*
  1222. * There is a timing window where we could lose a disconnect request.
  1223. * The known path to this window occurs during the DISCONNECT_RECONNECT
  1224. * case above: reset_queue calls free_command_q, which will release the
  1225. * interrupt lock. During that time, a new post_disconnect call can be
  1226. * made with a "more severe" state (DISCONNECT or UNCONFIGURING).
  1227. * Because the DISCONNECT_SCHEDULED flag is already set, post_disconnect
  1228. * will only set the new_state. Now free_command_q reacquires the intr
  1229. * lock and clears the DISCONNECT_SCHEDULED flag (using PRESERVE_FLAG_
  1230. * FIELDS), and the disconnect is lost. This is particularly bad when
  1231. * the new disconnect was for UNCONFIGURING, since the unconfigure hangs
  1232. * forever.
  1233. * Fix is that free command queue sets acr state and acr flags if there
  1234. * is a change under the lock
  1235. * note free command queue writes to this state it clears it
  1236. * before releasing the lock, different drivers call the free command
  1237. * queue different times so dont initialize above
  1238. */
  1239. if (vscsi->phyp_acr_state != 0) {
  1240. /*
  1241. * set any bits in flags that may have been cleared by
  1242. * a call to free command queue in switch statement
  1243. * or reset queue
  1244. */
  1245. vscsi->flags |= vscsi->phyp_acr_flags;
  1246. ibmvscsis_post_disconnect(vscsi, vscsi->phyp_acr_state, 0);
  1247. vscsi->phyp_acr_state = 0;
  1248. vscsi->phyp_acr_flags = 0;
  1249. dev_dbg(&vscsi->dev, "adapter_idle: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
  1250. vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
  1251. vscsi->phyp_acr_state);
  1252. }
  1253. dev_dbg(&vscsi->dev, "Leaving adapter_idle: flags 0x%x, state 0x%hx, new_state 0x%x\n",
  1254. vscsi->flags, vscsi->state, vscsi->new_state);
  1255. }
  1256. /**
  1257. * ibmvscsis_copy_crq_packet() - Copy CRQ Packet
  1258. * @vscsi: Pointer to our adapter structure
  1259. * @cmd: Pointer to command element to use to process the request
  1260. * @crq: Pointer to CRQ entry containing the request
  1261. *
  1262. * Copy the srp information unit from the hosted
  1263. * partition using remote dma
  1264. *
  1265. * EXECUTION ENVIRONMENT:
  1266. * Interrupt, interrupt lock held
  1267. */
  1268. static long ibmvscsis_copy_crq_packet(struct scsi_info *vscsi,
  1269. struct ibmvscsis_cmd *cmd,
  1270. struct viosrp_crq *crq)
  1271. {
  1272. struct iu_entry *iue = cmd->iue;
  1273. long rc = 0;
  1274. u16 len;
  1275. len = be16_to_cpu(crq->IU_length);
  1276. if ((len > SRP_MAX_IU_LEN) || (len == 0)) {
  1277. dev_err(&vscsi->dev, "copy_crq: Invalid len %d passed", len);
  1278. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1279. return SRP_VIOLATION;
  1280. }
  1281. rc = h_copy_rdma(len, vscsi->dds.window[REMOTE].liobn,
  1282. be64_to_cpu(crq->IU_data_ptr),
  1283. vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma);
  1284. switch (rc) {
  1285. case H_SUCCESS:
  1286. cmd->init_time = mftb();
  1287. iue->remote_token = crq->IU_data_ptr;
  1288. iue->iu_len = len;
  1289. dev_dbg(&vscsi->dev, "copy_crq: ioba 0x%llx, init_time 0x%llx\n",
  1290. be64_to_cpu(crq->IU_data_ptr), cmd->init_time);
  1291. break;
  1292. case H_PERMISSION:
  1293. if (connection_broken(vscsi))
  1294. ibmvscsis_post_disconnect(vscsi,
  1295. ERR_DISCONNECT_RECONNECT,
  1296. (RESPONSE_Q_DOWN |
  1297. CLIENT_FAILED));
  1298. else
  1299. ibmvscsis_post_disconnect(vscsi,
  1300. ERR_DISCONNECT_RECONNECT, 0);
  1301. dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
  1302. rc);
  1303. break;
  1304. case H_DEST_PARM:
  1305. case H_SOURCE_PARM:
  1306. default:
  1307. dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
  1308. rc);
  1309. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1310. break;
  1311. }
  1312. return rc;
  1313. }
  1314. /**
  1315. * ibmvscsis_adapter_info - Service an Adapter Info MAnagement Data gram
  1316. * @vscsi: Pointer to our adapter structure
  1317. * @iue: Information Unit containing the Adapter Info MAD request
  1318. *
  1319. * EXECUTION ENVIRONMENT:
  1320. * Interrupt adapter lock is held
  1321. */
  1322. static long ibmvscsis_adapter_info(struct scsi_info *vscsi,
  1323. struct iu_entry *iue)
  1324. {
  1325. struct viosrp_adapter_info *mad = &vio_iu(iue)->mad.adapter_info;
  1326. struct mad_adapter_info_data *info;
  1327. uint flag_bits = 0;
  1328. dma_addr_t token;
  1329. long rc;
  1330. mad->common.status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
  1331. if (be16_to_cpu(mad->common.length) > sizeof(*info)) {
  1332. mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1333. return 0;
  1334. }
  1335. info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
  1336. GFP_ATOMIC);
  1337. if (!info) {
  1338. dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
  1339. iue->target);
  1340. mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1341. return 0;
  1342. }
  1343. /* Get remote info */
  1344. rc = h_copy_rdma(be16_to_cpu(mad->common.length),
  1345. vscsi->dds.window[REMOTE].liobn,
  1346. be64_to_cpu(mad->buffer),
  1347. vscsi->dds.window[LOCAL].liobn, token);
  1348. if (rc != H_SUCCESS) {
  1349. if (rc == H_PERMISSION) {
  1350. if (connection_broken(vscsi))
  1351. flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
  1352. }
  1353. dev_warn(&vscsi->dev, "adapter_info: h_copy_rdma from client failed, rc %ld\n",
  1354. rc);
  1355. dev_dbg(&vscsi->dev, "adapter_info: ioba 0x%llx, flags 0x%x, flag_bits 0x%x\n",
  1356. be64_to_cpu(mad->buffer), vscsi->flags, flag_bits);
  1357. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
  1358. flag_bits);
  1359. goto free_dma;
  1360. }
  1361. /*
  1362. * Copy client info, but ignore partition number, which we
  1363. * already got from phyp - unless we failed to get it from
  1364. * phyp (e.g. if we're running on a p5 system).
  1365. */
  1366. if (vscsi->client_data.partition_number == 0)
  1367. vscsi->client_data.partition_number =
  1368. be32_to_cpu(info->partition_number);
  1369. strncpy(vscsi->client_data.srp_version, info->srp_version,
  1370. sizeof(vscsi->client_data.srp_version));
  1371. strncpy(vscsi->client_data.partition_name, info->partition_name,
  1372. sizeof(vscsi->client_data.partition_name));
  1373. vscsi->client_data.mad_version = be32_to_cpu(info->mad_version);
  1374. vscsi->client_data.os_type = be32_to_cpu(info->os_type);
  1375. /* Copy our info */
  1376. strncpy(info->srp_version, SRP_VERSION,
  1377. sizeof(info->srp_version));
  1378. strncpy(info->partition_name, vscsi->dds.partition_name,
  1379. sizeof(info->partition_name));
  1380. info->partition_number = cpu_to_be32(vscsi->dds.partition_num);
  1381. info->mad_version = cpu_to_be32(MAD_VERSION_1);
  1382. info->os_type = cpu_to_be32(LINUX);
  1383. memset(&info->port_max_txu[0], 0, sizeof(info->port_max_txu));
  1384. info->port_max_txu[0] = cpu_to_be32(MAX_TXU);
  1385. dma_wmb();
  1386. rc = h_copy_rdma(sizeof(*info), vscsi->dds.window[LOCAL].liobn,
  1387. token, vscsi->dds.window[REMOTE].liobn,
  1388. be64_to_cpu(mad->buffer));
  1389. switch (rc) {
  1390. case H_SUCCESS:
  1391. break;
  1392. case H_SOURCE_PARM:
  1393. case H_DEST_PARM:
  1394. case H_PERMISSION:
  1395. if (connection_broken(vscsi))
  1396. flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
  1397. fallthrough;
  1398. default:
  1399. dev_err(&vscsi->dev, "adapter_info: h_copy_rdma to client failed, rc %ld\n",
  1400. rc);
  1401. ibmvscsis_post_disconnect(vscsi,
  1402. ERR_DISCONNECT_RECONNECT,
  1403. flag_bits);
  1404. break;
  1405. }
  1406. free_dma:
  1407. dma_free_coherent(&vscsi->dma_dev->dev, sizeof(*info), info, token);
  1408. dev_dbg(&vscsi->dev, "Leaving adapter_info, rc %ld\n", rc);
  1409. return rc;
  1410. }
  1411. /**
  1412. * ibmvscsis_cap_mad() - Service a Capabilities MAnagement Data gram
  1413. * @vscsi: Pointer to our adapter structure
  1414. * @iue: Information Unit containing the Capabilities MAD request
  1415. *
  1416. * NOTE: if you return an error from this routine you must be
  1417. * disconnecting or you will cause a hang
  1418. *
  1419. * EXECUTION ENVIRONMENT:
  1420. * Interrupt called with adapter lock held
  1421. */
  1422. static int ibmvscsis_cap_mad(struct scsi_info *vscsi, struct iu_entry *iue)
  1423. {
  1424. struct viosrp_capabilities *mad = &vio_iu(iue)->mad.capabilities;
  1425. struct capabilities *cap;
  1426. struct mad_capability_common *common;
  1427. dma_addr_t token;
  1428. u16 olen, len, status, min_len, cap_len;
  1429. u32 flag;
  1430. uint flag_bits = 0;
  1431. long rc = 0;
  1432. olen = be16_to_cpu(mad->common.length);
  1433. /*
  1434. * struct capabilities hardcodes a couple capabilities after the
  1435. * header, but the capabilities can actually be in any order.
  1436. */
  1437. min_len = offsetof(struct capabilities, migration);
  1438. if ((olen < min_len) || (olen > PAGE_SIZE)) {
  1439. dev_warn(&vscsi->dev, "cap_mad: invalid len %d\n", olen);
  1440. mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1441. return 0;
  1442. }
  1443. cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
  1444. GFP_ATOMIC);
  1445. if (!cap) {
  1446. dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
  1447. iue->target);
  1448. mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1449. return 0;
  1450. }
  1451. rc = h_copy_rdma(olen, vscsi->dds.window[REMOTE].liobn,
  1452. be64_to_cpu(mad->buffer),
  1453. vscsi->dds.window[LOCAL].liobn, token);
  1454. if (rc == H_SUCCESS) {
  1455. strncpy(cap->name, dev_name(&vscsi->dma_dev->dev),
  1456. SRP_MAX_LOC_LEN);
  1457. len = olen - min_len;
  1458. status = VIOSRP_MAD_SUCCESS;
  1459. common = (struct mad_capability_common *)&cap->migration;
  1460. while ((len > 0) && (status == VIOSRP_MAD_SUCCESS) && !rc) {
  1461. dev_dbg(&vscsi->dev, "cap_mad: len left %hd, cap type %d, cap len %hd\n",
  1462. len, be32_to_cpu(common->cap_type),
  1463. be16_to_cpu(common->length));
  1464. cap_len = be16_to_cpu(common->length);
  1465. if (cap_len > len) {
  1466. dev_err(&vscsi->dev, "cap_mad: cap len mismatch with total len\n");
  1467. status = VIOSRP_MAD_FAILED;
  1468. break;
  1469. }
  1470. if (cap_len == 0) {
  1471. dev_err(&vscsi->dev, "cap_mad: cap len is 0\n");
  1472. status = VIOSRP_MAD_FAILED;
  1473. break;
  1474. }
  1475. switch (common->cap_type) {
  1476. default:
  1477. dev_dbg(&vscsi->dev, "cap_mad: unsupported capability\n");
  1478. common->server_support = 0;
  1479. flag = cpu_to_be32((u32)CAP_LIST_SUPPORTED);
  1480. cap->flags &= ~flag;
  1481. break;
  1482. }
  1483. len = len - cap_len;
  1484. common = (struct mad_capability_common *)
  1485. ((char *)common + cap_len);
  1486. }
  1487. mad->common.status = cpu_to_be16(status);
  1488. dma_wmb();
  1489. rc = h_copy_rdma(olen, vscsi->dds.window[LOCAL].liobn, token,
  1490. vscsi->dds.window[REMOTE].liobn,
  1491. be64_to_cpu(mad->buffer));
  1492. if (rc != H_SUCCESS) {
  1493. dev_dbg(&vscsi->dev, "cap_mad: failed to copy to client, rc %ld\n",
  1494. rc);
  1495. if (rc == H_PERMISSION) {
  1496. if (connection_broken(vscsi))
  1497. flag_bits = (RESPONSE_Q_DOWN |
  1498. CLIENT_FAILED);
  1499. }
  1500. dev_warn(&vscsi->dev, "cap_mad: error copying data to client, rc %ld\n",
  1501. rc);
  1502. ibmvscsis_post_disconnect(vscsi,
  1503. ERR_DISCONNECT_RECONNECT,
  1504. flag_bits);
  1505. }
  1506. }
  1507. dma_free_coherent(&vscsi->dma_dev->dev, olen, cap, token);
  1508. dev_dbg(&vscsi->dev, "Leaving cap_mad, rc %ld, client_cap 0x%x\n",
  1509. rc, vscsi->client_cap);
  1510. return rc;
  1511. }
  1512. /**
  1513. * ibmvscsis_process_mad() - Service a MAnagement Data gram
  1514. * @vscsi: Pointer to our adapter structure
  1515. * @iue: Information Unit containing the MAD request
  1516. *
  1517. * Must be called with interrupt lock held.
  1518. */
  1519. static long ibmvscsis_process_mad(struct scsi_info *vscsi, struct iu_entry *iue)
  1520. {
  1521. struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
  1522. struct viosrp_empty_iu *empty;
  1523. long rc = ADAPT_SUCCESS;
  1524. switch (be32_to_cpu(mad->type)) {
  1525. case VIOSRP_EMPTY_IU_TYPE:
  1526. empty = &vio_iu(iue)->mad.empty_iu;
  1527. vscsi->empty_iu_id = be64_to_cpu(empty->buffer);
  1528. vscsi->empty_iu_tag = be64_to_cpu(empty->common.tag);
  1529. mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
  1530. break;
  1531. case VIOSRP_ADAPTER_INFO_TYPE:
  1532. rc = ibmvscsis_adapter_info(vscsi, iue);
  1533. break;
  1534. case VIOSRP_CAPABILITIES_TYPE:
  1535. rc = ibmvscsis_cap_mad(vscsi, iue);
  1536. break;
  1537. case VIOSRP_ENABLE_FAST_FAIL:
  1538. if (vscsi->state == CONNECTED) {
  1539. vscsi->fast_fail = true;
  1540. mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
  1541. } else {
  1542. dev_warn(&vscsi->dev, "fast fail mad sent after login\n");
  1543. mad->status = cpu_to_be16(VIOSRP_MAD_FAILED);
  1544. }
  1545. break;
  1546. default:
  1547. mad->status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
  1548. break;
  1549. }
  1550. return rc;
  1551. }
  1552. /**
  1553. * srp_snd_msg_failed() - Handle an error when sending a response
  1554. * @vscsi: Pointer to our adapter structure
  1555. * @rc: The return code from the h_send_crq command
  1556. *
  1557. * Must be called with interrupt lock held.
  1558. */
  1559. static void srp_snd_msg_failed(struct scsi_info *vscsi, long rc)
  1560. {
  1561. ktime_t kt;
  1562. if (rc != H_DROPPED) {
  1563. ibmvscsis_free_cmd_qs(vscsi);
  1564. if (rc == H_CLOSED)
  1565. vscsi->flags |= CLIENT_FAILED;
  1566. /* don't flag the same problem multiple times */
  1567. if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
  1568. vscsi->flags |= RESPONSE_Q_DOWN;
  1569. if (!(vscsi->state & (ERR_DISCONNECT |
  1570. ERR_DISCONNECT_RECONNECT |
  1571. ERR_DISCONNECTED | UNDEFINED))) {
  1572. dev_err(&vscsi->dev, "snd_msg_failed: setting RESPONSE_Q_DOWN, state 0x%hx, flags 0x%x, rc %ld\n",
  1573. vscsi->state, vscsi->flags, rc);
  1574. }
  1575. ibmvscsis_post_disconnect(vscsi,
  1576. ERR_DISCONNECT_RECONNECT, 0);
  1577. }
  1578. return;
  1579. }
  1580. /*
  1581. * The response queue is full.
  1582. * If the server is processing SRP requests, i.e.
  1583. * the client has successfully done an
  1584. * SRP_LOGIN, then it will wait forever for room in
  1585. * the queue. However if the system admin
  1586. * is attempting to unconfigure the server then one
  1587. * or more children will be in a state where
  1588. * they are being removed. So if there is even one
  1589. * child being removed then the driver assumes
  1590. * the system admin is attempting to break the
  1591. * connection with the client and MAX_TIMER_POPS
  1592. * is honored.
  1593. */
  1594. if ((vscsi->rsp_q_timer.timer_pops < MAX_TIMER_POPS) ||
  1595. (vscsi->state == SRP_PROCESSING)) {
  1596. dev_dbg(&vscsi->dev, "snd_msg_failed: response queue full, flags 0x%x, timer started %d, pops %d\n",
  1597. vscsi->flags, (int)vscsi->rsp_q_timer.started,
  1598. vscsi->rsp_q_timer.timer_pops);
  1599. /*
  1600. * Check if the timer is running; if it
  1601. * is not then start it up.
  1602. */
  1603. if (!vscsi->rsp_q_timer.started) {
  1604. if (vscsi->rsp_q_timer.timer_pops <
  1605. MAX_TIMER_POPS) {
  1606. kt = WAIT_NANO_SECONDS;
  1607. } else {
  1608. /*
  1609. * slide the timeslice if the maximum
  1610. * timer pops have already happened
  1611. */
  1612. kt = ktime_set(WAIT_SECONDS, 0);
  1613. }
  1614. vscsi->rsp_q_timer.started = true;
  1615. hrtimer_start(&vscsi->rsp_q_timer.timer, kt,
  1616. HRTIMER_MODE_REL);
  1617. }
  1618. } else {
  1619. /*
  1620. * TBD: Do we need to worry about this? Need to get
  1621. * remove working.
  1622. */
  1623. /*
  1624. * waited a long time and it appears the system admin
  1625. * is bring this driver down
  1626. */
  1627. vscsi->flags |= RESPONSE_Q_DOWN;
  1628. ibmvscsis_free_cmd_qs(vscsi);
  1629. /*
  1630. * if the driver is already attempting to disconnect
  1631. * from the client and has already logged an error
  1632. * trace this event but don't put it in the error log
  1633. */
  1634. if (!(vscsi->state & (ERR_DISCONNECT |
  1635. ERR_DISCONNECT_RECONNECT |
  1636. ERR_DISCONNECTED | UNDEFINED))) {
  1637. dev_err(&vscsi->dev, "client crq full too long\n");
  1638. ibmvscsis_post_disconnect(vscsi,
  1639. ERR_DISCONNECT_RECONNECT,
  1640. 0);
  1641. }
  1642. }
  1643. }
  1644. /**
  1645. * ibmvscsis_send_messages() - Send a Response
  1646. * @vscsi: Pointer to our adapter structure
  1647. *
  1648. * Send a response, first checking the waiting queue. Responses are
  1649. * sent in order they are received. If the response cannot be sent,
  1650. * because the client queue is full, it stays on the waiting queue.
  1651. *
  1652. * PRECONDITION:
  1653. * Called with interrupt lock held
  1654. */
  1655. static void ibmvscsis_send_messages(struct scsi_info *vscsi)
  1656. {
  1657. struct viosrp_crq empty_crq = { };
  1658. struct viosrp_crq *crq = &empty_crq;
  1659. struct ibmvscsis_cmd *cmd, *nxt;
  1660. long rc = ADAPT_SUCCESS;
  1661. bool retry = false;
  1662. if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
  1663. do {
  1664. retry = false;
  1665. list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp,
  1666. list) {
  1667. /*
  1668. * Check to make sure abort cmd gets processed
  1669. * prior to the abort tmr cmd
  1670. */
  1671. if (cmd->flags & DELAY_SEND)
  1672. continue;
  1673. if (cmd->abort_cmd) {
  1674. retry = true;
  1675. cmd->abort_cmd->flags &= ~(DELAY_SEND);
  1676. cmd->abort_cmd = NULL;
  1677. }
  1678. /*
  1679. * If CMD_T_ABORTED w/o CMD_T_TAS scenarios and
  1680. * the case where LIO issued a
  1681. * ABORT_TASK: Sending TMR_TASK_DOES_NOT_EXIST
  1682. * case then we dont send a response, since it
  1683. * was already done.
  1684. */
  1685. if (cmd->se_cmd.transport_state & CMD_T_ABORTED &&
  1686. !(cmd->se_cmd.transport_state & CMD_T_TAS)) {
  1687. list_del(&cmd->list);
  1688. ibmvscsis_free_cmd_resources(vscsi,
  1689. cmd);
  1690. /*
  1691. * With a successfully aborted op
  1692. * through LIO we want to increment the
  1693. * the vscsi credit so that when we dont
  1694. * send a rsp to the original scsi abort
  1695. * op (h_send_crq), but the tm rsp to
  1696. * the abort is sent, the credit is
  1697. * correctly sent with the abort tm rsp.
  1698. * We would need 1 for the abort tm rsp
  1699. * and 1 credit for the aborted scsi op.
  1700. * Thus we need to increment here.
  1701. * Also we want to increment the credit
  1702. * here because we want to make sure
  1703. * cmd is actually released first
  1704. * otherwise the client will think it
  1705. * it can send a new cmd, and we could
  1706. * find ourselves short of cmd elements.
  1707. */
  1708. vscsi->credit += 1;
  1709. } else {
  1710. crq->valid = VALID_CMD_RESP_EL;
  1711. crq->format = cmd->rsp.format;
  1712. if (cmd->flags & CMD_FAST_FAIL)
  1713. crq->status = VIOSRP_ADAPTER_FAIL;
  1714. crq->IU_length = cpu_to_be16(cmd->rsp.len);
  1715. rc = h_send_crq(vscsi->dma_dev->unit_address,
  1716. be64_to_cpu(crq->high),
  1717. be64_to_cpu(cmd->rsp.tag));
  1718. dev_dbg(&vscsi->dev, "send_messages: cmd %p, tag 0x%llx, rc %ld\n",
  1719. cmd, be64_to_cpu(cmd->rsp.tag),
  1720. rc);
  1721. /* if all ok free up the command
  1722. * element resources
  1723. */
  1724. if (rc == H_SUCCESS) {
  1725. /* some movement has occurred */
  1726. vscsi->rsp_q_timer.timer_pops = 0;
  1727. list_del(&cmd->list);
  1728. ibmvscsis_free_cmd_resources(vscsi,
  1729. cmd);
  1730. } else {
  1731. srp_snd_msg_failed(vscsi, rc);
  1732. break;
  1733. }
  1734. }
  1735. }
  1736. } while (retry);
  1737. if (!rc) {
  1738. /*
  1739. * The timer could pop with the queue empty. If
  1740. * this happens, rc will always indicate a
  1741. * success; clear the pop count.
  1742. */
  1743. vscsi->rsp_q_timer.timer_pops = 0;
  1744. }
  1745. } else {
  1746. ibmvscsis_free_cmd_qs(vscsi);
  1747. }
  1748. }
  1749. /* Called with intr lock held */
  1750. static void ibmvscsis_send_mad_resp(struct scsi_info *vscsi,
  1751. struct ibmvscsis_cmd *cmd,
  1752. struct viosrp_crq *crq)
  1753. {
  1754. struct iu_entry *iue = cmd->iue;
  1755. struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
  1756. uint flag_bits = 0;
  1757. long rc;
  1758. dma_wmb();
  1759. rc = h_copy_rdma(sizeof(struct mad_common),
  1760. vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
  1761. vscsi->dds.window[REMOTE].liobn,
  1762. be64_to_cpu(crq->IU_data_ptr));
  1763. if (!rc) {
  1764. cmd->rsp.format = VIOSRP_MAD_FORMAT;
  1765. cmd->rsp.len = sizeof(struct mad_common);
  1766. cmd->rsp.tag = mad->tag;
  1767. list_add_tail(&cmd->list, &vscsi->waiting_rsp);
  1768. ibmvscsis_send_messages(vscsi);
  1769. } else {
  1770. dev_dbg(&vscsi->dev, "Error sending mad response, rc %ld\n",
  1771. rc);
  1772. if (rc == H_PERMISSION) {
  1773. if (connection_broken(vscsi))
  1774. flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
  1775. }
  1776. dev_err(&vscsi->dev, "mad: failed to copy to client, rc %ld\n",
  1777. rc);
  1778. ibmvscsis_free_cmd_resources(vscsi, cmd);
  1779. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
  1780. flag_bits);
  1781. }
  1782. }
  1783. /**
  1784. * ibmvscsis_mad() - Service a MAnagement Data gram.
  1785. * @vscsi: Pointer to our adapter structure
  1786. * @crq: Pointer to the CRQ entry containing the MAD request
  1787. *
  1788. * EXECUTION ENVIRONMENT:
  1789. * Interrupt, called with adapter lock held
  1790. */
  1791. static long ibmvscsis_mad(struct scsi_info *vscsi, struct viosrp_crq *crq)
  1792. {
  1793. struct iu_entry *iue;
  1794. struct ibmvscsis_cmd *cmd;
  1795. struct mad_common *mad;
  1796. long rc = ADAPT_SUCCESS;
  1797. switch (vscsi->state) {
  1798. /*
  1799. * We have not exchanged Init Msgs yet, so this MAD was sent
  1800. * before the last Transport Event; client will not be
  1801. * expecting a response.
  1802. */
  1803. case WAIT_CONNECTION:
  1804. dev_dbg(&vscsi->dev, "mad: in Wait Connection state, ignoring MAD, flags %d\n",
  1805. vscsi->flags);
  1806. return ADAPT_SUCCESS;
  1807. case SRP_PROCESSING:
  1808. case CONNECTED:
  1809. break;
  1810. /*
  1811. * We should never get here while we're in these states.
  1812. * Just log an error and get out.
  1813. */
  1814. case UNCONFIGURING:
  1815. case WAIT_IDLE:
  1816. case ERR_DISCONNECT:
  1817. case ERR_DISCONNECT_RECONNECT:
  1818. default:
  1819. dev_err(&vscsi->dev, "mad: invalid adapter state %d for mad\n",
  1820. vscsi->state);
  1821. return ADAPT_SUCCESS;
  1822. }
  1823. cmd = ibmvscsis_get_free_cmd(vscsi);
  1824. if (!cmd) {
  1825. dev_err(&vscsi->dev, "mad: failed to get cmd, debit %d\n",
  1826. vscsi->debit);
  1827. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1828. return ERROR;
  1829. }
  1830. iue = cmd->iue;
  1831. cmd->type = ADAPTER_MAD;
  1832. rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
  1833. if (!rc) {
  1834. mad = (struct mad_common *)&vio_iu(iue)->mad;
  1835. dev_dbg(&vscsi->dev, "mad: type %d\n", be32_to_cpu(mad->type));
  1836. rc = ibmvscsis_process_mad(vscsi, iue);
  1837. dev_dbg(&vscsi->dev, "mad: status %hd, rc %ld\n",
  1838. be16_to_cpu(mad->status), rc);
  1839. if (!rc)
  1840. ibmvscsis_send_mad_resp(vscsi, cmd, crq);
  1841. } else {
  1842. ibmvscsis_free_cmd_resources(vscsi, cmd);
  1843. }
  1844. dev_dbg(&vscsi->dev, "Leaving mad, rc %ld\n", rc);
  1845. return rc;
  1846. }
  1847. /**
  1848. * ibmvscsis_login_rsp() - Create/copy a login response notice to the client
  1849. * @vscsi: Pointer to our adapter structure
  1850. * @cmd: Pointer to the command for the SRP Login request
  1851. *
  1852. * EXECUTION ENVIRONMENT:
  1853. * Interrupt, interrupt lock held
  1854. */
  1855. static long ibmvscsis_login_rsp(struct scsi_info *vscsi,
  1856. struct ibmvscsis_cmd *cmd)
  1857. {
  1858. struct iu_entry *iue = cmd->iue;
  1859. struct srp_login_rsp *rsp = &vio_iu(iue)->srp.login_rsp;
  1860. struct format_code *fmt;
  1861. uint flag_bits = 0;
  1862. long rc = ADAPT_SUCCESS;
  1863. memset(rsp, 0, sizeof(struct srp_login_rsp));
  1864. rsp->opcode = SRP_LOGIN_RSP;
  1865. rsp->req_lim_delta = cpu_to_be32(vscsi->request_limit);
  1866. rsp->tag = cmd->rsp.tag;
  1867. rsp->max_it_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
  1868. rsp->max_ti_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
  1869. fmt = (struct format_code *)&rsp->buf_fmt;
  1870. fmt->buffers = SUPPORTED_FORMATS;
  1871. vscsi->credit = 0;
  1872. cmd->rsp.len = sizeof(struct srp_login_rsp);
  1873. dma_wmb();
  1874. rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
  1875. iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
  1876. be64_to_cpu(iue->remote_token));
  1877. switch (rc) {
  1878. case H_SUCCESS:
  1879. break;
  1880. case H_PERMISSION:
  1881. if (connection_broken(vscsi))
  1882. flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
  1883. dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
  1884. rc);
  1885. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
  1886. flag_bits);
  1887. break;
  1888. case H_SOURCE_PARM:
  1889. case H_DEST_PARM:
  1890. default:
  1891. dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
  1892. rc);
  1893. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1894. break;
  1895. }
  1896. return rc;
  1897. }
  1898. /**
  1899. * ibmvscsis_srp_login_rej() - Create/copy a login rejection notice to client
  1900. * @vscsi: Pointer to our adapter structure
  1901. * @cmd: Pointer to the command for the SRP Login request
  1902. * @reason: The reason the SRP Login is being rejected, per SRP protocol
  1903. *
  1904. * EXECUTION ENVIRONMENT:
  1905. * Interrupt, interrupt lock held
  1906. */
  1907. static long ibmvscsis_srp_login_rej(struct scsi_info *vscsi,
  1908. struct ibmvscsis_cmd *cmd, u32 reason)
  1909. {
  1910. struct iu_entry *iue = cmd->iue;
  1911. struct srp_login_rej *rej = &vio_iu(iue)->srp.login_rej;
  1912. struct format_code *fmt;
  1913. uint flag_bits = 0;
  1914. long rc = ADAPT_SUCCESS;
  1915. memset(rej, 0, sizeof(*rej));
  1916. rej->opcode = SRP_LOGIN_REJ;
  1917. rej->reason = cpu_to_be32(reason);
  1918. rej->tag = cmd->rsp.tag;
  1919. fmt = (struct format_code *)&rej->buf_fmt;
  1920. fmt->buffers = SUPPORTED_FORMATS;
  1921. cmd->rsp.len = sizeof(*rej);
  1922. dma_wmb();
  1923. rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
  1924. iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
  1925. be64_to_cpu(iue->remote_token));
  1926. switch (rc) {
  1927. case H_SUCCESS:
  1928. break;
  1929. case H_PERMISSION:
  1930. if (connection_broken(vscsi))
  1931. flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
  1932. dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
  1933. rc);
  1934. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
  1935. flag_bits);
  1936. break;
  1937. case H_SOURCE_PARM:
  1938. case H_DEST_PARM:
  1939. default:
  1940. dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
  1941. rc);
  1942. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  1943. break;
  1944. }
  1945. return rc;
  1946. }
  1947. static int ibmvscsis_make_nexus(struct ibmvscsis_tport *tport)
  1948. {
  1949. char *name = tport->tport_name;
  1950. struct ibmvscsis_nexus *nexus;
  1951. struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
  1952. int rc;
  1953. if (tport->ibmv_nexus) {
  1954. dev_dbg(&vscsi->dev, "tport->ibmv_nexus already exists\n");
  1955. return 0;
  1956. }
  1957. nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
  1958. if (!nexus) {
  1959. dev_err(&vscsi->dev, "Unable to allocate struct ibmvscsis_nexus\n");
  1960. return -ENOMEM;
  1961. }
  1962. nexus->se_sess = target_setup_session(&tport->se_tpg, 0, 0,
  1963. TARGET_PROT_NORMAL, name, nexus,
  1964. NULL);
  1965. if (IS_ERR(nexus->se_sess)) {
  1966. rc = PTR_ERR(nexus->se_sess);
  1967. goto transport_init_fail;
  1968. }
  1969. tport->ibmv_nexus = nexus;
  1970. return 0;
  1971. transport_init_fail:
  1972. kfree(nexus);
  1973. return rc;
  1974. }
  1975. static int ibmvscsis_drop_nexus(struct ibmvscsis_tport *tport)
  1976. {
  1977. struct se_session *se_sess;
  1978. struct ibmvscsis_nexus *nexus;
  1979. nexus = tport->ibmv_nexus;
  1980. if (!nexus)
  1981. return -ENODEV;
  1982. se_sess = nexus->se_sess;
  1983. if (!se_sess)
  1984. return -ENODEV;
  1985. /*
  1986. * Release the SCSI I_T Nexus to the emulated ibmvscsis Target Port
  1987. */
  1988. target_remove_session(se_sess);
  1989. tport->ibmv_nexus = NULL;
  1990. kfree(nexus);
  1991. return 0;
  1992. }
  1993. /**
  1994. * ibmvscsis_srp_login() - Process an SRP Login Request
  1995. * @vscsi: Pointer to our adapter structure
  1996. * @cmd: Command element to use to process the SRP Login request
  1997. * @crq: Pointer to CRQ entry containing the SRP Login request
  1998. *
  1999. * EXECUTION ENVIRONMENT:
  2000. * Interrupt, called with interrupt lock held
  2001. */
  2002. static long ibmvscsis_srp_login(struct scsi_info *vscsi,
  2003. struct ibmvscsis_cmd *cmd,
  2004. struct viosrp_crq *crq)
  2005. {
  2006. struct iu_entry *iue = cmd->iue;
  2007. struct srp_login_req *req = &vio_iu(iue)->srp.login_req;
  2008. struct port_id {
  2009. __be64 id_extension;
  2010. __be64 io_guid;
  2011. } *iport, *tport;
  2012. struct format_code *fmt;
  2013. u32 reason = 0x0;
  2014. long rc = ADAPT_SUCCESS;
  2015. iport = (struct port_id *)req->initiator_port_id;
  2016. tport = (struct port_id *)req->target_port_id;
  2017. fmt = (struct format_code *)&req->req_buf_fmt;
  2018. if (be32_to_cpu(req->req_it_iu_len) > SRP_MAX_IU_LEN)
  2019. reason = SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE;
  2020. else if (be32_to_cpu(req->req_it_iu_len) < 64)
  2021. reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
  2022. else if ((be64_to_cpu(iport->id_extension) > (MAX_NUM_PORTS - 1)) ||
  2023. (be64_to_cpu(tport->id_extension) > (MAX_NUM_PORTS - 1)))
  2024. reason = SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL;
  2025. else if (req->req_flags & SRP_MULTICHAN_MULTI)
  2026. reason = SRP_LOGIN_REJ_MULTI_CHANNEL_UNSUPPORTED;
  2027. else if (fmt->buffers & (~SUPPORTED_FORMATS))
  2028. reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
  2029. else if ((fmt->buffers & SUPPORTED_FORMATS) == 0)
  2030. reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
  2031. if (vscsi->state == SRP_PROCESSING)
  2032. reason = SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED;
  2033. rc = ibmvscsis_make_nexus(&vscsi->tport);
  2034. if (rc)
  2035. reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
  2036. cmd->rsp.format = VIOSRP_SRP_FORMAT;
  2037. cmd->rsp.tag = req->tag;
  2038. dev_dbg(&vscsi->dev, "srp_login: reason 0x%x\n", reason);
  2039. if (reason)
  2040. rc = ibmvscsis_srp_login_rej(vscsi, cmd, reason);
  2041. else
  2042. rc = ibmvscsis_login_rsp(vscsi, cmd);
  2043. if (!rc) {
  2044. if (!reason)
  2045. vscsi->state = SRP_PROCESSING;
  2046. list_add_tail(&cmd->list, &vscsi->waiting_rsp);
  2047. ibmvscsis_send_messages(vscsi);
  2048. } else {
  2049. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2050. }
  2051. dev_dbg(&vscsi->dev, "Leaving srp_login, rc %ld\n", rc);
  2052. return rc;
  2053. }
  2054. /**
  2055. * ibmvscsis_srp_i_logout() - Helper Function to close I_T Nexus
  2056. * @vscsi: Pointer to our adapter structure
  2057. * @cmd: Command element to use to process the Implicit Logout request
  2058. * @crq: Pointer to CRQ entry containing the Implicit Logout request
  2059. *
  2060. * Do the logic to close the I_T nexus. This function may not
  2061. * behave to specification.
  2062. *
  2063. * EXECUTION ENVIRONMENT:
  2064. * Interrupt, interrupt lock held
  2065. */
  2066. static long ibmvscsis_srp_i_logout(struct scsi_info *vscsi,
  2067. struct ibmvscsis_cmd *cmd,
  2068. struct viosrp_crq *crq)
  2069. {
  2070. struct iu_entry *iue = cmd->iue;
  2071. struct srp_i_logout *log_out = &vio_iu(iue)->srp.i_logout;
  2072. if ((vscsi->debit > 0) || !list_empty(&vscsi->schedule_q) ||
  2073. !list_empty(&vscsi->waiting_rsp)) {
  2074. dev_err(&vscsi->dev, "i_logout: outstanding work\n");
  2075. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
  2076. } else {
  2077. cmd->rsp.format = SRP_FORMAT;
  2078. cmd->rsp.tag = log_out->tag;
  2079. cmd->rsp.len = sizeof(struct mad_common);
  2080. list_add_tail(&cmd->list, &vscsi->waiting_rsp);
  2081. ibmvscsis_send_messages(vscsi);
  2082. ibmvscsis_post_disconnect(vscsi, WAIT_IDLE, 0);
  2083. }
  2084. return ADAPT_SUCCESS;
  2085. }
  2086. /* Called with intr lock held */
  2087. static void ibmvscsis_srp_cmd(struct scsi_info *vscsi, struct viosrp_crq *crq)
  2088. {
  2089. struct ibmvscsis_cmd *cmd;
  2090. struct iu_entry *iue;
  2091. struct srp_cmd *srp;
  2092. struct srp_tsk_mgmt *tsk;
  2093. long rc;
  2094. if (vscsi->request_limit - vscsi->debit <= 0) {
  2095. /* Client has exceeded request limit */
  2096. dev_err(&vscsi->dev, "Client exceeded the request limit (%d), debit %d\n",
  2097. vscsi->request_limit, vscsi->debit);
  2098. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2099. return;
  2100. }
  2101. cmd = ibmvscsis_get_free_cmd(vscsi);
  2102. if (!cmd) {
  2103. dev_err(&vscsi->dev, "srp_cmd failed to get cmd, debit %d\n",
  2104. vscsi->debit);
  2105. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2106. return;
  2107. }
  2108. iue = cmd->iue;
  2109. srp = &vio_iu(iue)->srp.cmd;
  2110. rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
  2111. if (rc) {
  2112. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2113. return;
  2114. }
  2115. if (vscsi->state == SRP_PROCESSING) {
  2116. switch (srp->opcode) {
  2117. case SRP_LOGIN_REQ:
  2118. rc = ibmvscsis_srp_login(vscsi, cmd, crq);
  2119. break;
  2120. case SRP_TSK_MGMT:
  2121. tsk = &vio_iu(iue)->srp.tsk_mgmt;
  2122. dev_dbg(&vscsi->dev, "tsk_mgmt tag: %llu (0x%llx)\n",
  2123. tsk->tag, tsk->tag);
  2124. cmd->rsp.tag = tsk->tag;
  2125. vscsi->debit += 1;
  2126. cmd->type = TASK_MANAGEMENT;
  2127. list_add_tail(&cmd->list, &vscsi->schedule_q);
  2128. queue_work(vscsi->work_q, &cmd->work);
  2129. break;
  2130. case SRP_CMD:
  2131. dev_dbg(&vscsi->dev, "srp_cmd tag: %llu (0x%llx)\n",
  2132. srp->tag, srp->tag);
  2133. cmd->rsp.tag = srp->tag;
  2134. vscsi->debit += 1;
  2135. cmd->type = SCSI_CDB;
  2136. /*
  2137. * We want to keep track of work waiting for
  2138. * the workqueue.
  2139. */
  2140. list_add_tail(&cmd->list, &vscsi->schedule_q);
  2141. queue_work(vscsi->work_q, &cmd->work);
  2142. break;
  2143. case SRP_I_LOGOUT:
  2144. rc = ibmvscsis_srp_i_logout(vscsi, cmd, crq);
  2145. break;
  2146. case SRP_CRED_RSP:
  2147. case SRP_AER_RSP:
  2148. default:
  2149. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2150. dev_err(&vscsi->dev, "invalid srp cmd, opcode %d\n",
  2151. (uint)srp->opcode);
  2152. ibmvscsis_post_disconnect(vscsi,
  2153. ERR_DISCONNECT_RECONNECT, 0);
  2154. break;
  2155. }
  2156. } else if (srp->opcode == SRP_LOGIN_REQ && vscsi->state == CONNECTED) {
  2157. rc = ibmvscsis_srp_login(vscsi, cmd, crq);
  2158. } else {
  2159. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2160. dev_err(&vscsi->dev, "Invalid state %d to handle srp cmd\n",
  2161. vscsi->state);
  2162. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2163. }
  2164. }
  2165. /**
  2166. * ibmvscsis_ping_response() - Respond to a ping request
  2167. * @vscsi: Pointer to our adapter structure
  2168. *
  2169. * Let the client know that the server is alive and waiting on
  2170. * its native I/O stack.
  2171. * If any type of error occurs from the call to queue a ping
  2172. * response then the client is either not accepting or receiving
  2173. * interrupts. Disconnect with an error.
  2174. *
  2175. * EXECUTION ENVIRONMENT:
  2176. * Interrupt, interrupt lock held
  2177. */
  2178. static long ibmvscsis_ping_response(struct scsi_info *vscsi)
  2179. {
  2180. struct viosrp_crq *crq;
  2181. u64 buffer[2] = { 0, 0 };
  2182. long rc;
  2183. crq = (struct viosrp_crq *)&buffer;
  2184. crq->valid = VALID_CMD_RESP_EL;
  2185. crq->format = (u8)MESSAGE_IN_CRQ;
  2186. crq->status = PING_RESPONSE;
  2187. rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
  2188. cpu_to_be64(buffer[MSG_LOW]));
  2189. switch (rc) {
  2190. case H_SUCCESS:
  2191. break;
  2192. case H_CLOSED:
  2193. vscsi->flags |= CLIENT_FAILED;
  2194. fallthrough;
  2195. case H_DROPPED:
  2196. vscsi->flags |= RESPONSE_Q_DOWN;
  2197. fallthrough;
  2198. case H_REMOTE_PARM:
  2199. dev_err(&vscsi->dev, "ping_response: h_send_crq failed, rc %ld\n",
  2200. rc);
  2201. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2202. break;
  2203. default:
  2204. dev_err(&vscsi->dev, "ping_response: h_send_crq returned unknown rc %ld\n",
  2205. rc);
  2206. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
  2207. break;
  2208. }
  2209. return rc;
  2210. }
  2211. /**
  2212. * ibmvscsis_parse_command() - Parse an element taken from the cmd rsp queue.
  2213. * @vscsi: Pointer to our adapter structure
  2214. * @crq: Pointer to CRQ element containing the SRP request
  2215. *
  2216. * This function will return success if the command queue element is valid
  2217. * and the srp iu or MAD request it pointed to was also valid. That does
  2218. * not mean that an error was not returned to the client.
  2219. *
  2220. * EXECUTION ENVIRONMENT:
  2221. * Interrupt, intr lock held
  2222. */
  2223. static long ibmvscsis_parse_command(struct scsi_info *vscsi,
  2224. struct viosrp_crq *crq)
  2225. {
  2226. long rc = ADAPT_SUCCESS;
  2227. switch (crq->valid) {
  2228. case VALID_CMD_RESP_EL:
  2229. switch (crq->format) {
  2230. case OS400_FORMAT:
  2231. case AIX_FORMAT:
  2232. case LINUX_FORMAT:
  2233. case MAD_FORMAT:
  2234. if (vscsi->flags & PROCESSING_MAD) {
  2235. rc = ERROR;
  2236. dev_err(&vscsi->dev, "parse_command: already processing mad\n");
  2237. ibmvscsis_post_disconnect(vscsi,
  2238. ERR_DISCONNECT_RECONNECT,
  2239. 0);
  2240. } else {
  2241. vscsi->flags |= PROCESSING_MAD;
  2242. rc = ibmvscsis_mad(vscsi, crq);
  2243. }
  2244. break;
  2245. case SRP_FORMAT:
  2246. ibmvscsis_srp_cmd(vscsi, crq);
  2247. break;
  2248. case MESSAGE_IN_CRQ:
  2249. if (crq->status == PING)
  2250. ibmvscsis_ping_response(vscsi);
  2251. break;
  2252. default:
  2253. dev_err(&vscsi->dev, "parse_command: invalid format %d\n",
  2254. (uint)crq->format);
  2255. ibmvscsis_post_disconnect(vscsi,
  2256. ERR_DISCONNECT_RECONNECT, 0);
  2257. break;
  2258. }
  2259. break;
  2260. case VALID_TRANS_EVENT:
  2261. rc = ibmvscsis_trans_event(vscsi, crq);
  2262. break;
  2263. case VALID_INIT_MSG:
  2264. rc = ibmvscsis_init_msg(vscsi, crq);
  2265. break;
  2266. default:
  2267. dev_err(&vscsi->dev, "parse_command: invalid valid field %d\n",
  2268. (uint)crq->valid);
  2269. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2270. break;
  2271. }
  2272. /*
  2273. * Return only what the interrupt handler cares
  2274. * about. Most errors we keep right on trucking.
  2275. */
  2276. rc = vscsi->flags & SCHEDULE_DISCONNECT;
  2277. return rc;
  2278. }
  2279. static int read_dma_window(struct scsi_info *vscsi)
  2280. {
  2281. struct vio_dev *vdev = vscsi->dma_dev;
  2282. const __be32 *dma_window;
  2283. const __be32 *prop;
  2284. /* TODO Using of_parse_dma_window would be better, but it doesn't give
  2285. * a way to read multiple windows without already knowing the size of
  2286. * a window or the number of windows.
  2287. */
  2288. dma_window = (const __be32 *)vio_get_attribute(vdev,
  2289. "ibm,my-dma-window",
  2290. NULL);
  2291. if (!dma_window) {
  2292. dev_err(&vscsi->dev, "Couldn't find ibm,my-dma-window property\n");
  2293. return -1;
  2294. }
  2295. vscsi->dds.window[LOCAL].liobn = be32_to_cpu(*dma_window);
  2296. dma_window++;
  2297. prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-address-cells",
  2298. NULL);
  2299. if (!prop) {
  2300. dev_warn(&vscsi->dev, "Couldn't find ibm,#dma-address-cells property\n");
  2301. dma_window++;
  2302. } else {
  2303. dma_window += be32_to_cpu(*prop);
  2304. }
  2305. prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-size-cells",
  2306. NULL);
  2307. if (!prop) {
  2308. dev_warn(&vscsi->dev, "Couldn't find ibm,#dma-size-cells property\n");
  2309. dma_window++;
  2310. } else {
  2311. dma_window += be32_to_cpu(*prop);
  2312. }
  2313. /* dma_window should point to the second window now */
  2314. vscsi->dds.window[REMOTE].liobn = be32_to_cpu(*dma_window);
  2315. return 0;
  2316. }
  2317. static struct ibmvscsis_tport *ibmvscsis_lookup_port(const char *name)
  2318. {
  2319. struct ibmvscsis_tport *tport = NULL;
  2320. struct vio_dev *vdev;
  2321. struct scsi_info *vscsi;
  2322. spin_lock_bh(&ibmvscsis_dev_lock);
  2323. list_for_each_entry(vscsi, &ibmvscsis_dev_list, list) {
  2324. vdev = vscsi->dma_dev;
  2325. if (!strcmp(dev_name(&vdev->dev), name)) {
  2326. tport = &vscsi->tport;
  2327. break;
  2328. }
  2329. }
  2330. spin_unlock_bh(&ibmvscsis_dev_lock);
  2331. return tport;
  2332. }
  2333. /**
  2334. * ibmvscsis_parse_cmd() - Parse SRP Command
  2335. * @vscsi: Pointer to our adapter structure
  2336. * @cmd: Pointer to command element with SRP command
  2337. *
  2338. * Parse the srp command; if it is valid then submit it to tcm.
  2339. * Note: The return code does not reflect the status of the SCSI CDB.
  2340. *
  2341. * EXECUTION ENVIRONMENT:
  2342. * Process level
  2343. */
  2344. static void ibmvscsis_parse_cmd(struct scsi_info *vscsi,
  2345. struct ibmvscsis_cmd *cmd)
  2346. {
  2347. struct iu_entry *iue = cmd->iue;
  2348. struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
  2349. struct ibmvscsis_nexus *nexus;
  2350. u64 data_len = 0;
  2351. enum dma_data_direction dir;
  2352. int attr = 0;
  2353. nexus = vscsi->tport.ibmv_nexus;
  2354. /*
  2355. * additional length in bytes. Note that the SRP spec says that
  2356. * additional length is in 4-byte words, but technically the
  2357. * additional length field is only the upper 6 bits of the byte.
  2358. * The lower 2 bits are reserved. If the lower 2 bits are 0 (as
  2359. * all reserved fields should be), then interpreting the byte as
  2360. * an int will yield the length in bytes.
  2361. */
  2362. if (srp->add_cdb_len & 0x03) {
  2363. dev_err(&vscsi->dev, "parse_cmd: reserved bits set in IU\n");
  2364. spin_lock_bh(&vscsi->intr_lock);
  2365. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2366. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2367. spin_unlock_bh(&vscsi->intr_lock);
  2368. return;
  2369. }
  2370. if (srp_get_desc_table(srp, &dir, &data_len)) {
  2371. dev_err(&vscsi->dev, "0x%llx: parsing SRP descriptor table failed.\n",
  2372. srp->tag);
  2373. goto fail;
  2374. }
  2375. cmd->rsp.sol_not = srp->sol_not;
  2376. switch (srp->task_attr) {
  2377. case SRP_SIMPLE_TASK:
  2378. attr = TCM_SIMPLE_TAG;
  2379. break;
  2380. case SRP_ORDERED_TASK:
  2381. attr = TCM_ORDERED_TAG;
  2382. break;
  2383. case SRP_HEAD_TASK:
  2384. attr = TCM_HEAD_TAG;
  2385. break;
  2386. case SRP_ACA_TASK:
  2387. attr = TCM_ACA_TAG;
  2388. break;
  2389. default:
  2390. dev_err(&vscsi->dev, "Invalid task attribute %d\n",
  2391. srp->task_attr);
  2392. goto fail;
  2393. }
  2394. cmd->se_cmd.tag = be64_to_cpu(srp->tag);
  2395. spin_lock_bh(&vscsi->intr_lock);
  2396. list_add_tail(&cmd->list, &vscsi->active_q);
  2397. spin_unlock_bh(&vscsi->intr_lock);
  2398. srp->lun.scsi_lun[0] &= 0x3f;
  2399. target_submit_cmd(&cmd->se_cmd, nexus->se_sess, srp->cdb,
  2400. cmd->sense_buf, scsilun_to_int(&srp->lun),
  2401. data_len, attr, dir, 0);
  2402. return;
  2403. fail:
  2404. spin_lock_bh(&vscsi->intr_lock);
  2405. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
  2406. spin_unlock_bh(&vscsi->intr_lock);
  2407. }
  2408. /**
  2409. * ibmvscsis_parse_task() - Parse SRP Task Management Request
  2410. * @vscsi: Pointer to our adapter structure
  2411. * @cmd: Pointer to command element with SRP task management request
  2412. *
  2413. * Parse the srp task management request; if it is valid then submit it to tcm.
  2414. * Note: The return code does not reflect the status of the task management
  2415. * request.
  2416. *
  2417. * EXECUTION ENVIRONMENT:
  2418. * Processor level
  2419. */
  2420. static void ibmvscsis_parse_task(struct scsi_info *vscsi,
  2421. struct ibmvscsis_cmd *cmd)
  2422. {
  2423. struct iu_entry *iue = cmd->iue;
  2424. struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
  2425. int tcm_type;
  2426. u64 tag_to_abort = 0;
  2427. int rc = 0;
  2428. struct ibmvscsis_nexus *nexus;
  2429. nexus = vscsi->tport.ibmv_nexus;
  2430. cmd->rsp.sol_not = srp_tsk->sol_not;
  2431. switch (srp_tsk->tsk_mgmt_func) {
  2432. case SRP_TSK_ABORT_TASK:
  2433. tcm_type = TMR_ABORT_TASK;
  2434. tag_to_abort = be64_to_cpu(srp_tsk->task_tag);
  2435. break;
  2436. case SRP_TSK_ABORT_TASK_SET:
  2437. tcm_type = TMR_ABORT_TASK_SET;
  2438. break;
  2439. case SRP_TSK_CLEAR_TASK_SET:
  2440. tcm_type = TMR_CLEAR_TASK_SET;
  2441. break;
  2442. case SRP_TSK_LUN_RESET:
  2443. tcm_type = TMR_LUN_RESET;
  2444. break;
  2445. case SRP_TSK_CLEAR_ACA:
  2446. tcm_type = TMR_CLEAR_ACA;
  2447. break;
  2448. default:
  2449. dev_err(&vscsi->dev, "unknown task mgmt func %d\n",
  2450. srp_tsk->tsk_mgmt_func);
  2451. cmd->se_cmd.se_tmr_req->response =
  2452. TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  2453. rc = -1;
  2454. break;
  2455. }
  2456. if (!rc) {
  2457. cmd->se_cmd.tag = be64_to_cpu(srp_tsk->tag);
  2458. spin_lock_bh(&vscsi->intr_lock);
  2459. list_add_tail(&cmd->list, &vscsi->active_q);
  2460. spin_unlock_bh(&vscsi->intr_lock);
  2461. srp_tsk->lun.scsi_lun[0] &= 0x3f;
  2462. dev_dbg(&vscsi->dev, "calling submit_tmr, func %d\n",
  2463. srp_tsk->tsk_mgmt_func);
  2464. rc = target_submit_tmr(&cmd->se_cmd, nexus->se_sess, NULL,
  2465. scsilun_to_int(&srp_tsk->lun), srp_tsk,
  2466. tcm_type, GFP_KERNEL, tag_to_abort, 0);
  2467. if (rc) {
  2468. dev_err(&vscsi->dev, "target_submit_tmr failed, rc %d\n",
  2469. rc);
  2470. spin_lock_bh(&vscsi->intr_lock);
  2471. list_del(&cmd->list);
  2472. spin_unlock_bh(&vscsi->intr_lock);
  2473. cmd->se_cmd.se_tmr_req->response =
  2474. TMR_FUNCTION_REJECTED;
  2475. }
  2476. }
  2477. if (rc)
  2478. transport_send_check_condition_and_sense(&cmd->se_cmd, 0, 0);
  2479. }
  2480. static void ibmvscsis_scheduler(struct work_struct *work)
  2481. {
  2482. struct ibmvscsis_cmd *cmd = container_of(work, struct ibmvscsis_cmd,
  2483. work);
  2484. struct scsi_info *vscsi = cmd->adapter;
  2485. spin_lock_bh(&vscsi->intr_lock);
  2486. /* Remove from schedule_q */
  2487. list_del(&cmd->list);
  2488. /* Don't submit cmd if we're disconnecting */
  2489. if (vscsi->flags & (SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED)) {
  2490. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2491. /* ibmvscsis_disconnect might be waiting for us */
  2492. if (list_empty(&vscsi->active_q) &&
  2493. list_empty(&vscsi->schedule_q) &&
  2494. (vscsi->flags & WAIT_FOR_IDLE)) {
  2495. vscsi->flags &= ~WAIT_FOR_IDLE;
  2496. complete(&vscsi->wait_idle);
  2497. }
  2498. spin_unlock_bh(&vscsi->intr_lock);
  2499. return;
  2500. }
  2501. spin_unlock_bh(&vscsi->intr_lock);
  2502. switch (cmd->type) {
  2503. case SCSI_CDB:
  2504. ibmvscsis_parse_cmd(vscsi, cmd);
  2505. break;
  2506. case TASK_MANAGEMENT:
  2507. ibmvscsis_parse_task(vscsi, cmd);
  2508. break;
  2509. default:
  2510. dev_err(&vscsi->dev, "scheduler, invalid cmd type %d\n",
  2511. cmd->type);
  2512. spin_lock_bh(&vscsi->intr_lock);
  2513. ibmvscsis_free_cmd_resources(vscsi, cmd);
  2514. spin_unlock_bh(&vscsi->intr_lock);
  2515. break;
  2516. }
  2517. }
  2518. static int ibmvscsis_alloc_cmds(struct scsi_info *vscsi, int num)
  2519. {
  2520. struct ibmvscsis_cmd *cmd;
  2521. int i;
  2522. INIT_LIST_HEAD(&vscsi->free_cmd);
  2523. vscsi->cmd_pool = kcalloc(num, sizeof(struct ibmvscsis_cmd),
  2524. GFP_KERNEL);
  2525. if (!vscsi->cmd_pool)
  2526. return -ENOMEM;
  2527. for (i = 0, cmd = (struct ibmvscsis_cmd *)vscsi->cmd_pool; i < num;
  2528. i++, cmd++) {
  2529. cmd->abort_cmd = NULL;
  2530. cmd->adapter = vscsi;
  2531. INIT_WORK(&cmd->work, ibmvscsis_scheduler);
  2532. list_add_tail(&cmd->list, &vscsi->free_cmd);
  2533. }
  2534. return 0;
  2535. }
  2536. static void ibmvscsis_free_cmds(struct scsi_info *vscsi)
  2537. {
  2538. kfree(vscsi->cmd_pool);
  2539. vscsi->cmd_pool = NULL;
  2540. INIT_LIST_HEAD(&vscsi->free_cmd);
  2541. }
  2542. /**
  2543. * ibmvscsis_service_wait_q() - Service Waiting Queue
  2544. * @timer: Pointer to timer which has expired
  2545. *
  2546. * This routine is called when the timer pops to service the waiting
  2547. * queue. Elements on the queue have completed, their responses have been
  2548. * copied to the client, but the client's response queue was full so
  2549. * the queue message could not be sent. The routine grabs the proper locks
  2550. * and calls send messages.
  2551. *
  2552. * EXECUTION ENVIRONMENT:
  2553. * called at interrupt level
  2554. */
  2555. static enum hrtimer_restart ibmvscsis_service_wait_q(struct hrtimer *timer)
  2556. {
  2557. struct timer_cb *p_timer = container_of(timer, struct timer_cb, timer);
  2558. struct scsi_info *vscsi = container_of(p_timer, struct scsi_info,
  2559. rsp_q_timer);
  2560. spin_lock_bh(&vscsi->intr_lock);
  2561. p_timer->timer_pops += 1;
  2562. p_timer->started = false;
  2563. ibmvscsis_send_messages(vscsi);
  2564. spin_unlock_bh(&vscsi->intr_lock);
  2565. return HRTIMER_NORESTART;
  2566. }
  2567. static long ibmvscsis_alloctimer(struct scsi_info *vscsi)
  2568. {
  2569. struct timer_cb *p_timer;
  2570. p_timer = &vscsi->rsp_q_timer;
  2571. hrtimer_init(&p_timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2572. p_timer->timer.function = ibmvscsis_service_wait_q;
  2573. p_timer->started = false;
  2574. p_timer->timer_pops = 0;
  2575. return ADAPT_SUCCESS;
  2576. }
  2577. static void ibmvscsis_freetimer(struct scsi_info *vscsi)
  2578. {
  2579. struct timer_cb *p_timer;
  2580. p_timer = &vscsi->rsp_q_timer;
  2581. (void)hrtimer_cancel(&p_timer->timer);
  2582. p_timer->started = false;
  2583. p_timer->timer_pops = 0;
  2584. }
  2585. static irqreturn_t ibmvscsis_interrupt(int dummy, void *data)
  2586. {
  2587. struct scsi_info *vscsi = data;
  2588. vio_disable_interrupts(vscsi->dma_dev);
  2589. tasklet_schedule(&vscsi->work_task);
  2590. return IRQ_HANDLED;
  2591. }
  2592. /**
  2593. * ibmvscsis_enable_change_state() - Set new state based on enabled status
  2594. * @vscsi: Pointer to our adapter structure
  2595. *
  2596. * This function determines our new state now that we are enabled. This
  2597. * may involve sending an Init Complete message to the client.
  2598. *
  2599. * Must be called with interrupt lock held.
  2600. */
  2601. static long ibmvscsis_enable_change_state(struct scsi_info *vscsi)
  2602. {
  2603. int bytes;
  2604. long rc = ADAPT_SUCCESS;
  2605. bytes = vscsi->cmd_q.size * PAGE_SIZE;
  2606. rc = h_reg_crq(vscsi->dds.unit_id, vscsi->cmd_q.crq_token, bytes);
  2607. if (rc == H_CLOSED || rc == H_SUCCESS) {
  2608. vscsi->state = WAIT_CONNECTION;
  2609. rc = ibmvscsis_establish_new_q(vscsi);
  2610. }
  2611. if (rc != ADAPT_SUCCESS) {
  2612. vscsi->state = ERR_DISCONNECTED;
  2613. vscsi->flags |= RESPONSE_Q_DOWN;
  2614. }
  2615. return rc;
  2616. }
  2617. /**
  2618. * ibmvscsis_create_command_q() - Create Command Queue
  2619. * @vscsi: Pointer to our adapter structure
  2620. * @num_cmds: Currently unused. In the future, may be used to determine
  2621. * the size of the CRQ.
  2622. *
  2623. * Allocates memory for command queue maps remote memory into an ioba
  2624. * initializes the command response queue
  2625. *
  2626. * EXECUTION ENVIRONMENT:
  2627. * Process level only
  2628. */
  2629. static long ibmvscsis_create_command_q(struct scsi_info *vscsi, int num_cmds)
  2630. {
  2631. int pages;
  2632. struct vio_dev *vdev = vscsi->dma_dev;
  2633. /* We might support multiple pages in the future, but just 1 for now */
  2634. pages = 1;
  2635. vscsi->cmd_q.size = pages;
  2636. vscsi->cmd_q.base_addr =
  2637. (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
  2638. if (!vscsi->cmd_q.base_addr)
  2639. return -ENOMEM;
  2640. vscsi->cmd_q.mask = ((uint)pages * CRQ_PER_PAGE) - 1;
  2641. vscsi->cmd_q.crq_token = dma_map_single(&vdev->dev,
  2642. vscsi->cmd_q.base_addr,
  2643. PAGE_SIZE, DMA_BIDIRECTIONAL);
  2644. if (dma_mapping_error(&vdev->dev, vscsi->cmd_q.crq_token)) {
  2645. free_page((unsigned long)vscsi->cmd_q.base_addr);
  2646. return -ENOMEM;
  2647. }
  2648. return 0;
  2649. }
  2650. /**
  2651. * ibmvscsis_destroy_command_q - Destroy Command Queue
  2652. * @vscsi: Pointer to our adapter structure
  2653. *
  2654. * Releases memory for command queue and unmaps mapped remote memory.
  2655. *
  2656. * EXECUTION ENVIRONMENT:
  2657. * Process level only
  2658. */
  2659. static void ibmvscsis_destroy_command_q(struct scsi_info *vscsi)
  2660. {
  2661. dma_unmap_single(&vscsi->dma_dev->dev, vscsi->cmd_q.crq_token,
  2662. PAGE_SIZE, DMA_BIDIRECTIONAL);
  2663. free_page((unsigned long)vscsi->cmd_q.base_addr);
  2664. vscsi->cmd_q.base_addr = NULL;
  2665. vscsi->state = NO_QUEUE;
  2666. }
  2667. static u8 ibmvscsis_fast_fail(struct scsi_info *vscsi,
  2668. struct ibmvscsis_cmd *cmd)
  2669. {
  2670. struct iu_entry *iue = cmd->iue;
  2671. struct se_cmd *se_cmd = &cmd->se_cmd;
  2672. struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
  2673. struct scsi_sense_hdr sshdr;
  2674. u8 rc = se_cmd->scsi_status;
  2675. if (vscsi->fast_fail && (READ_CMD(srp->cdb) || WRITE_CMD(srp->cdb)))
  2676. if (scsi_normalize_sense(se_cmd->sense_buffer,
  2677. se_cmd->scsi_sense_length, &sshdr))
  2678. if (sshdr.sense_key == HARDWARE_ERROR &&
  2679. (se_cmd->residual_count == 0 ||
  2680. se_cmd->residual_count == se_cmd->data_length)) {
  2681. rc = NO_SENSE;
  2682. cmd->flags |= CMD_FAST_FAIL;
  2683. }
  2684. return rc;
  2685. }
  2686. /**
  2687. * srp_build_response() - Build an SRP response buffer
  2688. * @vscsi: Pointer to our adapter structure
  2689. * @cmd: Pointer to command for which to send the response
  2690. * @len_p: Where to return the length of the IU response sent. This
  2691. * is needed to construct the CRQ response.
  2692. *
  2693. * Build the SRP response buffer and copy it to the client's memory space.
  2694. */
  2695. static long srp_build_response(struct scsi_info *vscsi,
  2696. struct ibmvscsis_cmd *cmd, uint *len_p)
  2697. {
  2698. struct iu_entry *iue = cmd->iue;
  2699. struct se_cmd *se_cmd = &cmd->se_cmd;
  2700. struct srp_rsp *rsp;
  2701. uint len;
  2702. u32 rsp_code;
  2703. char *data;
  2704. u32 *tsk_status;
  2705. long rc = ADAPT_SUCCESS;
  2706. spin_lock_bh(&vscsi->intr_lock);
  2707. rsp = &vio_iu(iue)->srp.rsp;
  2708. len = sizeof(*rsp);
  2709. memset(rsp, 0, len);
  2710. data = rsp->data;
  2711. rsp->opcode = SRP_RSP;
  2712. rsp->req_lim_delta = cpu_to_be32(1 + vscsi->credit);
  2713. rsp->tag = cmd->rsp.tag;
  2714. rsp->flags = 0;
  2715. if (cmd->type == SCSI_CDB) {
  2716. rsp->status = ibmvscsis_fast_fail(vscsi, cmd);
  2717. if (rsp->status) {
  2718. dev_dbg(&vscsi->dev, "build_resp: cmd %p, scsi status %d\n",
  2719. cmd, (int)rsp->status);
  2720. ibmvscsis_determine_resid(se_cmd, rsp);
  2721. if (se_cmd->scsi_sense_length && se_cmd->sense_buffer) {
  2722. rsp->sense_data_len =
  2723. cpu_to_be32(se_cmd->scsi_sense_length);
  2724. rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  2725. len += se_cmd->scsi_sense_length;
  2726. memcpy(data, se_cmd->sense_buffer,
  2727. se_cmd->scsi_sense_length);
  2728. }
  2729. rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
  2730. UCSOLNT_RESP_SHIFT;
  2731. } else if (cmd->flags & CMD_FAST_FAIL) {
  2732. dev_dbg(&vscsi->dev, "build_resp: cmd %p, fast fail\n",
  2733. cmd);
  2734. rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
  2735. UCSOLNT_RESP_SHIFT;
  2736. } else {
  2737. rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
  2738. SCSOLNT_RESP_SHIFT;
  2739. }
  2740. } else {
  2741. /* this is task management */
  2742. rsp->status = 0;
  2743. rsp->resp_data_len = cpu_to_be32(4);
  2744. rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  2745. switch (se_cmd->se_tmr_req->response) {
  2746. case TMR_FUNCTION_COMPLETE:
  2747. case TMR_TASK_DOES_NOT_EXIST:
  2748. rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_COMPLETE;
  2749. rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
  2750. SCSOLNT_RESP_SHIFT;
  2751. break;
  2752. case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
  2753. case TMR_LUN_DOES_NOT_EXIST:
  2754. rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_NOT_SUPPORTED;
  2755. rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
  2756. UCSOLNT_RESP_SHIFT;
  2757. break;
  2758. case TMR_FUNCTION_FAILED:
  2759. case TMR_FUNCTION_REJECTED:
  2760. default:
  2761. rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_FAILED;
  2762. rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
  2763. UCSOLNT_RESP_SHIFT;
  2764. break;
  2765. }
  2766. tsk_status = (u32 *)data;
  2767. *tsk_status = cpu_to_be32(rsp_code);
  2768. data = (char *)(tsk_status + 1);
  2769. len += 4;
  2770. }
  2771. dma_wmb();
  2772. rc = h_copy_rdma(len, vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
  2773. vscsi->dds.window[REMOTE].liobn,
  2774. be64_to_cpu(iue->remote_token));
  2775. switch (rc) {
  2776. case H_SUCCESS:
  2777. vscsi->credit = 0;
  2778. *len_p = len;
  2779. break;
  2780. case H_PERMISSION:
  2781. if (connection_broken(vscsi))
  2782. vscsi->flags |= RESPONSE_Q_DOWN | CLIENT_FAILED;
  2783. dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld, flags 0x%x, state 0x%hx\n",
  2784. rc, vscsi->flags, vscsi->state);
  2785. break;
  2786. case H_SOURCE_PARM:
  2787. case H_DEST_PARM:
  2788. default:
  2789. dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld\n",
  2790. rc);
  2791. break;
  2792. }
  2793. spin_unlock_bh(&vscsi->intr_lock);
  2794. return rc;
  2795. }
  2796. static int ibmvscsis_rdma(struct ibmvscsis_cmd *cmd, struct scatterlist *sg,
  2797. int nsg, struct srp_direct_buf *md, int nmd,
  2798. enum dma_data_direction dir, unsigned int bytes)
  2799. {
  2800. struct iu_entry *iue = cmd->iue;
  2801. struct srp_target *target = iue->target;
  2802. struct scsi_info *vscsi = target->ldata;
  2803. struct scatterlist *sgp;
  2804. dma_addr_t client_ioba, server_ioba;
  2805. ulong buf_len;
  2806. ulong client_len, server_len;
  2807. int md_idx;
  2808. long tx_len;
  2809. long rc = 0;
  2810. if (bytes == 0)
  2811. return 0;
  2812. sgp = sg;
  2813. client_len = 0;
  2814. server_len = 0;
  2815. md_idx = 0;
  2816. tx_len = bytes;
  2817. do {
  2818. if (client_len == 0) {
  2819. if (md_idx >= nmd) {
  2820. dev_err(&vscsi->dev, "rdma: ran out of client memory descriptors\n");
  2821. rc = -EIO;
  2822. break;
  2823. }
  2824. client_ioba = be64_to_cpu(md[md_idx].va);
  2825. client_len = be32_to_cpu(md[md_idx].len);
  2826. }
  2827. if (server_len == 0) {
  2828. if (!sgp) {
  2829. dev_err(&vscsi->dev, "rdma: ran out of scatter/gather list\n");
  2830. rc = -EIO;
  2831. break;
  2832. }
  2833. server_ioba = sg_dma_address(sgp);
  2834. server_len = sg_dma_len(sgp);
  2835. }
  2836. buf_len = tx_len;
  2837. if (buf_len > client_len)
  2838. buf_len = client_len;
  2839. if (buf_len > server_len)
  2840. buf_len = server_len;
  2841. if (buf_len > max_vdma_size)
  2842. buf_len = max_vdma_size;
  2843. if (dir == DMA_TO_DEVICE) {
  2844. /* read from client */
  2845. rc = h_copy_rdma(buf_len,
  2846. vscsi->dds.window[REMOTE].liobn,
  2847. client_ioba,
  2848. vscsi->dds.window[LOCAL].liobn,
  2849. server_ioba);
  2850. } else {
  2851. /* The h_copy_rdma will cause phyp, running in another
  2852. * partition, to read memory, so we need to make sure
  2853. * the data has been written out, hence these syncs.
  2854. */
  2855. /* ensure that everything is in memory */
  2856. isync();
  2857. /* ensure that memory has been made visible */
  2858. dma_wmb();
  2859. rc = h_copy_rdma(buf_len,
  2860. vscsi->dds.window[LOCAL].liobn,
  2861. server_ioba,
  2862. vscsi->dds.window[REMOTE].liobn,
  2863. client_ioba);
  2864. }
  2865. switch (rc) {
  2866. case H_SUCCESS:
  2867. break;
  2868. case H_PERMISSION:
  2869. case H_SOURCE_PARM:
  2870. case H_DEST_PARM:
  2871. if (connection_broken(vscsi)) {
  2872. spin_lock_bh(&vscsi->intr_lock);
  2873. vscsi->flags |=
  2874. (RESPONSE_Q_DOWN | CLIENT_FAILED);
  2875. spin_unlock_bh(&vscsi->intr_lock);
  2876. }
  2877. dev_err(&vscsi->dev, "rdma: h_copy_rdma failed, rc %ld\n",
  2878. rc);
  2879. break;
  2880. default:
  2881. dev_err(&vscsi->dev, "rdma: unknown error %ld from h_copy_rdma\n",
  2882. rc);
  2883. break;
  2884. }
  2885. if (!rc) {
  2886. tx_len -= buf_len;
  2887. if (tx_len) {
  2888. client_len -= buf_len;
  2889. if (client_len == 0)
  2890. md_idx++;
  2891. else
  2892. client_ioba += buf_len;
  2893. server_len -= buf_len;
  2894. if (server_len == 0)
  2895. sgp = sg_next(sgp);
  2896. else
  2897. server_ioba += buf_len;
  2898. } else {
  2899. break;
  2900. }
  2901. }
  2902. } while (!rc);
  2903. return rc;
  2904. }
  2905. /**
  2906. * ibmvscsis_handle_crq() - Handle CRQ
  2907. * @data: Pointer to our adapter structure
  2908. *
  2909. * Read the command elements from the command queue and copy the payloads
  2910. * associated with the command elements to local memory and execute the
  2911. * SRP requests.
  2912. *
  2913. * Note: this is an edge triggered interrupt. It can not be shared.
  2914. */
  2915. static void ibmvscsis_handle_crq(unsigned long data)
  2916. {
  2917. struct scsi_info *vscsi = (struct scsi_info *)data;
  2918. struct viosrp_crq *crq;
  2919. long rc;
  2920. bool ack = true;
  2921. volatile u8 valid;
  2922. spin_lock_bh(&vscsi->intr_lock);
  2923. dev_dbg(&vscsi->dev, "got interrupt\n");
  2924. /*
  2925. * if we are in a path where we are waiting for all pending commands
  2926. * to complete because we received a transport event and anything in
  2927. * the command queue is for a new connection, do nothing
  2928. */
  2929. if (TARGET_STOP(vscsi)) {
  2930. vio_enable_interrupts(vscsi->dma_dev);
  2931. dev_dbg(&vscsi->dev, "handle_crq, don't process: flags 0x%x, state 0x%hx\n",
  2932. vscsi->flags, vscsi->state);
  2933. spin_unlock_bh(&vscsi->intr_lock);
  2934. return;
  2935. }
  2936. rc = vscsi->flags & SCHEDULE_DISCONNECT;
  2937. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  2938. valid = crq->valid;
  2939. dma_rmb();
  2940. while (valid) {
  2941. /*
  2942. * These are edege triggered interrupts. After dropping out of
  2943. * the while loop, the code must check for work since an
  2944. * interrupt could be lost, and an elment be left on the queue,
  2945. * hence the label.
  2946. */
  2947. cmd_work:
  2948. vscsi->cmd_q.index =
  2949. (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
  2950. if (!rc) {
  2951. rc = ibmvscsis_parse_command(vscsi, crq);
  2952. } else {
  2953. if ((uint)crq->valid == VALID_TRANS_EVENT) {
  2954. /*
  2955. * must service the transport layer events even
  2956. * in an error state, dont break out until all
  2957. * the consecutive transport events have been
  2958. * processed
  2959. */
  2960. rc = ibmvscsis_trans_event(vscsi, crq);
  2961. } else if (vscsi->flags & TRANS_EVENT) {
  2962. /*
  2963. * if a transport event has occurred leave
  2964. * everything but transport events on the queue
  2965. *
  2966. * need to decrement the queue index so we can
  2967. * look at the element again
  2968. */
  2969. if (vscsi->cmd_q.index)
  2970. vscsi->cmd_q.index -= 1;
  2971. else
  2972. /*
  2973. * index is at 0 it just wrapped.
  2974. * have it index last element in q
  2975. */
  2976. vscsi->cmd_q.index = vscsi->cmd_q.mask;
  2977. break;
  2978. }
  2979. }
  2980. crq->valid = INVALIDATE_CMD_RESP_EL;
  2981. crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
  2982. valid = crq->valid;
  2983. dma_rmb();
  2984. }
  2985. if (!rc) {
  2986. if (ack) {
  2987. vio_enable_interrupts(vscsi->dma_dev);
  2988. ack = false;
  2989. dev_dbg(&vscsi->dev, "handle_crq, reenabling interrupts\n");
  2990. }
  2991. valid = crq->valid;
  2992. dma_rmb();
  2993. if (valid)
  2994. goto cmd_work;
  2995. } else {
  2996. dev_dbg(&vscsi->dev, "handle_crq, error: flags 0x%x, state 0x%hx, crq index 0x%x\n",
  2997. vscsi->flags, vscsi->state, vscsi->cmd_q.index);
  2998. }
  2999. dev_dbg(&vscsi->dev, "Leaving handle_crq: schedule_q empty %d, flags 0x%x, state 0x%hx\n",
  3000. (int)list_empty(&vscsi->schedule_q), vscsi->flags,
  3001. vscsi->state);
  3002. spin_unlock_bh(&vscsi->intr_lock);
  3003. }
  3004. static int ibmvscsis_probe(struct vio_dev *vdev,
  3005. const struct vio_device_id *id)
  3006. {
  3007. struct scsi_info *vscsi;
  3008. int rc = 0;
  3009. long hrc = 0;
  3010. char wq_name[24];
  3011. vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
  3012. if (!vscsi) {
  3013. rc = -ENOMEM;
  3014. dev_err(&vdev->dev, "probe: allocation of adapter failed\n");
  3015. return rc;
  3016. }
  3017. vscsi->dma_dev = vdev;
  3018. vscsi->dev = vdev->dev;
  3019. INIT_LIST_HEAD(&vscsi->schedule_q);
  3020. INIT_LIST_HEAD(&vscsi->waiting_rsp);
  3021. INIT_LIST_HEAD(&vscsi->active_q);
  3022. snprintf(vscsi->tport.tport_name, IBMVSCSIS_NAMELEN, "%s",
  3023. dev_name(&vdev->dev));
  3024. dev_dbg(&vscsi->dev, "probe tport_name: %s\n", vscsi->tport.tport_name);
  3025. rc = read_dma_window(vscsi);
  3026. if (rc)
  3027. goto free_adapter;
  3028. dev_dbg(&vscsi->dev, "Probe: liobn 0x%x, riobn 0x%x\n",
  3029. vscsi->dds.window[LOCAL].liobn,
  3030. vscsi->dds.window[REMOTE].liobn);
  3031. snprintf(vscsi->eye, sizeof(vscsi->eye), "VSCSI %s", vdev->name);
  3032. vscsi->dds.unit_id = vdev->unit_address;
  3033. strscpy(vscsi->dds.partition_name, partition_name,
  3034. sizeof(vscsi->dds.partition_name));
  3035. vscsi->dds.partition_num = partition_number;
  3036. spin_lock_bh(&ibmvscsis_dev_lock);
  3037. list_add_tail(&vscsi->list, &ibmvscsis_dev_list);
  3038. spin_unlock_bh(&ibmvscsis_dev_lock);
  3039. /*
  3040. * TBD: How do we determine # of cmds to request? Do we know how
  3041. * many "children" we have?
  3042. */
  3043. vscsi->request_limit = INITIAL_SRP_LIMIT;
  3044. rc = srp_target_alloc(&vscsi->target, &vdev->dev, vscsi->request_limit,
  3045. SRP_MAX_IU_LEN);
  3046. if (rc)
  3047. goto rem_list;
  3048. vscsi->target.ldata = vscsi;
  3049. rc = ibmvscsis_alloc_cmds(vscsi, vscsi->request_limit);
  3050. if (rc) {
  3051. dev_err(&vscsi->dev, "alloc_cmds failed, rc %d, num %d\n",
  3052. rc, vscsi->request_limit);
  3053. goto free_target;
  3054. }
  3055. /*
  3056. * Note: the lock is used in freeing timers, so must initialize
  3057. * first so that ordering in case of error is correct.
  3058. */
  3059. spin_lock_init(&vscsi->intr_lock);
  3060. rc = ibmvscsis_alloctimer(vscsi);
  3061. if (rc) {
  3062. dev_err(&vscsi->dev, "probe: alloctimer failed, rc %d\n", rc);
  3063. goto free_cmds;
  3064. }
  3065. rc = ibmvscsis_create_command_q(vscsi, 256);
  3066. if (rc) {
  3067. dev_err(&vscsi->dev, "probe: create_command_q failed, rc %d\n",
  3068. rc);
  3069. goto free_timer;
  3070. }
  3071. vscsi->map_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
  3072. if (!vscsi->map_buf) {
  3073. rc = -ENOMEM;
  3074. dev_err(&vscsi->dev, "probe: allocating cmd buffer failed\n");
  3075. goto destroy_queue;
  3076. }
  3077. vscsi->map_ioba = dma_map_single(&vdev->dev, vscsi->map_buf, PAGE_SIZE,
  3078. DMA_BIDIRECTIONAL);
  3079. if (dma_mapping_error(&vdev->dev, vscsi->map_ioba)) {
  3080. rc = -ENOMEM;
  3081. dev_err(&vscsi->dev, "probe: error mapping command buffer\n");
  3082. goto free_buf;
  3083. }
  3084. hrc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
  3085. (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
  3086. 0);
  3087. if (hrc == H_SUCCESS)
  3088. vscsi->client_data.partition_number =
  3089. be64_to_cpu(*(u64 *)vscsi->map_buf);
  3090. /*
  3091. * We expect the VIOCTL to fail if we're configured as "any
  3092. * client can connect" and the client isn't activated yet.
  3093. * We'll make the call again when he sends an init msg.
  3094. */
  3095. dev_dbg(&vscsi->dev, "probe hrc %ld, client partition num %d\n",
  3096. hrc, vscsi->client_data.partition_number);
  3097. tasklet_init(&vscsi->work_task, ibmvscsis_handle_crq,
  3098. (unsigned long)vscsi);
  3099. init_completion(&vscsi->wait_idle);
  3100. init_completion(&vscsi->unconfig);
  3101. snprintf(wq_name, 24, "ibmvscsis%s", dev_name(&vdev->dev));
  3102. vscsi->work_q = create_workqueue(wq_name);
  3103. if (!vscsi->work_q) {
  3104. rc = -ENOMEM;
  3105. dev_err(&vscsi->dev, "create_workqueue failed\n");
  3106. goto unmap_buf;
  3107. }
  3108. rc = request_irq(vdev->irq, ibmvscsis_interrupt, 0, "ibmvscsis", vscsi);
  3109. if (rc) {
  3110. rc = -EPERM;
  3111. dev_err(&vscsi->dev, "probe: request_irq failed, rc %d\n", rc);
  3112. goto destroy_WQ;
  3113. }
  3114. vscsi->state = WAIT_ENABLED;
  3115. dev_set_drvdata(&vdev->dev, vscsi);
  3116. return 0;
  3117. destroy_WQ:
  3118. destroy_workqueue(vscsi->work_q);
  3119. unmap_buf:
  3120. dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
  3121. DMA_BIDIRECTIONAL);
  3122. free_buf:
  3123. kfree(vscsi->map_buf);
  3124. destroy_queue:
  3125. tasklet_kill(&vscsi->work_task);
  3126. ibmvscsis_unregister_command_q(vscsi);
  3127. ibmvscsis_destroy_command_q(vscsi);
  3128. free_timer:
  3129. ibmvscsis_freetimer(vscsi);
  3130. free_cmds:
  3131. ibmvscsis_free_cmds(vscsi);
  3132. free_target:
  3133. srp_target_free(&vscsi->target);
  3134. rem_list:
  3135. spin_lock_bh(&ibmvscsis_dev_lock);
  3136. list_del(&vscsi->list);
  3137. spin_unlock_bh(&ibmvscsis_dev_lock);
  3138. free_adapter:
  3139. kfree(vscsi);
  3140. return rc;
  3141. }
  3142. static void ibmvscsis_remove(struct vio_dev *vdev)
  3143. {
  3144. struct scsi_info *vscsi = dev_get_drvdata(&vdev->dev);
  3145. dev_dbg(&vscsi->dev, "remove (%s)\n", dev_name(&vscsi->dma_dev->dev));
  3146. spin_lock_bh(&vscsi->intr_lock);
  3147. ibmvscsis_post_disconnect(vscsi, UNCONFIGURING, 0);
  3148. vscsi->flags |= CFG_SLEEPING;
  3149. spin_unlock_bh(&vscsi->intr_lock);
  3150. wait_for_completion(&vscsi->unconfig);
  3151. vio_disable_interrupts(vdev);
  3152. free_irq(vdev->irq, vscsi);
  3153. destroy_workqueue(vscsi->work_q);
  3154. dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
  3155. DMA_BIDIRECTIONAL);
  3156. kfree(vscsi->map_buf);
  3157. tasklet_kill(&vscsi->work_task);
  3158. ibmvscsis_destroy_command_q(vscsi);
  3159. ibmvscsis_freetimer(vscsi);
  3160. ibmvscsis_free_cmds(vscsi);
  3161. srp_target_free(&vscsi->target);
  3162. spin_lock_bh(&ibmvscsis_dev_lock);
  3163. list_del(&vscsi->list);
  3164. spin_unlock_bh(&ibmvscsis_dev_lock);
  3165. kfree(vscsi);
  3166. }
  3167. static ssize_t system_id_show(struct device *dev,
  3168. struct device_attribute *attr, char *buf)
  3169. {
  3170. return snprintf(buf, PAGE_SIZE, "%s\n", system_id);
  3171. }
  3172. static ssize_t partition_number_show(struct device *dev,
  3173. struct device_attribute *attr, char *buf)
  3174. {
  3175. return snprintf(buf, PAGE_SIZE, "%x\n", partition_number);
  3176. }
  3177. static ssize_t unit_address_show(struct device *dev,
  3178. struct device_attribute *attr, char *buf)
  3179. {
  3180. struct scsi_info *vscsi = container_of(dev, struct scsi_info, dev);
  3181. return snprintf(buf, PAGE_SIZE, "%x\n", vscsi->dma_dev->unit_address);
  3182. }
  3183. static int ibmvscsis_get_system_info(void)
  3184. {
  3185. struct device_node *rootdn, *vdevdn;
  3186. const char *id, *model, *name;
  3187. const uint *num;
  3188. rootdn = of_find_node_by_path("/");
  3189. if (!rootdn)
  3190. return -ENOENT;
  3191. model = of_get_property(rootdn, "model", NULL);
  3192. id = of_get_property(rootdn, "system-id", NULL);
  3193. if (model && id)
  3194. snprintf(system_id, sizeof(system_id), "%s-%s", model, id);
  3195. name = of_get_property(rootdn, "ibm,partition-name", NULL);
  3196. if (name)
  3197. strncpy(partition_name, name, sizeof(partition_name));
  3198. num = of_get_property(rootdn, "ibm,partition-no", NULL);
  3199. if (num)
  3200. partition_number = of_read_number(num, 1);
  3201. of_node_put(rootdn);
  3202. vdevdn = of_find_node_by_path("/vdevice");
  3203. if (vdevdn) {
  3204. const uint *mvds;
  3205. mvds = of_get_property(vdevdn, "ibm,max-virtual-dma-size",
  3206. NULL);
  3207. if (mvds)
  3208. max_vdma_size = *mvds;
  3209. of_node_put(vdevdn);
  3210. }
  3211. return 0;
  3212. }
  3213. static char *ibmvscsis_get_fabric_wwn(struct se_portal_group *se_tpg)
  3214. {
  3215. struct ibmvscsis_tport *tport =
  3216. container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
  3217. return tport->tport_name;
  3218. }
  3219. static u16 ibmvscsis_get_tag(struct se_portal_group *se_tpg)
  3220. {
  3221. struct ibmvscsis_tport *tport =
  3222. container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
  3223. return tport->tport_tpgt;
  3224. }
  3225. static u32 ibmvscsis_get_default_depth(struct se_portal_group *se_tpg)
  3226. {
  3227. return 1;
  3228. }
  3229. static int ibmvscsis_check_true(struct se_portal_group *se_tpg)
  3230. {
  3231. return 1;
  3232. }
  3233. static int ibmvscsis_check_false(struct se_portal_group *se_tpg)
  3234. {
  3235. return 0;
  3236. }
  3237. static u32 ibmvscsis_tpg_get_inst_index(struct se_portal_group *se_tpg)
  3238. {
  3239. return 1;
  3240. }
  3241. static int ibmvscsis_check_stop_free(struct se_cmd *se_cmd)
  3242. {
  3243. return target_put_sess_cmd(se_cmd);
  3244. }
  3245. static void ibmvscsis_release_cmd(struct se_cmd *se_cmd)
  3246. {
  3247. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3248. se_cmd);
  3249. struct scsi_info *vscsi = cmd->adapter;
  3250. spin_lock_bh(&vscsi->intr_lock);
  3251. /* Remove from active_q */
  3252. list_move_tail(&cmd->list, &vscsi->waiting_rsp);
  3253. ibmvscsis_send_messages(vscsi);
  3254. spin_unlock_bh(&vscsi->intr_lock);
  3255. }
  3256. static u32 ibmvscsis_sess_get_index(struct se_session *se_sess)
  3257. {
  3258. return 0;
  3259. }
  3260. static int ibmvscsis_write_pending(struct se_cmd *se_cmd)
  3261. {
  3262. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3263. se_cmd);
  3264. struct scsi_info *vscsi = cmd->adapter;
  3265. struct iu_entry *iue = cmd->iue;
  3266. int rc;
  3267. /*
  3268. * If CLIENT_FAILED OR RESPONSE_Q_DOWN, then just return success
  3269. * since LIO can't do anything about it, and we dont want to
  3270. * attempt an srp_transfer_data.
  3271. */
  3272. if ((vscsi->flags & (CLIENT_FAILED | RESPONSE_Q_DOWN))) {
  3273. dev_err(&vscsi->dev, "write_pending failed since: %d\n",
  3274. vscsi->flags);
  3275. return -EIO;
  3276. }
  3277. rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma,
  3278. 1, 1);
  3279. if (rc) {
  3280. dev_err(&vscsi->dev, "srp_transfer_data() failed: %d\n", rc);
  3281. return -EIO;
  3282. }
  3283. /*
  3284. * We now tell TCM to add this WRITE CDB directly into the TCM storage
  3285. * object execution queue.
  3286. */
  3287. target_execute_cmd(se_cmd);
  3288. return 0;
  3289. }
  3290. static void ibmvscsis_set_default_node_attrs(struct se_node_acl *nacl)
  3291. {
  3292. }
  3293. static int ibmvscsis_get_cmd_state(struct se_cmd *se_cmd)
  3294. {
  3295. return 0;
  3296. }
  3297. static int ibmvscsis_queue_data_in(struct se_cmd *se_cmd)
  3298. {
  3299. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3300. se_cmd);
  3301. struct iu_entry *iue = cmd->iue;
  3302. struct scsi_info *vscsi = cmd->adapter;
  3303. uint len = 0;
  3304. int rc;
  3305. rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma, 1,
  3306. 1);
  3307. if (rc) {
  3308. dev_err(&vscsi->dev, "srp_transfer_data failed: %d\n", rc);
  3309. se_cmd->scsi_sense_length = 18;
  3310. memset(se_cmd->sense_buffer, 0, se_cmd->scsi_sense_length);
  3311. /* Logical Unit Communication Time-out asc/ascq = 0x0801 */
  3312. scsi_build_sense_buffer(0, se_cmd->sense_buffer, MEDIUM_ERROR,
  3313. 0x08, 0x01);
  3314. }
  3315. srp_build_response(vscsi, cmd, &len);
  3316. cmd->rsp.format = SRP_FORMAT;
  3317. cmd->rsp.len = len;
  3318. return 0;
  3319. }
  3320. static int ibmvscsis_queue_status(struct se_cmd *se_cmd)
  3321. {
  3322. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3323. se_cmd);
  3324. struct scsi_info *vscsi = cmd->adapter;
  3325. uint len;
  3326. dev_dbg(&vscsi->dev, "queue_status %p\n", se_cmd);
  3327. srp_build_response(vscsi, cmd, &len);
  3328. cmd->rsp.format = SRP_FORMAT;
  3329. cmd->rsp.len = len;
  3330. return 0;
  3331. }
  3332. static void ibmvscsis_queue_tm_rsp(struct se_cmd *se_cmd)
  3333. {
  3334. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3335. se_cmd);
  3336. struct scsi_info *vscsi = cmd->adapter;
  3337. struct ibmvscsis_cmd *cmd_itr;
  3338. struct iu_entry *iue = iue = cmd->iue;
  3339. struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
  3340. u64 tag_to_abort = be64_to_cpu(srp_tsk->task_tag);
  3341. uint len;
  3342. dev_dbg(&vscsi->dev, "queue_tm_rsp %p, status %d\n",
  3343. se_cmd, (int)se_cmd->se_tmr_req->response);
  3344. if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK &&
  3345. cmd->se_cmd.se_tmr_req->response == TMR_TASK_DOES_NOT_EXIST) {
  3346. spin_lock_bh(&vscsi->intr_lock);
  3347. list_for_each_entry(cmd_itr, &vscsi->active_q, list) {
  3348. if (tag_to_abort == cmd_itr->se_cmd.tag) {
  3349. cmd_itr->abort_cmd = cmd;
  3350. cmd->flags |= DELAY_SEND;
  3351. break;
  3352. }
  3353. }
  3354. spin_unlock_bh(&vscsi->intr_lock);
  3355. }
  3356. srp_build_response(vscsi, cmd, &len);
  3357. cmd->rsp.format = SRP_FORMAT;
  3358. cmd->rsp.len = len;
  3359. }
  3360. static void ibmvscsis_aborted_task(struct se_cmd *se_cmd)
  3361. {
  3362. struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
  3363. se_cmd);
  3364. struct scsi_info *vscsi = cmd->adapter;
  3365. dev_dbg(&vscsi->dev, "ibmvscsis_aborted_task %p task_tag: %llu\n",
  3366. se_cmd, se_cmd->tag);
  3367. }
  3368. static struct se_wwn *ibmvscsis_make_tport(struct target_fabric_configfs *tf,
  3369. struct config_group *group,
  3370. const char *name)
  3371. {
  3372. struct ibmvscsis_tport *tport;
  3373. struct scsi_info *vscsi;
  3374. tport = ibmvscsis_lookup_port(name);
  3375. if (tport) {
  3376. vscsi = container_of(tport, struct scsi_info, tport);
  3377. tport->tport_proto_id = SCSI_PROTOCOL_SRP;
  3378. dev_dbg(&vscsi->dev, "make_tport(%s), pointer:%p, tport_id:%x\n",
  3379. name, tport, tport->tport_proto_id);
  3380. return &tport->tport_wwn;
  3381. }
  3382. return ERR_PTR(-EINVAL);
  3383. }
  3384. static void ibmvscsis_drop_tport(struct se_wwn *wwn)
  3385. {
  3386. struct ibmvscsis_tport *tport = container_of(wwn,
  3387. struct ibmvscsis_tport,
  3388. tport_wwn);
  3389. struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
  3390. dev_dbg(&vscsi->dev, "drop_tport(%s)\n",
  3391. config_item_name(&tport->tport_wwn.wwn_group.cg_item));
  3392. }
  3393. static struct se_portal_group *ibmvscsis_make_tpg(struct se_wwn *wwn,
  3394. const char *name)
  3395. {
  3396. struct ibmvscsis_tport *tport =
  3397. container_of(wwn, struct ibmvscsis_tport, tport_wwn);
  3398. u16 tpgt;
  3399. int rc;
  3400. if (strstr(name, "tpgt_") != name)
  3401. return ERR_PTR(-EINVAL);
  3402. rc = kstrtou16(name + 5, 0, &tpgt);
  3403. if (rc)
  3404. return ERR_PTR(rc);
  3405. tport->tport_tpgt = tpgt;
  3406. tport->releasing = false;
  3407. rc = core_tpg_register(&tport->tport_wwn, &tport->se_tpg,
  3408. tport->tport_proto_id);
  3409. if (rc)
  3410. return ERR_PTR(rc);
  3411. return &tport->se_tpg;
  3412. }
  3413. static void ibmvscsis_drop_tpg(struct se_portal_group *se_tpg)
  3414. {
  3415. struct ibmvscsis_tport *tport = container_of(se_tpg,
  3416. struct ibmvscsis_tport,
  3417. se_tpg);
  3418. tport->releasing = true;
  3419. tport->enabled = false;
  3420. /*
  3421. * Release the virtual I_T Nexus for this ibmvscsis TPG
  3422. */
  3423. ibmvscsis_drop_nexus(tport);
  3424. /*
  3425. * Deregister the se_tpg from TCM..
  3426. */
  3427. core_tpg_deregister(se_tpg);
  3428. }
  3429. static ssize_t ibmvscsis_wwn_version_show(struct config_item *item,
  3430. char *page)
  3431. {
  3432. return scnprintf(page, PAGE_SIZE, "%s\n", IBMVSCSIS_VERSION);
  3433. }
  3434. CONFIGFS_ATTR_RO(ibmvscsis_wwn_, version);
  3435. static struct configfs_attribute *ibmvscsis_wwn_attrs[] = {
  3436. &ibmvscsis_wwn_attr_version,
  3437. NULL,
  3438. };
  3439. static int ibmvscsis_enable_tpg(struct se_portal_group *se_tpg, bool enable)
  3440. {
  3441. struct ibmvscsis_tport *tport = container_of(se_tpg,
  3442. struct ibmvscsis_tport,
  3443. se_tpg);
  3444. struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
  3445. long lrc;
  3446. if (enable) {
  3447. spin_lock_bh(&vscsi->intr_lock);
  3448. tport->enabled = true;
  3449. lrc = ibmvscsis_enable_change_state(vscsi);
  3450. if (lrc)
  3451. dev_err(&vscsi->dev, "enable_change_state failed, rc %ld state %d\n",
  3452. lrc, vscsi->state);
  3453. spin_unlock_bh(&vscsi->intr_lock);
  3454. } else {
  3455. spin_lock_bh(&vscsi->intr_lock);
  3456. tport->enabled = false;
  3457. /* This simulates the server going down */
  3458. ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
  3459. spin_unlock_bh(&vscsi->intr_lock);
  3460. }
  3461. return 0;
  3462. }
  3463. static const struct target_core_fabric_ops ibmvscsis_ops = {
  3464. .module = THIS_MODULE,
  3465. .fabric_name = "ibmvscsis",
  3466. .max_data_sg_nents = MAX_TXU / PAGE_SIZE,
  3467. .tpg_get_wwn = ibmvscsis_get_fabric_wwn,
  3468. .tpg_get_tag = ibmvscsis_get_tag,
  3469. .tpg_get_default_depth = ibmvscsis_get_default_depth,
  3470. .tpg_check_demo_mode = ibmvscsis_check_true,
  3471. .tpg_check_demo_mode_cache = ibmvscsis_check_true,
  3472. .tpg_check_demo_mode_write_protect = ibmvscsis_check_false,
  3473. .tpg_check_prod_mode_write_protect = ibmvscsis_check_false,
  3474. .tpg_get_inst_index = ibmvscsis_tpg_get_inst_index,
  3475. .check_stop_free = ibmvscsis_check_stop_free,
  3476. .release_cmd = ibmvscsis_release_cmd,
  3477. .sess_get_index = ibmvscsis_sess_get_index,
  3478. .write_pending = ibmvscsis_write_pending,
  3479. .set_default_node_attributes = ibmvscsis_set_default_node_attrs,
  3480. .get_cmd_state = ibmvscsis_get_cmd_state,
  3481. .queue_data_in = ibmvscsis_queue_data_in,
  3482. .queue_status = ibmvscsis_queue_status,
  3483. .queue_tm_rsp = ibmvscsis_queue_tm_rsp,
  3484. .aborted_task = ibmvscsis_aborted_task,
  3485. /*
  3486. * Setup function pointers for logic in target_core_fabric_configfs.c
  3487. */
  3488. .fabric_make_wwn = ibmvscsis_make_tport,
  3489. .fabric_drop_wwn = ibmvscsis_drop_tport,
  3490. .fabric_make_tpg = ibmvscsis_make_tpg,
  3491. .fabric_enable_tpg = ibmvscsis_enable_tpg,
  3492. .fabric_drop_tpg = ibmvscsis_drop_tpg,
  3493. .tfc_wwn_attrs = ibmvscsis_wwn_attrs,
  3494. };
  3495. static void ibmvscsis_dev_release(struct device *dev) {};
  3496. static struct device_attribute dev_attr_system_id =
  3497. __ATTR(system_id, S_IRUGO, system_id_show, NULL);
  3498. static struct device_attribute dev_attr_partition_number =
  3499. __ATTR(partition_number, S_IRUGO, partition_number_show, NULL);
  3500. static struct device_attribute dev_attr_unit_address =
  3501. __ATTR(unit_address, S_IRUGO, unit_address_show, NULL);
  3502. static struct attribute *ibmvscsis_dev_attrs[] = {
  3503. &dev_attr_system_id.attr,
  3504. &dev_attr_partition_number.attr,
  3505. &dev_attr_unit_address.attr,
  3506. };
  3507. ATTRIBUTE_GROUPS(ibmvscsis_dev);
  3508. static struct class ibmvscsis_class = {
  3509. .name = "ibmvscsis",
  3510. .dev_release = ibmvscsis_dev_release,
  3511. .dev_groups = ibmvscsis_dev_groups,
  3512. };
  3513. static const struct vio_device_id ibmvscsis_device_table[] = {
  3514. { "v-scsi-host", "IBM,v-scsi-host" },
  3515. { "", "" }
  3516. };
  3517. MODULE_DEVICE_TABLE(vio, ibmvscsis_device_table);
  3518. static struct vio_driver ibmvscsis_driver = {
  3519. .name = "ibmvscsis",
  3520. .id_table = ibmvscsis_device_table,
  3521. .probe = ibmvscsis_probe,
  3522. .remove = ibmvscsis_remove,
  3523. };
  3524. /*
  3525. * ibmvscsis_init() - Kernel Module initialization
  3526. *
  3527. * Note: vio_register_driver() registers callback functions, and at least one
  3528. * of those callback functions calls TCM - Linux IO Target Subsystem, thus
  3529. * the SCSI Target template must be registered before vio_register_driver()
  3530. * is called.
  3531. */
  3532. static int __init ibmvscsis_init(void)
  3533. {
  3534. int rc = 0;
  3535. rc = ibmvscsis_get_system_info();
  3536. if (rc) {
  3537. pr_err("rc %d from get_system_info\n", rc);
  3538. goto out;
  3539. }
  3540. rc = class_register(&ibmvscsis_class);
  3541. if (rc) {
  3542. pr_err("failed class register\n");
  3543. goto out;
  3544. }
  3545. rc = target_register_template(&ibmvscsis_ops);
  3546. if (rc) {
  3547. pr_err("rc %d from target_register_template\n", rc);
  3548. goto unregister_class;
  3549. }
  3550. rc = vio_register_driver(&ibmvscsis_driver);
  3551. if (rc) {
  3552. pr_err("rc %d from vio_register_driver\n", rc);
  3553. goto unregister_target;
  3554. }
  3555. return 0;
  3556. unregister_target:
  3557. target_unregister_template(&ibmvscsis_ops);
  3558. unregister_class:
  3559. class_unregister(&ibmvscsis_class);
  3560. out:
  3561. return rc;
  3562. }
  3563. static void __exit ibmvscsis_exit(void)
  3564. {
  3565. pr_info("Unregister IBM virtual SCSI host driver\n");
  3566. vio_unregister_driver(&ibmvscsis_driver);
  3567. target_unregister_template(&ibmvscsis_ops);
  3568. class_unregister(&ibmvscsis_class);
  3569. }
  3570. MODULE_DESCRIPTION("IBMVSCSIS fabric driver");
  3571. MODULE_AUTHOR("Bryant G. Ly and Michael Cyr");
  3572. MODULE_LICENSE("GPL");
  3573. MODULE_VERSION(IBMVSCSIS_VERSION);
  3574. module_init(ibmvscsis_init);
  3575. module_exit(ibmvscsis_exit);