ibmvscsi_tgt.c 104 KB

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