ib_srpt.c 105 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070
  1. /*
  2. * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
  3. * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/err.h>
  38. #include <linux/ctype.h>
  39. #include <linux/kthread.h>
  40. #include <linux/string.h>
  41. #include <linux/delay.h>
  42. #include <linux/atomic.h>
  43. #include <scsi/scsi_tcq.h>
  44. #include <target/configfs_macros.h>
  45. #include <target/target_core_base.h>
  46. #include <target/target_core_fabric_configfs.h>
  47. #include <target/target_core_fabric.h>
  48. #include <target/target_core_configfs.h>
  49. #include "ib_srpt.h"
  50. /* Name of this kernel module. */
  51. #define DRV_NAME "ib_srpt"
  52. #define DRV_VERSION "2.0.0"
  53. #define DRV_RELDATE "2011-02-14"
  54. #define SRPT_ID_STRING "Linux SRP target"
  55. #undef pr_fmt
  56. #define pr_fmt(fmt) DRV_NAME " " fmt
  57. MODULE_AUTHOR("Vu Pham and Bart Van Assche");
  58. MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
  59. "v" DRV_VERSION " (" DRV_RELDATE ")");
  60. MODULE_LICENSE("Dual BSD/GPL");
  61. /*
  62. * Global Variables
  63. */
  64. static u64 srpt_service_guid;
  65. static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
  66. static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
  67. static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
  68. module_param(srp_max_req_size, int, 0444);
  69. MODULE_PARM_DESC(srp_max_req_size,
  70. "Maximum size of SRP request messages in bytes.");
  71. static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
  72. module_param(srpt_srq_size, int, 0444);
  73. MODULE_PARM_DESC(srpt_srq_size,
  74. "Shared receive queue (SRQ) size.");
  75. static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
  76. {
  77. return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
  78. }
  79. module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
  80. 0444);
  81. MODULE_PARM_DESC(srpt_service_guid,
  82. "Using this value for ioc_guid, id_ext, and cm_listen_id"
  83. " instead of using the node_guid of the first HCA.");
  84. static struct ib_client srpt_client;
  85. static struct target_fabric_configfs *srpt_target;
  86. static void srpt_release_channel(struct srpt_rdma_ch *ch);
  87. static int srpt_queue_status(struct se_cmd *cmd);
  88. /**
  89. * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
  90. */
  91. static inline
  92. enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
  93. {
  94. switch (dir) {
  95. case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
  96. case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
  97. default: return dir;
  98. }
  99. }
  100. /**
  101. * srpt_sdev_name() - Return the name associated with the HCA.
  102. *
  103. * Examples are ib0, ib1, ...
  104. */
  105. static inline const char *srpt_sdev_name(struct srpt_device *sdev)
  106. {
  107. return sdev->device->name;
  108. }
  109. static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
  110. {
  111. unsigned long flags;
  112. enum rdma_ch_state state;
  113. spin_lock_irqsave(&ch->spinlock, flags);
  114. state = ch->state;
  115. spin_unlock_irqrestore(&ch->spinlock, flags);
  116. return state;
  117. }
  118. static enum rdma_ch_state
  119. srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
  120. {
  121. unsigned long flags;
  122. enum rdma_ch_state prev;
  123. spin_lock_irqsave(&ch->spinlock, flags);
  124. prev = ch->state;
  125. ch->state = new_state;
  126. spin_unlock_irqrestore(&ch->spinlock, flags);
  127. return prev;
  128. }
  129. /**
  130. * srpt_test_and_set_ch_state() - Test and set the channel state.
  131. *
  132. * Returns true if and only if the channel state has been set to the new state.
  133. */
  134. static bool
  135. srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
  136. enum rdma_ch_state new)
  137. {
  138. unsigned long flags;
  139. enum rdma_ch_state prev;
  140. spin_lock_irqsave(&ch->spinlock, flags);
  141. prev = ch->state;
  142. if (prev == old)
  143. ch->state = new;
  144. spin_unlock_irqrestore(&ch->spinlock, flags);
  145. return prev == old;
  146. }
  147. /**
  148. * srpt_event_handler() - Asynchronous IB event callback function.
  149. *
  150. * Callback function called by the InfiniBand core when an asynchronous IB
  151. * event occurs. This callback may occur in interrupt context. See also
  152. * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
  153. * Architecture Specification.
  154. */
  155. static void srpt_event_handler(struct ib_event_handler *handler,
  156. struct ib_event *event)
  157. {
  158. struct srpt_device *sdev;
  159. struct srpt_port *sport;
  160. sdev = ib_get_client_data(event->device, &srpt_client);
  161. if (!sdev || sdev->device != event->device)
  162. return;
  163. pr_debug("ASYNC event= %d on device= %s\n", event->event,
  164. srpt_sdev_name(sdev));
  165. switch (event->event) {
  166. case IB_EVENT_PORT_ERR:
  167. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  168. sport = &sdev->port[event->element.port_num - 1];
  169. sport->lid = 0;
  170. sport->sm_lid = 0;
  171. }
  172. break;
  173. case IB_EVENT_PORT_ACTIVE:
  174. case IB_EVENT_LID_CHANGE:
  175. case IB_EVENT_PKEY_CHANGE:
  176. case IB_EVENT_SM_CHANGE:
  177. case IB_EVENT_CLIENT_REREGISTER:
  178. /* Refresh port data asynchronously. */
  179. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  180. sport = &sdev->port[event->element.port_num - 1];
  181. if (!sport->lid && !sport->sm_lid)
  182. schedule_work(&sport->work);
  183. }
  184. break;
  185. default:
  186. printk(KERN_ERR "received unrecognized IB event %d\n",
  187. event->event);
  188. break;
  189. }
  190. }
  191. /**
  192. * srpt_srq_event() - SRQ event callback function.
  193. */
  194. static void srpt_srq_event(struct ib_event *event, void *ctx)
  195. {
  196. printk(KERN_INFO "SRQ event %d\n", event->event);
  197. }
  198. /**
  199. * srpt_qp_event() - QP event callback function.
  200. */
  201. static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
  202. {
  203. pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
  204. event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
  205. switch (event->event) {
  206. case IB_EVENT_COMM_EST:
  207. ib_cm_notify(ch->cm_id, event->event);
  208. break;
  209. case IB_EVENT_QP_LAST_WQE_REACHED:
  210. if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
  211. CH_RELEASING))
  212. srpt_release_channel(ch);
  213. else
  214. pr_debug("%s: state %d - ignored LAST_WQE.\n",
  215. ch->sess_name, srpt_get_ch_state(ch));
  216. break;
  217. default:
  218. printk(KERN_ERR "received unrecognized IB QP event %d\n",
  219. event->event);
  220. break;
  221. }
  222. }
  223. /**
  224. * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
  225. *
  226. * @slot: one-based slot number.
  227. * @value: four-bit value.
  228. *
  229. * Copies the lowest four bits of value in element slot of the array of four
  230. * bit elements called c_list (controller list). The index slot is one-based.
  231. */
  232. static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
  233. {
  234. u16 id;
  235. u8 tmp;
  236. id = (slot - 1) / 2;
  237. if (slot & 0x1) {
  238. tmp = c_list[id] & 0xf;
  239. c_list[id] = (value << 4) | tmp;
  240. } else {
  241. tmp = c_list[id] & 0xf0;
  242. c_list[id] = (value & 0xf) | tmp;
  243. }
  244. }
  245. /**
  246. * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
  247. *
  248. * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
  249. * Specification.
  250. */
  251. static void srpt_get_class_port_info(struct ib_dm_mad *mad)
  252. {
  253. struct ib_class_port_info *cif;
  254. cif = (struct ib_class_port_info *)mad->data;
  255. memset(cif, 0, sizeof *cif);
  256. cif->base_version = 1;
  257. cif->class_version = 1;
  258. cif->resp_time_value = 20;
  259. mad->mad_hdr.status = 0;
  260. }
  261. /**
  262. * srpt_get_iou() - Write IOUnitInfo to a management datagram.
  263. *
  264. * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
  265. * Specification. See also section B.7, table B.6 in the SRP r16a document.
  266. */
  267. static void srpt_get_iou(struct ib_dm_mad *mad)
  268. {
  269. struct ib_dm_iou_info *ioui;
  270. u8 slot;
  271. int i;
  272. ioui = (struct ib_dm_iou_info *)mad->data;
  273. ioui->change_id = __constant_cpu_to_be16(1);
  274. ioui->max_controllers = 16;
  275. /* set present for slot 1 and empty for the rest */
  276. srpt_set_ioc(ioui->controller_list, 1, 1);
  277. for (i = 1, slot = 2; i < 16; i++, slot++)
  278. srpt_set_ioc(ioui->controller_list, slot, 0);
  279. mad->mad_hdr.status = 0;
  280. }
  281. /**
  282. * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
  283. *
  284. * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
  285. * Architecture Specification. See also section B.7, table B.7 in the SRP
  286. * r16a document.
  287. */
  288. static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
  289. struct ib_dm_mad *mad)
  290. {
  291. struct srpt_device *sdev = sport->sdev;
  292. struct ib_dm_ioc_profile *iocp;
  293. iocp = (struct ib_dm_ioc_profile *)mad->data;
  294. if (!slot || slot > 16) {
  295. mad->mad_hdr.status
  296. = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  297. return;
  298. }
  299. if (slot > 2) {
  300. mad->mad_hdr.status
  301. = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  302. return;
  303. }
  304. memset(iocp, 0, sizeof *iocp);
  305. strcpy(iocp->id_string, SRPT_ID_STRING);
  306. iocp->guid = cpu_to_be64(srpt_service_guid);
  307. iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
  308. iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
  309. iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
  310. iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
  311. iocp->subsys_device_id = 0x0;
  312. iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
  313. iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
  314. iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
  315. iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
  316. iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
  317. iocp->rdma_read_depth = 4;
  318. iocp->send_size = cpu_to_be32(srp_max_req_size);
  319. iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
  320. 1U << 24));
  321. iocp->num_svc_entries = 1;
  322. iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
  323. SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
  324. mad->mad_hdr.status = 0;
  325. }
  326. /**
  327. * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
  328. *
  329. * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
  330. * Specification. See also section B.7, table B.8 in the SRP r16a document.
  331. */
  332. static void srpt_get_svc_entries(u64 ioc_guid,
  333. u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
  334. {
  335. struct ib_dm_svc_entries *svc_entries;
  336. WARN_ON(!ioc_guid);
  337. if (!slot || slot > 16) {
  338. mad->mad_hdr.status
  339. = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  340. return;
  341. }
  342. if (slot > 2 || lo > hi || hi > 1) {
  343. mad->mad_hdr.status
  344. = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  345. return;
  346. }
  347. svc_entries = (struct ib_dm_svc_entries *)mad->data;
  348. memset(svc_entries, 0, sizeof *svc_entries);
  349. svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
  350. snprintf(svc_entries->service_entries[0].name,
  351. sizeof(svc_entries->service_entries[0].name),
  352. "%s%016llx",
  353. SRP_SERVICE_NAME_PREFIX,
  354. ioc_guid);
  355. mad->mad_hdr.status = 0;
  356. }
  357. /**
  358. * srpt_mgmt_method_get() - Process a received management datagram.
  359. * @sp: source port through which the MAD has been received.
  360. * @rq_mad: received MAD.
  361. * @rsp_mad: response MAD.
  362. */
  363. static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
  364. struct ib_dm_mad *rsp_mad)
  365. {
  366. u16 attr_id;
  367. u32 slot;
  368. u8 hi, lo;
  369. attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
  370. switch (attr_id) {
  371. case DM_ATTR_CLASS_PORT_INFO:
  372. srpt_get_class_port_info(rsp_mad);
  373. break;
  374. case DM_ATTR_IOU_INFO:
  375. srpt_get_iou(rsp_mad);
  376. break;
  377. case DM_ATTR_IOC_PROFILE:
  378. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  379. srpt_get_ioc(sp, slot, rsp_mad);
  380. break;
  381. case DM_ATTR_SVC_ENTRIES:
  382. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  383. hi = (u8) ((slot >> 8) & 0xff);
  384. lo = (u8) (slot & 0xff);
  385. slot = (u16) ((slot >> 16) & 0xffff);
  386. srpt_get_svc_entries(srpt_service_guid,
  387. slot, hi, lo, rsp_mad);
  388. break;
  389. default:
  390. rsp_mad->mad_hdr.status =
  391. __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  392. break;
  393. }
  394. }
  395. /**
  396. * srpt_mad_send_handler() - Post MAD-send callback function.
  397. */
  398. static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
  399. struct ib_mad_send_wc *mad_wc)
  400. {
  401. ib_destroy_ah(mad_wc->send_buf->ah);
  402. ib_free_send_mad(mad_wc->send_buf);
  403. }
  404. /**
  405. * srpt_mad_recv_handler() - MAD reception callback function.
  406. */
  407. static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
  408. struct ib_mad_recv_wc *mad_wc)
  409. {
  410. struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
  411. struct ib_ah *ah;
  412. struct ib_mad_send_buf *rsp;
  413. struct ib_dm_mad *dm_mad;
  414. if (!mad_wc || !mad_wc->recv_buf.mad)
  415. return;
  416. ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
  417. mad_wc->recv_buf.grh, mad_agent->port_num);
  418. if (IS_ERR(ah))
  419. goto err;
  420. BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
  421. rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
  422. mad_wc->wc->pkey_index, 0,
  423. IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
  424. GFP_KERNEL);
  425. if (IS_ERR(rsp))
  426. goto err_rsp;
  427. rsp->ah = ah;
  428. dm_mad = rsp->mad;
  429. memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
  430. dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  431. dm_mad->mad_hdr.status = 0;
  432. switch (mad_wc->recv_buf.mad->mad_hdr.method) {
  433. case IB_MGMT_METHOD_GET:
  434. srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
  435. break;
  436. case IB_MGMT_METHOD_SET:
  437. dm_mad->mad_hdr.status =
  438. __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  439. break;
  440. default:
  441. dm_mad->mad_hdr.status =
  442. __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
  443. break;
  444. }
  445. if (!ib_post_send_mad(rsp, NULL)) {
  446. ib_free_recv_mad(mad_wc);
  447. /* will destroy_ah & free_send_mad in send completion */
  448. return;
  449. }
  450. ib_free_send_mad(rsp);
  451. err_rsp:
  452. ib_destroy_ah(ah);
  453. err:
  454. ib_free_recv_mad(mad_wc);
  455. }
  456. /**
  457. * srpt_refresh_port() - Configure a HCA port.
  458. *
  459. * Enable InfiniBand management datagram processing, update the cached sm_lid,
  460. * lid and gid values, and register a callback function for processing MADs
  461. * on the specified port.
  462. *
  463. * Note: It is safe to call this function more than once for the same port.
  464. */
  465. static int srpt_refresh_port(struct srpt_port *sport)
  466. {
  467. struct ib_mad_reg_req reg_req;
  468. struct ib_port_modify port_modify;
  469. struct ib_port_attr port_attr;
  470. int ret;
  471. memset(&port_modify, 0, sizeof port_modify);
  472. port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  473. port_modify.clr_port_cap_mask = 0;
  474. ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  475. if (ret)
  476. goto err_mod_port;
  477. ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
  478. if (ret)
  479. goto err_query_port;
  480. sport->sm_lid = port_attr.sm_lid;
  481. sport->lid = port_attr.lid;
  482. ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
  483. if (ret)
  484. goto err_query_port;
  485. if (!sport->mad_agent) {
  486. memset(&reg_req, 0, sizeof reg_req);
  487. reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
  488. reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
  489. set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
  490. set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
  491. sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
  492. sport->port,
  493. IB_QPT_GSI,
  494. &reg_req, 0,
  495. srpt_mad_send_handler,
  496. srpt_mad_recv_handler,
  497. sport);
  498. if (IS_ERR(sport->mad_agent)) {
  499. ret = PTR_ERR(sport->mad_agent);
  500. sport->mad_agent = NULL;
  501. goto err_query_port;
  502. }
  503. }
  504. return 0;
  505. err_query_port:
  506. port_modify.set_port_cap_mask = 0;
  507. port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  508. ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  509. err_mod_port:
  510. return ret;
  511. }
  512. /**
  513. * srpt_unregister_mad_agent() - Unregister MAD callback functions.
  514. *
  515. * Note: It is safe to call this function more than once for the same device.
  516. */
  517. static void srpt_unregister_mad_agent(struct srpt_device *sdev)
  518. {
  519. struct ib_port_modify port_modify = {
  520. .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
  521. };
  522. struct srpt_port *sport;
  523. int i;
  524. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  525. sport = &sdev->port[i - 1];
  526. WARN_ON(sport->port != i);
  527. if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
  528. printk(KERN_ERR "disabling MAD processing failed.\n");
  529. if (sport->mad_agent) {
  530. ib_unregister_mad_agent(sport->mad_agent);
  531. sport->mad_agent = NULL;
  532. }
  533. }
  534. }
  535. /**
  536. * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
  537. */
  538. static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
  539. int ioctx_size, int dma_size,
  540. enum dma_data_direction dir)
  541. {
  542. struct srpt_ioctx *ioctx;
  543. ioctx = kmalloc(ioctx_size, GFP_KERNEL);
  544. if (!ioctx)
  545. goto err;
  546. ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
  547. if (!ioctx->buf)
  548. goto err_free_ioctx;
  549. ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
  550. if (ib_dma_mapping_error(sdev->device, ioctx->dma))
  551. goto err_free_buf;
  552. return ioctx;
  553. err_free_buf:
  554. kfree(ioctx->buf);
  555. err_free_ioctx:
  556. kfree(ioctx);
  557. err:
  558. return NULL;
  559. }
  560. /**
  561. * srpt_free_ioctx() - Free an SRPT I/O context structure.
  562. */
  563. static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
  564. int dma_size, enum dma_data_direction dir)
  565. {
  566. if (!ioctx)
  567. return;
  568. ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
  569. kfree(ioctx->buf);
  570. kfree(ioctx);
  571. }
  572. /**
  573. * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
  574. * @sdev: Device to allocate the I/O context ring for.
  575. * @ring_size: Number of elements in the I/O context ring.
  576. * @ioctx_size: I/O context size.
  577. * @dma_size: DMA buffer size.
  578. * @dir: DMA data direction.
  579. */
  580. static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
  581. int ring_size, int ioctx_size,
  582. int dma_size, enum dma_data_direction dir)
  583. {
  584. struct srpt_ioctx **ring;
  585. int i;
  586. WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
  587. && ioctx_size != sizeof(struct srpt_send_ioctx));
  588. ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
  589. if (!ring)
  590. goto out;
  591. for (i = 0; i < ring_size; ++i) {
  592. ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
  593. if (!ring[i])
  594. goto err;
  595. ring[i]->index = i;
  596. }
  597. goto out;
  598. err:
  599. while (--i >= 0)
  600. srpt_free_ioctx(sdev, ring[i], dma_size, dir);
  601. kfree(ring);
  602. ring = NULL;
  603. out:
  604. return ring;
  605. }
  606. /**
  607. * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
  608. */
  609. static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
  610. struct srpt_device *sdev, int ring_size,
  611. int dma_size, enum dma_data_direction dir)
  612. {
  613. int i;
  614. for (i = 0; i < ring_size; ++i)
  615. srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
  616. kfree(ioctx_ring);
  617. }
  618. /**
  619. * srpt_get_cmd_state() - Get the state of a SCSI command.
  620. */
  621. static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
  622. {
  623. enum srpt_command_state state;
  624. unsigned long flags;
  625. BUG_ON(!ioctx);
  626. spin_lock_irqsave(&ioctx->spinlock, flags);
  627. state = ioctx->state;
  628. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  629. return state;
  630. }
  631. /**
  632. * srpt_set_cmd_state() - Set the state of a SCSI command.
  633. *
  634. * Does not modify the state of aborted commands. Returns the previous command
  635. * state.
  636. */
  637. static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
  638. enum srpt_command_state new)
  639. {
  640. enum srpt_command_state previous;
  641. unsigned long flags;
  642. BUG_ON(!ioctx);
  643. spin_lock_irqsave(&ioctx->spinlock, flags);
  644. previous = ioctx->state;
  645. if (previous != SRPT_STATE_DONE)
  646. ioctx->state = new;
  647. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  648. return previous;
  649. }
  650. /**
  651. * srpt_test_and_set_cmd_state() - Test and set the state of a command.
  652. *
  653. * Returns true if and only if the previous command state was equal to 'old'.
  654. */
  655. static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
  656. enum srpt_command_state old,
  657. enum srpt_command_state new)
  658. {
  659. enum srpt_command_state previous;
  660. unsigned long flags;
  661. WARN_ON(!ioctx);
  662. WARN_ON(old == SRPT_STATE_DONE);
  663. WARN_ON(new == SRPT_STATE_NEW);
  664. spin_lock_irqsave(&ioctx->spinlock, flags);
  665. previous = ioctx->state;
  666. if (previous == old)
  667. ioctx->state = new;
  668. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  669. return previous == old;
  670. }
  671. /**
  672. * srpt_post_recv() - Post an IB receive request.
  673. */
  674. static int srpt_post_recv(struct srpt_device *sdev,
  675. struct srpt_recv_ioctx *ioctx)
  676. {
  677. struct ib_sge list;
  678. struct ib_recv_wr wr, *bad_wr;
  679. BUG_ON(!sdev);
  680. wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
  681. list.addr = ioctx->ioctx.dma;
  682. list.length = srp_max_req_size;
  683. list.lkey = sdev->mr->lkey;
  684. wr.next = NULL;
  685. wr.sg_list = &list;
  686. wr.num_sge = 1;
  687. return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
  688. }
  689. /**
  690. * srpt_post_send() - Post an IB send request.
  691. *
  692. * Returns zero upon success and a non-zero value upon failure.
  693. */
  694. static int srpt_post_send(struct srpt_rdma_ch *ch,
  695. struct srpt_send_ioctx *ioctx, int len)
  696. {
  697. struct ib_sge list;
  698. struct ib_send_wr wr, *bad_wr;
  699. struct srpt_device *sdev = ch->sport->sdev;
  700. int ret;
  701. atomic_inc(&ch->req_lim);
  702. ret = -ENOMEM;
  703. if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
  704. printk(KERN_WARNING "IB send queue full (needed 1)\n");
  705. goto out;
  706. }
  707. ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
  708. DMA_TO_DEVICE);
  709. list.addr = ioctx->ioctx.dma;
  710. list.length = len;
  711. list.lkey = sdev->mr->lkey;
  712. wr.next = NULL;
  713. wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
  714. wr.sg_list = &list;
  715. wr.num_sge = 1;
  716. wr.opcode = IB_WR_SEND;
  717. wr.send_flags = IB_SEND_SIGNALED;
  718. ret = ib_post_send(ch->qp, &wr, &bad_wr);
  719. out:
  720. if (ret < 0) {
  721. atomic_inc(&ch->sq_wr_avail);
  722. atomic_dec(&ch->req_lim);
  723. }
  724. return ret;
  725. }
  726. /**
  727. * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
  728. * @ioctx: Pointer to the I/O context associated with the request.
  729. * @srp_cmd: Pointer to the SRP_CMD request data.
  730. * @dir: Pointer to the variable to which the transfer direction will be
  731. * written.
  732. * @data_len: Pointer to the variable to which the total data length of all
  733. * descriptors in the SRP_CMD request will be written.
  734. *
  735. * This function initializes ioctx->nrbuf and ioctx->r_bufs.
  736. *
  737. * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
  738. * -ENOMEM when memory allocation fails and zero upon success.
  739. */
  740. static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
  741. struct srp_cmd *srp_cmd,
  742. enum dma_data_direction *dir, u64 *data_len)
  743. {
  744. struct srp_indirect_buf *idb;
  745. struct srp_direct_buf *db;
  746. unsigned add_cdb_offset;
  747. int ret;
  748. /*
  749. * The pointer computations below will only be compiled correctly
  750. * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
  751. * whether srp_cmd::add_data has been declared as a byte pointer.
  752. */
  753. BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
  754. && !__same_type(srp_cmd->add_data[0], (u8)0));
  755. BUG_ON(!dir);
  756. BUG_ON(!data_len);
  757. ret = 0;
  758. *data_len = 0;
  759. /*
  760. * The lower four bits of the buffer format field contain the DATA-IN
  761. * buffer descriptor format, and the highest four bits contain the
  762. * DATA-OUT buffer descriptor format.
  763. */
  764. *dir = DMA_NONE;
  765. if (srp_cmd->buf_fmt & 0xf)
  766. /* DATA-IN: transfer data from target to initiator (read). */
  767. *dir = DMA_FROM_DEVICE;
  768. else if (srp_cmd->buf_fmt >> 4)
  769. /* DATA-OUT: transfer data from initiator to target (write). */
  770. *dir = DMA_TO_DEVICE;
  771. /*
  772. * According to the SRP spec, the lower two bits of the 'ADDITIONAL
  773. * CDB LENGTH' field are reserved and the size in bytes of this field
  774. * is four times the value specified in bits 3..7. Hence the "& ~3".
  775. */
  776. add_cdb_offset = srp_cmd->add_cdb_len & ~3;
  777. if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
  778. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
  779. ioctx->n_rbuf = 1;
  780. ioctx->rbufs = &ioctx->single_rbuf;
  781. db = (struct srp_direct_buf *)(srp_cmd->add_data
  782. + add_cdb_offset);
  783. memcpy(ioctx->rbufs, db, sizeof *db);
  784. *data_len = be32_to_cpu(db->len);
  785. } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
  786. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
  787. idb = (struct srp_indirect_buf *)(srp_cmd->add_data
  788. + add_cdb_offset);
  789. ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
  790. if (ioctx->n_rbuf >
  791. (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
  792. printk(KERN_ERR "received unsupported SRP_CMD request"
  793. " type (%u out + %u in != %u / %zu)\n",
  794. srp_cmd->data_out_desc_cnt,
  795. srp_cmd->data_in_desc_cnt,
  796. be32_to_cpu(idb->table_desc.len),
  797. sizeof(*db));
  798. ioctx->n_rbuf = 0;
  799. ret = -EINVAL;
  800. goto out;
  801. }
  802. if (ioctx->n_rbuf == 1)
  803. ioctx->rbufs = &ioctx->single_rbuf;
  804. else {
  805. ioctx->rbufs =
  806. kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
  807. if (!ioctx->rbufs) {
  808. ioctx->n_rbuf = 0;
  809. ret = -ENOMEM;
  810. goto out;
  811. }
  812. }
  813. db = idb->desc_list;
  814. memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
  815. *data_len = be32_to_cpu(idb->len);
  816. }
  817. out:
  818. return ret;
  819. }
  820. /**
  821. * srpt_init_ch_qp() - Initialize queue pair attributes.
  822. *
  823. * Initialized the attributes of queue pair 'qp' by allowing local write,
  824. * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
  825. */
  826. static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  827. {
  828. struct ib_qp_attr *attr;
  829. int ret;
  830. attr = kzalloc(sizeof *attr, GFP_KERNEL);
  831. if (!attr)
  832. return -ENOMEM;
  833. attr->qp_state = IB_QPS_INIT;
  834. attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
  835. IB_ACCESS_REMOTE_WRITE;
  836. attr->port_num = ch->sport->port;
  837. attr->pkey_index = 0;
  838. ret = ib_modify_qp(qp, attr,
  839. IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
  840. IB_QP_PKEY_INDEX);
  841. kfree(attr);
  842. return ret;
  843. }
  844. /**
  845. * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
  846. * @ch: channel of the queue pair.
  847. * @qp: queue pair to change the state of.
  848. *
  849. * Returns zero upon success and a negative value upon failure.
  850. *
  851. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  852. * If this structure ever becomes larger, it might be necessary to allocate
  853. * it dynamically instead of on the stack.
  854. */
  855. static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  856. {
  857. struct ib_qp_attr qp_attr;
  858. int attr_mask;
  859. int ret;
  860. qp_attr.qp_state = IB_QPS_RTR;
  861. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  862. if (ret)
  863. goto out;
  864. qp_attr.max_dest_rd_atomic = 4;
  865. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  866. out:
  867. return ret;
  868. }
  869. /**
  870. * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
  871. * @ch: channel of the queue pair.
  872. * @qp: queue pair to change the state of.
  873. *
  874. * Returns zero upon success and a negative value upon failure.
  875. *
  876. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  877. * If this structure ever becomes larger, it might be necessary to allocate
  878. * it dynamically instead of on the stack.
  879. */
  880. static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  881. {
  882. struct ib_qp_attr qp_attr;
  883. int attr_mask;
  884. int ret;
  885. qp_attr.qp_state = IB_QPS_RTS;
  886. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  887. if (ret)
  888. goto out;
  889. qp_attr.max_rd_atomic = 4;
  890. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  891. out:
  892. return ret;
  893. }
  894. /**
  895. * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
  896. */
  897. static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
  898. {
  899. struct ib_qp_attr qp_attr;
  900. qp_attr.qp_state = IB_QPS_ERR;
  901. return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
  902. }
  903. /**
  904. * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
  905. */
  906. static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
  907. struct srpt_send_ioctx *ioctx)
  908. {
  909. struct scatterlist *sg;
  910. enum dma_data_direction dir;
  911. BUG_ON(!ch);
  912. BUG_ON(!ioctx);
  913. BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
  914. while (ioctx->n_rdma)
  915. kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
  916. kfree(ioctx->rdma_ius);
  917. ioctx->rdma_ius = NULL;
  918. if (ioctx->mapped_sg_count) {
  919. sg = ioctx->sg;
  920. WARN_ON(!sg);
  921. dir = ioctx->cmd.data_direction;
  922. BUG_ON(dir == DMA_NONE);
  923. ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
  924. opposite_dma_dir(dir));
  925. ioctx->mapped_sg_count = 0;
  926. }
  927. }
  928. /**
  929. * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
  930. */
  931. static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
  932. struct srpt_send_ioctx *ioctx)
  933. {
  934. struct se_cmd *cmd;
  935. struct scatterlist *sg, *sg_orig;
  936. int sg_cnt;
  937. enum dma_data_direction dir;
  938. struct rdma_iu *riu;
  939. struct srp_direct_buf *db;
  940. dma_addr_t dma_addr;
  941. struct ib_sge *sge;
  942. u64 raddr;
  943. u32 rsize;
  944. u32 tsize;
  945. u32 dma_len;
  946. int count, nrdma;
  947. int i, j, k;
  948. BUG_ON(!ch);
  949. BUG_ON(!ioctx);
  950. cmd = &ioctx->cmd;
  951. dir = cmd->data_direction;
  952. BUG_ON(dir == DMA_NONE);
  953. transport_do_task_sg_chain(cmd);
  954. ioctx->sg = sg = sg_orig = cmd->t_tasks_sg_chained;
  955. ioctx->sg_cnt = sg_cnt = cmd->t_tasks_sg_chained_no;
  956. count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
  957. opposite_dma_dir(dir));
  958. if (unlikely(!count))
  959. return -EAGAIN;
  960. ioctx->mapped_sg_count = count;
  961. if (ioctx->rdma_ius && ioctx->n_rdma_ius)
  962. nrdma = ioctx->n_rdma_ius;
  963. else {
  964. nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
  965. + ioctx->n_rbuf;
  966. ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
  967. if (!ioctx->rdma_ius)
  968. goto free_mem;
  969. ioctx->n_rdma_ius = nrdma;
  970. }
  971. db = ioctx->rbufs;
  972. tsize = cmd->data_length;
  973. dma_len = sg_dma_len(&sg[0]);
  974. riu = ioctx->rdma_ius;
  975. /*
  976. * For each remote desc - calculate the #ib_sge.
  977. * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
  978. * each remote desc rdma_iu is required a rdma wr;
  979. * else
  980. * we need to allocate extra rdma_iu to carry extra #ib_sge in
  981. * another rdma wr
  982. */
  983. for (i = 0, j = 0;
  984. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  985. rsize = be32_to_cpu(db->len);
  986. raddr = be64_to_cpu(db->va);
  987. riu->raddr = raddr;
  988. riu->rkey = be32_to_cpu(db->key);
  989. riu->sge_cnt = 0;
  990. /* calculate how many sge required for this remote_buf */
  991. while (rsize > 0 && tsize > 0) {
  992. if (rsize >= dma_len) {
  993. tsize -= dma_len;
  994. rsize -= dma_len;
  995. raddr += dma_len;
  996. if (tsize > 0) {
  997. ++j;
  998. if (j < count) {
  999. sg = sg_next(sg);
  1000. dma_len = sg_dma_len(sg);
  1001. }
  1002. }
  1003. } else {
  1004. tsize -= rsize;
  1005. dma_len -= rsize;
  1006. rsize = 0;
  1007. }
  1008. ++riu->sge_cnt;
  1009. if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
  1010. ++ioctx->n_rdma;
  1011. riu->sge =
  1012. kmalloc(riu->sge_cnt * sizeof *riu->sge,
  1013. GFP_KERNEL);
  1014. if (!riu->sge)
  1015. goto free_mem;
  1016. ++riu;
  1017. riu->sge_cnt = 0;
  1018. riu->raddr = raddr;
  1019. riu->rkey = be32_to_cpu(db->key);
  1020. }
  1021. }
  1022. ++ioctx->n_rdma;
  1023. riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
  1024. GFP_KERNEL);
  1025. if (!riu->sge)
  1026. goto free_mem;
  1027. }
  1028. db = ioctx->rbufs;
  1029. tsize = cmd->data_length;
  1030. riu = ioctx->rdma_ius;
  1031. sg = sg_orig;
  1032. dma_len = sg_dma_len(&sg[0]);
  1033. dma_addr = sg_dma_address(&sg[0]);
  1034. /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
  1035. for (i = 0, j = 0;
  1036. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  1037. rsize = be32_to_cpu(db->len);
  1038. sge = riu->sge;
  1039. k = 0;
  1040. while (rsize > 0 && tsize > 0) {
  1041. sge->addr = dma_addr;
  1042. sge->lkey = ch->sport->sdev->mr->lkey;
  1043. if (rsize >= dma_len) {
  1044. sge->length =
  1045. (tsize < dma_len) ? tsize : dma_len;
  1046. tsize -= dma_len;
  1047. rsize -= dma_len;
  1048. if (tsize > 0) {
  1049. ++j;
  1050. if (j < count) {
  1051. sg = sg_next(sg);
  1052. dma_len = sg_dma_len(sg);
  1053. dma_addr = sg_dma_address(sg);
  1054. }
  1055. }
  1056. } else {
  1057. sge->length = (tsize < rsize) ? tsize : rsize;
  1058. tsize -= rsize;
  1059. dma_len -= rsize;
  1060. dma_addr += rsize;
  1061. rsize = 0;
  1062. }
  1063. ++k;
  1064. if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
  1065. ++riu;
  1066. sge = riu->sge;
  1067. k = 0;
  1068. } else if (rsize > 0 && tsize > 0)
  1069. ++sge;
  1070. }
  1071. }
  1072. return 0;
  1073. free_mem:
  1074. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  1075. return -ENOMEM;
  1076. }
  1077. /**
  1078. * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
  1079. */
  1080. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  1081. {
  1082. struct srpt_send_ioctx *ioctx;
  1083. unsigned long flags;
  1084. BUG_ON(!ch);
  1085. ioctx = NULL;
  1086. spin_lock_irqsave(&ch->spinlock, flags);
  1087. if (!list_empty(&ch->free_list)) {
  1088. ioctx = list_first_entry(&ch->free_list,
  1089. struct srpt_send_ioctx, free_list);
  1090. list_del(&ioctx->free_list);
  1091. }
  1092. spin_unlock_irqrestore(&ch->spinlock, flags);
  1093. if (!ioctx)
  1094. return ioctx;
  1095. BUG_ON(ioctx->ch != ch);
  1096. kref_init(&ioctx->kref);
  1097. spin_lock_init(&ioctx->spinlock);
  1098. ioctx->state = SRPT_STATE_NEW;
  1099. ioctx->n_rbuf = 0;
  1100. ioctx->rbufs = NULL;
  1101. ioctx->n_rdma = 0;
  1102. ioctx->n_rdma_ius = 0;
  1103. ioctx->rdma_ius = NULL;
  1104. ioctx->mapped_sg_count = 0;
  1105. init_completion(&ioctx->tx_done);
  1106. ioctx->queue_status_only = false;
  1107. /*
  1108. * transport_init_se_cmd() does not initialize all fields, so do it
  1109. * here.
  1110. */
  1111. memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
  1112. memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
  1113. return ioctx;
  1114. }
  1115. /**
  1116. * srpt_put_send_ioctx() - Free up resources.
  1117. */
  1118. static void srpt_put_send_ioctx(struct srpt_send_ioctx *ioctx)
  1119. {
  1120. struct srpt_rdma_ch *ch;
  1121. unsigned long flags;
  1122. BUG_ON(!ioctx);
  1123. ch = ioctx->ch;
  1124. BUG_ON(!ch);
  1125. WARN_ON(srpt_get_cmd_state(ioctx) != SRPT_STATE_DONE);
  1126. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  1127. transport_generic_free_cmd(&ioctx->cmd, 0);
  1128. if (ioctx->n_rbuf > 1) {
  1129. kfree(ioctx->rbufs);
  1130. ioctx->rbufs = NULL;
  1131. ioctx->n_rbuf = 0;
  1132. }
  1133. spin_lock_irqsave(&ch->spinlock, flags);
  1134. list_add(&ioctx->free_list, &ch->free_list);
  1135. spin_unlock_irqrestore(&ch->spinlock, flags);
  1136. }
  1137. static void srpt_put_send_ioctx_kref(struct kref *kref)
  1138. {
  1139. srpt_put_send_ioctx(container_of(kref, struct srpt_send_ioctx, kref));
  1140. }
  1141. /**
  1142. * srpt_abort_cmd() - Abort a SCSI command.
  1143. * @ioctx: I/O context associated with the SCSI command.
  1144. * @context: Preferred execution context.
  1145. */
  1146. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  1147. {
  1148. enum srpt_command_state state;
  1149. unsigned long flags;
  1150. BUG_ON(!ioctx);
  1151. /*
  1152. * If the command is in a state where the target core is waiting for
  1153. * the ib_srpt driver, change the state to the next state. Changing
  1154. * the state of the command from SRPT_STATE_NEED_DATA to
  1155. * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
  1156. * function a second time.
  1157. */
  1158. spin_lock_irqsave(&ioctx->spinlock, flags);
  1159. state = ioctx->state;
  1160. switch (state) {
  1161. case SRPT_STATE_NEED_DATA:
  1162. ioctx->state = SRPT_STATE_DATA_IN;
  1163. break;
  1164. case SRPT_STATE_DATA_IN:
  1165. case SRPT_STATE_CMD_RSP_SENT:
  1166. case SRPT_STATE_MGMT_RSP_SENT:
  1167. ioctx->state = SRPT_STATE_DONE;
  1168. break;
  1169. default:
  1170. break;
  1171. }
  1172. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  1173. if (state == SRPT_STATE_DONE)
  1174. goto out;
  1175. pr_debug("Aborting cmd with state %d and tag %lld\n", state,
  1176. ioctx->tag);
  1177. switch (state) {
  1178. case SRPT_STATE_NEW:
  1179. case SRPT_STATE_DATA_IN:
  1180. case SRPT_STATE_MGMT:
  1181. /*
  1182. * Do nothing - defer abort processing until
  1183. * srpt_queue_response() is invoked.
  1184. */
  1185. WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
  1186. break;
  1187. case SRPT_STATE_NEED_DATA:
  1188. /* DMA_TO_DEVICE (write) - RDMA read error. */
  1189. atomic_set(&ioctx->cmd.transport_lun_stop, 1);
  1190. transport_generic_handle_data(&ioctx->cmd);
  1191. break;
  1192. case SRPT_STATE_CMD_RSP_SENT:
  1193. /*
  1194. * SRP_RSP sending failed or the SRP_RSP send completion has
  1195. * not been received in time.
  1196. */
  1197. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  1198. atomic_set(&ioctx->cmd.transport_lun_stop, 1);
  1199. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1200. break;
  1201. case SRPT_STATE_MGMT_RSP_SENT:
  1202. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1203. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1204. break;
  1205. default:
  1206. WARN_ON("ERROR: unexpected command state");
  1207. break;
  1208. }
  1209. out:
  1210. return state;
  1211. }
  1212. /**
  1213. * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
  1214. */
  1215. static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
  1216. {
  1217. struct srpt_send_ioctx *ioctx;
  1218. enum srpt_command_state state;
  1219. struct se_cmd *cmd;
  1220. u32 index;
  1221. atomic_inc(&ch->sq_wr_avail);
  1222. index = idx_from_wr_id(wr_id);
  1223. ioctx = ch->ioctx_ring[index];
  1224. state = srpt_get_cmd_state(ioctx);
  1225. cmd = &ioctx->cmd;
  1226. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
  1227. && state != SRPT_STATE_MGMT_RSP_SENT
  1228. && state != SRPT_STATE_NEED_DATA
  1229. && state != SRPT_STATE_DONE);
  1230. /* If SRP_RSP sending failed, undo the ch->req_lim change. */
  1231. if (state == SRPT_STATE_CMD_RSP_SENT
  1232. || state == SRPT_STATE_MGMT_RSP_SENT)
  1233. atomic_dec(&ch->req_lim);
  1234. srpt_abort_cmd(ioctx);
  1235. }
  1236. /**
  1237. * srpt_handle_send_comp() - Process an IB send completion notification.
  1238. */
  1239. static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
  1240. struct srpt_send_ioctx *ioctx)
  1241. {
  1242. enum srpt_command_state state;
  1243. atomic_inc(&ch->sq_wr_avail);
  1244. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1245. if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
  1246. && state != SRPT_STATE_MGMT_RSP_SENT
  1247. && state != SRPT_STATE_DONE))
  1248. pr_debug("state = %d\n", state);
  1249. if (state != SRPT_STATE_DONE)
  1250. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1251. else
  1252. printk(KERN_ERR "IB completion has been received too late for"
  1253. " wr_id = %u.\n", ioctx->ioctx.index);
  1254. }
  1255. /**
  1256. * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
  1257. *
  1258. * Note: transport_generic_handle_data() is asynchronous so unmapping the
  1259. * data that has been transferred via IB RDMA must be postponed until the
  1260. * check_stop_free() callback.
  1261. */
  1262. static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
  1263. struct srpt_send_ioctx *ioctx,
  1264. enum srpt_opcode opcode)
  1265. {
  1266. WARN_ON(ioctx->n_rdma <= 0);
  1267. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1268. if (opcode == SRPT_RDMA_READ_LAST) {
  1269. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1270. SRPT_STATE_DATA_IN))
  1271. transport_generic_handle_data(&ioctx->cmd);
  1272. else
  1273. printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
  1274. __LINE__, srpt_get_cmd_state(ioctx));
  1275. } else if (opcode == SRPT_RDMA_ABORT) {
  1276. ioctx->rdma_aborted = true;
  1277. } else {
  1278. WARN(true, "unexpected opcode %d\n", opcode);
  1279. }
  1280. }
  1281. /**
  1282. * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
  1283. */
  1284. static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
  1285. struct srpt_send_ioctx *ioctx,
  1286. enum srpt_opcode opcode)
  1287. {
  1288. struct se_cmd *cmd;
  1289. enum srpt_command_state state;
  1290. cmd = &ioctx->cmd;
  1291. state = srpt_get_cmd_state(ioctx);
  1292. switch (opcode) {
  1293. case SRPT_RDMA_READ_LAST:
  1294. if (ioctx->n_rdma <= 0) {
  1295. printk(KERN_ERR "Received invalid RDMA read"
  1296. " error completion with idx %d\n",
  1297. ioctx->ioctx.index);
  1298. break;
  1299. }
  1300. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1301. if (state == SRPT_STATE_NEED_DATA)
  1302. srpt_abort_cmd(ioctx);
  1303. else
  1304. printk(KERN_ERR "%s[%d]: wrong state = %d\n",
  1305. __func__, __LINE__, state);
  1306. break;
  1307. case SRPT_RDMA_WRITE_LAST:
  1308. atomic_set(&ioctx->cmd.transport_lun_stop, 1);
  1309. break;
  1310. default:
  1311. printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
  1312. __LINE__, opcode);
  1313. break;
  1314. }
  1315. }
  1316. /**
  1317. * srpt_build_cmd_rsp() - Build an SRP_RSP response.
  1318. * @ch: RDMA channel through which the request has been received.
  1319. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1320. * be built in the buffer ioctx->buf points at and hence this function will
  1321. * overwrite the request data.
  1322. * @tag: tag of the request for which this response is being generated.
  1323. * @status: value for the STATUS field of the SRP_RSP information unit.
  1324. *
  1325. * Returns the size in bytes of the SRP_RSP response.
  1326. *
  1327. * An SRP_RSP response contains a SCSI status or service response. See also
  1328. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1329. * response. See also SPC-2 for more information about sense data.
  1330. */
  1331. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1332. struct srpt_send_ioctx *ioctx, u64 tag,
  1333. int status)
  1334. {
  1335. struct srp_rsp *srp_rsp;
  1336. const u8 *sense_data;
  1337. int sense_data_len, max_sense_len;
  1338. /*
  1339. * The lowest bit of all SAM-3 status codes is zero (see also
  1340. * paragraph 5.3 in SAM-3).
  1341. */
  1342. WARN_ON(status & 1);
  1343. srp_rsp = ioctx->ioctx.buf;
  1344. BUG_ON(!srp_rsp);
  1345. sense_data = ioctx->sense_data;
  1346. sense_data_len = ioctx->cmd.scsi_sense_length;
  1347. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1348. memset(srp_rsp, 0, sizeof *srp_rsp);
  1349. srp_rsp->opcode = SRP_RSP;
  1350. srp_rsp->req_lim_delta =
  1351. __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1352. srp_rsp->tag = tag;
  1353. srp_rsp->status = status;
  1354. if (sense_data_len) {
  1355. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1356. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1357. if (sense_data_len > max_sense_len) {
  1358. printk(KERN_WARNING "truncated sense data from %d to %d"
  1359. " bytes\n", sense_data_len, max_sense_len);
  1360. sense_data_len = max_sense_len;
  1361. }
  1362. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1363. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1364. memcpy(srp_rsp + 1, sense_data, sense_data_len);
  1365. }
  1366. return sizeof(*srp_rsp) + sense_data_len;
  1367. }
  1368. /**
  1369. * srpt_build_tskmgmt_rsp() - Build a task management response.
  1370. * @ch: RDMA channel through which the request has been received.
  1371. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1372. * @rsp_code: RSP_CODE that will be stored in the response.
  1373. * @tag: Tag of the request for which this response is being generated.
  1374. *
  1375. * Returns the size in bytes of the SRP_RSP response.
  1376. *
  1377. * An SRP_RSP response contains a SCSI status or service response. See also
  1378. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1379. * response.
  1380. */
  1381. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1382. struct srpt_send_ioctx *ioctx,
  1383. u8 rsp_code, u64 tag)
  1384. {
  1385. struct srp_rsp *srp_rsp;
  1386. int resp_data_len;
  1387. int resp_len;
  1388. resp_data_len = (rsp_code == SRP_TSK_MGMT_SUCCESS) ? 0 : 4;
  1389. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1390. srp_rsp = ioctx->ioctx.buf;
  1391. BUG_ON(!srp_rsp);
  1392. memset(srp_rsp, 0, sizeof *srp_rsp);
  1393. srp_rsp->opcode = SRP_RSP;
  1394. srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
  1395. + atomic_xchg(&ch->req_lim_delta, 0));
  1396. srp_rsp->tag = tag;
  1397. if (rsp_code != SRP_TSK_MGMT_SUCCESS) {
  1398. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1399. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1400. srp_rsp->data[3] = rsp_code;
  1401. }
  1402. return resp_len;
  1403. }
  1404. #define NO_SUCH_LUN ((uint64_t)-1LL)
  1405. /*
  1406. * SCSI LUN addressing method. See also SAM-2 and the section about
  1407. * eight byte LUNs.
  1408. */
  1409. enum scsi_lun_addr_method {
  1410. SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
  1411. SCSI_LUN_ADDR_METHOD_FLAT = 1,
  1412. SCSI_LUN_ADDR_METHOD_LUN = 2,
  1413. SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
  1414. };
  1415. /*
  1416. * srpt_unpack_lun() - Convert from network LUN to linear LUN.
  1417. *
  1418. * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
  1419. * order (big endian) to a linear LUN. Supports three LUN addressing methods:
  1420. * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
  1421. */
  1422. static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
  1423. {
  1424. uint64_t res = NO_SUCH_LUN;
  1425. int addressing_method;
  1426. if (unlikely(len < 2)) {
  1427. printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
  1428. "more", len);
  1429. goto out;
  1430. }
  1431. switch (len) {
  1432. case 8:
  1433. if ((*((__be64 *)lun) &
  1434. __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
  1435. goto out_err;
  1436. break;
  1437. case 4:
  1438. if (*((__be16 *)&lun[2]) != 0)
  1439. goto out_err;
  1440. break;
  1441. case 6:
  1442. if (*((__be32 *)&lun[2]) != 0)
  1443. goto out_err;
  1444. break;
  1445. case 2:
  1446. break;
  1447. default:
  1448. goto out_err;
  1449. }
  1450. addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
  1451. switch (addressing_method) {
  1452. case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
  1453. case SCSI_LUN_ADDR_METHOD_FLAT:
  1454. case SCSI_LUN_ADDR_METHOD_LUN:
  1455. res = *(lun + 1) | (((*lun) & 0x3f) << 8);
  1456. break;
  1457. case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
  1458. default:
  1459. printk(KERN_ERR "Unimplemented LUN addressing method %u",
  1460. addressing_method);
  1461. break;
  1462. }
  1463. out:
  1464. return res;
  1465. out_err:
  1466. printk(KERN_ERR "Support for multi-level LUNs has not yet been"
  1467. " implemented");
  1468. goto out;
  1469. }
  1470. static int srpt_check_stop_free(struct se_cmd *cmd)
  1471. {
  1472. struct srpt_send_ioctx *ioctx;
  1473. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  1474. return kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1475. }
  1476. /**
  1477. * srpt_handle_cmd() - Process SRP_CMD.
  1478. */
  1479. static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1480. struct srpt_recv_ioctx *recv_ioctx,
  1481. struct srpt_send_ioctx *send_ioctx)
  1482. {
  1483. struct se_cmd *cmd;
  1484. struct srp_cmd *srp_cmd;
  1485. uint64_t unpacked_lun;
  1486. u64 data_len;
  1487. enum dma_data_direction dir;
  1488. int ret;
  1489. BUG_ON(!send_ioctx);
  1490. srp_cmd = recv_ioctx->ioctx.buf;
  1491. kref_get(&send_ioctx->kref);
  1492. cmd = &send_ioctx->cmd;
  1493. send_ioctx->tag = srp_cmd->tag;
  1494. switch (srp_cmd->task_attr) {
  1495. case SRP_CMD_SIMPLE_Q:
  1496. cmd->sam_task_attr = MSG_SIMPLE_TAG;
  1497. break;
  1498. case SRP_CMD_ORDERED_Q:
  1499. default:
  1500. cmd->sam_task_attr = MSG_ORDERED_TAG;
  1501. break;
  1502. case SRP_CMD_HEAD_OF_Q:
  1503. cmd->sam_task_attr = MSG_HEAD_TAG;
  1504. break;
  1505. case SRP_CMD_ACA:
  1506. cmd->sam_task_attr = MSG_ACA_TAG;
  1507. break;
  1508. }
  1509. ret = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len);
  1510. if (ret) {
  1511. printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
  1512. srp_cmd->tag);
  1513. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1514. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1515. goto send_sense;
  1516. }
  1517. cmd->data_length = data_len;
  1518. cmd->data_direction = dir;
  1519. unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
  1520. sizeof(srp_cmd->lun));
  1521. if (transport_lookup_cmd_lun(cmd, unpacked_lun) < 0)
  1522. goto send_sense;
  1523. ret = transport_generic_allocate_tasks(cmd, srp_cmd->cdb);
  1524. if (cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT)
  1525. srpt_queue_status(cmd);
  1526. else if (cmd->se_cmd_flags & SCF_SCSI_CDB_EXCEPTION)
  1527. goto send_sense;
  1528. else
  1529. WARN_ON_ONCE(ret);
  1530. transport_handle_cdb_direct(cmd);
  1531. return 0;
  1532. send_sense:
  1533. transport_send_check_condition_and_sense(cmd, cmd->scsi_sense_reason,
  1534. 0);
  1535. return -1;
  1536. }
  1537. /**
  1538. * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
  1539. * @ch: RDMA channel of the task management request.
  1540. * @fn: Task management function to perform.
  1541. * @req_tag: Tag of the SRP task management request.
  1542. * @mgmt_ioctx: I/O context of the task management request.
  1543. *
  1544. * Returns zero if the target core will process the task management
  1545. * request asynchronously.
  1546. *
  1547. * Note: It is assumed that the initiator serializes tag-based task management
  1548. * requests.
  1549. */
  1550. static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
  1551. {
  1552. struct srpt_device *sdev;
  1553. struct srpt_rdma_ch *ch;
  1554. struct srpt_send_ioctx *target;
  1555. int ret, i;
  1556. ret = -EINVAL;
  1557. ch = ioctx->ch;
  1558. BUG_ON(!ch);
  1559. BUG_ON(!ch->sport);
  1560. sdev = ch->sport->sdev;
  1561. BUG_ON(!sdev);
  1562. spin_lock_irq(&sdev->spinlock);
  1563. for (i = 0; i < ch->rq_size; ++i) {
  1564. target = ch->ioctx_ring[i];
  1565. if (target->cmd.se_lun == ioctx->cmd.se_lun &&
  1566. target->tag == tag &&
  1567. srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
  1568. ret = 0;
  1569. /* now let the target core abort &target->cmd; */
  1570. break;
  1571. }
  1572. }
  1573. spin_unlock_irq(&sdev->spinlock);
  1574. return ret;
  1575. }
  1576. static int srp_tmr_to_tcm(int fn)
  1577. {
  1578. switch (fn) {
  1579. case SRP_TSK_ABORT_TASK:
  1580. return TMR_ABORT_TASK;
  1581. case SRP_TSK_ABORT_TASK_SET:
  1582. return TMR_ABORT_TASK_SET;
  1583. case SRP_TSK_CLEAR_TASK_SET:
  1584. return TMR_CLEAR_TASK_SET;
  1585. case SRP_TSK_LUN_RESET:
  1586. return TMR_LUN_RESET;
  1587. case SRP_TSK_CLEAR_ACA:
  1588. return TMR_CLEAR_ACA;
  1589. default:
  1590. return -1;
  1591. }
  1592. }
  1593. /**
  1594. * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
  1595. *
  1596. * Returns 0 if and only if the request will be processed by the target core.
  1597. *
  1598. * For more information about SRP_TSK_MGMT information units, see also section
  1599. * 6.7 in the SRP r16a document.
  1600. */
  1601. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1602. struct srpt_recv_ioctx *recv_ioctx,
  1603. struct srpt_send_ioctx *send_ioctx)
  1604. {
  1605. struct srp_tsk_mgmt *srp_tsk;
  1606. struct se_cmd *cmd;
  1607. uint64_t unpacked_lun;
  1608. int tcm_tmr;
  1609. int res;
  1610. BUG_ON(!send_ioctx);
  1611. srp_tsk = recv_ioctx->ioctx.buf;
  1612. cmd = &send_ioctx->cmd;
  1613. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
  1614. " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
  1615. srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
  1616. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1617. send_ioctx->tag = srp_tsk->tag;
  1618. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1619. if (tcm_tmr < 0) {
  1620. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1621. send_ioctx->cmd.se_tmr_req->response =
  1622. TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  1623. goto process_tmr;
  1624. }
  1625. cmd->se_tmr_req = core_tmr_alloc_req(cmd, NULL, tcm_tmr, GFP_KERNEL);
  1626. if (!cmd->se_tmr_req) {
  1627. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1628. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1629. goto process_tmr;
  1630. }
  1631. unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
  1632. sizeof(srp_tsk->lun));
  1633. res = transport_lookup_tmr_lun(&send_ioctx->cmd, unpacked_lun);
  1634. if (res) {
  1635. pr_debug("rejecting TMR for LUN %lld\n", unpacked_lun);
  1636. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1637. send_ioctx->cmd.se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1638. goto process_tmr;
  1639. }
  1640. if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK)
  1641. srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
  1642. process_tmr:
  1643. kref_get(&send_ioctx->kref);
  1644. if (!(send_ioctx->cmd.se_cmd_flags & SCF_SCSI_CDB_EXCEPTION))
  1645. transport_generic_handle_tmr(&send_ioctx->cmd);
  1646. else
  1647. transport_send_check_condition_and_sense(cmd,
  1648. cmd->scsi_sense_reason, 0);
  1649. }
  1650. /**
  1651. * srpt_handle_new_iu() - Process a newly received information unit.
  1652. * @ch: RDMA channel through which the information unit has been received.
  1653. * @ioctx: SRPT I/O context associated with the information unit.
  1654. */
  1655. static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
  1656. struct srpt_recv_ioctx *recv_ioctx,
  1657. struct srpt_send_ioctx *send_ioctx)
  1658. {
  1659. struct srp_cmd *srp_cmd;
  1660. enum rdma_ch_state ch_state;
  1661. BUG_ON(!ch);
  1662. BUG_ON(!recv_ioctx);
  1663. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1664. recv_ioctx->ioctx.dma, srp_max_req_size,
  1665. DMA_FROM_DEVICE);
  1666. ch_state = srpt_get_ch_state(ch);
  1667. if (unlikely(ch_state == CH_CONNECTING)) {
  1668. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1669. goto out;
  1670. }
  1671. if (unlikely(ch_state != CH_LIVE))
  1672. goto out;
  1673. srp_cmd = recv_ioctx->ioctx.buf;
  1674. if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
  1675. if (!send_ioctx)
  1676. send_ioctx = srpt_get_send_ioctx(ch);
  1677. if (unlikely(!send_ioctx)) {
  1678. list_add_tail(&recv_ioctx->wait_list,
  1679. &ch->cmd_wait_list);
  1680. goto out;
  1681. }
  1682. }
  1683. transport_init_se_cmd(&send_ioctx->cmd, &srpt_target->tf_ops, ch->sess,
  1684. 0, DMA_NONE, MSG_SIMPLE_TAG,
  1685. send_ioctx->sense_data);
  1686. switch (srp_cmd->opcode) {
  1687. case SRP_CMD:
  1688. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1689. break;
  1690. case SRP_TSK_MGMT:
  1691. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1692. break;
  1693. case SRP_I_LOGOUT:
  1694. printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
  1695. break;
  1696. case SRP_CRED_RSP:
  1697. pr_debug("received SRP_CRED_RSP\n");
  1698. break;
  1699. case SRP_AER_RSP:
  1700. pr_debug("received SRP_AER_RSP\n");
  1701. break;
  1702. case SRP_RSP:
  1703. printk(KERN_ERR "Received SRP_RSP\n");
  1704. break;
  1705. default:
  1706. printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
  1707. srp_cmd->opcode);
  1708. break;
  1709. }
  1710. srpt_post_recv(ch->sport->sdev, recv_ioctx);
  1711. out:
  1712. return;
  1713. }
  1714. static void srpt_process_rcv_completion(struct ib_cq *cq,
  1715. struct srpt_rdma_ch *ch,
  1716. struct ib_wc *wc)
  1717. {
  1718. struct srpt_device *sdev = ch->sport->sdev;
  1719. struct srpt_recv_ioctx *ioctx;
  1720. u32 index;
  1721. index = idx_from_wr_id(wc->wr_id);
  1722. if (wc->status == IB_WC_SUCCESS) {
  1723. int req_lim;
  1724. req_lim = atomic_dec_return(&ch->req_lim);
  1725. if (unlikely(req_lim < 0))
  1726. printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
  1727. ioctx = sdev->ioctx_ring[index];
  1728. srpt_handle_new_iu(ch, ioctx, NULL);
  1729. } else {
  1730. printk(KERN_INFO "receiving failed for idx %u with status %d\n",
  1731. index, wc->status);
  1732. }
  1733. }
  1734. /**
  1735. * srpt_process_send_completion() - Process an IB send completion.
  1736. *
  1737. * Note: Although this has not yet been observed during tests, at least in
  1738. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1739. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1740. * value in each response is set to one, and it is possible that this response
  1741. * makes the initiator send a new request before the send completion for that
  1742. * response has been processed. This could e.g. happen if the call to
  1743. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1744. * if IB retransmission causes generation of the send completion to be
  1745. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1746. * are queued on cmd_wait_list. The code below processes these delayed
  1747. * requests one at a time.
  1748. */
  1749. static void srpt_process_send_completion(struct ib_cq *cq,
  1750. struct srpt_rdma_ch *ch,
  1751. struct ib_wc *wc)
  1752. {
  1753. struct srpt_send_ioctx *send_ioctx;
  1754. uint32_t index;
  1755. enum srpt_opcode opcode;
  1756. index = idx_from_wr_id(wc->wr_id);
  1757. opcode = opcode_from_wr_id(wc->wr_id);
  1758. send_ioctx = ch->ioctx_ring[index];
  1759. if (wc->status == IB_WC_SUCCESS) {
  1760. if (opcode == SRPT_SEND)
  1761. srpt_handle_send_comp(ch, send_ioctx);
  1762. else {
  1763. WARN_ON(opcode != SRPT_RDMA_ABORT &&
  1764. wc->opcode != IB_WC_RDMA_READ);
  1765. srpt_handle_rdma_comp(ch, send_ioctx, opcode);
  1766. }
  1767. } else {
  1768. if (opcode == SRPT_SEND) {
  1769. printk(KERN_INFO "sending response for idx %u failed"
  1770. " with status %d\n", index, wc->status);
  1771. srpt_handle_send_err_comp(ch, wc->wr_id);
  1772. } else if (opcode != SRPT_RDMA_MID) {
  1773. printk(KERN_INFO "RDMA t %d for idx %u failed with"
  1774. " status %d", opcode, index, wc->status);
  1775. srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
  1776. }
  1777. }
  1778. while (unlikely(opcode == SRPT_SEND
  1779. && !list_empty(&ch->cmd_wait_list)
  1780. && srpt_get_ch_state(ch) == CH_LIVE
  1781. && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
  1782. struct srpt_recv_ioctx *recv_ioctx;
  1783. recv_ioctx = list_first_entry(&ch->cmd_wait_list,
  1784. struct srpt_recv_ioctx,
  1785. wait_list);
  1786. list_del(&recv_ioctx->wait_list);
  1787. srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
  1788. }
  1789. }
  1790. static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
  1791. {
  1792. struct ib_wc *const wc = ch->wc;
  1793. int i, n;
  1794. WARN_ON(cq != ch->cq);
  1795. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  1796. while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
  1797. for (i = 0; i < n; i++) {
  1798. if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
  1799. srpt_process_rcv_completion(cq, ch, &wc[i]);
  1800. else
  1801. srpt_process_send_completion(cq, ch, &wc[i]);
  1802. }
  1803. }
  1804. }
  1805. /**
  1806. * srpt_completion() - IB completion queue callback function.
  1807. *
  1808. * Notes:
  1809. * - It is guaranteed that a completion handler will never be invoked
  1810. * concurrently on two different CPUs for the same completion queue. See also
  1811. * Documentation/infiniband/core_locking.txt and the implementation of
  1812. * handle_edge_irq() in kernel/irq/chip.c.
  1813. * - When threaded IRQs are enabled, completion handlers are invoked in thread
  1814. * context instead of interrupt context.
  1815. */
  1816. static void srpt_completion(struct ib_cq *cq, void *ctx)
  1817. {
  1818. struct srpt_rdma_ch *ch = ctx;
  1819. wake_up_interruptible(&ch->wait_queue);
  1820. }
  1821. static int srpt_compl_thread(void *arg)
  1822. {
  1823. struct srpt_rdma_ch *ch;
  1824. /* Hibernation / freezing of the SRPT kernel thread is not supported. */
  1825. current->flags |= PF_NOFREEZE;
  1826. ch = arg;
  1827. BUG_ON(!ch);
  1828. printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
  1829. ch->sess_name, ch->thread->comm, current->pid);
  1830. while (!kthread_should_stop()) {
  1831. wait_event_interruptible(ch->wait_queue,
  1832. (srpt_process_completion(ch->cq, ch),
  1833. kthread_should_stop()));
  1834. }
  1835. printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
  1836. ch->sess_name, ch->thread->comm, current->pid);
  1837. return 0;
  1838. }
  1839. /**
  1840. * srpt_create_ch_ib() - Create receive and send completion queues.
  1841. */
  1842. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1843. {
  1844. struct ib_qp_init_attr *qp_init;
  1845. struct srpt_port *sport = ch->sport;
  1846. struct srpt_device *sdev = sport->sdev;
  1847. u32 srp_sq_size = sport->port_attrib.srp_sq_size;
  1848. int ret;
  1849. WARN_ON(ch->rq_size < 1);
  1850. ret = -ENOMEM;
  1851. qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
  1852. if (!qp_init)
  1853. goto out;
  1854. ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
  1855. ch->rq_size + srp_sq_size, 0);
  1856. if (IS_ERR(ch->cq)) {
  1857. ret = PTR_ERR(ch->cq);
  1858. printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
  1859. ch->rq_size + srp_sq_size, ret);
  1860. goto out;
  1861. }
  1862. qp_init->qp_context = (void *)ch;
  1863. qp_init->event_handler
  1864. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1865. qp_init->send_cq = ch->cq;
  1866. qp_init->recv_cq = ch->cq;
  1867. qp_init->srq = sdev->srq;
  1868. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1869. qp_init->qp_type = IB_QPT_RC;
  1870. qp_init->cap.max_send_wr = srp_sq_size;
  1871. qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
  1872. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1873. if (IS_ERR(ch->qp)) {
  1874. ret = PTR_ERR(ch->qp);
  1875. printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
  1876. goto err_destroy_cq;
  1877. }
  1878. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1879. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  1880. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1881. qp_init->cap.max_send_wr, ch->cm_id);
  1882. ret = srpt_init_ch_qp(ch, ch->qp);
  1883. if (ret)
  1884. goto err_destroy_qp;
  1885. init_waitqueue_head(&ch->wait_queue);
  1886. pr_debug("creating thread for session %s\n", ch->sess_name);
  1887. ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
  1888. if (IS_ERR(ch->thread)) {
  1889. printk(KERN_ERR "failed to create kernel thread %ld\n",
  1890. PTR_ERR(ch->thread));
  1891. ch->thread = NULL;
  1892. goto err_destroy_qp;
  1893. }
  1894. out:
  1895. kfree(qp_init);
  1896. return ret;
  1897. err_destroy_qp:
  1898. ib_destroy_qp(ch->qp);
  1899. err_destroy_cq:
  1900. ib_destroy_cq(ch->cq);
  1901. goto out;
  1902. }
  1903. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1904. {
  1905. if (ch->thread)
  1906. kthread_stop(ch->thread);
  1907. ib_destroy_qp(ch->qp);
  1908. ib_destroy_cq(ch->cq);
  1909. }
  1910. /**
  1911. * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
  1912. *
  1913. * Reset the QP and make sure all resources associated with the channel will
  1914. * be deallocated at an appropriate time.
  1915. *
  1916. * Note: The caller must hold ch->sport->sdev->spinlock.
  1917. */
  1918. static void __srpt_close_ch(struct srpt_rdma_ch *ch)
  1919. {
  1920. struct srpt_device *sdev;
  1921. enum rdma_ch_state prev_state;
  1922. unsigned long flags;
  1923. sdev = ch->sport->sdev;
  1924. spin_lock_irqsave(&ch->spinlock, flags);
  1925. prev_state = ch->state;
  1926. switch (prev_state) {
  1927. case CH_CONNECTING:
  1928. case CH_LIVE:
  1929. ch->state = CH_DISCONNECTING;
  1930. break;
  1931. default:
  1932. break;
  1933. }
  1934. spin_unlock_irqrestore(&ch->spinlock, flags);
  1935. switch (prev_state) {
  1936. case CH_CONNECTING:
  1937. ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
  1938. NULL, 0);
  1939. /* fall through */
  1940. case CH_LIVE:
  1941. if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
  1942. printk(KERN_ERR "sending CM DREQ failed.\n");
  1943. break;
  1944. case CH_DISCONNECTING:
  1945. break;
  1946. case CH_DRAINING:
  1947. case CH_RELEASING:
  1948. break;
  1949. }
  1950. }
  1951. /**
  1952. * srpt_close_ch() - Close an RDMA channel.
  1953. */
  1954. static void srpt_close_ch(struct srpt_rdma_ch *ch)
  1955. {
  1956. struct srpt_device *sdev;
  1957. sdev = ch->sport->sdev;
  1958. spin_lock_irq(&sdev->spinlock);
  1959. __srpt_close_ch(ch);
  1960. spin_unlock_irq(&sdev->spinlock);
  1961. }
  1962. /**
  1963. * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
  1964. * @cm_id: Pointer to the CM ID of the channel to be drained.
  1965. *
  1966. * Note: Must be called from inside srpt_cm_handler to avoid a race between
  1967. * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
  1968. * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
  1969. * waits until all target sessions for the associated IB device have been
  1970. * unregistered and target session registration involves a call to
  1971. * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
  1972. * this function has finished).
  1973. */
  1974. static void srpt_drain_channel(struct ib_cm_id *cm_id)
  1975. {
  1976. struct srpt_device *sdev;
  1977. struct srpt_rdma_ch *ch;
  1978. int ret;
  1979. bool do_reset = false;
  1980. WARN_ON_ONCE(irqs_disabled());
  1981. sdev = cm_id->context;
  1982. BUG_ON(!sdev);
  1983. spin_lock_irq(&sdev->spinlock);
  1984. list_for_each_entry(ch, &sdev->rch_list, list) {
  1985. if (ch->cm_id == cm_id) {
  1986. do_reset = srpt_test_and_set_ch_state(ch,
  1987. CH_CONNECTING, CH_DRAINING) ||
  1988. srpt_test_and_set_ch_state(ch,
  1989. CH_LIVE, CH_DRAINING) ||
  1990. srpt_test_and_set_ch_state(ch,
  1991. CH_DISCONNECTING, CH_DRAINING);
  1992. break;
  1993. }
  1994. }
  1995. spin_unlock_irq(&sdev->spinlock);
  1996. if (do_reset) {
  1997. ret = srpt_ch_qp_err(ch);
  1998. if (ret < 0)
  1999. printk(KERN_ERR "Setting queue pair in error state"
  2000. " failed: %d\n", ret);
  2001. }
  2002. }
  2003. /**
  2004. * srpt_find_channel() - Look up an RDMA channel.
  2005. * @cm_id: Pointer to the CM ID of the channel to be looked up.
  2006. *
  2007. * Return NULL if no matching RDMA channel has been found.
  2008. */
  2009. static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
  2010. struct ib_cm_id *cm_id)
  2011. {
  2012. struct srpt_rdma_ch *ch;
  2013. bool found;
  2014. WARN_ON_ONCE(irqs_disabled());
  2015. BUG_ON(!sdev);
  2016. found = false;
  2017. spin_lock_irq(&sdev->spinlock);
  2018. list_for_each_entry(ch, &sdev->rch_list, list) {
  2019. if (ch->cm_id == cm_id) {
  2020. found = true;
  2021. break;
  2022. }
  2023. }
  2024. spin_unlock_irq(&sdev->spinlock);
  2025. return found ? ch : NULL;
  2026. }
  2027. /**
  2028. * srpt_release_channel() - Release channel resources.
  2029. *
  2030. * Schedules the actual release because:
  2031. * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
  2032. * trigger a deadlock.
  2033. * - It is not safe to call TCM transport_* functions from interrupt context.
  2034. */
  2035. static void srpt_release_channel(struct srpt_rdma_ch *ch)
  2036. {
  2037. schedule_work(&ch->release_work);
  2038. }
  2039. static void srpt_release_channel_work(struct work_struct *w)
  2040. {
  2041. struct srpt_rdma_ch *ch;
  2042. struct srpt_device *sdev;
  2043. ch = container_of(w, struct srpt_rdma_ch, release_work);
  2044. pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
  2045. ch->release_done);
  2046. sdev = ch->sport->sdev;
  2047. BUG_ON(!sdev);
  2048. transport_deregister_session_configfs(ch->sess);
  2049. transport_deregister_session(ch->sess);
  2050. ch->sess = NULL;
  2051. srpt_destroy_ch_ib(ch);
  2052. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2053. ch->sport->sdev, ch->rq_size,
  2054. ch->rsp_size, DMA_TO_DEVICE);
  2055. spin_lock_irq(&sdev->spinlock);
  2056. list_del(&ch->list);
  2057. spin_unlock_irq(&sdev->spinlock);
  2058. ib_destroy_cm_id(ch->cm_id);
  2059. if (ch->release_done)
  2060. complete(ch->release_done);
  2061. wake_up(&sdev->ch_releaseQ);
  2062. kfree(ch);
  2063. }
  2064. static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
  2065. u8 i_port_id[16])
  2066. {
  2067. struct srpt_node_acl *nacl;
  2068. list_for_each_entry(nacl, &sport->port_acl_list, list)
  2069. if (memcmp(nacl->i_port_id, i_port_id,
  2070. sizeof(nacl->i_port_id)) == 0)
  2071. return nacl;
  2072. return NULL;
  2073. }
  2074. static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
  2075. u8 i_port_id[16])
  2076. {
  2077. struct srpt_node_acl *nacl;
  2078. spin_lock_irq(&sport->port_acl_lock);
  2079. nacl = __srpt_lookup_acl(sport, i_port_id);
  2080. spin_unlock_irq(&sport->port_acl_lock);
  2081. return nacl;
  2082. }
  2083. /**
  2084. * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
  2085. *
  2086. * Ownership of the cm_id is transferred to the target session if this
  2087. * functions returns zero. Otherwise the caller remains the owner of cm_id.
  2088. */
  2089. static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
  2090. struct ib_cm_req_event_param *param,
  2091. void *private_data)
  2092. {
  2093. struct srpt_device *sdev = cm_id->context;
  2094. struct srpt_port *sport = &sdev->port[param->port - 1];
  2095. struct srp_login_req *req;
  2096. struct srp_login_rsp *rsp;
  2097. struct srp_login_rej *rej;
  2098. struct ib_cm_rep_param *rep_param;
  2099. struct srpt_rdma_ch *ch, *tmp_ch;
  2100. struct srpt_node_acl *nacl;
  2101. u32 it_iu_len;
  2102. int i;
  2103. int ret = 0;
  2104. WARN_ON_ONCE(irqs_disabled());
  2105. if (WARN_ON(!sdev || !private_data))
  2106. return -EINVAL;
  2107. req = (struct srp_login_req *)private_data;
  2108. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  2109. printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
  2110. " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
  2111. " (guid=0x%llx:0x%llx)\n",
  2112. be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
  2113. be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
  2114. be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
  2115. be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
  2116. it_iu_len,
  2117. param->port,
  2118. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
  2119. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
  2120. rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
  2121. rej = kzalloc(sizeof *rej, GFP_KERNEL);
  2122. rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
  2123. if (!rsp || !rej || !rep_param) {
  2124. ret = -ENOMEM;
  2125. goto out;
  2126. }
  2127. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  2128. rej->reason = __constant_cpu_to_be32(
  2129. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  2130. ret = -EINVAL;
  2131. printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
  2132. " length (%d bytes) is out of range (%d .. %d)\n",
  2133. it_iu_len, 64, srp_max_req_size);
  2134. goto reject;
  2135. }
  2136. if (!sport->enabled) {
  2137. rej->reason = __constant_cpu_to_be32(
  2138. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2139. ret = -EINVAL;
  2140. printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
  2141. " has not yet been enabled\n");
  2142. goto reject;
  2143. }
  2144. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  2145. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
  2146. spin_lock_irq(&sdev->spinlock);
  2147. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
  2148. if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
  2149. && !memcmp(ch->t_port_id, req->target_port_id, 16)
  2150. && param->port == ch->sport->port
  2151. && param->listen_id == ch->sport->sdev->cm_id
  2152. && ch->cm_id) {
  2153. enum rdma_ch_state ch_state;
  2154. ch_state = srpt_get_ch_state(ch);
  2155. if (ch_state != CH_CONNECTING
  2156. && ch_state != CH_LIVE)
  2157. continue;
  2158. /* found an existing channel */
  2159. pr_debug("Found existing channel %s"
  2160. " cm_id= %p state= %d\n",
  2161. ch->sess_name, ch->cm_id, ch_state);
  2162. __srpt_close_ch(ch);
  2163. rsp->rsp_flags =
  2164. SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  2165. }
  2166. }
  2167. spin_unlock_irq(&sdev->spinlock);
  2168. } else
  2169. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  2170. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  2171. || *(__be64 *)(req->target_port_id + 8) !=
  2172. cpu_to_be64(srpt_service_guid)) {
  2173. rej->reason = __constant_cpu_to_be32(
  2174. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  2175. ret = -ENOMEM;
  2176. printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
  2177. " has an invalid target port identifier.\n");
  2178. goto reject;
  2179. }
  2180. ch = kzalloc(sizeof *ch, GFP_KERNEL);
  2181. if (!ch) {
  2182. rej->reason = __constant_cpu_to_be32(
  2183. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2184. printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
  2185. ret = -ENOMEM;
  2186. goto reject;
  2187. }
  2188. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  2189. memcpy(ch->i_port_id, req->initiator_port_id, 16);
  2190. memcpy(ch->t_port_id, req->target_port_id, 16);
  2191. ch->sport = &sdev->port[param->port - 1];
  2192. ch->cm_id = cm_id;
  2193. /*
  2194. * Avoid QUEUE_FULL conditions by limiting the number of buffers used
  2195. * for the SRP protocol to the command queue size.
  2196. */
  2197. ch->rq_size = SRPT_RQ_SIZE;
  2198. spin_lock_init(&ch->spinlock);
  2199. ch->state = CH_CONNECTING;
  2200. INIT_LIST_HEAD(&ch->cmd_wait_list);
  2201. ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  2202. ch->ioctx_ring = (struct srpt_send_ioctx **)
  2203. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  2204. sizeof(*ch->ioctx_ring[0]),
  2205. ch->rsp_size, DMA_TO_DEVICE);
  2206. if (!ch->ioctx_ring)
  2207. goto free_ch;
  2208. INIT_LIST_HEAD(&ch->free_list);
  2209. for (i = 0; i < ch->rq_size; i++) {
  2210. ch->ioctx_ring[i]->ch = ch;
  2211. list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
  2212. }
  2213. ret = srpt_create_ch_ib(ch);
  2214. if (ret) {
  2215. rej->reason = __constant_cpu_to_be32(
  2216. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2217. printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
  2218. " a new RDMA channel failed.\n");
  2219. goto free_ring;
  2220. }
  2221. ret = srpt_ch_qp_rtr(ch, ch->qp);
  2222. if (ret) {
  2223. rej->reason = __constant_cpu_to_be32(
  2224. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2225. printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
  2226. " RTR failed (error code = %d)\n", ret);
  2227. goto destroy_ib;
  2228. }
  2229. /*
  2230. * Use the initator port identifier as the session name.
  2231. */
  2232. snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
  2233. be64_to_cpu(*(__be64 *)ch->i_port_id),
  2234. be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
  2235. pr_debug("registering session %s\n", ch->sess_name);
  2236. nacl = srpt_lookup_acl(sport, ch->i_port_id);
  2237. if (!nacl) {
  2238. printk(KERN_INFO "Rejected login because no ACL has been"
  2239. " configured yet for initiator %s.\n", ch->sess_name);
  2240. rej->reason = __constant_cpu_to_be32(
  2241. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  2242. goto destroy_ib;
  2243. }
  2244. ch->sess = transport_init_session();
  2245. if (IS_ERR(ch->sess)) {
  2246. rej->reason = __constant_cpu_to_be32(
  2247. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2248. pr_debug("Failed to create session\n");
  2249. goto deregister_session;
  2250. }
  2251. ch->sess->se_node_acl = &nacl->nacl;
  2252. transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
  2253. pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
  2254. ch->sess_name, ch->cm_id);
  2255. /* create srp_login_response */
  2256. rsp->opcode = SRP_LOGIN_RSP;
  2257. rsp->tag = req->tag;
  2258. rsp->max_it_iu_len = req->req_it_iu_len;
  2259. rsp->max_ti_iu_len = req->req_it_iu_len;
  2260. ch->max_ti_iu_len = it_iu_len;
  2261. rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  2262. | SRP_BUF_FORMAT_INDIRECT);
  2263. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  2264. atomic_set(&ch->req_lim, ch->rq_size);
  2265. atomic_set(&ch->req_lim_delta, 0);
  2266. /* create cm reply */
  2267. rep_param->qp_num = ch->qp->qp_num;
  2268. rep_param->private_data = (void *)rsp;
  2269. rep_param->private_data_len = sizeof *rsp;
  2270. rep_param->rnr_retry_count = 7;
  2271. rep_param->flow_control = 1;
  2272. rep_param->failover_accepted = 0;
  2273. rep_param->srq = 1;
  2274. rep_param->responder_resources = 4;
  2275. rep_param->initiator_depth = 4;
  2276. ret = ib_send_cm_rep(cm_id, rep_param);
  2277. if (ret) {
  2278. printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
  2279. " (error code = %d)\n", ret);
  2280. goto release_channel;
  2281. }
  2282. spin_lock_irq(&sdev->spinlock);
  2283. list_add_tail(&ch->list, &sdev->rch_list);
  2284. spin_unlock_irq(&sdev->spinlock);
  2285. goto out;
  2286. release_channel:
  2287. srpt_set_ch_state(ch, CH_RELEASING);
  2288. transport_deregister_session_configfs(ch->sess);
  2289. deregister_session:
  2290. transport_deregister_session(ch->sess);
  2291. ch->sess = NULL;
  2292. destroy_ib:
  2293. srpt_destroy_ch_ib(ch);
  2294. free_ring:
  2295. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2296. ch->sport->sdev, ch->rq_size,
  2297. ch->rsp_size, DMA_TO_DEVICE);
  2298. free_ch:
  2299. kfree(ch);
  2300. reject:
  2301. rej->opcode = SRP_LOGIN_REJ;
  2302. rej->tag = req->tag;
  2303. rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  2304. | SRP_BUF_FORMAT_INDIRECT);
  2305. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  2306. (void *)rej, sizeof *rej);
  2307. out:
  2308. kfree(rep_param);
  2309. kfree(rsp);
  2310. kfree(rej);
  2311. return ret;
  2312. }
  2313. static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
  2314. {
  2315. printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
  2316. srpt_drain_channel(cm_id);
  2317. }
  2318. /**
  2319. * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
  2320. *
  2321. * An IB_CM_RTU_RECEIVED message indicates that the connection is established
  2322. * and that the recipient may begin transmitting (RTU = ready to use).
  2323. */
  2324. static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
  2325. {
  2326. struct srpt_rdma_ch *ch;
  2327. int ret;
  2328. ch = srpt_find_channel(cm_id->context, cm_id);
  2329. BUG_ON(!ch);
  2330. if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
  2331. struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
  2332. ret = srpt_ch_qp_rts(ch, ch->qp);
  2333. list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
  2334. wait_list) {
  2335. list_del(&ioctx->wait_list);
  2336. srpt_handle_new_iu(ch, ioctx, NULL);
  2337. }
  2338. if (ret)
  2339. srpt_close_ch(ch);
  2340. }
  2341. }
  2342. static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
  2343. {
  2344. printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
  2345. srpt_drain_channel(cm_id);
  2346. }
  2347. static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
  2348. {
  2349. printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
  2350. srpt_drain_channel(cm_id);
  2351. }
  2352. /**
  2353. * srpt_cm_dreq_recv() - Process reception of a DREQ message.
  2354. */
  2355. static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
  2356. {
  2357. struct srpt_rdma_ch *ch;
  2358. unsigned long flags;
  2359. bool send_drep = false;
  2360. ch = srpt_find_channel(cm_id->context, cm_id);
  2361. BUG_ON(!ch);
  2362. pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
  2363. spin_lock_irqsave(&ch->spinlock, flags);
  2364. switch (ch->state) {
  2365. case CH_CONNECTING:
  2366. case CH_LIVE:
  2367. send_drep = true;
  2368. ch->state = CH_DISCONNECTING;
  2369. break;
  2370. case CH_DISCONNECTING:
  2371. case CH_DRAINING:
  2372. case CH_RELEASING:
  2373. WARN(true, "unexpected channel state %d\n", ch->state);
  2374. break;
  2375. }
  2376. spin_unlock_irqrestore(&ch->spinlock, flags);
  2377. if (send_drep) {
  2378. if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
  2379. printk(KERN_ERR "Sending IB DREP failed.\n");
  2380. printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
  2381. ch->sess_name);
  2382. }
  2383. }
  2384. /**
  2385. * srpt_cm_drep_recv() - Process reception of a DREP message.
  2386. */
  2387. static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
  2388. {
  2389. printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
  2390. cm_id);
  2391. srpt_drain_channel(cm_id);
  2392. }
  2393. /**
  2394. * srpt_cm_handler() - IB connection manager callback function.
  2395. *
  2396. * A non-zero return value will cause the caller destroy the CM ID.
  2397. *
  2398. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  2399. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  2400. * a non-zero value in any other case will trigger a race with the
  2401. * ib_destroy_cm_id() call in srpt_release_channel().
  2402. */
  2403. static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  2404. {
  2405. int ret;
  2406. ret = 0;
  2407. switch (event->event) {
  2408. case IB_CM_REQ_RECEIVED:
  2409. ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
  2410. event->private_data);
  2411. break;
  2412. case IB_CM_REJ_RECEIVED:
  2413. srpt_cm_rej_recv(cm_id);
  2414. break;
  2415. case IB_CM_RTU_RECEIVED:
  2416. case IB_CM_USER_ESTABLISHED:
  2417. srpt_cm_rtu_recv(cm_id);
  2418. break;
  2419. case IB_CM_DREQ_RECEIVED:
  2420. srpt_cm_dreq_recv(cm_id);
  2421. break;
  2422. case IB_CM_DREP_RECEIVED:
  2423. srpt_cm_drep_recv(cm_id);
  2424. break;
  2425. case IB_CM_TIMEWAIT_EXIT:
  2426. srpt_cm_timewait_exit(cm_id);
  2427. break;
  2428. case IB_CM_REP_ERROR:
  2429. srpt_cm_rep_error(cm_id);
  2430. break;
  2431. case IB_CM_DREQ_ERROR:
  2432. printk(KERN_INFO "Received IB DREQ ERROR event.\n");
  2433. break;
  2434. case IB_CM_MRA_RECEIVED:
  2435. printk(KERN_INFO "Received IB MRA event\n");
  2436. break;
  2437. default:
  2438. printk(KERN_ERR "received unrecognized IB CM event %d\n",
  2439. event->event);
  2440. break;
  2441. }
  2442. return ret;
  2443. }
  2444. /**
  2445. * srpt_perform_rdmas() - Perform IB RDMA.
  2446. *
  2447. * Returns zero upon success or a negative number upon failure.
  2448. */
  2449. static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
  2450. struct srpt_send_ioctx *ioctx)
  2451. {
  2452. struct ib_send_wr wr;
  2453. struct ib_send_wr *bad_wr;
  2454. struct rdma_iu *riu;
  2455. int i;
  2456. int ret;
  2457. int sq_wr_avail;
  2458. enum dma_data_direction dir;
  2459. const int n_rdma = ioctx->n_rdma;
  2460. dir = ioctx->cmd.data_direction;
  2461. if (dir == DMA_TO_DEVICE) {
  2462. /* write */
  2463. ret = -ENOMEM;
  2464. sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
  2465. if (sq_wr_avail < 0) {
  2466. printk(KERN_WARNING "IB send queue full (needed %d)\n",
  2467. n_rdma);
  2468. goto out;
  2469. }
  2470. }
  2471. ioctx->rdma_aborted = false;
  2472. ret = 0;
  2473. riu = ioctx->rdma_ius;
  2474. memset(&wr, 0, sizeof wr);
  2475. for (i = 0; i < n_rdma; ++i, ++riu) {
  2476. if (dir == DMA_FROM_DEVICE) {
  2477. wr.opcode = IB_WR_RDMA_WRITE;
  2478. wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
  2479. SRPT_RDMA_WRITE_LAST :
  2480. SRPT_RDMA_MID,
  2481. ioctx->ioctx.index);
  2482. } else {
  2483. wr.opcode = IB_WR_RDMA_READ;
  2484. wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
  2485. SRPT_RDMA_READ_LAST :
  2486. SRPT_RDMA_MID,
  2487. ioctx->ioctx.index);
  2488. }
  2489. wr.next = NULL;
  2490. wr.wr.rdma.remote_addr = riu->raddr;
  2491. wr.wr.rdma.rkey = riu->rkey;
  2492. wr.num_sge = riu->sge_cnt;
  2493. wr.sg_list = riu->sge;
  2494. /* only get completion event for the last rdma write */
  2495. if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
  2496. wr.send_flags = IB_SEND_SIGNALED;
  2497. ret = ib_post_send(ch->qp, &wr, &bad_wr);
  2498. if (ret)
  2499. break;
  2500. }
  2501. if (ret)
  2502. printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
  2503. __func__, __LINE__, ret, i, n_rdma);
  2504. if (ret && i > 0) {
  2505. wr.num_sge = 0;
  2506. wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
  2507. wr.send_flags = IB_SEND_SIGNALED;
  2508. while (ch->state == CH_LIVE &&
  2509. ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
  2510. printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
  2511. ioctx->ioctx.index);
  2512. msleep(1000);
  2513. }
  2514. while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
  2515. printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
  2516. ioctx->ioctx.index);
  2517. msleep(1000);
  2518. }
  2519. }
  2520. out:
  2521. if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
  2522. atomic_add(n_rdma, &ch->sq_wr_avail);
  2523. return ret;
  2524. }
  2525. /**
  2526. * srpt_xfer_data() - Start data transfer from initiator to target.
  2527. */
  2528. static int srpt_xfer_data(struct srpt_rdma_ch *ch,
  2529. struct srpt_send_ioctx *ioctx)
  2530. {
  2531. int ret;
  2532. ret = srpt_map_sg_to_ib_sge(ch, ioctx);
  2533. if (ret) {
  2534. printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
  2535. goto out;
  2536. }
  2537. ret = srpt_perform_rdmas(ch, ioctx);
  2538. if (ret) {
  2539. if (ret == -EAGAIN || ret == -ENOMEM)
  2540. printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
  2541. __func__, __LINE__, ret);
  2542. else
  2543. printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
  2544. __func__, __LINE__, ret);
  2545. goto out_unmap;
  2546. }
  2547. out:
  2548. return ret;
  2549. out_unmap:
  2550. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2551. goto out;
  2552. }
  2553. static int srpt_write_pending_status(struct se_cmd *se_cmd)
  2554. {
  2555. struct srpt_send_ioctx *ioctx;
  2556. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2557. return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
  2558. }
  2559. /*
  2560. * srpt_write_pending() - Start data transfer from initiator to target (write).
  2561. */
  2562. static int srpt_write_pending(struct se_cmd *se_cmd)
  2563. {
  2564. struct srpt_rdma_ch *ch;
  2565. struct srpt_send_ioctx *ioctx;
  2566. enum srpt_command_state new_state;
  2567. enum rdma_ch_state ch_state;
  2568. int ret;
  2569. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2570. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  2571. WARN_ON(new_state == SRPT_STATE_DONE);
  2572. ch = ioctx->ch;
  2573. BUG_ON(!ch);
  2574. ch_state = srpt_get_ch_state(ch);
  2575. switch (ch_state) {
  2576. case CH_CONNECTING:
  2577. WARN(true, "unexpected channel state %d\n", ch_state);
  2578. ret = -EINVAL;
  2579. goto out;
  2580. case CH_LIVE:
  2581. break;
  2582. case CH_DISCONNECTING:
  2583. case CH_DRAINING:
  2584. case CH_RELEASING:
  2585. pr_debug("cmd with tag %lld: channel disconnecting\n",
  2586. ioctx->tag);
  2587. srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
  2588. ret = -EINVAL;
  2589. goto out;
  2590. }
  2591. ret = srpt_xfer_data(ch, ioctx);
  2592. out:
  2593. return ret;
  2594. }
  2595. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  2596. {
  2597. switch (tcm_mgmt_status) {
  2598. case TMR_FUNCTION_COMPLETE:
  2599. return SRP_TSK_MGMT_SUCCESS;
  2600. case TMR_FUNCTION_REJECTED:
  2601. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  2602. }
  2603. return SRP_TSK_MGMT_FAILED;
  2604. }
  2605. /**
  2606. * srpt_queue_response() - Transmits the response to a SCSI command.
  2607. *
  2608. * Callback function called by the TCM core. Must not block since it can be
  2609. * invoked on the context of the IB completion handler.
  2610. */
  2611. static int srpt_queue_response(struct se_cmd *cmd)
  2612. {
  2613. struct srpt_rdma_ch *ch;
  2614. struct srpt_send_ioctx *ioctx;
  2615. enum srpt_command_state state;
  2616. unsigned long flags;
  2617. int ret;
  2618. enum dma_data_direction dir;
  2619. int resp_len;
  2620. u8 srp_tm_status;
  2621. ret = 0;
  2622. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2623. ch = ioctx->ch;
  2624. BUG_ON(!ch);
  2625. spin_lock_irqsave(&ioctx->spinlock, flags);
  2626. state = ioctx->state;
  2627. switch (state) {
  2628. case SRPT_STATE_NEW:
  2629. case SRPT_STATE_DATA_IN:
  2630. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  2631. break;
  2632. case SRPT_STATE_MGMT:
  2633. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  2634. break;
  2635. default:
  2636. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  2637. ch, ioctx->ioctx.index, ioctx->state);
  2638. break;
  2639. }
  2640. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  2641. if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
  2642. || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
  2643. atomic_inc(&ch->req_lim_delta);
  2644. srpt_abort_cmd(ioctx);
  2645. goto out;
  2646. }
  2647. dir = ioctx->cmd.data_direction;
  2648. /* For read commands, transfer the data to the initiator. */
  2649. if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
  2650. !ioctx->queue_status_only) {
  2651. ret = srpt_xfer_data(ch, ioctx);
  2652. if (ret) {
  2653. printk(KERN_ERR "xfer_data failed for tag %llu\n",
  2654. ioctx->tag);
  2655. goto out;
  2656. }
  2657. }
  2658. if (state != SRPT_STATE_MGMT)
  2659. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
  2660. cmd->scsi_status);
  2661. else {
  2662. srp_tm_status
  2663. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2664. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2665. ioctx->tag);
  2666. }
  2667. ret = srpt_post_send(ch, ioctx, resp_len);
  2668. if (ret) {
  2669. printk(KERN_ERR "sending cmd response failed for tag %llu\n",
  2670. ioctx->tag);
  2671. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2672. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2673. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  2674. }
  2675. out:
  2676. return ret;
  2677. }
  2678. static int srpt_queue_status(struct se_cmd *cmd)
  2679. {
  2680. struct srpt_send_ioctx *ioctx;
  2681. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2682. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2683. if (cmd->se_cmd_flags &
  2684. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2685. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2686. ioctx->queue_status_only = true;
  2687. return srpt_queue_response(cmd);
  2688. }
  2689. static void srpt_refresh_port_work(struct work_struct *work)
  2690. {
  2691. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2692. srpt_refresh_port(sport);
  2693. }
  2694. static int srpt_ch_list_empty(struct srpt_device *sdev)
  2695. {
  2696. int res;
  2697. spin_lock_irq(&sdev->spinlock);
  2698. res = list_empty(&sdev->rch_list);
  2699. spin_unlock_irq(&sdev->spinlock);
  2700. return res;
  2701. }
  2702. /**
  2703. * srpt_release_sdev() - Free the channel resources associated with a target.
  2704. */
  2705. static int srpt_release_sdev(struct srpt_device *sdev)
  2706. {
  2707. struct srpt_rdma_ch *ch, *tmp_ch;
  2708. int res;
  2709. WARN_ON_ONCE(irqs_disabled());
  2710. BUG_ON(!sdev);
  2711. spin_lock_irq(&sdev->spinlock);
  2712. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
  2713. __srpt_close_ch(ch);
  2714. spin_unlock_irq(&sdev->spinlock);
  2715. res = wait_event_interruptible(sdev->ch_releaseQ,
  2716. srpt_ch_list_empty(sdev));
  2717. if (res)
  2718. printk(KERN_ERR "%s: interrupted.\n", __func__);
  2719. return 0;
  2720. }
  2721. static struct srpt_port *__srpt_lookup_port(const char *name)
  2722. {
  2723. struct ib_device *dev;
  2724. struct srpt_device *sdev;
  2725. struct srpt_port *sport;
  2726. int i;
  2727. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2728. dev = sdev->device;
  2729. if (!dev)
  2730. continue;
  2731. for (i = 0; i < dev->phys_port_cnt; i++) {
  2732. sport = &sdev->port[i];
  2733. if (!strcmp(sport->port_guid, name))
  2734. return sport;
  2735. }
  2736. }
  2737. return NULL;
  2738. }
  2739. static struct srpt_port *srpt_lookup_port(const char *name)
  2740. {
  2741. struct srpt_port *sport;
  2742. spin_lock(&srpt_dev_lock);
  2743. sport = __srpt_lookup_port(name);
  2744. spin_unlock(&srpt_dev_lock);
  2745. return sport;
  2746. }
  2747. /**
  2748. * srpt_add_one() - Infiniband device addition callback function.
  2749. */
  2750. static void srpt_add_one(struct ib_device *device)
  2751. {
  2752. struct srpt_device *sdev;
  2753. struct srpt_port *sport;
  2754. struct ib_srq_init_attr srq_attr;
  2755. int i;
  2756. pr_debug("device = %p, device->dma_ops = %p\n", device,
  2757. device->dma_ops);
  2758. sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
  2759. if (!sdev)
  2760. goto err;
  2761. sdev->device = device;
  2762. INIT_LIST_HEAD(&sdev->rch_list);
  2763. init_waitqueue_head(&sdev->ch_releaseQ);
  2764. spin_lock_init(&sdev->spinlock);
  2765. if (ib_query_device(device, &sdev->dev_attr))
  2766. goto free_dev;
  2767. sdev->pd = ib_alloc_pd(device);
  2768. if (IS_ERR(sdev->pd))
  2769. goto free_dev;
  2770. sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
  2771. if (IS_ERR(sdev->mr))
  2772. goto err_pd;
  2773. sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
  2774. srq_attr.event_handler = srpt_srq_event;
  2775. srq_attr.srq_context = (void *)sdev;
  2776. srq_attr.attr.max_wr = sdev->srq_size;
  2777. srq_attr.attr.max_sge = 1;
  2778. srq_attr.attr.srq_limit = 0;
  2779. sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
  2780. if (IS_ERR(sdev->srq))
  2781. goto err_mr;
  2782. pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
  2783. __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
  2784. device->name);
  2785. if (!srpt_service_guid)
  2786. srpt_service_guid = be64_to_cpu(device->node_guid);
  2787. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2788. if (IS_ERR(sdev->cm_id))
  2789. goto err_srq;
  2790. /* print out target login information */
  2791. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2792. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2793. srpt_service_guid, srpt_service_guid);
  2794. /*
  2795. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2796. * to identify this target. We currently use the guid of the first HCA
  2797. * in the system as service_id; therefore, the target_id will change
  2798. * if this HCA is gone bad and replaced by different HCA
  2799. */
  2800. if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
  2801. goto err_cm;
  2802. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2803. srpt_event_handler);
  2804. if (ib_register_event_handler(&sdev->event_handler))
  2805. goto err_cm;
  2806. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2807. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2808. sizeof(*sdev->ioctx_ring[0]),
  2809. srp_max_req_size, DMA_FROM_DEVICE);
  2810. if (!sdev->ioctx_ring)
  2811. goto err_event;
  2812. for (i = 0; i < sdev->srq_size; ++i)
  2813. srpt_post_recv(sdev, sdev->ioctx_ring[i]);
  2814. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2815. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2816. sport = &sdev->port[i - 1];
  2817. sport->sdev = sdev;
  2818. sport->port = i;
  2819. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2820. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2821. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2822. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2823. INIT_LIST_HEAD(&sport->port_acl_list);
  2824. spin_lock_init(&sport->port_acl_lock);
  2825. if (srpt_refresh_port(sport)) {
  2826. printk(KERN_ERR "MAD registration failed for %s-%d.\n",
  2827. srpt_sdev_name(sdev), i);
  2828. goto err_ring;
  2829. }
  2830. snprintf(sport->port_guid, sizeof(sport->port_guid),
  2831. "0x%016llx%016llx",
  2832. be64_to_cpu(sport->gid.global.subnet_prefix),
  2833. be64_to_cpu(sport->gid.global.interface_id));
  2834. }
  2835. spin_lock(&srpt_dev_lock);
  2836. list_add_tail(&sdev->list, &srpt_dev_list);
  2837. spin_unlock(&srpt_dev_lock);
  2838. out:
  2839. ib_set_client_data(device, &srpt_client, sdev);
  2840. pr_debug("added %s.\n", device->name);
  2841. return;
  2842. err_ring:
  2843. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2844. sdev->srq_size, srp_max_req_size,
  2845. DMA_FROM_DEVICE);
  2846. err_event:
  2847. ib_unregister_event_handler(&sdev->event_handler);
  2848. err_cm:
  2849. ib_destroy_cm_id(sdev->cm_id);
  2850. err_srq:
  2851. ib_destroy_srq(sdev->srq);
  2852. err_mr:
  2853. ib_dereg_mr(sdev->mr);
  2854. err_pd:
  2855. ib_dealloc_pd(sdev->pd);
  2856. free_dev:
  2857. kfree(sdev);
  2858. err:
  2859. sdev = NULL;
  2860. printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
  2861. goto out;
  2862. }
  2863. /**
  2864. * srpt_remove_one() - InfiniBand device removal callback function.
  2865. */
  2866. static void srpt_remove_one(struct ib_device *device)
  2867. {
  2868. struct srpt_device *sdev;
  2869. int i;
  2870. sdev = ib_get_client_data(device, &srpt_client);
  2871. if (!sdev) {
  2872. printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
  2873. device->name);
  2874. return;
  2875. }
  2876. srpt_unregister_mad_agent(sdev);
  2877. ib_unregister_event_handler(&sdev->event_handler);
  2878. /* Cancel any work queued by the just unregistered IB event handler. */
  2879. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2880. cancel_work_sync(&sdev->port[i].work);
  2881. ib_destroy_cm_id(sdev->cm_id);
  2882. /*
  2883. * Unregistering a target must happen after destroying sdev->cm_id
  2884. * such that no new SRP_LOGIN_REQ information units can arrive while
  2885. * destroying the target.
  2886. */
  2887. spin_lock(&srpt_dev_lock);
  2888. list_del(&sdev->list);
  2889. spin_unlock(&srpt_dev_lock);
  2890. srpt_release_sdev(sdev);
  2891. ib_destroy_srq(sdev->srq);
  2892. ib_dereg_mr(sdev->mr);
  2893. ib_dealloc_pd(sdev->pd);
  2894. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2895. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2896. sdev->ioctx_ring = NULL;
  2897. kfree(sdev);
  2898. }
  2899. static struct ib_client srpt_client = {
  2900. .name = DRV_NAME,
  2901. .add = srpt_add_one,
  2902. .remove = srpt_remove_one
  2903. };
  2904. static int srpt_check_true(struct se_portal_group *se_tpg)
  2905. {
  2906. return 1;
  2907. }
  2908. static int srpt_check_false(struct se_portal_group *se_tpg)
  2909. {
  2910. return 0;
  2911. }
  2912. static char *srpt_get_fabric_name(void)
  2913. {
  2914. return "srpt";
  2915. }
  2916. static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  2917. {
  2918. return SCSI_TRANSPORTID_PROTOCOLID_SRP;
  2919. }
  2920. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2921. {
  2922. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  2923. return sport->port_guid;
  2924. }
  2925. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2926. {
  2927. return 1;
  2928. }
  2929. static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
  2930. {
  2931. return 1;
  2932. }
  2933. static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
  2934. struct se_node_acl *se_nacl,
  2935. struct t10_pr_registration *pr_reg,
  2936. int *format_code, unsigned char *buf)
  2937. {
  2938. struct srpt_node_acl *nacl;
  2939. struct spc_rdma_transport_id *tr_id;
  2940. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  2941. tr_id = (void *)buf;
  2942. tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
  2943. memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
  2944. return sizeof(*tr_id);
  2945. }
  2946. static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
  2947. struct se_node_acl *se_nacl,
  2948. struct t10_pr_registration *pr_reg,
  2949. int *format_code)
  2950. {
  2951. *format_code = 0;
  2952. return sizeof(struct spc_rdma_transport_id);
  2953. }
  2954. static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
  2955. const char *buf, u32 *out_tid_len,
  2956. char **port_nexus_ptr)
  2957. {
  2958. struct spc_rdma_transport_id *tr_id;
  2959. *port_nexus_ptr = NULL;
  2960. *out_tid_len = sizeof(struct spc_rdma_transport_id);
  2961. tr_id = (void *)buf;
  2962. return (char *)tr_id->i_port_id;
  2963. }
  2964. static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
  2965. {
  2966. struct srpt_node_acl *nacl;
  2967. nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
  2968. if (!nacl) {
  2969. printk(KERN_ERR "Unable to alocate struct srpt_node_acl\n");
  2970. return NULL;
  2971. }
  2972. return &nacl->nacl;
  2973. }
  2974. static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
  2975. struct se_node_acl *se_nacl)
  2976. {
  2977. struct srpt_node_acl *nacl;
  2978. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  2979. kfree(nacl);
  2980. }
  2981. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2982. {
  2983. return 1;
  2984. }
  2985. static void srpt_release_cmd(struct se_cmd *se_cmd)
  2986. {
  2987. }
  2988. /**
  2989. * srpt_shutdown_session() - Whether or not a session may be shut down.
  2990. */
  2991. static int srpt_shutdown_session(struct se_session *se_sess)
  2992. {
  2993. return true;
  2994. }
  2995. /**
  2996. * srpt_close_session() - Forcibly close a session.
  2997. *
  2998. * Callback function invoked by the TCM core to clean up sessions associated
  2999. * with a node ACL when the user invokes
  3000. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3001. */
  3002. static void srpt_close_session(struct se_session *se_sess)
  3003. {
  3004. DECLARE_COMPLETION_ONSTACK(release_done);
  3005. struct srpt_rdma_ch *ch;
  3006. struct srpt_device *sdev;
  3007. int res;
  3008. ch = se_sess->fabric_sess_ptr;
  3009. WARN_ON(ch->sess != se_sess);
  3010. pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
  3011. sdev = ch->sport->sdev;
  3012. spin_lock_irq(&sdev->spinlock);
  3013. BUG_ON(ch->release_done);
  3014. ch->release_done = &release_done;
  3015. __srpt_close_ch(ch);
  3016. spin_unlock_irq(&sdev->spinlock);
  3017. res = wait_for_completion_timeout(&release_done, 60 * HZ);
  3018. WARN_ON(res <= 0);
  3019. }
  3020. /**
  3021. * To do: Find out whether stop_session() has a meaning for transports
  3022. * other than iSCSI.
  3023. */
  3024. static void srpt_stop_session(struct se_session *se_sess, int sess_sleep,
  3025. int conn_sleep)
  3026. {
  3027. }
  3028. static void srpt_reset_nexus(struct se_session *sess)
  3029. {
  3030. printk(KERN_ERR "This is the SRP protocol, not iSCSI\n");
  3031. }
  3032. static int srpt_sess_logged_in(struct se_session *se_sess)
  3033. {
  3034. return true;
  3035. }
  3036. /**
  3037. * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
  3038. *
  3039. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  3040. * This object represents an arbitrary integer used to uniquely identify a
  3041. * particular attached remote initiator port to a particular SCSI target port
  3042. * within a particular SCSI target device within a particular SCSI instance.
  3043. */
  3044. static u32 srpt_sess_get_index(struct se_session *se_sess)
  3045. {
  3046. return 0;
  3047. }
  3048. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  3049. {
  3050. }
  3051. static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
  3052. {
  3053. struct srpt_send_ioctx *ioctx;
  3054. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  3055. return ioctx->tag;
  3056. }
  3057. /* Note: only used from inside debug printk's by the TCM core. */
  3058. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  3059. {
  3060. struct srpt_send_ioctx *ioctx;
  3061. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  3062. return srpt_get_cmd_state(ioctx);
  3063. }
  3064. static u16 srpt_set_fabric_sense_len(struct se_cmd *cmd, u32 sense_length)
  3065. {
  3066. return 0;
  3067. }
  3068. static u16 srpt_get_fabric_sense_len(void)
  3069. {
  3070. return 0;
  3071. }
  3072. static int srpt_is_state_remove(struct se_cmd *se_cmd)
  3073. {
  3074. return 0;
  3075. }
  3076. /**
  3077. * srpt_parse_i_port_id() - Parse an initiator port ID.
  3078. * @name: ASCII representation of a 128-bit initiator port ID.
  3079. * @i_port_id: Binary 128-bit port ID.
  3080. */
  3081. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  3082. {
  3083. const char *p;
  3084. unsigned len, count, leading_zero_bytes;
  3085. int ret, rc;
  3086. p = name;
  3087. if (strnicmp(p, "0x", 2) == 0)
  3088. p += 2;
  3089. ret = -EINVAL;
  3090. len = strlen(p);
  3091. if (len % 2)
  3092. goto out;
  3093. count = min(len / 2, 16U);
  3094. leading_zero_bytes = 16 - count;
  3095. memset(i_port_id, 0, leading_zero_bytes);
  3096. rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
  3097. if (rc < 0)
  3098. pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
  3099. ret = 0;
  3100. out:
  3101. return ret;
  3102. }
  3103. /*
  3104. * configfs callback function invoked for
  3105. * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3106. */
  3107. static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
  3108. struct config_group *group,
  3109. const char *name)
  3110. {
  3111. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  3112. struct se_node_acl *se_nacl, *se_nacl_new;
  3113. struct srpt_node_acl *nacl;
  3114. int ret = 0;
  3115. u32 nexus_depth = 1;
  3116. u8 i_port_id[16];
  3117. if (srpt_parse_i_port_id(i_port_id, name) < 0) {
  3118. printk(KERN_ERR "invalid initiator port ID %s\n", name);
  3119. ret = -EINVAL;
  3120. goto err;
  3121. }
  3122. se_nacl_new = srpt_alloc_fabric_acl(tpg);
  3123. if (!se_nacl_new) {
  3124. ret = -ENOMEM;
  3125. goto err;
  3126. }
  3127. /*
  3128. * nacl_new may be released by core_tpg_add_initiator_node_acl()
  3129. * when converting a node ACL from demo mode to explict
  3130. */
  3131. se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
  3132. nexus_depth);
  3133. if (IS_ERR(se_nacl)) {
  3134. ret = PTR_ERR(se_nacl);
  3135. goto err;
  3136. }
  3137. /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
  3138. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3139. memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
  3140. nacl->sport = sport;
  3141. spin_lock_irq(&sport->port_acl_lock);
  3142. list_add_tail(&nacl->list, &sport->port_acl_list);
  3143. spin_unlock_irq(&sport->port_acl_lock);
  3144. return se_nacl;
  3145. err:
  3146. return ERR_PTR(ret);
  3147. }
  3148. /*
  3149. * configfs callback function invoked for
  3150. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3151. */
  3152. static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
  3153. {
  3154. struct srpt_node_acl *nacl;
  3155. struct srpt_device *sdev;
  3156. struct srpt_port *sport;
  3157. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3158. sport = nacl->sport;
  3159. sdev = sport->sdev;
  3160. spin_lock_irq(&sport->port_acl_lock);
  3161. list_del(&nacl->list);
  3162. spin_unlock_irq(&sport->port_acl_lock);
  3163. core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
  3164. srpt_release_fabric_acl(NULL, se_nacl);
  3165. }
  3166. static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
  3167. struct se_portal_group *se_tpg,
  3168. char *page)
  3169. {
  3170. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3171. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  3172. }
  3173. static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
  3174. struct se_portal_group *se_tpg,
  3175. const char *page,
  3176. size_t count)
  3177. {
  3178. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3179. unsigned long val;
  3180. int ret;
  3181. ret = strict_strtoul(page, 0, &val);
  3182. if (ret < 0) {
  3183. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3184. return -EINVAL;
  3185. }
  3186. if (val > MAX_SRPT_RDMA_SIZE) {
  3187. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  3188. MAX_SRPT_RDMA_SIZE);
  3189. return -EINVAL;
  3190. }
  3191. if (val < DEFAULT_MAX_RDMA_SIZE) {
  3192. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  3193. val, DEFAULT_MAX_RDMA_SIZE);
  3194. return -EINVAL;
  3195. }
  3196. sport->port_attrib.srp_max_rdma_size = val;
  3197. return count;
  3198. }
  3199. TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
  3200. static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
  3201. struct se_portal_group *se_tpg,
  3202. char *page)
  3203. {
  3204. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3205. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  3206. }
  3207. static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
  3208. struct se_portal_group *se_tpg,
  3209. const char *page,
  3210. size_t count)
  3211. {
  3212. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3213. unsigned long val;
  3214. int ret;
  3215. ret = strict_strtoul(page, 0, &val);
  3216. if (ret < 0) {
  3217. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3218. return -EINVAL;
  3219. }
  3220. if (val > MAX_SRPT_RSP_SIZE) {
  3221. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  3222. MAX_SRPT_RSP_SIZE);
  3223. return -EINVAL;
  3224. }
  3225. if (val < MIN_MAX_RSP_SIZE) {
  3226. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  3227. MIN_MAX_RSP_SIZE);
  3228. return -EINVAL;
  3229. }
  3230. sport->port_attrib.srp_max_rsp_size = val;
  3231. return count;
  3232. }
  3233. TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
  3234. static ssize_t srpt_tpg_attrib_show_srp_sq_size(
  3235. struct se_portal_group *se_tpg,
  3236. char *page)
  3237. {
  3238. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3239. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  3240. }
  3241. static ssize_t srpt_tpg_attrib_store_srp_sq_size(
  3242. struct se_portal_group *se_tpg,
  3243. const char *page,
  3244. size_t count)
  3245. {
  3246. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3247. unsigned long val;
  3248. int ret;
  3249. ret = strict_strtoul(page, 0, &val);
  3250. if (ret < 0) {
  3251. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3252. return -EINVAL;
  3253. }
  3254. if (val > MAX_SRPT_SRQ_SIZE) {
  3255. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  3256. MAX_SRPT_SRQ_SIZE);
  3257. return -EINVAL;
  3258. }
  3259. if (val < MIN_SRPT_SRQ_SIZE) {
  3260. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  3261. MIN_SRPT_SRQ_SIZE);
  3262. return -EINVAL;
  3263. }
  3264. sport->port_attrib.srp_sq_size = val;
  3265. return count;
  3266. }
  3267. TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
  3268. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  3269. &srpt_tpg_attrib_srp_max_rdma_size.attr,
  3270. &srpt_tpg_attrib_srp_max_rsp_size.attr,
  3271. &srpt_tpg_attrib_srp_sq_size.attr,
  3272. NULL,
  3273. };
  3274. static ssize_t srpt_tpg_show_enable(
  3275. struct se_portal_group *se_tpg,
  3276. char *page)
  3277. {
  3278. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3279. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  3280. }
  3281. static ssize_t srpt_tpg_store_enable(
  3282. struct se_portal_group *se_tpg,
  3283. const char *page,
  3284. size_t count)
  3285. {
  3286. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3287. unsigned long tmp;
  3288. int ret;
  3289. ret = strict_strtoul(page, 0, &tmp);
  3290. if (ret < 0) {
  3291. printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
  3292. return -EINVAL;
  3293. }
  3294. if ((tmp != 0) && (tmp != 1)) {
  3295. printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  3296. return -EINVAL;
  3297. }
  3298. if (tmp == 1)
  3299. sport->enabled = true;
  3300. else
  3301. sport->enabled = false;
  3302. return count;
  3303. }
  3304. TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
  3305. static struct configfs_attribute *srpt_tpg_attrs[] = {
  3306. &srpt_tpg_enable.attr,
  3307. NULL,
  3308. };
  3309. /**
  3310. * configfs callback invoked for
  3311. * mkdir /sys/kernel/config/target/$driver/$port/$tpg
  3312. */
  3313. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  3314. struct config_group *group,
  3315. const char *name)
  3316. {
  3317. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  3318. int res;
  3319. /* Initialize sport->port_wwn and sport->port_tpg_1 */
  3320. res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
  3321. &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
  3322. if (res)
  3323. return ERR_PTR(res);
  3324. return &sport->port_tpg_1;
  3325. }
  3326. /**
  3327. * configfs callback invoked for
  3328. * rmdir /sys/kernel/config/target/$driver/$port/$tpg
  3329. */
  3330. static void srpt_drop_tpg(struct se_portal_group *tpg)
  3331. {
  3332. struct srpt_port *sport = container_of(tpg,
  3333. struct srpt_port, port_tpg_1);
  3334. sport->enabled = false;
  3335. core_tpg_deregister(&sport->port_tpg_1);
  3336. }
  3337. /**
  3338. * configfs callback invoked for
  3339. * mkdir /sys/kernel/config/target/$driver/$port
  3340. */
  3341. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  3342. struct config_group *group,
  3343. const char *name)
  3344. {
  3345. struct srpt_port *sport;
  3346. int ret;
  3347. sport = srpt_lookup_port(name);
  3348. pr_debug("make_tport(%s)\n", name);
  3349. ret = -EINVAL;
  3350. if (!sport)
  3351. goto err;
  3352. return &sport->port_wwn;
  3353. err:
  3354. return ERR_PTR(ret);
  3355. }
  3356. /**
  3357. * configfs callback invoked for
  3358. * rmdir /sys/kernel/config/target/$driver/$port
  3359. */
  3360. static void srpt_drop_tport(struct se_wwn *wwn)
  3361. {
  3362. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  3363. pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
  3364. }
  3365. static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
  3366. char *buf)
  3367. {
  3368. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  3369. }
  3370. TF_WWN_ATTR_RO(srpt, version);
  3371. static struct configfs_attribute *srpt_wwn_attrs[] = {
  3372. &srpt_wwn_version.attr,
  3373. NULL,
  3374. };
  3375. static struct target_core_fabric_ops srpt_template = {
  3376. .get_fabric_name = srpt_get_fabric_name,
  3377. .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
  3378. .tpg_get_wwn = srpt_get_fabric_wwn,
  3379. .tpg_get_tag = srpt_get_tag,
  3380. .tpg_get_default_depth = srpt_get_default_depth,
  3381. .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
  3382. .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
  3383. .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
  3384. .tpg_check_demo_mode = srpt_check_false,
  3385. .tpg_check_demo_mode_cache = srpt_check_true,
  3386. .tpg_check_demo_mode_write_protect = srpt_check_true,
  3387. .tpg_check_prod_mode_write_protect = srpt_check_false,
  3388. .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
  3389. .tpg_release_fabric_acl = srpt_release_fabric_acl,
  3390. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  3391. .release_cmd = srpt_release_cmd,
  3392. .check_stop_free = srpt_check_stop_free,
  3393. .shutdown_session = srpt_shutdown_session,
  3394. .close_session = srpt_close_session,
  3395. .stop_session = srpt_stop_session,
  3396. .fall_back_to_erl0 = srpt_reset_nexus,
  3397. .sess_logged_in = srpt_sess_logged_in,
  3398. .sess_get_index = srpt_sess_get_index,
  3399. .sess_get_initiator_sid = NULL,
  3400. .write_pending = srpt_write_pending,
  3401. .write_pending_status = srpt_write_pending_status,
  3402. .set_default_node_attributes = srpt_set_default_node_attrs,
  3403. .get_task_tag = srpt_get_task_tag,
  3404. .get_cmd_state = srpt_get_tcm_cmd_state,
  3405. .queue_data_in = srpt_queue_response,
  3406. .queue_status = srpt_queue_status,
  3407. .queue_tm_rsp = srpt_queue_response,
  3408. .get_fabric_sense_len = srpt_get_fabric_sense_len,
  3409. .set_fabric_sense_len = srpt_set_fabric_sense_len,
  3410. .is_state_remove = srpt_is_state_remove,
  3411. /*
  3412. * Setup function pointers for generic logic in
  3413. * target_core_fabric_configfs.c
  3414. */
  3415. .fabric_make_wwn = srpt_make_tport,
  3416. .fabric_drop_wwn = srpt_drop_tport,
  3417. .fabric_make_tpg = srpt_make_tpg,
  3418. .fabric_drop_tpg = srpt_drop_tpg,
  3419. .fabric_post_link = NULL,
  3420. .fabric_pre_unlink = NULL,
  3421. .fabric_make_np = NULL,
  3422. .fabric_drop_np = NULL,
  3423. .fabric_make_nodeacl = srpt_make_nodeacl,
  3424. .fabric_drop_nodeacl = srpt_drop_nodeacl,
  3425. };
  3426. /**
  3427. * srpt_init_module() - Kernel module initialization.
  3428. *
  3429. * Note: Since ib_register_client() registers callback functions, and since at
  3430. * least one of these callback functions (srpt_add_one()) calls target core
  3431. * functions, this driver must be registered with the target core before
  3432. * ib_register_client() is called.
  3433. */
  3434. static int __init srpt_init_module(void)
  3435. {
  3436. int ret;
  3437. ret = -EINVAL;
  3438. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  3439. printk(KERN_ERR "invalid value %d for kernel module parameter"
  3440. " srp_max_req_size -- must be at least %d.\n",
  3441. srp_max_req_size, MIN_MAX_REQ_SIZE);
  3442. goto out;
  3443. }
  3444. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  3445. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  3446. printk(KERN_ERR "invalid value %d for kernel module parameter"
  3447. " srpt_srq_size -- must be in the range [%d..%d].\n",
  3448. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  3449. goto out;
  3450. }
  3451. srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
  3452. if (IS_ERR(srpt_target)) {
  3453. printk(KERN_ERR "couldn't register\n");
  3454. ret = PTR_ERR(srpt_target);
  3455. goto out;
  3456. }
  3457. srpt_target->tf_ops = srpt_template;
  3458. /* Enable SG chaining */
  3459. srpt_target->tf_ops.task_sg_chaining = true;
  3460. /*
  3461. * Set up default attribute lists.
  3462. */
  3463. srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
  3464. srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
  3465. srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
  3466. srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
  3467. srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
  3468. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  3469. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  3470. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  3471. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  3472. ret = target_fabric_configfs_register(srpt_target);
  3473. if (ret < 0) {
  3474. printk(KERN_ERR "couldn't register\n");
  3475. goto out_free_target;
  3476. }
  3477. ret = ib_register_client(&srpt_client);
  3478. if (ret) {
  3479. printk(KERN_ERR "couldn't register IB client\n");
  3480. goto out_unregister_target;
  3481. }
  3482. return 0;
  3483. out_unregister_target:
  3484. target_fabric_configfs_deregister(srpt_target);
  3485. srpt_target = NULL;
  3486. out_free_target:
  3487. if (srpt_target)
  3488. target_fabric_configfs_free(srpt_target);
  3489. out:
  3490. return ret;
  3491. }
  3492. static void __exit srpt_cleanup_module(void)
  3493. {
  3494. ib_unregister_client(&srpt_client);
  3495. target_fabric_configfs_deregister(srpt_target);
  3496. srpt_target = NULL;
  3497. }
  3498. module_init(srpt_init_module);
  3499. module_exit(srpt_cleanup_module);