ib_srpt.c 105 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056
  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. ioctx->sg = sg = sg_orig = cmd->t_data_sg;
  954. ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
  955. count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
  956. opposite_dma_dir(dir));
  957. if (unlikely(!count))
  958. return -EAGAIN;
  959. ioctx->mapped_sg_count = count;
  960. if (ioctx->rdma_ius && ioctx->n_rdma_ius)
  961. nrdma = ioctx->n_rdma_ius;
  962. else {
  963. nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
  964. + ioctx->n_rbuf;
  965. ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
  966. if (!ioctx->rdma_ius)
  967. goto free_mem;
  968. ioctx->n_rdma_ius = nrdma;
  969. }
  970. db = ioctx->rbufs;
  971. tsize = cmd->data_length;
  972. dma_len = sg_dma_len(&sg[0]);
  973. riu = ioctx->rdma_ius;
  974. /*
  975. * For each remote desc - calculate the #ib_sge.
  976. * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
  977. * each remote desc rdma_iu is required a rdma wr;
  978. * else
  979. * we need to allocate extra rdma_iu to carry extra #ib_sge in
  980. * another rdma wr
  981. */
  982. for (i = 0, j = 0;
  983. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  984. rsize = be32_to_cpu(db->len);
  985. raddr = be64_to_cpu(db->va);
  986. riu->raddr = raddr;
  987. riu->rkey = be32_to_cpu(db->key);
  988. riu->sge_cnt = 0;
  989. /* calculate how many sge required for this remote_buf */
  990. while (rsize > 0 && tsize > 0) {
  991. if (rsize >= dma_len) {
  992. tsize -= dma_len;
  993. rsize -= dma_len;
  994. raddr += dma_len;
  995. if (tsize > 0) {
  996. ++j;
  997. if (j < count) {
  998. sg = sg_next(sg);
  999. dma_len = sg_dma_len(sg);
  1000. }
  1001. }
  1002. } else {
  1003. tsize -= rsize;
  1004. dma_len -= rsize;
  1005. rsize = 0;
  1006. }
  1007. ++riu->sge_cnt;
  1008. if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
  1009. ++ioctx->n_rdma;
  1010. riu->sge =
  1011. kmalloc(riu->sge_cnt * sizeof *riu->sge,
  1012. GFP_KERNEL);
  1013. if (!riu->sge)
  1014. goto free_mem;
  1015. ++riu;
  1016. riu->sge_cnt = 0;
  1017. riu->raddr = raddr;
  1018. riu->rkey = be32_to_cpu(db->key);
  1019. }
  1020. }
  1021. ++ioctx->n_rdma;
  1022. riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
  1023. GFP_KERNEL);
  1024. if (!riu->sge)
  1025. goto free_mem;
  1026. }
  1027. db = ioctx->rbufs;
  1028. tsize = cmd->data_length;
  1029. riu = ioctx->rdma_ius;
  1030. sg = sg_orig;
  1031. dma_len = sg_dma_len(&sg[0]);
  1032. dma_addr = sg_dma_address(&sg[0]);
  1033. /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
  1034. for (i = 0, j = 0;
  1035. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  1036. rsize = be32_to_cpu(db->len);
  1037. sge = riu->sge;
  1038. k = 0;
  1039. while (rsize > 0 && tsize > 0) {
  1040. sge->addr = dma_addr;
  1041. sge->lkey = ch->sport->sdev->mr->lkey;
  1042. if (rsize >= dma_len) {
  1043. sge->length =
  1044. (tsize < dma_len) ? tsize : dma_len;
  1045. tsize -= dma_len;
  1046. rsize -= dma_len;
  1047. if (tsize > 0) {
  1048. ++j;
  1049. if (j < count) {
  1050. sg = sg_next(sg);
  1051. dma_len = sg_dma_len(sg);
  1052. dma_addr = sg_dma_address(sg);
  1053. }
  1054. }
  1055. } else {
  1056. sge->length = (tsize < rsize) ? tsize : rsize;
  1057. tsize -= rsize;
  1058. dma_len -= rsize;
  1059. dma_addr += rsize;
  1060. rsize = 0;
  1061. }
  1062. ++k;
  1063. if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
  1064. ++riu;
  1065. sge = riu->sge;
  1066. k = 0;
  1067. } else if (rsize > 0 && tsize > 0)
  1068. ++sge;
  1069. }
  1070. }
  1071. return 0;
  1072. free_mem:
  1073. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  1074. return -ENOMEM;
  1075. }
  1076. /**
  1077. * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
  1078. */
  1079. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  1080. {
  1081. struct srpt_send_ioctx *ioctx;
  1082. unsigned long flags;
  1083. BUG_ON(!ch);
  1084. ioctx = NULL;
  1085. spin_lock_irqsave(&ch->spinlock, flags);
  1086. if (!list_empty(&ch->free_list)) {
  1087. ioctx = list_first_entry(&ch->free_list,
  1088. struct srpt_send_ioctx, free_list);
  1089. list_del(&ioctx->free_list);
  1090. }
  1091. spin_unlock_irqrestore(&ch->spinlock, flags);
  1092. if (!ioctx)
  1093. return ioctx;
  1094. BUG_ON(ioctx->ch != ch);
  1095. kref_init(&ioctx->kref);
  1096. spin_lock_init(&ioctx->spinlock);
  1097. ioctx->state = SRPT_STATE_NEW;
  1098. ioctx->n_rbuf = 0;
  1099. ioctx->rbufs = NULL;
  1100. ioctx->n_rdma = 0;
  1101. ioctx->n_rdma_ius = 0;
  1102. ioctx->rdma_ius = NULL;
  1103. ioctx->mapped_sg_count = 0;
  1104. init_completion(&ioctx->tx_done);
  1105. ioctx->queue_status_only = false;
  1106. /*
  1107. * transport_init_se_cmd() does not initialize all fields, so do it
  1108. * here.
  1109. */
  1110. memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
  1111. memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
  1112. return ioctx;
  1113. }
  1114. /**
  1115. * srpt_put_send_ioctx() - Free up resources.
  1116. */
  1117. static void srpt_put_send_ioctx(struct srpt_send_ioctx *ioctx)
  1118. {
  1119. struct srpt_rdma_ch *ch;
  1120. unsigned long flags;
  1121. BUG_ON(!ioctx);
  1122. ch = ioctx->ch;
  1123. BUG_ON(!ch);
  1124. WARN_ON(srpt_get_cmd_state(ioctx) != SRPT_STATE_DONE);
  1125. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  1126. transport_generic_free_cmd(&ioctx->cmd, 0);
  1127. if (ioctx->n_rbuf > 1) {
  1128. kfree(ioctx->rbufs);
  1129. ioctx->rbufs = NULL;
  1130. ioctx->n_rbuf = 0;
  1131. }
  1132. spin_lock_irqsave(&ch->spinlock, flags);
  1133. list_add(&ioctx->free_list, &ch->free_list);
  1134. spin_unlock_irqrestore(&ch->spinlock, flags);
  1135. }
  1136. static void srpt_put_send_ioctx_kref(struct kref *kref)
  1137. {
  1138. srpt_put_send_ioctx(container_of(kref, struct srpt_send_ioctx, kref));
  1139. }
  1140. /**
  1141. * srpt_abort_cmd() - Abort a SCSI command.
  1142. * @ioctx: I/O context associated with the SCSI command.
  1143. * @context: Preferred execution context.
  1144. */
  1145. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  1146. {
  1147. enum srpt_command_state state;
  1148. unsigned long flags;
  1149. BUG_ON(!ioctx);
  1150. /*
  1151. * If the command is in a state where the target core is waiting for
  1152. * the ib_srpt driver, change the state to the next state. Changing
  1153. * the state of the command from SRPT_STATE_NEED_DATA to
  1154. * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
  1155. * function a second time.
  1156. */
  1157. spin_lock_irqsave(&ioctx->spinlock, flags);
  1158. state = ioctx->state;
  1159. switch (state) {
  1160. case SRPT_STATE_NEED_DATA:
  1161. ioctx->state = SRPT_STATE_DATA_IN;
  1162. break;
  1163. case SRPT_STATE_DATA_IN:
  1164. case SRPT_STATE_CMD_RSP_SENT:
  1165. case SRPT_STATE_MGMT_RSP_SENT:
  1166. ioctx->state = SRPT_STATE_DONE;
  1167. break;
  1168. default:
  1169. break;
  1170. }
  1171. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  1172. if (state == SRPT_STATE_DONE)
  1173. goto out;
  1174. pr_debug("Aborting cmd with state %d and tag %lld\n", state,
  1175. ioctx->tag);
  1176. switch (state) {
  1177. case SRPT_STATE_NEW:
  1178. case SRPT_STATE_DATA_IN:
  1179. case SRPT_STATE_MGMT:
  1180. /*
  1181. * Do nothing - defer abort processing until
  1182. * srpt_queue_response() is invoked.
  1183. */
  1184. WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
  1185. break;
  1186. case SRPT_STATE_NEED_DATA:
  1187. /* DMA_TO_DEVICE (write) - RDMA read error. */
  1188. /* XXX(hch): this is a horrible layering violation.. */
  1189. spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
  1190. ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
  1191. ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
  1192. spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
  1193. complete(&ioctx->cmd.transport_lun_stop_comp);
  1194. break;
  1195. case SRPT_STATE_CMD_RSP_SENT:
  1196. /*
  1197. * SRP_RSP sending failed or the SRP_RSP send completion has
  1198. * not been received in time.
  1199. */
  1200. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  1201. spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
  1202. ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
  1203. spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
  1204. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1205. break;
  1206. case SRPT_STATE_MGMT_RSP_SENT:
  1207. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1208. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1209. break;
  1210. default:
  1211. WARN_ON("ERROR: unexpected command state");
  1212. break;
  1213. }
  1214. out:
  1215. return state;
  1216. }
  1217. /**
  1218. * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
  1219. */
  1220. static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
  1221. {
  1222. struct srpt_send_ioctx *ioctx;
  1223. enum srpt_command_state state;
  1224. struct se_cmd *cmd;
  1225. u32 index;
  1226. atomic_inc(&ch->sq_wr_avail);
  1227. index = idx_from_wr_id(wr_id);
  1228. ioctx = ch->ioctx_ring[index];
  1229. state = srpt_get_cmd_state(ioctx);
  1230. cmd = &ioctx->cmd;
  1231. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
  1232. && state != SRPT_STATE_MGMT_RSP_SENT
  1233. && state != SRPT_STATE_NEED_DATA
  1234. && state != SRPT_STATE_DONE);
  1235. /* If SRP_RSP sending failed, undo the ch->req_lim change. */
  1236. if (state == SRPT_STATE_CMD_RSP_SENT
  1237. || state == SRPT_STATE_MGMT_RSP_SENT)
  1238. atomic_dec(&ch->req_lim);
  1239. srpt_abort_cmd(ioctx);
  1240. }
  1241. /**
  1242. * srpt_handle_send_comp() - Process an IB send completion notification.
  1243. */
  1244. static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
  1245. struct srpt_send_ioctx *ioctx)
  1246. {
  1247. enum srpt_command_state state;
  1248. atomic_inc(&ch->sq_wr_avail);
  1249. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1250. if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
  1251. && state != SRPT_STATE_MGMT_RSP_SENT
  1252. && state != SRPT_STATE_DONE))
  1253. pr_debug("state = %d\n", state);
  1254. if (state != SRPT_STATE_DONE)
  1255. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1256. else
  1257. printk(KERN_ERR "IB completion has been received too late for"
  1258. " wr_id = %u.\n", ioctx->ioctx.index);
  1259. }
  1260. /**
  1261. * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
  1262. *
  1263. * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
  1264. * the data that has been transferred via IB RDMA had to be postponed until the
  1265. * check_stop_free() callback. None of this is nessecary anymore and needs to
  1266. * be cleaned up.
  1267. */
  1268. static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
  1269. struct srpt_send_ioctx *ioctx,
  1270. enum srpt_opcode opcode)
  1271. {
  1272. WARN_ON(ioctx->n_rdma <= 0);
  1273. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1274. if (opcode == SRPT_RDMA_READ_LAST) {
  1275. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1276. SRPT_STATE_DATA_IN))
  1277. target_execute_cmd(&ioctx->cmd);
  1278. else
  1279. printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
  1280. __LINE__, srpt_get_cmd_state(ioctx));
  1281. } else if (opcode == SRPT_RDMA_ABORT) {
  1282. ioctx->rdma_aborted = true;
  1283. } else {
  1284. WARN(true, "unexpected opcode %d\n", opcode);
  1285. }
  1286. }
  1287. /**
  1288. * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
  1289. */
  1290. static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
  1291. struct srpt_send_ioctx *ioctx,
  1292. enum srpt_opcode opcode)
  1293. {
  1294. struct se_cmd *cmd;
  1295. enum srpt_command_state state;
  1296. unsigned long flags;
  1297. cmd = &ioctx->cmd;
  1298. state = srpt_get_cmd_state(ioctx);
  1299. switch (opcode) {
  1300. case SRPT_RDMA_READ_LAST:
  1301. if (ioctx->n_rdma <= 0) {
  1302. printk(KERN_ERR "Received invalid RDMA read"
  1303. " error completion with idx %d\n",
  1304. ioctx->ioctx.index);
  1305. break;
  1306. }
  1307. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1308. if (state == SRPT_STATE_NEED_DATA)
  1309. srpt_abort_cmd(ioctx);
  1310. else
  1311. printk(KERN_ERR "%s[%d]: wrong state = %d\n",
  1312. __func__, __LINE__, state);
  1313. break;
  1314. case SRPT_RDMA_WRITE_LAST:
  1315. spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
  1316. ioctx->cmd.transport_state |= CMD_T_LUN_STOP;
  1317. spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
  1318. break;
  1319. default:
  1320. printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
  1321. __LINE__, opcode);
  1322. break;
  1323. }
  1324. }
  1325. /**
  1326. * srpt_build_cmd_rsp() - Build an SRP_RSP response.
  1327. * @ch: RDMA channel through which the request has been received.
  1328. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1329. * be built in the buffer ioctx->buf points at and hence this function will
  1330. * overwrite the request data.
  1331. * @tag: tag of the request for which this response is being generated.
  1332. * @status: value for the STATUS field of the SRP_RSP information unit.
  1333. *
  1334. * Returns the size in bytes of the SRP_RSP response.
  1335. *
  1336. * An SRP_RSP response contains a SCSI status or service response. See also
  1337. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1338. * response. See also SPC-2 for more information about sense data.
  1339. */
  1340. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1341. struct srpt_send_ioctx *ioctx, u64 tag,
  1342. int status)
  1343. {
  1344. struct srp_rsp *srp_rsp;
  1345. const u8 *sense_data;
  1346. int sense_data_len, max_sense_len;
  1347. /*
  1348. * The lowest bit of all SAM-3 status codes is zero (see also
  1349. * paragraph 5.3 in SAM-3).
  1350. */
  1351. WARN_ON(status & 1);
  1352. srp_rsp = ioctx->ioctx.buf;
  1353. BUG_ON(!srp_rsp);
  1354. sense_data = ioctx->sense_data;
  1355. sense_data_len = ioctx->cmd.scsi_sense_length;
  1356. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1357. memset(srp_rsp, 0, sizeof *srp_rsp);
  1358. srp_rsp->opcode = SRP_RSP;
  1359. srp_rsp->req_lim_delta =
  1360. __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1361. srp_rsp->tag = tag;
  1362. srp_rsp->status = status;
  1363. if (sense_data_len) {
  1364. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1365. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1366. if (sense_data_len > max_sense_len) {
  1367. printk(KERN_WARNING "truncated sense data from %d to %d"
  1368. " bytes\n", sense_data_len, max_sense_len);
  1369. sense_data_len = max_sense_len;
  1370. }
  1371. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1372. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1373. memcpy(srp_rsp + 1, sense_data, sense_data_len);
  1374. }
  1375. return sizeof(*srp_rsp) + sense_data_len;
  1376. }
  1377. /**
  1378. * srpt_build_tskmgmt_rsp() - Build a task management response.
  1379. * @ch: RDMA channel through which the request has been received.
  1380. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1381. * @rsp_code: RSP_CODE that will be stored in the response.
  1382. * @tag: Tag of the request for which this response is being generated.
  1383. *
  1384. * Returns the size in bytes of the SRP_RSP response.
  1385. *
  1386. * An SRP_RSP response contains a SCSI status or service response. See also
  1387. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1388. * response.
  1389. */
  1390. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1391. struct srpt_send_ioctx *ioctx,
  1392. u8 rsp_code, u64 tag)
  1393. {
  1394. struct srp_rsp *srp_rsp;
  1395. int resp_data_len;
  1396. int resp_len;
  1397. resp_data_len = (rsp_code == SRP_TSK_MGMT_SUCCESS) ? 0 : 4;
  1398. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1399. srp_rsp = ioctx->ioctx.buf;
  1400. BUG_ON(!srp_rsp);
  1401. memset(srp_rsp, 0, sizeof *srp_rsp);
  1402. srp_rsp->opcode = SRP_RSP;
  1403. srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
  1404. + atomic_xchg(&ch->req_lim_delta, 0));
  1405. srp_rsp->tag = tag;
  1406. if (rsp_code != SRP_TSK_MGMT_SUCCESS) {
  1407. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1408. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1409. srp_rsp->data[3] = rsp_code;
  1410. }
  1411. return resp_len;
  1412. }
  1413. #define NO_SUCH_LUN ((uint64_t)-1LL)
  1414. /*
  1415. * SCSI LUN addressing method. See also SAM-2 and the section about
  1416. * eight byte LUNs.
  1417. */
  1418. enum scsi_lun_addr_method {
  1419. SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
  1420. SCSI_LUN_ADDR_METHOD_FLAT = 1,
  1421. SCSI_LUN_ADDR_METHOD_LUN = 2,
  1422. SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
  1423. };
  1424. /*
  1425. * srpt_unpack_lun() - Convert from network LUN to linear LUN.
  1426. *
  1427. * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
  1428. * order (big endian) to a linear LUN. Supports three LUN addressing methods:
  1429. * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
  1430. */
  1431. static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
  1432. {
  1433. uint64_t res = NO_SUCH_LUN;
  1434. int addressing_method;
  1435. if (unlikely(len < 2)) {
  1436. printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
  1437. "more", len);
  1438. goto out;
  1439. }
  1440. switch (len) {
  1441. case 8:
  1442. if ((*((__be64 *)lun) &
  1443. __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
  1444. goto out_err;
  1445. break;
  1446. case 4:
  1447. if (*((__be16 *)&lun[2]) != 0)
  1448. goto out_err;
  1449. break;
  1450. case 6:
  1451. if (*((__be32 *)&lun[2]) != 0)
  1452. goto out_err;
  1453. break;
  1454. case 2:
  1455. break;
  1456. default:
  1457. goto out_err;
  1458. }
  1459. addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
  1460. switch (addressing_method) {
  1461. case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
  1462. case SCSI_LUN_ADDR_METHOD_FLAT:
  1463. case SCSI_LUN_ADDR_METHOD_LUN:
  1464. res = *(lun + 1) | (((*lun) & 0x3f) << 8);
  1465. break;
  1466. case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
  1467. default:
  1468. printk(KERN_ERR "Unimplemented LUN addressing method %u",
  1469. addressing_method);
  1470. break;
  1471. }
  1472. out:
  1473. return res;
  1474. out_err:
  1475. printk(KERN_ERR "Support for multi-level LUNs has not yet been"
  1476. " implemented");
  1477. goto out;
  1478. }
  1479. static int srpt_check_stop_free(struct se_cmd *cmd)
  1480. {
  1481. struct srpt_send_ioctx *ioctx;
  1482. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  1483. return kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  1484. }
  1485. /**
  1486. * srpt_handle_cmd() - Process SRP_CMD.
  1487. */
  1488. static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1489. struct srpt_recv_ioctx *recv_ioctx,
  1490. struct srpt_send_ioctx *send_ioctx)
  1491. {
  1492. struct se_cmd *cmd;
  1493. struct srp_cmd *srp_cmd;
  1494. uint64_t unpacked_lun;
  1495. u64 data_len;
  1496. enum dma_data_direction dir;
  1497. int ret;
  1498. BUG_ON(!send_ioctx);
  1499. srp_cmd = recv_ioctx->ioctx.buf;
  1500. kref_get(&send_ioctx->kref);
  1501. cmd = &send_ioctx->cmd;
  1502. send_ioctx->tag = srp_cmd->tag;
  1503. switch (srp_cmd->task_attr) {
  1504. case SRP_CMD_SIMPLE_Q:
  1505. cmd->sam_task_attr = MSG_SIMPLE_TAG;
  1506. break;
  1507. case SRP_CMD_ORDERED_Q:
  1508. default:
  1509. cmd->sam_task_attr = MSG_ORDERED_TAG;
  1510. break;
  1511. case SRP_CMD_HEAD_OF_Q:
  1512. cmd->sam_task_attr = MSG_HEAD_TAG;
  1513. break;
  1514. case SRP_CMD_ACA:
  1515. cmd->sam_task_attr = MSG_ACA_TAG;
  1516. break;
  1517. }
  1518. ret = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len);
  1519. if (ret) {
  1520. printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
  1521. srp_cmd->tag);
  1522. cmd->se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1523. cmd->scsi_sense_reason = TCM_INVALID_CDB_FIELD;
  1524. kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
  1525. goto send_sense;
  1526. }
  1527. cmd->data_length = data_len;
  1528. cmd->data_direction = dir;
  1529. unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
  1530. sizeof(srp_cmd->lun));
  1531. if (transport_lookup_cmd_lun(cmd, unpacked_lun) < 0) {
  1532. kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
  1533. goto send_sense;
  1534. }
  1535. ret = target_setup_cmd_from_cdb(cmd, srp_cmd->cdb);
  1536. if (ret < 0) {
  1537. kref_put(&send_ioctx->kref, srpt_put_send_ioctx_kref);
  1538. if (cmd->se_cmd_flags & SCF_SCSI_RESERVATION_CONFLICT) {
  1539. srpt_queue_status(cmd);
  1540. return 0;
  1541. } else
  1542. goto send_sense;
  1543. }
  1544. transport_handle_cdb_direct(cmd);
  1545. return 0;
  1546. send_sense:
  1547. transport_send_check_condition_and_sense(cmd, cmd->scsi_sense_reason,
  1548. 0);
  1549. return -1;
  1550. }
  1551. /**
  1552. * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
  1553. * @ch: RDMA channel of the task management request.
  1554. * @fn: Task management function to perform.
  1555. * @req_tag: Tag of the SRP task management request.
  1556. * @mgmt_ioctx: I/O context of the task management request.
  1557. *
  1558. * Returns zero if the target core will process the task management
  1559. * request asynchronously.
  1560. *
  1561. * Note: It is assumed that the initiator serializes tag-based task management
  1562. * requests.
  1563. */
  1564. static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
  1565. {
  1566. struct srpt_device *sdev;
  1567. struct srpt_rdma_ch *ch;
  1568. struct srpt_send_ioctx *target;
  1569. int ret, i;
  1570. ret = -EINVAL;
  1571. ch = ioctx->ch;
  1572. BUG_ON(!ch);
  1573. BUG_ON(!ch->sport);
  1574. sdev = ch->sport->sdev;
  1575. BUG_ON(!sdev);
  1576. spin_lock_irq(&sdev->spinlock);
  1577. for (i = 0; i < ch->rq_size; ++i) {
  1578. target = ch->ioctx_ring[i];
  1579. if (target->cmd.se_lun == ioctx->cmd.se_lun &&
  1580. target->tag == tag &&
  1581. srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
  1582. ret = 0;
  1583. /* now let the target core abort &target->cmd; */
  1584. break;
  1585. }
  1586. }
  1587. spin_unlock_irq(&sdev->spinlock);
  1588. return ret;
  1589. }
  1590. static int srp_tmr_to_tcm(int fn)
  1591. {
  1592. switch (fn) {
  1593. case SRP_TSK_ABORT_TASK:
  1594. return TMR_ABORT_TASK;
  1595. case SRP_TSK_ABORT_TASK_SET:
  1596. return TMR_ABORT_TASK_SET;
  1597. case SRP_TSK_CLEAR_TASK_SET:
  1598. return TMR_CLEAR_TASK_SET;
  1599. case SRP_TSK_LUN_RESET:
  1600. return TMR_LUN_RESET;
  1601. case SRP_TSK_CLEAR_ACA:
  1602. return TMR_CLEAR_ACA;
  1603. default:
  1604. return -1;
  1605. }
  1606. }
  1607. /**
  1608. * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
  1609. *
  1610. * Returns 0 if and only if the request will be processed by the target core.
  1611. *
  1612. * For more information about SRP_TSK_MGMT information units, see also section
  1613. * 6.7 in the SRP r16a document.
  1614. */
  1615. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1616. struct srpt_recv_ioctx *recv_ioctx,
  1617. struct srpt_send_ioctx *send_ioctx)
  1618. {
  1619. struct srp_tsk_mgmt *srp_tsk;
  1620. struct se_cmd *cmd;
  1621. uint64_t unpacked_lun;
  1622. int tcm_tmr;
  1623. int res;
  1624. BUG_ON(!send_ioctx);
  1625. srp_tsk = recv_ioctx->ioctx.buf;
  1626. cmd = &send_ioctx->cmd;
  1627. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
  1628. " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
  1629. srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
  1630. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1631. send_ioctx->tag = srp_tsk->tag;
  1632. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1633. if (tcm_tmr < 0) {
  1634. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1635. send_ioctx->cmd.se_tmr_req->response =
  1636. TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  1637. goto process_tmr;
  1638. }
  1639. res = core_tmr_alloc_req(cmd, NULL, tcm_tmr, GFP_KERNEL);
  1640. if (res < 0) {
  1641. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1642. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1643. goto process_tmr;
  1644. }
  1645. unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
  1646. sizeof(srp_tsk->lun));
  1647. res = transport_lookup_tmr_lun(&send_ioctx->cmd, unpacked_lun);
  1648. if (res) {
  1649. pr_debug("rejecting TMR for LUN %lld\n", unpacked_lun);
  1650. send_ioctx->cmd.se_cmd_flags |= SCF_SCSI_CDB_EXCEPTION;
  1651. send_ioctx->cmd.se_tmr_req->response = TMR_LUN_DOES_NOT_EXIST;
  1652. goto process_tmr;
  1653. }
  1654. if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK)
  1655. srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
  1656. process_tmr:
  1657. kref_get(&send_ioctx->kref);
  1658. if (!(send_ioctx->cmd.se_cmd_flags & SCF_SCSI_CDB_EXCEPTION))
  1659. transport_generic_handle_tmr(&send_ioctx->cmd);
  1660. else
  1661. transport_send_check_condition_and_sense(cmd,
  1662. cmd->scsi_sense_reason, 0);
  1663. }
  1664. /**
  1665. * srpt_handle_new_iu() - Process a newly received information unit.
  1666. * @ch: RDMA channel through which the information unit has been received.
  1667. * @ioctx: SRPT I/O context associated with the information unit.
  1668. */
  1669. static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
  1670. struct srpt_recv_ioctx *recv_ioctx,
  1671. struct srpt_send_ioctx *send_ioctx)
  1672. {
  1673. struct srp_cmd *srp_cmd;
  1674. enum rdma_ch_state ch_state;
  1675. BUG_ON(!ch);
  1676. BUG_ON(!recv_ioctx);
  1677. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1678. recv_ioctx->ioctx.dma, srp_max_req_size,
  1679. DMA_FROM_DEVICE);
  1680. ch_state = srpt_get_ch_state(ch);
  1681. if (unlikely(ch_state == CH_CONNECTING)) {
  1682. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1683. goto out;
  1684. }
  1685. if (unlikely(ch_state != CH_LIVE))
  1686. goto out;
  1687. srp_cmd = recv_ioctx->ioctx.buf;
  1688. if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
  1689. if (!send_ioctx)
  1690. send_ioctx = srpt_get_send_ioctx(ch);
  1691. if (unlikely(!send_ioctx)) {
  1692. list_add_tail(&recv_ioctx->wait_list,
  1693. &ch->cmd_wait_list);
  1694. goto out;
  1695. }
  1696. }
  1697. transport_init_se_cmd(&send_ioctx->cmd, &srpt_target->tf_ops, ch->sess,
  1698. 0, DMA_NONE, MSG_SIMPLE_TAG,
  1699. send_ioctx->sense_data);
  1700. switch (srp_cmd->opcode) {
  1701. case SRP_CMD:
  1702. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1703. break;
  1704. case SRP_TSK_MGMT:
  1705. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1706. break;
  1707. case SRP_I_LOGOUT:
  1708. printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
  1709. break;
  1710. case SRP_CRED_RSP:
  1711. pr_debug("received SRP_CRED_RSP\n");
  1712. break;
  1713. case SRP_AER_RSP:
  1714. pr_debug("received SRP_AER_RSP\n");
  1715. break;
  1716. case SRP_RSP:
  1717. printk(KERN_ERR "Received SRP_RSP\n");
  1718. break;
  1719. default:
  1720. printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
  1721. srp_cmd->opcode);
  1722. break;
  1723. }
  1724. srpt_post_recv(ch->sport->sdev, recv_ioctx);
  1725. out:
  1726. return;
  1727. }
  1728. static void srpt_process_rcv_completion(struct ib_cq *cq,
  1729. struct srpt_rdma_ch *ch,
  1730. struct ib_wc *wc)
  1731. {
  1732. struct srpt_device *sdev = ch->sport->sdev;
  1733. struct srpt_recv_ioctx *ioctx;
  1734. u32 index;
  1735. index = idx_from_wr_id(wc->wr_id);
  1736. if (wc->status == IB_WC_SUCCESS) {
  1737. int req_lim;
  1738. req_lim = atomic_dec_return(&ch->req_lim);
  1739. if (unlikely(req_lim < 0))
  1740. printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
  1741. ioctx = sdev->ioctx_ring[index];
  1742. srpt_handle_new_iu(ch, ioctx, NULL);
  1743. } else {
  1744. printk(KERN_INFO "receiving failed for idx %u with status %d\n",
  1745. index, wc->status);
  1746. }
  1747. }
  1748. /**
  1749. * srpt_process_send_completion() - Process an IB send completion.
  1750. *
  1751. * Note: Although this has not yet been observed during tests, at least in
  1752. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1753. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1754. * value in each response is set to one, and it is possible that this response
  1755. * makes the initiator send a new request before the send completion for that
  1756. * response has been processed. This could e.g. happen if the call to
  1757. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1758. * if IB retransmission causes generation of the send completion to be
  1759. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1760. * are queued on cmd_wait_list. The code below processes these delayed
  1761. * requests one at a time.
  1762. */
  1763. static void srpt_process_send_completion(struct ib_cq *cq,
  1764. struct srpt_rdma_ch *ch,
  1765. struct ib_wc *wc)
  1766. {
  1767. struct srpt_send_ioctx *send_ioctx;
  1768. uint32_t index;
  1769. enum srpt_opcode opcode;
  1770. index = idx_from_wr_id(wc->wr_id);
  1771. opcode = opcode_from_wr_id(wc->wr_id);
  1772. send_ioctx = ch->ioctx_ring[index];
  1773. if (wc->status == IB_WC_SUCCESS) {
  1774. if (opcode == SRPT_SEND)
  1775. srpt_handle_send_comp(ch, send_ioctx);
  1776. else {
  1777. WARN_ON(opcode != SRPT_RDMA_ABORT &&
  1778. wc->opcode != IB_WC_RDMA_READ);
  1779. srpt_handle_rdma_comp(ch, send_ioctx, opcode);
  1780. }
  1781. } else {
  1782. if (opcode == SRPT_SEND) {
  1783. printk(KERN_INFO "sending response for idx %u failed"
  1784. " with status %d\n", index, wc->status);
  1785. srpt_handle_send_err_comp(ch, wc->wr_id);
  1786. } else if (opcode != SRPT_RDMA_MID) {
  1787. printk(KERN_INFO "RDMA t %d for idx %u failed with"
  1788. " status %d", opcode, index, wc->status);
  1789. srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
  1790. }
  1791. }
  1792. while (unlikely(opcode == SRPT_SEND
  1793. && !list_empty(&ch->cmd_wait_list)
  1794. && srpt_get_ch_state(ch) == CH_LIVE
  1795. && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
  1796. struct srpt_recv_ioctx *recv_ioctx;
  1797. recv_ioctx = list_first_entry(&ch->cmd_wait_list,
  1798. struct srpt_recv_ioctx,
  1799. wait_list);
  1800. list_del(&recv_ioctx->wait_list);
  1801. srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
  1802. }
  1803. }
  1804. static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
  1805. {
  1806. struct ib_wc *const wc = ch->wc;
  1807. int i, n;
  1808. WARN_ON(cq != ch->cq);
  1809. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  1810. while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
  1811. for (i = 0; i < n; i++) {
  1812. if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
  1813. srpt_process_rcv_completion(cq, ch, &wc[i]);
  1814. else
  1815. srpt_process_send_completion(cq, ch, &wc[i]);
  1816. }
  1817. }
  1818. }
  1819. /**
  1820. * srpt_completion() - IB completion queue callback function.
  1821. *
  1822. * Notes:
  1823. * - It is guaranteed that a completion handler will never be invoked
  1824. * concurrently on two different CPUs for the same completion queue. See also
  1825. * Documentation/infiniband/core_locking.txt and the implementation of
  1826. * handle_edge_irq() in kernel/irq/chip.c.
  1827. * - When threaded IRQs are enabled, completion handlers are invoked in thread
  1828. * context instead of interrupt context.
  1829. */
  1830. static void srpt_completion(struct ib_cq *cq, void *ctx)
  1831. {
  1832. struct srpt_rdma_ch *ch = ctx;
  1833. wake_up_interruptible(&ch->wait_queue);
  1834. }
  1835. static int srpt_compl_thread(void *arg)
  1836. {
  1837. struct srpt_rdma_ch *ch;
  1838. /* Hibernation / freezing of the SRPT kernel thread is not supported. */
  1839. current->flags |= PF_NOFREEZE;
  1840. ch = arg;
  1841. BUG_ON(!ch);
  1842. printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
  1843. ch->sess_name, ch->thread->comm, current->pid);
  1844. while (!kthread_should_stop()) {
  1845. wait_event_interruptible(ch->wait_queue,
  1846. (srpt_process_completion(ch->cq, ch),
  1847. kthread_should_stop()));
  1848. }
  1849. printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
  1850. ch->sess_name, ch->thread->comm, current->pid);
  1851. return 0;
  1852. }
  1853. /**
  1854. * srpt_create_ch_ib() - Create receive and send completion queues.
  1855. */
  1856. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1857. {
  1858. struct ib_qp_init_attr *qp_init;
  1859. struct srpt_port *sport = ch->sport;
  1860. struct srpt_device *sdev = sport->sdev;
  1861. u32 srp_sq_size = sport->port_attrib.srp_sq_size;
  1862. int ret;
  1863. WARN_ON(ch->rq_size < 1);
  1864. ret = -ENOMEM;
  1865. qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
  1866. if (!qp_init)
  1867. goto out;
  1868. ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
  1869. ch->rq_size + srp_sq_size, 0);
  1870. if (IS_ERR(ch->cq)) {
  1871. ret = PTR_ERR(ch->cq);
  1872. printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
  1873. ch->rq_size + srp_sq_size, ret);
  1874. goto out;
  1875. }
  1876. qp_init->qp_context = (void *)ch;
  1877. qp_init->event_handler
  1878. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1879. qp_init->send_cq = ch->cq;
  1880. qp_init->recv_cq = ch->cq;
  1881. qp_init->srq = sdev->srq;
  1882. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1883. qp_init->qp_type = IB_QPT_RC;
  1884. qp_init->cap.max_send_wr = srp_sq_size;
  1885. qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
  1886. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1887. if (IS_ERR(ch->qp)) {
  1888. ret = PTR_ERR(ch->qp);
  1889. printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
  1890. goto err_destroy_cq;
  1891. }
  1892. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1893. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  1894. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1895. qp_init->cap.max_send_wr, ch->cm_id);
  1896. ret = srpt_init_ch_qp(ch, ch->qp);
  1897. if (ret)
  1898. goto err_destroy_qp;
  1899. init_waitqueue_head(&ch->wait_queue);
  1900. pr_debug("creating thread for session %s\n", ch->sess_name);
  1901. ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
  1902. if (IS_ERR(ch->thread)) {
  1903. printk(KERN_ERR "failed to create kernel thread %ld\n",
  1904. PTR_ERR(ch->thread));
  1905. ch->thread = NULL;
  1906. goto err_destroy_qp;
  1907. }
  1908. out:
  1909. kfree(qp_init);
  1910. return ret;
  1911. err_destroy_qp:
  1912. ib_destroy_qp(ch->qp);
  1913. err_destroy_cq:
  1914. ib_destroy_cq(ch->cq);
  1915. goto out;
  1916. }
  1917. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1918. {
  1919. if (ch->thread)
  1920. kthread_stop(ch->thread);
  1921. ib_destroy_qp(ch->qp);
  1922. ib_destroy_cq(ch->cq);
  1923. }
  1924. /**
  1925. * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
  1926. *
  1927. * Reset the QP and make sure all resources associated with the channel will
  1928. * be deallocated at an appropriate time.
  1929. *
  1930. * Note: The caller must hold ch->sport->sdev->spinlock.
  1931. */
  1932. static void __srpt_close_ch(struct srpt_rdma_ch *ch)
  1933. {
  1934. struct srpt_device *sdev;
  1935. enum rdma_ch_state prev_state;
  1936. unsigned long flags;
  1937. sdev = ch->sport->sdev;
  1938. spin_lock_irqsave(&ch->spinlock, flags);
  1939. prev_state = ch->state;
  1940. switch (prev_state) {
  1941. case CH_CONNECTING:
  1942. case CH_LIVE:
  1943. ch->state = CH_DISCONNECTING;
  1944. break;
  1945. default:
  1946. break;
  1947. }
  1948. spin_unlock_irqrestore(&ch->spinlock, flags);
  1949. switch (prev_state) {
  1950. case CH_CONNECTING:
  1951. ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
  1952. NULL, 0);
  1953. /* fall through */
  1954. case CH_LIVE:
  1955. if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
  1956. printk(KERN_ERR "sending CM DREQ failed.\n");
  1957. break;
  1958. case CH_DISCONNECTING:
  1959. break;
  1960. case CH_DRAINING:
  1961. case CH_RELEASING:
  1962. break;
  1963. }
  1964. }
  1965. /**
  1966. * srpt_close_ch() - Close an RDMA channel.
  1967. */
  1968. static void srpt_close_ch(struct srpt_rdma_ch *ch)
  1969. {
  1970. struct srpt_device *sdev;
  1971. sdev = ch->sport->sdev;
  1972. spin_lock_irq(&sdev->spinlock);
  1973. __srpt_close_ch(ch);
  1974. spin_unlock_irq(&sdev->spinlock);
  1975. }
  1976. /**
  1977. * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
  1978. * @cm_id: Pointer to the CM ID of the channel to be drained.
  1979. *
  1980. * Note: Must be called from inside srpt_cm_handler to avoid a race between
  1981. * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
  1982. * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
  1983. * waits until all target sessions for the associated IB device have been
  1984. * unregistered and target session registration involves a call to
  1985. * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
  1986. * this function has finished).
  1987. */
  1988. static void srpt_drain_channel(struct ib_cm_id *cm_id)
  1989. {
  1990. struct srpt_device *sdev;
  1991. struct srpt_rdma_ch *ch;
  1992. int ret;
  1993. bool do_reset = false;
  1994. WARN_ON_ONCE(irqs_disabled());
  1995. sdev = cm_id->context;
  1996. BUG_ON(!sdev);
  1997. spin_lock_irq(&sdev->spinlock);
  1998. list_for_each_entry(ch, &sdev->rch_list, list) {
  1999. if (ch->cm_id == cm_id) {
  2000. do_reset = srpt_test_and_set_ch_state(ch,
  2001. CH_CONNECTING, CH_DRAINING) ||
  2002. srpt_test_and_set_ch_state(ch,
  2003. CH_LIVE, CH_DRAINING) ||
  2004. srpt_test_and_set_ch_state(ch,
  2005. CH_DISCONNECTING, CH_DRAINING);
  2006. break;
  2007. }
  2008. }
  2009. spin_unlock_irq(&sdev->spinlock);
  2010. if (do_reset) {
  2011. ret = srpt_ch_qp_err(ch);
  2012. if (ret < 0)
  2013. printk(KERN_ERR "Setting queue pair in error state"
  2014. " failed: %d\n", ret);
  2015. }
  2016. }
  2017. /**
  2018. * srpt_find_channel() - Look up an RDMA channel.
  2019. * @cm_id: Pointer to the CM ID of the channel to be looked up.
  2020. *
  2021. * Return NULL if no matching RDMA channel has been found.
  2022. */
  2023. static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
  2024. struct ib_cm_id *cm_id)
  2025. {
  2026. struct srpt_rdma_ch *ch;
  2027. bool found;
  2028. WARN_ON_ONCE(irqs_disabled());
  2029. BUG_ON(!sdev);
  2030. found = false;
  2031. spin_lock_irq(&sdev->spinlock);
  2032. list_for_each_entry(ch, &sdev->rch_list, list) {
  2033. if (ch->cm_id == cm_id) {
  2034. found = true;
  2035. break;
  2036. }
  2037. }
  2038. spin_unlock_irq(&sdev->spinlock);
  2039. return found ? ch : NULL;
  2040. }
  2041. /**
  2042. * srpt_release_channel() - Release channel resources.
  2043. *
  2044. * Schedules the actual release because:
  2045. * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
  2046. * trigger a deadlock.
  2047. * - It is not safe to call TCM transport_* functions from interrupt context.
  2048. */
  2049. static void srpt_release_channel(struct srpt_rdma_ch *ch)
  2050. {
  2051. schedule_work(&ch->release_work);
  2052. }
  2053. static void srpt_release_channel_work(struct work_struct *w)
  2054. {
  2055. struct srpt_rdma_ch *ch;
  2056. struct srpt_device *sdev;
  2057. ch = container_of(w, struct srpt_rdma_ch, release_work);
  2058. pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
  2059. ch->release_done);
  2060. sdev = ch->sport->sdev;
  2061. BUG_ON(!sdev);
  2062. transport_deregister_session_configfs(ch->sess);
  2063. transport_deregister_session(ch->sess);
  2064. ch->sess = NULL;
  2065. srpt_destroy_ch_ib(ch);
  2066. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2067. ch->sport->sdev, ch->rq_size,
  2068. ch->rsp_size, DMA_TO_DEVICE);
  2069. spin_lock_irq(&sdev->spinlock);
  2070. list_del(&ch->list);
  2071. spin_unlock_irq(&sdev->spinlock);
  2072. ib_destroy_cm_id(ch->cm_id);
  2073. if (ch->release_done)
  2074. complete(ch->release_done);
  2075. wake_up(&sdev->ch_releaseQ);
  2076. kfree(ch);
  2077. }
  2078. static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
  2079. u8 i_port_id[16])
  2080. {
  2081. struct srpt_node_acl *nacl;
  2082. list_for_each_entry(nacl, &sport->port_acl_list, list)
  2083. if (memcmp(nacl->i_port_id, i_port_id,
  2084. sizeof(nacl->i_port_id)) == 0)
  2085. return nacl;
  2086. return NULL;
  2087. }
  2088. static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
  2089. u8 i_port_id[16])
  2090. {
  2091. struct srpt_node_acl *nacl;
  2092. spin_lock_irq(&sport->port_acl_lock);
  2093. nacl = __srpt_lookup_acl(sport, i_port_id);
  2094. spin_unlock_irq(&sport->port_acl_lock);
  2095. return nacl;
  2096. }
  2097. /**
  2098. * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
  2099. *
  2100. * Ownership of the cm_id is transferred to the target session if this
  2101. * functions returns zero. Otherwise the caller remains the owner of cm_id.
  2102. */
  2103. static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
  2104. struct ib_cm_req_event_param *param,
  2105. void *private_data)
  2106. {
  2107. struct srpt_device *sdev = cm_id->context;
  2108. struct srpt_port *sport = &sdev->port[param->port - 1];
  2109. struct srp_login_req *req;
  2110. struct srp_login_rsp *rsp;
  2111. struct srp_login_rej *rej;
  2112. struct ib_cm_rep_param *rep_param;
  2113. struct srpt_rdma_ch *ch, *tmp_ch;
  2114. struct srpt_node_acl *nacl;
  2115. u32 it_iu_len;
  2116. int i;
  2117. int ret = 0;
  2118. WARN_ON_ONCE(irqs_disabled());
  2119. if (WARN_ON(!sdev || !private_data))
  2120. return -EINVAL;
  2121. req = (struct srp_login_req *)private_data;
  2122. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  2123. printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
  2124. " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
  2125. " (guid=0x%llx:0x%llx)\n",
  2126. be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
  2127. be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
  2128. be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
  2129. be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
  2130. it_iu_len,
  2131. param->port,
  2132. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
  2133. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
  2134. rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
  2135. rej = kzalloc(sizeof *rej, GFP_KERNEL);
  2136. rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
  2137. if (!rsp || !rej || !rep_param) {
  2138. ret = -ENOMEM;
  2139. goto out;
  2140. }
  2141. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  2142. rej->reason = __constant_cpu_to_be32(
  2143. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  2144. ret = -EINVAL;
  2145. printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
  2146. " length (%d bytes) is out of range (%d .. %d)\n",
  2147. it_iu_len, 64, srp_max_req_size);
  2148. goto reject;
  2149. }
  2150. if (!sport->enabled) {
  2151. rej->reason = __constant_cpu_to_be32(
  2152. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2153. ret = -EINVAL;
  2154. printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
  2155. " has not yet been enabled\n");
  2156. goto reject;
  2157. }
  2158. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  2159. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
  2160. spin_lock_irq(&sdev->spinlock);
  2161. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
  2162. if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
  2163. && !memcmp(ch->t_port_id, req->target_port_id, 16)
  2164. && param->port == ch->sport->port
  2165. && param->listen_id == ch->sport->sdev->cm_id
  2166. && ch->cm_id) {
  2167. enum rdma_ch_state ch_state;
  2168. ch_state = srpt_get_ch_state(ch);
  2169. if (ch_state != CH_CONNECTING
  2170. && ch_state != CH_LIVE)
  2171. continue;
  2172. /* found an existing channel */
  2173. pr_debug("Found existing channel %s"
  2174. " cm_id= %p state= %d\n",
  2175. ch->sess_name, ch->cm_id, ch_state);
  2176. __srpt_close_ch(ch);
  2177. rsp->rsp_flags =
  2178. SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  2179. }
  2180. }
  2181. spin_unlock_irq(&sdev->spinlock);
  2182. } else
  2183. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  2184. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  2185. || *(__be64 *)(req->target_port_id + 8) !=
  2186. cpu_to_be64(srpt_service_guid)) {
  2187. rej->reason = __constant_cpu_to_be32(
  2188. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  2189. ret = -ENOMEM;
  2190. printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
  2191. " has an invalid target port identifier.\n");
  2192. goto reject;
  2193. }
  2194. ch = kzalloc(sizeof *ch, GFP_KERNEL);
  2195. if (!ch) {
  2196. rej->reason = __constant_cpu_to_be32(
  2197. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2198. printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
  2199. ret = -ENOMEM;
  2200. goto reject;
  2201. }
  2202. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  2203. memcpy(ch->i_port_id, req->initiator_port_id, 16);
  2204. memcpy(ch->t_port_id, req->target_port_id, 16);
  2205. ch->sport = &sdev->port[param->port - 1];
  2206. ch->cm_id = cm_id;
  2207. /*
  2208. * Avoid QUEUE_FULL conditions by limiting the number of buffers used
  2209. * for the SRP protocol to the command queue size.
  2210. */
  2211. ch->rq_size = SRPT_RQ_SIZE;
  2212. spin_lock_init(&ch->spinlock);
  2213. ch->state = CH_CONNECTING;
  2214. INIT_LIST_HEAD(&ch->cmd_wait_list);
  2215. ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  2216. ch->ioctx_ring = (struct srpt_send_ioctx **)
  2217. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  2218. sizeof(*ch->ioctx_ring[0]),
  2219. ch->rsp_size, DMA_TO_DEVICE);
  2220. if (!ch->ioctx_ring)
  2221. goto free_ch;
  2222. INIT_LIST_HEAD(&ch->free_list);
  2223. for (i = 0; i < ch->rq_size; i++) {
  2224. ch->ioctx_ring[i]->ch = ch;
  2225. list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
  2226. }
  2227. ret = srpt_create_ch_ib(ch);
  2228. if (ret) {
  2229. rej->reason = __constant_cpu_to_be32(
  2230. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2231. printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
  2232. " a new RDMA channel failed.\n");
  2233. goto free_ring;
  2234. }
  2235. ret = srpt_ch_qp_rtr(ch, ch->qp);
  2236. if (ret) {
  2237. rej->reason = __constant_cpu_to_be32(
  2238. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2239. printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
  2240. " RTR failed (error code = %d)\n", ret);
  2241. goto destroy_ib;
  2242. }
  2243. /*
  2244. * Use the initator port identifier as the session name.
  2245. */
  2246. snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
  2247. be64_to_cpu(*(__be64 *)ch->i_port_id),
  2248. be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
  2249. pr_debug("registering session %s\n", ch->sess_name);
  2250. nacl = srpt_lookup_acl(sport, ch->i_port_id);
  2251. if (!nacl) {
  2252. printk(KERN_INFO "Rejected login because no ACL has been"
  2253. " configured yet for initiator %s.\n", ch->sess_name);
  2254. rej->reason = __constant_cpu_to_be32(
  2255. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  2256. goto destroy_ib;
  2257. }
  2258. ch->sess = transport_init_session();
  2259. if (IS_ERR(ch->sess)) {
  2260. rej->reason = __constant_cpu_to_be32(
  2261. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2262. pr_debug("Failed to create session\n");
  2263. goto deregister_session;
  2264. }
  2265. ch->sess->se_node_acl = &nacl->nacl;
  2266. transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
  2267. pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
  2268. ch->sess_name, ch->cm_id);
  2269. /* create srp_login_response */
  2270. rsp->opcode = SRP_LOGIN_RSP;
  2271. rsp->tag = req->tag;
  2272. rsp->max_it_iu_len = req->req_it_iu_len;
  2273. rsp->max_ti_iu_len = req->req_it_iu_len;
  2274. ch->max_ti_iu_len = it_iu_len;
  2275. rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  2276. | SRP_BUF_FORMAT_INDIRECT);
  2277. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  2278. atomic_set(&ch->req_lim, ch->rq_size);
  2279. atomic_set(&ch->req_lim_delta, 0);
  2280. /* create cm reply */
  2281. rep_param->qp_num = ch->qp->qp_num;
  2282. rep_param->private_data = (void *)rsp;
  2283. rep_param->private_data_len = sizeof *rsp;
  2284. rep_param->rnr_retry_count = 7;
  2285. rep_param->flow_control = 1;
  2286. rep_param->failover_accepted = 0;
  2287. rep_param->srq = 1;
  2288. rep_param->responder_resources = 4;
  2289. rep_param->initiator_depth = 4;
  2290. ret = ib_send_cm_rep(cm_id, rep_param);
  2291. if (ret) {
  2292. printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
  2293. " (error code = %d)\n", ret);
  2294. goto release_channel;
  2295. }
  2296. spin_lock_irq(&sdev->spinlock);
  2297. list_add_tail(&ch->list, &sdev->rch_list);
  2298. spin_unlock_irq(&sdev->spinlock);
  2299. goto out;
  2300. release_channel:
  2301. srpt_set_ch_state(ch, CH_RELEASING);
  2302. transport_deregister_session_configfs(ch->sess);
  2303. deregister_session:
  2304. transport_deregister_session(ch->sess);
  2305. ch->sess = NULL;
  2306. destroy_ib:
  2307. srpt_destroy_ch_ib(ch);
  2308. free_ring:
  2309. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2310. ch->sport->sdev, ch->rq_size,
  2311. ch->rsp_size, DMA_TO_DEVICE);
  2312. free_ch:
  2313. kfree(ch);
  2314. reject:
  2315. rej->opcode = SRP_LOGIN_REJ;
  2316. rej->tag = req->tag;
  2317. rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  2318. | SRP_BUF_FORMAT_INDIRECT);
  2319. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  2320. (void *)rej, sizeof *rej);
  2321. out:
  2322. kfree(rep_param);
  2323. kfree(rsp);
  2324. kfree(rej);
  2325. return ret;
  2326. }
  2327. static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
  2328. {
  2329. printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
  2330. srpt_drain_channel(cm_id);
  2331. }
  2332. /**
  2333. * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
  2334. *
  2335. * An IB_CM_RTU_RECEIVED message indicates that the connection is established
  2336. * and that the recipient may begin transmitting (RTU = ready to use).
  2337. */
  2338. static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
  2339. {
  2340. struct srpt_rdma_ch *ch;
  2341. int ret;
  2342. ch = srpt_find_channel(cm_id->context, cm_id);
  2343. BUG_ON(!ch);
  2344. if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
  2345. struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
  2346. ret = srpt_ch_qp_rts(ch, ch->qp);
  2347. list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
  2348. wait_list) {
  2349. list_del(&ioctx->wait_list);
  2350. srpt_handle_new_iu(ch, ioctx, NULL);
  2351. }
  2352. if (ret)
  2353. srpt_close_ch(ch);
  2354. }
  2355. }
  2356. static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
  2357. {
  2358. printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
  2359. srpt_drain_channel(cm_id);
  2360. }
  2361. static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
  2362. {
  2363. printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
  2364. srpt_drain_channel(cm_id);
  2365. }
  2366. /**
  2367. * srpt_cm_dreq_recv() - Process reception of a DREQ message.
  2368. */
  2369. static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
  2370. {
  2371. struct srpt_rdma_ch *ch;
  2372. unsigned long flags;
  2373. bool send_drep = false;
  2374. ch = srpt_find_channel(cm_id->context, cm_id);
  2375. BUG_ON(!ch);
  2376. pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
  2377. spin_lock_irqsave(&ch->spinlock, flags);
  2378. switch (ch->state) {
  2379. case CH_CONNECTING:
  2380. case CH_LIVE:
  2381. send_drep = true;
  2382. ch->state = CH_DISCONNECTING;
  2383. break;
  2384. case CH_DISCONNECTING:
  2385. case CH_DRAINING:
  2386. case CH_RELEASING:
  2387. WARN(true, "unexpected channel state %d\n", ch->state);
  2388. break;
  2389. }
  2390. spin_unlock_irqrestore(&ch->spinlock, flags);
  2391. if (send_drep) {
  2392. if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
  2393. printk(KERN_ERR "Sending IB DREP failed.\n");
  2394. printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
  2395. ch->sess_name);
  2396. }
  2397. }
  2398. /**
  2399. * srpt_cm_drep_recv() - Process reception of a DREP message.
  2400. */
  2401. static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
  2402. {
  2403. printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
  2404. cm_id);
  2405. srpt_drain_channel(cm_id);
  2406. }
  2407. /**
  2408. * srpt_cm_handler() - IB connection manager callback function.
  2409. *
  2410. * A non-zero return value will cause the caller destroy the CM ID.
  2411. *
  2412. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  2413. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  2414. * a non-zero value in any other case will trigger a race with the
  2415. * ib_destroy_cm_id() call in srpt_release_channel().
  2416. */
  2417. static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  2418. {
  2419. int ret;
  2420. ret = 0;
  2421. switch (event->event) {
  2422. case IB_CM_REQ_RECEIVED:
  2423. ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
  2424. event->private_data);
  2425. break;
  2426. case IB_CM_REJ_RECEIVED:
  2427. srpt_cm_rej_recv(cm_id);
  2428. break;
  2429. case IB_CM_RTU_RECEIVED:
  2430. case IB_CM_USER_ESTABLISHED:
  2431. srpt_cm_rtu_recv(cm_id);
  2432. break;
  2433. case IB_CM_DREQ_RECEIVED:
  2434. srpt_cm_dreq_recv(cm_id);
  2435. break;
  2436. case IB_CM_DREP_RECEIVED:
  2437. srpt_cm_drep_recv(cm_id);
  2438. break;
  2439. case IB_CM_TIMEWAIT_EXIT:
  2440. srpt_cm_timewait_exit(cm_id);
  2441. break;
  2442. case IB_CM_REP_ERROR:
  2443. srpt_cm_rep_error(cm_id);
  2444. break;
  2445. case IB_CM_DREQ_ERROR:
  2446. printk(KERN_INFO "Received IB DREQ ERROR event.\n");
  2447. break;
  2448. case IB_CM_MRA_RECEIVED:
  2449. printk(KERN_INFO "Received IB MRA event\n");
  2450. break;
  2451. default:
  2452. printk(KERN_ERR "received unrecognized IB CM event %d\n",
  2453. event->event);
  2454. break;
  2455. }
  2456. return ret;
  2457. }
  2458. /**
  2459. * srpt_perform_rdmas() - Perform IB RDMA.
  2460. *
  2461. * Returns zero upon success or a negative number upon failure.
  2462. */
  2463. static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
  2464. struct srpt_send_ioctx *ioctx)
  2465. {
  2466. struct ib_send_wr wr;
  2467. struct ib_send_wr *bad_wr;
  2468. struct rdma_iu *riu;
  2469. int i;
  2470. int ret;
  2471. int sq_wr_avail;
  2472. enum dma_data_direction dir;
  2473. const int n_rdma = ioctx->n_rdma;
  2474. dir = ioctx->cmd.data_direction;
  2475. if (dir == DMA_TO_DEVICE) {
  2476. /* write */
  2477. ret = -ENOMEM;
  2478. sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
  2479. if (sq_wr_avail < 0) {
  2480. printk(KERN_WARNING "IB send queue full (needed %d)\n",
  2481. n_rdma);
  2482. goto out;
  2483. }
  2484. }
  2485. ioctx->rdma_aborted = false;
  2486. ret = 0;
  2487. riu = ioctx->rdma_ius;
  2488. memset(&wr, 0, sizeof wr);
  2489. for (i = 0; i < n_rdma; ++i, ++riu) {
  2490. if (dir == DMA_FROM_DEVICE) {
  2491. wr.opcode = IB_WR_RDMA_WRITE;
  2492. wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
  2493. SRPT_RDMA_WRITE_LAST :
  2494. SRPT_RDMA_MID,
  2495. ioctx->ioctx.index);
  2496. } else {
  2497. wr.opcode = IB_WR_RDMA_READ;
  2498. wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
  2499. SRPT_RDMA_READ_LAST :
  2500. SRPT_RDMA_MID,
  2501. ioctx->ioctx.index);
  2502. }
  2503. wr.next = NULL;
  2504. wr.wr.rdma.remote_addr = riu->raddr;
  2505. wr.wr.rdma.rkey = riu->rkey;
  2506. wr.num_sge = riu->sge_cnt;
  2507. wr.sg_list = riu->sge;
  2508. /* only get completion event for the last rdma write */
  2509. if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
  2510. wr.send_flags = IB_SEND_SIGNALED;
  2511. ret = ib_post_send(ch->qp, &wr, &bad_wr);
  2512. if (ret)
  2513. break;
  2514. }
  2515. if (ret)
  2516. printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
  2517. __func__, __LINE__, ret, i, n_rdma);
  2518. if (ret && i > 0) {
  2519. wr.num_sge = 0;
  2520. wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
  2521. wr.send_flags = IB_SEND_SIGNALED;
  2522. while (ch->state == CH_LIVE &&
  2523. ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
  2524. printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
  2525. ioctx->ioctx.index);
  2526. msleep(1000);
  2527. }
  2528. while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
  2529. printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
  2530. ioctx->ioctx.index);
  2531. msleep(1000);
  2532. }
  2533. }
  2534. out:
  2535. if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
  2536. atomic_add(n_rdma, &ch->sq_wr_avail);
  2537. return ret;
  2538. }
  2539. /**
  2540. * srpt_xfer_data() - Start data transfer from initiator to target.
  2541. */
  2542. static int srpt_xfer_data(struct srpt_rdma_ch *ch,
  2543. struct srpt_send_ioctx *ioctx)
  2544. {
  2545. int ret;
  2546. ret = srpt_map_sg_to_ib_sge(ch, ioctx);
  2547. if (ret) {
  2548. printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
  2549. goto out;
  2550. }
  2551. ret = srpt_perform_rdmas(ch, ioctx);
  2552. if (ret) {
  2553. if (ret == -EAGAIN || ret == -ENOMEM)
  2554. printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
  2555. __func__, __LINE__, ret);
  2556. else
  2557. printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
  2558. __func__, __LINE__, ret);
  2559. goto out_unmap;
  2560. }
  2561. out:
  2562. return ret;
  2563. out_unmap:
  2564. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2565. goto out;
  2566. }
  2567. static int srpt_write_pending_status(struct se_cmd *se_cmd)
  2568. {
  2569. struct srpt_send_ioctx *ioctx;
  2570. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2571. return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
  2572. }
  2573. /*
  2574. * srpt_write_pending() - Start data transfer from initiator to target (write).
  2575. */
  2576. static int srpt_write_pending(struct se_cmd *se_cmd)
  2577. {
  2578. struct srpt_rdma_ch *ch;
  2579. struct srpt_send_ioctx *ioctx;
  2580. enum srpt_command_state new_state;
  2581. enum rdma_ch_state ch_state;
  2582. int ret;
  2583. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2584. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  2585. WARN_ON(new_state == SRPT_STATE_DONE);
  2586. ch = ioctx->ch;
  2587. BUG_ON(!ch);
  2588. ch_state = srpt_get_ch_state(ch);
  2589. switch (ch_state) {
  2590. case CH_CONNECTING:
  2591. WARN(true, "unexpected channel state %d\n", ch_state);
  2592. ret = -EINVAL;
  2593. goto out;
  2594. case CH_LIVE:
  2595. break;
  2596. case CH_DISCONNECTING:
  2597. case CH_DRAINING:
  2598. case CH_RELEASING:
  2599. pr_debug("cmd with tag %lld: channel disconnecting\n",
  2600. ioctx->tag);
  2601. srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
  2602. ret = -EINVAL;
  2603. goto out;
  2604. }
  2605. ret = srpt_xfer_data(ch, ioctx);
  2606. out:
  2607. return ret;
  2608. }
  2609. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  2610. {
  2611. switch (tcm_mgmt_status) {
  2612. case TMR_FUNCTION_COMPLETE:
  2613. return SRP_TSK_MGMT_SUCCESS;
  2614. case TMR_FUNCTION_REJECTED:
  2615. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  2616. }
  2617. return SRP_TSK_MGMT_FAILED;
  2618. }
  2619. /**
  2620. * srpt_queue_response() - Transmits the response to a SCSI command.
  2621. *
  2622. * Callback function called by the TCM core. Must not block since it can be
  2623. * invoked on the context of the IB completion handler.
  2624. */
  2625. static int srpt_queue_response(struct se_cmd *cmd)
  2626. {
  2627. struct srpt_rdma_ch *ch;
  2628. struct srpt_send_ioctx *ioctx;
  2629. enum srpt_command_state state;
  2630. unsigned long flags;
  2631. int ret;
  2632. enum dma_data_direction dir;
  2633. int resp_len;
  2634. u8 srp_tm_status;
  2635. ret = 0;
  2636. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2637. ch = ioctx->ch;
  2638. BUG_ON(!ch);
  2639. spin_lock_irqsave(&ioctx->spinlock, flags);
  2640. state = ioctx->state;
  2641. switch (state) {
  2642. case SRPT_STATE_NEW:
  2643. case SRPT_STATE_DATA_IN:
  2644. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  2645. break;
  2646. case SRPT_STATE_MGMT:
  2647. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  2648. break;
  2649. default:
  2650. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  2651. ch, ioctx->ioctx.index, ioctx->state);
  2652. break;
  2653. }
  2654. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  2655. if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
  2656. || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
  2657. atomic_inc(&ch->req_lim_delta);
  2658. srpt_abort_cmd(ioctx);
  2659. goto out;
  2660. }
  2661. dir = ioctx->cmd.data_direction;
  2662. /* For read commands, transfer the data to the initiator. */
  2663. if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
  2664. !ioctx->queue_status_only) {
  2665. ret = srpt_xfer_data(ch, ioctx);
  2666. if (ret) {
  2667. printk(KERN_ERR "xfer_data failed for tag %llu\n",
  2668. ioctx->tag);
  2669. goto out;
  2670. }
  2671. }
  2672. if (state != SRPT_STATE_MGMT)
  2673. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
  2674. cmd->scsi_status);
  2675. else {
  2676. srp_tm_status
  2677. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2678. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2679. ioctx->tag);
  2680. }
  2681. ret = srpt_post_send(ch, ioctx, resp_len);
  2682. if (ret) {
  2683. printk(KERN_ERR "sending cmd response failed for tag %llu\n",
  2684. ioctx->tag);
  2685. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2686. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2687. kref_put(&ioctx->kref, srpt_put_send_ioctx_kref);
  2688. }
  2689. out:
  2690. return ret;
  2691. }
  2692. static int srpt_queue_status(struct se_cmd *cmd)
  2693. {
  2694. struct srpt_send_ioctx *ioctx;
  2695. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2696. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2697. if (cmd->se_cmd_flags &
  2698. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2699. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2700. ioctx->queue_status_only = true;
  2701. return srpt_queue_response(cmd);
  2702. }
  2703. static void srpt_refresh_port_work(struct work_struct *work)
  2704. {
  2705. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2706. srpt_refresh_port(sport);
  2707. }
  2708. static int srpt_ch_list_empty(struct srpt_device *sdev)
  2709. {
  2710. int res;
  2711. spin_lock_irq(&sdev->spinlock);
  2712. res = list_empty(&sdev->rch_list);
  2713. spin_unlock_irq(&sdev->spinlock);
  2714. return res;
  2715. }
  2716. /**
  2717. * srpt_release_sdev() - Free the channel resources associated with a target.
  2718. */
  2719. static int srpt_release_sdev(struct srpt_device *sdev)
  2720. {
  2721. struct srpt_rdma_ch *ch, *tmp_ch;
  2722. int res;
  2723. WARN_ON_ONCE(irqs_disabled());
  2724. BUG_ON(!sdev);
  2725. spin_lock_irq(&sdev->spinlock);
  2726. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
  2727. __srpt_close_ch(ch);
  2728. spin_unlock_irq(&sdev->spinlock);
  2729. res = wait_event_interruptible(sdev->ch_releaseQ,
  2730. srpt_ch_list_empty(sdev));
  2731. if (res)
  2732. printk(KERN_ERR "%s: interrupted.\n", __func__);
  2733. return 0;
  2734. }
  2735. static struct srpt_port *__srpt_lookup_port(const char *name)
  2736. {
  2737. struct ib_device *dev;
  2738. struct srpt_device *sdev;
  2739. struct srpt_port *sport;
  2740. int i;
  2741. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2742. dev = sdev->device;
  2743. if (!dev)
  2744. continue;
  2745. for (i = 0; i < dev->phys_port_cnt; i++) {
  2746. sport = &sdev->port[i];
  2747. if (!strcmp(sport->port_guid, name))
  2748. return sport;
  2749. }
  2750. }
  2751. return NULL;
  2752. }
  2753. static struct srpt_port *srpt_lookup_port(const char *name)
  2754. {
  2755. struct srpt_port *sport;
  2756. spin_lock(&srpt_dev_lock);
  2757. sport = __srpt_lookup_port(name);
  2758. spin_unlock(&srpt_dev_lock);
  2759. return sport;
  2760. }
  2761. /**
  2762. * srpt_add_one() - Infiniband device addition callback function.
  2763. */
  2764. static void srpt_add_one(struct ib_device *device)
  2765. {
  2766. struct srpt_device *sdev;
  2767. struct srpt_port *sport;
  2768. struct ib_srq_init_attr srq_attr;
  2769. int i;
  2770. pr_debug("device = %p, device->dma_ops = %p\n", device,
  2771. device->dma_ops);
  2772. sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
  2773. if (!sdev)
  2774. goto err;
  2775. sdev->device = device;
  2776. INIT_LIST_HEAD(&sdev->rch_list);
  2777. init_waitqueue_head(&sdev->ch_releaseQ);
  2778. spin_lock_init(&sdev->spinlock);
  2779. if (ib_query_device(device, &sdev->dev_attr))
  2780. goto free_dev;
  2781. sdev->pd = ib_alloc_pd(device);
  2782. if (IS_ERR(sdev->pd))
  2783. goto free_dev;
  2784. sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
  2785. if (IS_ERR(sdev->mr))
  2786. goto err_pd;
  2787. sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
  2788. srq_attr.event_handler = srpt_srq_event;
  2789. srq_attr.srq_context = (void *)sdev;
  2790. srq_attr.attr.max_wr = sdev->srq_size;
  2791. srq_attr.attr.max_sge = 1;
  2792. srq_attr.attr.srq_limit = 0;
  2793. srq_attr.srq_type = IB_SRQT_BASIC;
  2794. sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
  2795. if (IS_ERR(sdev->srq))
  2796. goto err_mr;
  2797. pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
  2798. __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
  2799. device->name);
  2800. if (!srpt_service_guid)
  2801. srpt_service_guid = be64_to_cpu(device->node_guid);
  2802. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2803. if (IS_ERR(sdev->cm_id))
  2804. goto err_srq;
  2805. /* print out target login information */
  2806. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2807. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2808. srpt_service_guid, srpt_service_guid);
  2809. /*
  2810. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2811. * to identify this target. We currently use the guid of the first HCA
  2812. * in the system as service_id; therefore, the target_id will change
  2813. * if this HCA is gone bad and replaced by different HCA
  2814. */
  2815. if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
  2816. goto err_cm;
  2817. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2818. srpt_event_handler);
  2819. if (ib_register_event_handler(&sdev->event_handler))
  2820. goto err_cm;
  2821. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2822. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2823. sizeof(*sdev->ioctx_ring[0]),
  2824. srp_max_req_size, DMA_FROM_DEVICE);
  2825. if (!sdev->ioctx_ring)
  2826. goto err_event;
  2827. for (i = 0; i < sdev->srq_size; ++i)
  2828. srpt_post_recv(sdev, sdev->ioctx_ring[i]);
  2829. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2830. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2831. sport = &sdev->port[i - 1];
  2832. sport->sdev = sdev;
  2833. sport->port = i;
  2834. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2835. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2836. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2837. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2838. INIT_LIST_HEAD(&sport->port_acl_list);
  2839. spin_lock_init(&sport->port_acl_lock);
  2840. if (srpt_refresh_port(sport)) {
  2841. printk(KERN_ERR "MAD registration failed for %s-%d.\n",
  2842. srpt_sdev_name(sdev), i);
  2843. goto err_ring;
  2844. }
  2845. snprintf(sport->port_guid, sizeof(sport->port_guid),
  2846. "0x%016llx%016llx",
  2847. be64_to_cpu(sport->gid.global.subnet_prefix),
  2848. be64_to_cpu(sport->gid.global.interface_id));
  2849. }
  2850. spin_lock(&srpt_dev_lock);
  2851. list_add_tail(&sdev->list, &srpt_dev_list);
  2852. spin_unlock(&srpt_dev_lock);
  2853. out:
  2854. ib_set_client_data(device, &srpt_client, sdev);
  2855. pr_debug("added %s.\n", device->name);
  2856. return;
  2857. err_ring:
  2858. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2859. sdev->srq_size, srp_max_req_size,
  2860. DMA_FROM_DEVICE);
  2861. err_event:
  2862. ib_unregister_event_handler(&sdev->event_handler);
  2863. err_cm:
  2864. ib_destroy_cm_id(sdev->cm_id);
  2865. err_srq:
  2866. ib_destroy_srq(sdev->srq);
  2867. err_mr:
  2868. ib_dereg_mr(sdev->mr);
  2869. err_pd:
  2870. ib_dealloc_pd(sdev->pd);
  2871. free_dev:
  2872. kfree(sdev);
  2873. err:
  2874. sdev = NULL;
  2875. printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
  2876. goto out;
  2877. }
  2878. /**
  2879. * srpt_remove_one() - InfiniBand device removal callback function.
  2880. */
  2881. static void srpt_remove_one(struct ib_device *device)
  2882. {
  2883. struct srpt_device *sdev;
  2884. int i;
  2885. sdev = ib_get_client_data(device, &srpt_client);
  2886. if (!sdev) {
  2887. printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
  2888. device->name);
  2889. return;
  2890. }
  2891. srpt_unregister_mad_agent(sdev);
  2892. ib_unregister_event_handler(&sdev->event_handler);
  2893. /* Cancel any work queued by the just unregistered IB event handler. */
  2894. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2895. cancel_work_sync(&sdev->port[i].work);
  2896. ib_destroy_cm_id(sdev->cm_id);
  2897. /*
  2898. * Unregistering a target must happen after destroying sdev->cm_id
  2899. * such that no new SRP_LOGIN_REQ information units can arrive while
  2900. * destroying the target.
  2901. */
  2902. spin_lock(&srpt_dev_lock);
  2903. list_del(&sdev->list);
  2904. spin_unlock(&srpt_dev_lock);
  2905. srpt_release_sdev(sdev);
  2906. ib_destroy_srq(sdev->srq);
  2907. ib_dereg_mr(sdev->mr);
  2908. ib_dealloc_pd(sdev->pd);
  2909. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2910. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2911. sdev->ioctx_ring = NULL;
  2912. kfree(sdev);
  2913. }
  2914. static struct ib_client srpt_client = {
  2915. .name = DRV_NAME,
  2916. .add = srpt_add_one,
  2917. .remove = srpt_remove_one
  2918. };
  2919. static int srpt_check_true(struct se_portal_group *se_tpg)
  2920. {
  2921. return 1;
  2922. }
  2923. static int srpt_check_false(struct se_portal_group *se_tpg)
  2924. {
  2925. return 0;
  2926. }
  2927. static char *srpt_get_fabric_name(void)
  2928. {
  2929. return "srpt";
  2930. }
  2931. static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  2932. {
  2933. return SCSI_TRANSPORTID_PROTOCOLID_SRP;
  2934. }
  2935. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2936. {
  2937. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  2938. return sport->port_guid;
  2939. }
  2940. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2941. {
  2942. return 1;
  2943. }
  2944. static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
  2945. {
  2946. return 1;
  2947. }
  2948. static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
  2949. struct se_node_acl *se_nacl,
  2950. struct t10_pr_registration *pr_reg,
  2951. int *format_code, unsigned char *buf)
  2952. {
  2953. struct srpt_node_acl *nacl;
  2954. struct spc_rdma_transport_id *tr_id;
  2955. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  2956. tr_id = (void *)buf;
  2957. tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
  2958. memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
  2959. return sizeof(*tr_id);
  2960. }
  2961. static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
  2962. struct se_node_acl *se_nacl,
  2963. struct t10_pr_registration *pr_reg,
  2964. int *format_code)
  2965. {
  2966. *format_code = 0;
  2967. return sizeof(struct spc_rdma_transport_id);
  2968. }
  2969. static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
  2970. const char *buf, u32 *out_tid_len,
  2971. char **port_nexus_ptr)
  2972. {
  2973. struct spc_rdma_transport_id *tr_id;
  2974. *port_nexus_ptr = NULL;
  2975. *out_tid_len = sizeof(struct spc_rdma_transport_id);
  2976. tr_id = (void *)buf;
  2977. return (char *)tr_id->i_port_id;
  2978. }
  2979. static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
  2980. {
  2981. struct srpt_node_acl *nacl;
  2982. nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
  2983. if (!nacl) {
  2984. printk(KERN_ERR "Unable to allocate struct srpt_node_acl\n");
  2985. return NULL;
  2986. }
  2987. return &nacl->nacl;
  2988. }
  2989. static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
  2990. struct se_node_acl *se_nacl)
  2991. {
  2992. struct srpt_node_acl *nacl;
  2993. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  2994. kfree(nacl);
  2995. }
  2996. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2997. {
  2998. return 1;
  2999. }
  3000. static void srpt_release_cmd(struct se_cmd *se_cmd)
  3001. {
  3002. }
  3003. /**
  3004. * srpt_shutdown_session() - Whether or not a session may be shut down.
  3005. */
  3006. static int srpt_shutdown_session(struct se_session *se_sess)
  3007. {
  3008. return true;
  3009. }
  3010. /**
  3011. * srpt_close_session() - Forcibly close a session.
  3012. *
  3013. * Callback function invoked by the TCM core to clean up sessions associated
  3014. * with a node ACL when the user invokes
  3015. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3016. */
  3017. static void srpt_close_session(struct se_session *se_sess)
  3018. {
  3019. DECLARE_COMPLETION_ONSTACK(release_done);
  3020. struct srpt_rdma_ch *ch;
  3021. struct srpt_device *sdev;
  3022. int res;
  3023. ch = se_sess->fabric_sess_ptr;
  3024. WARN_ON(ch->sess != se_sess);
  3025. pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
  3026. sdev = ch->sport->sdev;
  3027. spin_lock_irq(&sdev->spinlock);
  3028. BUG_ON(ch->release_done);
  3029. ch->release_done = &release_done;
  3030. __srpt_close_ch(ch);
  3031. spin_unlock_irq(&sdev->spinlock);
  3032. res = wait_for_completion_timeout(&release_done, 60 * HZ);
  3033. WARN_ON(res <= 0);
  3034. }
  3035. /**
  3036. * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
  3037. *
  3038. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  3039. * This object represents an arbitrary integer used to uniquely identify a
  3040. * particular attached remote initiator port to a particular SCSI target port
  3041. * within a particular SCSI target device within a particular SCSI instance.
  3042. */
  3043. static u32 srpt_sess_get_index(struct se_session *se_sess)
  3044. {
  3045. return 0;
  3046. }
  3047. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  3048. {
  3049. }
  3050. static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
  3051. {
  3052. struct srpt_send_ioctx *ioctx;
  3053. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  3054. return ioctx->tag;
  3055. }
  3056. /* Note: only used from inside debug printk's by the TCM core. */
  3057. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  3058. {
  3059. struct srpt_send_ioctx *ioctx;
  3060. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  3061. return srpt_get_cmd_state(ioctx);
  3062. }
  3063. static u16 srpt_set_fabric_sense_len(struct se_cmd *cmd, u32 sense_length)
  3064. {
  3065. return 0;
  3066. }
  3067. static u16 srpt_get_fabric_sense_len(void)
  3068. {
  3069. return 0;
  3070. }
  3071. /**
  3072. * srpt_parse_i_port_id() - Parse an initiator port ID.
  3073. * @name: ASCII representation of a 128-bit initiator port ID.
  3074. * @i_port_id: Binary 128-bit port ID.
  3075. */
  3076. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  3077. {
  3078. const char *p;
  3079. unsigned len, count, leading_zero_bytes;
  3080. int ret, rc;
  3081. p = name;
  3082. if (strnicmp(p, "0x", 2) == 0)
  3083. p += 2;
  3084. ret = -EINVAL;
  3085. len = strlen(p);
  3086. if (len % 2)
  3087. goto out;
  3088. count = min(len / 2, 16U);
  3089. leading_zero_bytes = 16 - count;
  3090. memset(i_port_id, 0, leading_zero_bytes);
  3091. rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
  3092. if (rc < 0)
  3093. pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
  3094. ret = 0;
  3095. out:
  3096. return ret;
  3097. }
  3098. /*
  3099. * configfs callback function invoked for
  3100. * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3101. */
  3102. static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
  3103. struct config_group *group,
  3104. const char *name)
  3105. {
  3106. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  3107. struct se_node_acl *se_nacl, *se_nacl_new;
  3108. struct srpt_node_acl *nacl;
  3109. int ret = 0;
  3110. u32 nexus_depth = 1;
  3111. u8 i_port_id[16];
  3112. if (srpt_parse_i_port_id(i_port_id, name) < 0) {
  3113. printk(KERN_ERR "invalid initiator port ID %s\n", name);
  3114. ret = -EINVAL;
  3115. goto err;
  3116. }
  3117. se_nacl_new = srpt_alloc_fabric_acl(tpg);
  3118. if (!se_nacl_new) {
  3119. ret = -ENOMEM;
  3120. goto err;
  3121. }
  3122. /*
  3123. * nacl_new may be released by core_tpg_add_initiator_node_acl()
  3124. * when converting a node ACL from demo mode to explict
  3125. */
  3126. se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
  3127. nexus_depth);
  3128. if (IS_ERR(se_nacl)) {
  3129. ret = PTR_ERR(se_nacl);
  3130. goto err;
  3131. }
  3132. /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
  3133. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3134. memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
  3135. nacl->sport = sport;
  3136. spin_lock_irq(&sport->port_acl_lock);
  3137. list_add_tail(&nacl->list, &sport->port_acl_list);
  3138. spin_unlock_irq(&sport->port_acl_lock);
  3139. return se_nacl;
  3140. err:
  3141. return ERR_PTR(ret);
  3142. }
  3143. /*
  3144. * configfs callback function invoked for
  3145. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3146. */
  3147. static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
  3148. {
  3149. struct srpt_node_acl *nacl;
  3150. struct srpt_device *sdev;
  3151. struct srpt_port *sport;
  3152. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3153. sport = nacl->sport;
  3154. sdev = sport->sdev;
  3155. spin_lock_irq(&sport->port_acl_lock);
  3156. list_del(&nacl->list);
  3157. spin_unlock_irq(&sport->port_acl_lock);
  3158. core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
  3159. srpt_release_fabric_acl(NULL, se_nacl);
  3160. }
  3161. static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
  3162. struct se_portal_group *se_tpg,
  3163. char *page)
  3164. {
  3165. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3166. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  3167. }
  3168. static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
  3169. struct se_portal_group *se_tpg,
  3170. const char *page,
  3171. size_t count)
  3172. {
  3173. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3174. unsigned long val;
  3175. int ret;
  3176. ret = strict_strtoul(page, 0, &val);
  3177. if (ret < 0) {
  3178. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3179. return -EINVAL;
  3180. }
  3181. if (val > MAX_SRPT_RDMA_SIZE) {
  3182. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  3183. MAX_SRPT_RDMA_SIZE);
  3184. return -EINVAL;
  3185. }
  3186. if (val < DEFAULT_MAX_RDMA_SIZE) {
  3187. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  3188. val, DEFAULT_MAX_RDMA_SIZE);
  3189. return -EINVAL;
  3190. }
  3191. sport->port_attrib.srp_max_rdma_size = val;
  3192. return count;
  3193. }
  3194. TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
  3195. static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
  3196. struct se_portal_group *se_tpg,
  3197. char *page)
  3198. {
  3199. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3200. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  3201. }
  3202. static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
  3203. struct se_portal_group *se_tpg,
  3204. const char *page,
  3205. size_t count)
  3206. {
  3207. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3208. unsigned long val;
  3209. int ret;
  3210. ret = strict_strtoul(page, 0, &val);
  3211. if (ret < 0) {
  3212. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3213. return -EINVAL;
  3214. }
  3215. if (val > MAX_SRPT_RSP_SIZE) {
  3216. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  3217. MAX_SRPT_RSP_SIZE);
  3218. return -EINVAL;
  3219. }
  3220. if (val < MIN_MAX_RSP_SIZE) {
  3221. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  3222. MIN_MAX_RSP_SIZE);
  3223. return -EINVAL;
  3224. }
  3225. sport->port_attrib.srp_max_rsp_size = val;
  3226. return count;
  3227. }
  3228. TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
  3229. static ssize_t srpt_tpg_attrib_show_srp_sq_size(
  3230. struct se_portal_group *se_tpg,
  3231. char *page)
  3232. {
  3233. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3234. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  3235. }
  3236. static ssize_t srpt_tpg_attrib_store_srp_sq_size(
  3237. struct se_portal_group *se_tpg,
  3238. const char *page,
  3239. size_t count)
  3240. {
  3241. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3242. unsigned long val;
  3243. int ret;
  3244. ret = strict_strtoul(page, 0, &val);
  3245. if (ret < 0) {
  3246. pr_err("strict_strtoul() failed with ret: %d\n", ret);
  3247. return -EINVAL;
  3248. }
  3249. if (val > MAX_SRPT_SRQ_SIZE) {
  3250. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  3251. MAX_SRPT_SRQ_SIZE);
  3252. return -EINVAL;
  3253. }
  3254. if (val < MIN_SRPT_SRQ_SIZE) {
  3255. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  3256. MIN_SRPT_SRQ_SIZE);
  3257. return -EINVAL;
  3258. }
  3259. sport->port_attrib.srp_sq_size = val;
  3260. return count;
  3261. }
  3262. TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
  3263. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  3264. &srpt_tpg_attrib_srp_max_rdma_size.attr,
  3265. &srpt_tpg_attrib_srp_max_rsp_size.attr,
  3266. &srpt_tpg_attrib_srp_sq_size.attr,
  3267. NULL,
  3268. };
  3269. static ssize_t srpt_tpg_show_enable(
  3270. struct se_portal_group *se_tpg,
  3271. char *page)
  3272. {
  3273. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3274. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  3275. }
  3276. static ssize_t srpt_tpg_store_enable(
  3277. struct se_portal_group *se_tpg,
  3278. const char *page,
  3279. size_t count)
  3280. {
  3281. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3282. unsigned long tmp;
  3283. int ret;
  3284. ret = strict_strtoul(page, 0, &tmp);
  3285. if (ret < 0) {
  3286. printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
  3287. return -EINVAL;
  3288. }
  3289. if ((tmp != 0) && (tmp != 1)) {
  3290. printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  3291. return -EINVAL;
  3292. }
  3293. if (tmp == 1)
  3294. sport->enabled = true;
  3295. else
  3296. sport->enabled = false;
  3297. return count;
  3298. }
  3299. TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
  3300. static struct configfs_attribute *srpt_tpg_attrs[] = {
  3301. &srpt_tpg_enable.attr,
  3302. NULL,
  3303. };
  3304. /**
  3305. * configfs callback invoked for
  3306. * mkdir /sys/kernel/config/target/$driver/$port/$tpg
  3307. */
  3308. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  3309. struct config_group *group,
  3310. const char *name)
  3311. {
  3312. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  3313. int res;
  3314. /* Initialize sport->port_wwn and sport->port_tpg_1 */
  3315. res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
  3316. &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
  3317. if (res)
  3318. return ERR_PTR(res);
  3319. return &sport->port_tpg_1;
  3320. }
  3321. /**
  3322. * configfs callback invoked for
  3323. * rmdir /sys/kernel/config/target/$driver/$port/$tpg
  3324. */
  3325. static void srpt_drop_tpg(struct se_portal_group *tpg)
  3326. {
  3327. struct srpt_port *sport = container_of(tpg,
  3328. struct srpt_port, port_tpg_1);
  3329. sport->enabled = false;
  3330. core_tpg_deregister(&sport->port_tpg_1);
  3331. }
  3332. /**
  3333. * configfs callback invoked for
  3334. * mkdir /sys/kernel/config/target/$driver/$port
  3335. */
  3336. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  3337. struct config_group *group,
  3338. const char *name)
  3339. {
  3340. struct srpt_port *sport;
  3341. int ret;
  3342. sport = srpt_lookup_port(name);
  3343. pr_debug("make_tport(%s)\n", name);
  3344. ret = -EINVAL;
  3345. if (!sport)
  3346. goto err;
  3347. return &sport->port_wwn;
  3348. err:
  3349. return ERR_PTR(ret);
  3350. }
  3351. /**
  3352. * configfs callback invoked for
  3353. * rmdir /sys/kernel/config/target/$driver/$port
  3354. */
  3355. static void srpt_drop_tport(struct se_wwn *wwn)
  3356. {
  3357. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  3358. pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
  3359. }
  3360. static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
  3361. char *buf)
  3362. {
  3363. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  3364. }
  3365. TF_WWN_ATTR_RO(srpt, version);
  3366. static struct configfs_attribute *srpt_wwn_attrs[] = {
  3367. &srpt_wwn_version.attr,
  3368. NULL,
  3369. };
  3370. static struct target_core_fabric_ops srpt_template = {
  3371. .get_fabric_name = srpt_get_fabric_name,
  3372. .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
  3373. .tpg_get_wwn = srpt_get_fabric_wwn,
  3374. .tpg_get_tag = srpt_get_tag,
  3375. .tpg_get_default_depth = srpt_get_default_depth,
  3376. .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
  3377. .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
  3378. .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
  3379. .tpg_check_demo_mode = srpt_check_false,
  3380. .tpg_check_demo_mode_cache = srpt_check_true,
  3381. .tpg_check_demo_mode_write_protect = srpt_check_true,
  3382. .tpg_check_prod_mode_write_protect = srpt_check_false,
  3383. .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
  3384. .tpg_release_fabric_acl = srpt_release_fabric_acl,
  3385. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  3386. .release_cmd = srpt_release_cmd,
  3387. .check_stop_free = srpt_check_stop_free,
  3388. .shutdown_session = srpt_shutdown_session,
  3389. .close_session = srpt_close_session,
  3390. .sess_get_index = srpt_sess_get_index,
  3391. .sess_get_initiator_sid = NULL,
  3392. .write_pending = srpt_write_pending,
  3393. .write_pending_status = srpt_write_pending_status,
  3394. .set_default_node_attributes = srpt_set_default_node_attrs,
  3395. .get_task_tag = srpt_get_task_tag,
  3396. .get_cmd_state = srpt_get_tcm_cmd_state,
  3397. .queue_data_in = srpt_queue_response,
  3398. .queue_status = srpt_queue_status,
  3399. .queue_tm_rsp = srpt_queue_response,
  3400. .get_fabric_sense_len = srpt_get_fabric_sense_len,
  3401. .set_fabric_sense_len = srpt_set_fabric_sense_len,
  3402. /*
  3403. * Setup function pointers for generic logic in
  3404. * target_core_fabric_configfs.c
  3405. */
  3406. .fabric_make_wwn = srpt_make_tport,
  3407. .fabric_drop_wwn = srpt_drop_tport,
  3408. .fabric_make_tpg = srpt_make_tpg,
  3409. .fabric_drop_tpg = srpt_drop_tpg,
  3410. .fabric_post_link = NULL,
  3411. .fabric_pre_unlink = NULL,
  3412. .fabric_make_np = NULL,
  3413. .fabric_drop_np = NULL,
  3414. .fabric_make_nodeacl = srpt_make_nodeacl,
  3415. .fabric_drop_nodeacl = srpt_drop_nodeacl,
  3416. };
  3417. /**
  3418. * srpt_init_module() - Kernel module initialization.
  3419. *
  3420. * Note: Since ib_register_client() registers callback functions, and since at
  3421. * least one of these callback functions (srpt_add_one()) calls target core
  3422. * functions, this driver must be registered with the target core before
  3423. * ib_register_client() is called.
  3424. */
  3425. static int __init srpt_init_module(void)
  3426. {
  3427. int ret;
  3428. ret = -EINVAL;
  3429. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  3430. printk(KERN_ERR "invalid value %d for kernel module parameter"
  3431. " srp_max_req_size -- must be at least %d.\n",
  3432. srp_max_req_size, MIN_MAX_REQ_SIZE);
  3433. goto out;
  3434. }
  3435. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  3436. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  3437. printk(KERN_ERR "invalid value %d for kernel module parameter"
  3438. " srpt_srq_size -- must be in the range [%d..%d].\n",
  3439. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  3440. goto out;
  3441. }
  3442. srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
  3443. if (IS_ERR(srpt_target)) {
  3444. printk(KERN_ERR "couldn't register\n");
  3445. ret = PTR_ERR(srpt_target);
  3446. goto out;
  3447. }
  3448. srpt_target->tf_ops = srpt_template;
  3449. /*
  3450. * Set up default attribute lists.
  3451. */
  3452. srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
  3453. srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
  3454. srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
  3455. srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
  3456. srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
  3457. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  3458. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  3459. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  3460. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  3461. ret = target_fabric_configfs_register(srpt_target);
  3462. if (ret < 0) {
  3463. printk(KERN_ERR "couldn't register\n");
  3464. goto out_free_target;
  3465. }
  3466. ret = ib_register_client(&srpt_client);
  3467. if (ret) {
  3468. printk(KERN_ERR "couldn't register IB client\n");
  3469. goto out_unregister_target;
  3470. }
  3471. return 0;
  3472. out_unregister_target:
  3473. target_fabric_configfs_deregister(srpt_target);
  3474. srpt_target = NULL;
  3475. out_free_target:
  3476. if (srpt_target)
  3477. target_fabric_configfs_free(srpt_target);
  3478. out:
  3479. return ret;
  3480. }
  3481. static void __exit srpt_cleanup_module(void)
  3482. {
  3483. ib_unregister_client(&srpt_client);
  3484. target_fabric_configfs_deregister(srpt_target);
  3485. srpt_target = NULL;
  3486. }
  3487. module_init(srpt_init_module);
  3488. module_exit(srpt_cleanup_module);