ib_srpt.c 99 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759
  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 <linux/inet.h>
  44. #include <rdma/ib_cache.h>
  45. #include <scsi/scsi_proto.h>
  46. #include <scsi/scsi_tcq.h>
  47. #include <target/target_core_base.h>
  48. #include <target/target_core_fabric.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, const 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. /* Protects both rdma_cm_port and rdma_cm_id. */
  86. static DEFINE_MUTEX(rdma_cm_mutex);
  87. /* Port number RDMA/CM will bind to. */
  88. static u16 rdma_cm_port;
  89. static struct rdma_cm_id *rdma_cm_id;
  90. static void srpt_release_cmd(struct se_cmd *se_cmd);
  91. static void srpt_free_ch(struct kref *kref);
  92. static int srpt_queue_status(struct se_cmd *cmd);
  93. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
  94. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
  95. static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
  96. /*
  97. * The only allowed channel state changes are those that change the channel
  98. * state into a state with a higher numerical value. Hence the new > prev test.
  99. */
  100. static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
  101. {
  102. unsigned long flags;
  103. enum rdma_ch_state prev;
  104. bool changed = false;
  105. spin_lock_irqsave(&ch->spinlock, flags);
  106. prev = ch->state;
  107. if (new > prev) {
  108. ch->state = new;
  109. changed = true;
  110. }
  111. spin_unlock_irqrestore(&ch->spinlock, flags);
  112. return changed;
  113. }
  114. /**
  115. * srpt_event_handler - asynchronous IB event callback function
  116. * @handler: IB event handler registered by ib_register_event_handler().
  117. * @event: Description of the event that occurred.
  118. *
  119. * Callback function called by the InfiniBand core when an asynchronous IB
  120. * event occurs. This callback may occur in interrupt context. See also
  121. * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
  122. * Architecture Specification.
  123. */
  124. static void srpt_event_handler(struct ib_event_handler *handler,
  125. struct ib_event *event)
  126. {
  127. struct srpt_device *sdev;
  128. struct srpt_port *sport;
  129. u8 port_num;
  130. sdev = ib_get_client_data(event->device, &srpt_client);
  131. if (!sdev || sdev->device != event->device)
  132. return;
  133. pr_debug("ASYNC event= %d on device= %s\n", event->event,
  134. sdev->device->name);
  135. switch (event->event) {
  136. case IB_EVENT_PORT_ERR:
  137. port_num = event->element.port_num - 1;
  138. if (port_num < sdev->device->phys_port_cnt) {
  139. sport = &sdev->port[port_num];
  140. sport->lid = 0;
  141. sport->sm_lid = 0;
  142. } else {
  143. WARN(true, "event %d: port_num %d out of range 1..%d\n",
  144. event->event, port_num + 1,
  145. sdev->device->phys_port_cnt);
  146. }
  147. break;
  148. case IB_EVENT_PORT_ACTIVE:
  149. case IB_EVENT_LID_CHANGE:
  150. case IB_EVENT_PKEY_CHANGE:
  151. case IB_EVENT_SM_CHANGE:
  152. case IB_EVENT_CLIENT_REREGISTER:
  153. case IB_EVENT_GID_CHANGE:
  154. /* Refresh port data asynchronously. */
  155. port_num = event->element.port_num - 1;
  156. if (port_num < sdev->device->phys_port_cnt) {
  157. sport = &sdev->port[port_num];
  158. if (!sport->lid && !sport->sm_lid)
  159. schedule_work(&sport->work);
  160. } else {
  161. WARN(true, "event %d: port_num %d out of range 1..%d\n",
  162. event->event, port_num + 1,
  163. sdev->device->phys_port_cnt);
  164. }
  165. break;
  166. default:
  167. pr_err("received unrecognized IB event %d\n", event->event);
  168. break;
  169. }
  170. }
  171. /**
  172. * srpt_srq_event - SRQ event callback function
  173. * @event: Description of the event that occurred.
  174. * @ctx: Context pointer specified at SRQ creation time.
  175. */
  176. static void srpt_srq_event(struct ib_event *event, void *ctx)
  177. {
  178. pr_debug("SRQ event %d\n", event->event);
  179. }
  180. static const char *get_ch_state_name(enum rdma_ch_state s)
  181. {
  182. switch (s) {
  183. case CH_CONNECTING:
  184. return "connecting";
  185. case CH_LIVE:
  186. return "live";
  187. case CH_DISCONNECTING:
  188. return "disconnecting";
  189. case CH_DRAINING:
  190. return "draining";
  191. case CH_DISCONNECTED:
  192. return "disconnected";
  193. }
  194. return "???";
  195. }
  196. /**
  197. * srpt_qp_event - QP event callback function
  198. * @event: Description of the event that occurred.
  199. * @ch: SRPT RDMA channel.
  200. */
  201. static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
  202. {
  203. pr_debug("QP event %d on ch=%p sess_name=%s state=%d\n",
  204. event->event, ch, ch->sess_name, ch->state);
  205. switch (event->event) {
  206. case IB_EVENT_COMM_EST:
  207. if (ch->using_rdma_cm)
  208. rdma_notify(ch->rdma_cm.cm_id, event->event);
  209. else
  210. ib_cm_notify(ch->ib_cm.cm_id, event->event);
  211. break;
  212. case IB_EVENT_QP_LAST_WQE_REACHED:
  213. pr_debug("%s-%d, state %s: received Last WQE event.\n",
  214. ch->sess_name, ch->qp->qp_num,
  215. get_ch_state_name(ch->state));
  216. break;
  217. default:
  218. pr_err("received unrecognized IB QP event %d\n", event->event);
  219. break;
  220. }
  221. }
  222. /**
  223. * srpt_set_ioc - initialize a IOUnitInfo structure
  224. * @c_list: controller list.
  225. * @slot: one-based slot number.
  226. * @value: four-bit value.
  227. *
  228. * Copies the lowest four bits of value in element slot of the array of four
  229. * bit elements called c_list (controller list). The index slot is one-based.
  230. */
  231. static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
  232. {
  233. u16 id;
  234. u8 tmp;
  235. id = (slot - 1) / 2;
  236. if (slot & 0x1) {
  237. tmp = c_list[id] & 0xf;
  238. c_list[id] = (value << 4) | tmp;
  239. } else {
  240. tmp = c_list[id] & 0xf0;
  241. c_list[id] = (value & 0xf) | tmp;
  242. }
  243. }
  244. /**
  245. * srpt_get_class_port_info - copy ClassPortInfo to a management datagram
  246. * @mad: Datagram that will be sent as response to DM_ATTR_CLASS_PORT_INFO.
  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. ib_set_cpi_resp_time(cif, 20);
  259. mad->mad_hdr.status = 0;
  260. }
  261. /**
  262. * srpt_get_iou - write IOUnitInfo to a management datagram
  263. * @mad: Datagram that will be sent as response to DM_ATTR_IOU_INFO.
  264. *
  265. * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
  266. * Specification. See also section B.7, table B.6 in the SRP r16a document.
  267. */
  268. static void srpt_get_iou(struct ib_dm_mad *mad)
  269. {
  270. struct ib_dm_iou_info *ioui;
  271. u8 slot;
  272. int i;
  273. ioui = (struct ib_dm_iou_info *)mad->data;
  274. ioui->change_id = cpu_to_be16(1);
  275. ioui->max_controllers = 16;
  276. /* set present for slot 1 and empty for the rest */
  277. srpt_set_ioc(ioui->controller_list, 1, 1);
  278. for (i = 1, slot = 2; i < 16; i++, slot++)
  279. srpt_set_ioc(ioui->controller_list, slot, 0);
  280. mad->mad_hdr.status = 0;
  281. }
  282. /**
  283. * srpt_get_ioc - write IOControllerprofile to a management datagram
  284. * @sport: HCA port through which the MAD has been received.
  285. * @slot: Slot number specified in DM_ATTR_IOC_PROFILE query.
  286. * @mad: Datagram that will be sent as response to DM_ATTR_IOC_PROFILE.
  287. *
  288. * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
  289. * Architecture Specification. See also section B.7, table B.7 in the SRP
  290. * r16a document.
  291. */
  292. static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
  293. struct ib_dm_mad *mad)
  294. {
  295. struct srpt_device *sdev = sport->sdev;
  296. struct ib_dm_ioc_profile *iocp;
  297. int send_queue_depth;
  298. iocp = (struct ib_dm_ioc_profile *)mad->data;
  299. if (!slot || slot > 16) {
  300. mad->mad_hdr.status
  301. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  302. return;
  303. }
  304. if (slot > 2) {
  305. mad->mad_hdr.status
  306. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  307. return;
  308. }
  309. if (sdev->use_srq)
  310. send_queue_depth = sdev->srq_size;
  311. else
  312. send_queue_depth = min(MAX_SRPT_RQ_SIZE,
  313. sdev->device->attrs.max_qp_wr);
  314. memset(iocp, 0, sizeof(*iocp));
  315. strcpy(iocp->id_string, SRPT_ID_STRING);
  316. iocp->guid = cpu_to_be64(srpt_service_guid);
  317. iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  318. iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
  319. iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
  320. iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  321. iocp->subsys_device_id = 0x0;
  322. iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
  323. iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
  324. iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
  325. iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
  326. iocp->send_queue_depth = cpu_to_be16(send_queue_depth);
  327. iocp->rdma_read_depth = 4;
  328. iocp->send_size = cpu_to_be32(srp_max_req_size);
  329. iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
  330. 1U << 24));
  331. iocp->num_svc_entries = 1;
  332. iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
  333. SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
  334. mad->mad_hdr.status = 0;
  335. }
  336. /**
  337. * srpt_get_svc_entries - write ServiceEntries to a management datagram
  338. * @ioc_guid: I/O controller GUID to use in reply.
  339. * @slot: I/O controller number.
  340. * @hi: End of the range of service entries to be specified in the reply.
  341. * @lo: Start of the range of service entries to be specified in the reply..
  342. * @mad: Datagram that will be sent as response to DM_ATTR_SVC_ENTRIES.
  343. *
  344. * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
  345. * Specification. See also section B.7, table B.8 in the SRP r16a document.
  346. */
  347. static void srpt_get_svc_entries(u64 ioc_guid,
  348. u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
  349. {
  350. struct ib_dm_svc_entries *svc_entries;
  351. WARN_ON(!ioc_guid);
  352. if (!slot || slot > 16) {
  353. mad->mad_hdr.status
  354. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  355. return;
  356. }
  357. if (slot > 2 || lo > hi || hi > 1) {
  358. mad->mad_hdr.status
  359. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  360. return;
  361. }
  362. svc_entries = (struct ib_dm_svc_entries *)mad->data;
  363. memset(svc_entries, 0, sizeof(*svc_entries));
  364. svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
  365. snprintf(svc_entries->service_entries[0].name,
  366. sizeof(svc_entries->service_entries[0].name),
  367. "%s%016llx",
  368. SRP_SERVICE_NAME_PREFIX,
  369. ioc_guid);
  370. mad->mad_hdr.status = 0;
  371. }
  372. /**
  373. * srpt_mgmt_method_get - process a received management datagram
  374. * @sp: HCA port through which the MAD has been received.
  375. * @rq_mad: received MAD.
  376. * @rsp_mad: response MAD.
  377. */
  378. static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
  379. struct ib_dm_mad *rsp_mad)
  380. {
  381. u16 attr_id;
  382. u32 slot;
  383. u8 hi, lo;
  384. attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
  385. switch (attr_id) {
  386. case DM_ATTR_CLASS_PORT_INFO:
  387. srpt_get_class_port_info(rsp_mad);
  388. break;
  389. case DM_ATTR_IOU_INFO:
  390. srpt_get_iou(rsp_mad);
  391. break;
  392. case DM_ATTR_IOC_PROFILE:
  393. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  394. srpt_get_ioc(sp, slot, rsp_mad);
  395. break;
  396. case DM_ATTR_SVC_ENTRIES:
  397. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  398. hi = (u8) ((slot >> 8) & 0xff);
  399. lo = (u8) (slot & 0xff);
  400. slot = (u16) ((slot >> 16) & 0xffff);
  401. srpt_get_svc_entries(srpt_service_guid,
  402. slot, hi, lo, rsp_mad);
  403. break;
  404. default:
  405. rsp_mad->mad_hdr.status =
  406. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  407. break;
  408. }
  409. }
  410. /**
  411. * srpt_mad_send_handler - MAD send completion callback
  412. * @mad_agent: Return value of ib_register_mad_agent().
  413. * @mad_wc: Work completion reporting that the MAD has been sent.
  414. */
  415. static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
  416. struct ib_mad_send_wc *mad_wc)
  417. {
  418. rdma_destroy_ah(mad_wc->send_buf->ah);
  419. ib_free_send_mad(mad_wc->send_buf);
  420. }
  421. /**
  422. * srpt_mad_recv_handler - MAD reception callback function
  423. * @mad_agent: Return value of ib_register_mad_agent().
  424. * @send_buf: Not used.
  425. * @mad_wc: Work completion reporting that a MAD has been received.
  426. */
  427. static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
  428. struct ib_mad_send_buf *send_buf,
  429. struct ib_mad_recv_wc *mad_wc)
  430. {
  431. struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
  432. struct ib_ah *ah;
  433. struct ib_mad_send_buf *rsp;
  434. struct ib_dm_mad *dm_mad;
  435. if (!mad_wc || !mad_wc->recv_buf.mad)
  436. return;
  437. ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
  438. mad_wc->recv_buf.grh, mad_agent->port_num);
  439. if (IS_ERR(ah))
  440. goto err;
  441. BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
  442. rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
  443. mad_wc->wc->pkey_index, 0,
  444. IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
  445. GFP_KERNEL,
  446. IB_MGMT_BASE_VERSION);
  447. if (IS_ERR(rsp))
  448. goto err_rsp;
  449. rsp->ah = ah;
  450. dm_mad = rsp->mad;
  451. memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
  452. dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  453. dm_mad->mad_hdr.status = 0;
  454. switch (mad_wc->recv_buf.mad->mad_hdr.method) {
  455. case IB_MGMT_METHOD_GET:
  456. srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
  457. break;
  458. case IB_MGMT_METHOD_SET:
  459. dm_mad->mad_hdr.status =
  460. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  461. break;
  462. default:
  463. dm_mad->mad_hdr.status =
  464. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
  465. break;
  466. }
  467. if (!ib_post_send_mad(rsp, NULL)) {
  468. ib_free_recv_mad(mad_wc);
  469. /* will destroy_ah & free_send_mad in send completion */
  470. return;
  471. }
  472. ib_free_send_mad(rsp);
  473. err_rsp:
  474. rdma_destroy_ah(ah);
  475. err:
  476. ib_free_recv_mad(mad_wc);
  477. }
  478. static int srpt_format_guid(char *buf, unsigned int size, const __be64 *guid)
  479. {
  480. const __be16 *g = (const __be16 *)guid;
  481. return snprintf(buf, size, "%04x:%04x:%04x:%04x",
  482. be16_to_cpu(g[0]), be16_to_cpu(g[1]),
  483. be16_to_cpu(g[2]), be16_to_cpu(g[3]));
  484. }
  485. /**
  486. * srpt_refresh_port - configure a HCA port
  487. * @sport: SRPT HCA port.
  488. *
  489. * Enable InfiniBand management datagram processing, update the cached sm_lid,
  490. * lid and gid values, and register a callback function for processing MADs
  491. * on the specified port.
  492. *
  493. * Note: It is safe to call this function more than once for the same port.
  494. */
  495. static int srpt_refresh_port(struct srpt_port *sport)
  496. {
  497. struct ib_mad_reg_req reg_req;
  498. struct ib_port_modify port_modify;
  499. struct ib_port_attr port_attr;
  500. int ret;
  501. memset(&port_modify, 0, sizeof(port_modify));
  502. port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  503. port_modify.clr_port_cap_mask = 0;
  504. ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  505. if (ret)
  506. goto err_mod_port;
  507. ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
  508. if (ret)
  509. goto err_query_port;
  510. sport->sm_lid = port_attr.sm_lid;
  511. sport->lid = port_attr.lid;
  512. ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
  513. NULL);
  514. if (ret)
  515. goto err_query_port;
  516. sport->port_guid_wwn.priv = sport;
  517. srpt_format_guid(sport->port_guid, sizeof(sport->port_guid),
  518. &sport->gid.global.interface_id);
  519. sport->port_gid_wwn.priv = sport;
  520. snprintf(sport->port_gid, sizeof(sport->port_gid),
  521. "0x%016llx%016llx",
  522. be64_to_cpu(sport->gid.global.subnet_prefix),
  523. be64_to_cpu(sport->gid.global.interface_id));
  524. if (!sport->mad_agent) {
  525. memset(&reg_req, 0, sizeof(reg_req));
  526. reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
  527. reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
  528. set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
  529. set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
  530. sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
  531. sport->port,
  532. IB_QPT_GSI,
  533. &reg_req, 0,
  534. srpt_mad_send_handler,
  535. srpt_mad_recv_handler,
  536. sport, 0);
  537. if (IS_ERR(sport->mad_agent)) {
  538. ret = PTR_ERR(sport->mad_agent);
  539. sport->mad_agent = NULL;
  540. goto err_query_port;
  541. }
  542. }
  543. return 0;
  544. err_query_port:
  545. port_modify.set_port_cap_mask = 0;
  546. port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  547. ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  548. err_mod_port:
  549. return ret;
  550. }
  551. /**
  552. * srpt_unregister_mad_agent - unregister MAD callback functions
  553. * @sdev: SRPT HCA pointer.
  554. *
  555. * Note: It is safe to call this function more than once for the same device.
  556. */
  557. static void srpt_unregister_mad_agent(struct srpt_device *sdev)
  558. {
  559. struct ib_port_modify port_modify = {
  560. .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
  561. };
  562. struct srpt_port *sport;
  563. int i;
  564. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  565. sport = &sdev->port[i - 1];
  566. WARN_ON(sport->port != i);
  567. if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
  568. pr_err("disabling MAD processing failed.\n");
  569. if (sport->mad_agent) {
  570. ib_unregister_mad_agent(sport->mad_agent);
  571. sport->mad_agent = NULL;
  572. }
  573. }
  574. }
  575. /**
  576. * srpt_alloc_ioctx - allocate a SRPT I/O context structure
  577. * @sdev: SRPT HCA pointer.
  578. * @ioctx_size: I/O context size.
  579. * @dma_size: Size of I/O context DMA buffer.
  580. * @dir: DMA data direction.
  581. */
  582. static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
  583. int ioctx_size, int dma_size,
  584. enum dma_data_direction dir)
  585. {
  586. struct srpt_ioctx *ioctx;
  587. ioctx = kmalloc(ioctx_size, GFP_KERNEL);
  588. if (!ioctx)
  589. goto err;
  590. ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
  591. if (!ioctx->buf)
  592. goto err_free_ioctx;
  593. ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
  594. if (ib_dma_mapping_error(sdev->device, ioctx->dma))
  595. goto err_free_buf;
  596. return ioctx;
  597. err_free_buf:
  598. kfree(ioctx->buf);
  599. err_free_ioctx:
  600. kfree(ioctx);
  601. err:
  602. return NULL;
  603. }
  604. /**
  605. * srpt_free_ioctx - free a SRPT I/O context structure
  606. * @sdev: SRPT HCA pointer.
  607. * @ioctx: I/O context pointer.
  608. * @dma_size: Size of I/O context DMA buffer.
  609. * @dir: DMA data direction.
  610. */
  611. static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
  612. int dma_size, enum dma_data_direction dir)
  613. {
  614. if (!ioctx)
  615. return;
  616. ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
  617. kfree(ioctx->buf);
  618. kfree(ioctx);
  619. }
  620. /**
  621. * srpt_alloc_ioctx_ring - allocate a ring of SRPT I/O context structures
  622. * @sdev: Device to allocate the I/O context ring for.
  623. * @ring_size: Number of elements in the I/O context ring.
  624. * @ioctx_size: I/O context size.
  625. * @dma_size: DMA buffer size.
  626. * @dir: DMA data direction.
  627. */
  628. static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
  629. int ring_size, int ioctx_size,
  630. int dma_size, enum dma_data_direction dir)
  631. {
  632. struct srpt_ioctx **ring;
  633. int i;
  634. WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
  635. && ioctx_size != sizeof(struct srpt_send_ioctx));
  636. ring = kmalloc_array(ring_size, sizeof(ring[0]), GFP_KERNEL);
  637. if (!ring)
  638. goto out;
  639. for (i = 0; i < ring_size; ++i) {
  640. ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
  641. if (!ring[i])
  642. goto err;
  643. ring[i]->index = i;
  644. }
  645. goto out;
  646. err:
  647. while (--i >= 0)
  648. srpt_free_ioctx(sdev, ring[i], dma_size, dir);
  649. kfree(ring);
  650. ring = NULL;
  651. out:
  652. return ring;
  653. }
  654. /**
  655. * srpt_free_ioctx_ring - free the ring of SRPT I/O context structures
  656. * @ioctx_ring: I/O context ring to be freed.
  657. * @sdev: SRPT HCA pointer.
  658. * @ring_size: Number of ring elements.
  659. * @dma_size: Size of I/O context DMA buffer.
  660. * @dir: DMA data direction.
  661. */
  662. static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
  663. struct srpt_device *sdev, int ring_size,
  664. int dma_size, enum dma_data_direction dir)
  665. {
  666. int i;
  667. if (!ioctx_ring)
  668. return;
  669. for (i = 0; i < ring_size; ++i)
  670. srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
  671. kfree(ioctx_ring);
  672. }
  673. /**
  674. * srpt_set_cmd_state - set the state of a SCSI command
  675. * @ioctx: Send I/O context.
  676. * @new: New I/O context state.
  677. *
  678. * Does not modify the state of aborted commands. Returns the previous command
  679. * state.
  680. */
  681. static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
  682. enum srpt_command_state new)
  683. {
  684. enum srpt_command_state previous;
  685. previous = ioctx->state;
  686. if (previous != SRPT_STATE_DONE)
  687. ioctx->state = new;
  688. return previous;
  689. }
  690. /**
  691. * srpt_test_and_set_cmd_state - test and set the state of a command
  692. * @ioctx: Send I/O context.
  693. * @old: Current I/O context state.
  694. * @new: New I/O context state.
  695. *
  696. * Returns true if and only if the previous command state was equal to 'old'.
  697. */
  698. static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
  699. enum srpt_command_state old,
  700. enum srpt_command_state new)
  701. {
  702. enum srpt_command_state previous;
  703. WARN_ON(!ioctx);
  704. WARN_ON(old == SRPT_STATE_DONE);
  705. WARN_ON(new == SRPT_STATE_NEW);
  706. previous = ioctx->state;
  707. if (previous == old)
  708. ioctx->state = new;
  709. return previous == old;
  710. }
  711. /**
  712. * srpt_post_recv - post an IB receive request
  713. * @sdev: SRPT HCA pointer.
  714. * @ch: SRPT RDMA channel.
  715. * @ioctx: Receive I/O context pointer.
  716. */
  717. static int srpt_post_recv(struct srpt_device *sdev, struct srpt_rdma_ch *ch,
  718. struct srpt_recv_ioctx *ioctx)
  719. {
  720. struct ib_sge list;
  721. struct ib_recv_wr wr, *bad_wr;
  722. BUG_ON(!sdev);
  723. list.addr = ioctx->ioctx.dma;
  724. list.length = srp_max_req_size;
  725. list.lkey = sdev->lkey;
  726. ioctx->ioctx.cqe.done = srpt_recv_done;
  727. wr.wr_cqe = &ioctx->ioctx.cqe;
  728. wr.next = NULL;
  729. wr.sg_list = &list;
  730. wr.num_sge = 1;
  731. if (sdev->use_srq)
  732. return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
  733. else
  734. return ib_post_recv(ch->qp, &wr, &bad_wr);
  735. }
  736. /**
  737. * srpt_zerolength_write - perform a zero-length RDMA write
  738. * @ch: SRPT RDMA channel.
  739. *
  740. * A quote from the InfiniBand specification: C9-88: For an HCA responder
  741. * using Reliable Connection service, for each zero-length RDMA READ or WRITE
  742. * request, the R_Key shall not be validated, even if the request includes
  743. * Immediate data.
  744. */
  745. static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
  746. {
  747. struct ib_send_wr *bad_wr;
  748. struct ib_rdma_wr wr = {
  749. .wr = {
  750. .next = NULL,
  751. { .wr_cqe = &ch->zw_cqe, },
  752. .opcode = IB_WR_RDMA_WRITE,
  753. .send_flags = IB_SEND_SIGNALED,
  754. }
  755. };
  756. pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
  757. ch->qp->qp_num);
  758. return ib_post_send(ch->qp, &wr.wr, &bad_wr);
  759. }
  760. static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
  761. {
  762. struct srpt_rdma_ch *ch = cq->cq_context;
  763. pr_debug("%s-%d wc->status %d\n", ch->sess_name, ch->qp->qp_num,
  764. wc->status);
  765. if (wc->status == IB_WC_SUCCESS) {
  766. srpt_process_wait_list(ch);
  767. } else {
  768. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  769. schedule_work(&ch->release_work);
  770. else
  771. pr_debug("%s-%d: already disconnected.\n",
  772. ch->sess_name, ch->qp->qp_num);
  773. }
  774. }
  775. static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
  776. struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
  777. unsigned *sg_cnt)
  778. {
  779. enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
  780. struct srpt_rdma_ch *ch = ioctx->ch;
  781. struct scatterlist *prev = NULL;
  782. unsigned prev_nents;
  783. int ret, i;
  784. if (nbufs == 1) {
  785. ioctx->rw_ctxs = &ioctx->s_rw_ctx;
  786. } else {
  787. ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
  788. GFP_KERNEL);
  789. if (!ioctx->rw_ctxs)
  790. return -ENOMEM;
  791. }
  792. for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
  793. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  794. u64 remote_addr = be64_to_cpu(db->va);
  795. u32 size = be32_to_cpu(db->len);
  796. u32 rkey = be32_to_cpu(db->key);
  797. ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
  798. i < nbufs - 1);
  799. if (ret)
  800. goto unwind;
  801. ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
  802. ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
  803. if (ret < 0) {
  804. target_free_sgl(ctx->sg, ctx->nents);
  805. goto unwind;
  806. }
  807. ioctx->n_rdma += ret;
  808. ioctx->n_rw_ctx++;
  809. if (prev) {
  810. sg_unmark_end(&prev[prev_nents - 1]);
  811. sg_chain(prev, prev_nents + 1, ctx->sg);
  812. } else {
  813. *sg = ctx->sg;
  814. }
  815. prev = ctx->sg;
  816. prev_nents = ctx->nents;
  817. *sg_cnt += ctx->nents;
  818. }
  819. return 0;
  820. unwind:
  821. while (--i >= 0) {
  822. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  823. rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
  824. ctx->sg, ctx->nents, dir);
  825. target_free_sgl(ctx->sg, ctx->nents);
  826. }
  827. if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
  828. kfree(ioctx->rw_ctxs);
  829. return ret;
  830. }
  831. static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
  832. struct srpt_send_ioctx *ioctx)
  833. {
  834. enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
  835. int i;
  836. for (i = 0; i < ioctx->n_rw_ctx; i++) {
  837. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  838. rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
  839. ctx->sg, ctx->nents, dir);
  840. target_free_sgl(ctx->sg, ctx->nents);
  841. }
  842. if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
  843. kfree(ioctx->rw_ctxs);
  844. }
  845. static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
  846. {
  847. /*
  848. * The pointer computations below will only be compiled correctly
  849. * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
  850. * whether srp_cmd::add_data has been declared as a byte pointer.
  851. */
  852. BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
  853. !__same_type(srp_cmd->add_data[0], (u8)0));
  854. /*
  855. * According to the SRP spec, the lower two bits of the 'ADDITIONAL
  856. * CDB LENGTH' field are reserved and the size in bytes of this field
  857. * is four times the value specified in bits 3..7. Hence the "& ~3".
  858. */
  859. return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
  860. }
  861. /**
  862. * srpt_get_desc_tbl - parse the data descriptors of a SRP_CMD request
  863. * @ioctx: Pointer to the I/O context associated with the request.
  864. * @srp_cmd: Pointer to the SRP_CMD request data.
  865. * @dir: Pointer to the variable to which the transfer direction will be
  866. * written.
  867. * @sg: [out] scatterlist allocated for the parsed SRP_CMD.
  868. * @sg_cnt: [out] length of @sg.
  869. * @data_len: Pointer to the variable to which the total data length of all
  870. * descriptors in the SRP_CMD request will be written.
  871. *
  872. * This function initializes ioctx->nrbuf and ioctx->r_bufs.
  873. *
  874. * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
  875. * -ENOMEM when memory allocation fails and zero upon success.
  876. */
  877. static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
  878. struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
  879. struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
  880. {
  881. BUG_ON(!dir);
  882. BUG_ON(!data_len);
  883. /*
  884. * The lower four bits of the buffer format field contain the DATA-IN
  885. * buffer descriptor format, and the highest four bits contain the
  886. * DATA-OUT buffer descriptor format.
  887. */
  888. if (srp_cmd->buf_fmt & 0xf)
  889. /* DATA-IN: transfer data from target to initiator (read). */
  890. *dir = DMA_FROM_DEVICE;
  891. else if (srp_cmd->buf_fmt >> 4)
  892. /* DATA-OUT: transfer data from initiator to target (write). */
  893. *dir = DMA_TO_DEVICE;
  894. else
  895. *dir = DMA_NONE;
  896. /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
  897. ioctx->cmd.data_direction = *dir;
  898. if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
  899. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
  900. struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
  901. *data_len = be32_to_cpu(db->len);
  902. return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
  903. } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
  904. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
  905. struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
  906. int nbufs = be32_to_cpu(idb->table_desc.len) /
  907. sizeof(struct srp_direct_buf);
  908. if (nbufs >
  909. (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
  910. pr_err("received unsupported SRP_CMD request"
  911. " type (%u out + %u in != %u / %zu)\n",
  912. srp_cmd->data_out_desc_cnt,
  913. srp_cmd->data_in_desc_cnt,
  914. be32_to_cpu(idb->table_desc.len),
  915. sizeof(struct srp_direct_buf));
  916. return -EINVAL;
  917. }
  918. *data_len = be32_to_cpu(idb->len);
  919. return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
  920. sg, sg_cnt);
  921. } else {
  922. *data_len = 0;
  923. return 0;
  924. }
  925. }
  926. /**
  927. * srpt_init_ch_qp - initialize queue pair attributes
  928. * @ch: SRPT RDMA channel.
  929. * @qp: Queue pair pointer.
  930. *
  931. * Initialized the attributes of queue pair 'qp' by allowing local write,
  932. * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
  933. */
  934. static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  935. {
  936. struct ib_qp_attr *attr;
  937. int ret;
  938. WARN_ON_ONCE(ch->using_rdma_cm);
  939. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  940. if (!attr)
  941. return -ENOMEM;
  942. attr->qp_state = IB_QPS_INIT;
  943. attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
  944. attr->port_num = ch->sport->port;
  945. ret = ib_find_cached_pkey(ch->sport->sdev->device, ch->sport->port,
  946. ch->pkey, &attr->pkey_index);
  947. if (ret < 0)
  948. pr_err("Translating pkey %#x failed (%d) - using index 0\n",
  949. ch->pkey, ret);
  950. ret = ib_modify_qp(qp, attr,
  951. IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
  952. IB_QP_PKEY_INDEX);
  953. kfree(attr);
  954. return ret;
  955. }
  956. /**
  957. * srpt_ch_qp_rtr - change the state of a channel to 'ready to receive' (RTR)
  958. * @ch: channel of the queue pair.
  959. * @qp: queue pair to change the state of.
  960. *
  961. * Returns zero upon success and a negative value upon failure.
  962. *
  963. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  964. * If this structure ever becomes larger, it might be necessary to allocate
  965. * it dynamically instead of on the stack.
  966. */
  967. static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  968. {
  969. struct ib_qp_attr qp_attr;
  970. int attr_mask;
  971. int ret;
  972. WARN_ON_ONCE(ch->using_rdma_cm);
  973. qp_attr.qp_state = IB_QPS_RTR;
  974. ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
  975. if (ret)
  976. goto out;
  977. qp_attr.max_dest_rd_atomic = 4;
  978. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  979. out:
  980. return ret;
  981. }
  982. /**
  983. * srpt_ch_qp_rts - change the state of a channel to 'ready to send' (RTS)
  984. * @ch: channel of the queue pair.
  985. * @qp: queue pair to change the state of.
  986. *
  987. * Returns zero upon success and a negative value upon failure.
  988. *
  989. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  990. * If this structure ever becomes larger, it might be necessary to allocate
  991. * it dynamically instead of on the stack.
  992. */
  993. static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  994. {
  995. struct ib_qp_attr qp_attr;
  996. int attr_mask;
  997. int ret;
  998. qp_attr.qp_state = IB_QPS_RTS;
  999. ret = ib_cm_init_qp_attr(ch->ib_cm.cm_id, &qp_attr, &attr_mask);
  1000. if (ret)
  1001. goto out;
  1002. qp_attr.max_rd_atomic = 4;
  1003. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  1004. out:
  1005. return ret;
  1006. }
  1007. /**
  1008. * srpt_ch_qp_err - set the channel queue pair state to 'error'
  1009. * @ch: SRPT RDMA channel.
  1010. */
  1011. static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
  1012. {
  1013. struct ib_qp_attr qp_attr;
  1014. qp_attr.qp_state = IB_QPS_ERR;
  1015. return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
  1016. }
  1017. /**
  1018. * srpt_get_send_ioctx - obtain an I/O context for sending to the initiator
  1019. * @ch: SRPT RDMA channel.
  1020. */
  1021. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  1022. {
  1023. struct srpt_send_ioctx *ioctx;
  1024. unsigned long flags;
  1025. BUG_ON(!ch);
  1026. ioctx = NULL;
  1027. spin_lock_irqsave(&ch->spinlock, flags);
  1028. if (!list_empty(&ch->free_list)) {
  1029. ioctx = list_first_entry(&ch->free_list,
  1030. struct srpt_send_ioctx, free_list);
  1031. list_del(&ioctx->free_list);
  1032. }
  1033. spin_unlock_irqrestore(&ch->spinlock, flags);
  1034. if (!ioctx)
  1035. return ioctx;
  1036. BUG_ON(ioctx->ch != ch);
  1037. ioctx->state = SRPT_STATE_NEW;
  1038. ioctx->n_rdma = 0;
  1039. ioctx->n_rw_ctx = 0;
  1040. ioctx->queue_status_only = false;
  1041. /*
  1042. * transport_init_se_cmd() does not initialize all fields, so do it
  1043. * here.
  1044. */
  1045. memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
  1046. memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
  1047. return ioctx;
  1048. }
  1049. /**
  1050. * srpt_abort_cmd - abort a SCSI command
  1051. * @ioctx: I/O context associated with the SCSI command.
  1052. */
  1053. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  1054. {
  1055. enum srpt_command_state state;
  1056. BUG_ON(!ioctx);
  1057. /*
  1058. * If the command is in a state where the target core is waiting for
  1059. * the ib_srpt driver, change the state to the next state.
  1060. */
  1061. state = ioctx->state;
  1062. switch (state) {
  1063. case SRPT_STATE_NEED_DATA:
  1064. ioctx->state = SRPT_STATE_DATA_IN;
  1065. break;
  1066. case SRPT_STATE_CMD_RSP_SENT:
  1067. case SRPT_STATE_MGMT_RSP_SENT:
  1068. ioctx->state = SRPT_STATE_DONE;
  1069. break;
  1070. default:
  1071. WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
  1072. __func__, state);
  1073. break;
  1074. }
  1075. pr_debug("Aborting cmd with state %d -> %d and tag %lld\n", state,
  1076. ioctx->state, ioctx->cmd.tag);
  1077. switch (state) {
  1078. case SRPT_STATE_NEW:
  1079. case SRPT_STATE_DATA_IN:
  1080. case SRPT_STATE_MGMT:
  1081. case SRPT_STATE_DONE:
  1082. /*
  1083. * Do nothing - defer abort processing until
  1084. * srpt_queue_response() is invoked.
  1085. */
  1086. break;
  1087. case SRPT_STATE_NEED_DATA:
  1088. pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
  1089. transport_generic_request_failure(&ioctx->cmd,
  1090. TCM_CHECK_CONDITION_ABORT_CMD);
  1091. break;
  1092. case SRPT_STATE_CMD_RSP_SENT:
  1093. /*
  1094. * SRP_RSP sending failed or the SRP_RSP send completion has
  1095. * not been received in time.
  1096. */
  1097. transport_generic_free_cmd(&ioctx->cmd, 0);
  1098. break;
  1099. case SRPT_STATE_MGMT_RSP_SENT:
  1100. transport_generic_free_cmd(&ioctx->cmd, 0);
  1101. break;
  1102. default:
  1103. WARN(1, "Unexpected command state (%d)", state);
  1104. break;
  1105. }
  1106. return state;
  1107. }
  1108. /**
  1109. * srpt_rdma_read_done - RDMA read completion callback
  1110. * @cq: Completion queue.
  1111. * @wc: Work completion.
  1112. *
  1113. * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
  1114. * the data that has been transferred via IB RDMA had to be postponed until the
  1115. * check_stop_free() callback. None of this is necessary anymore and needs to
  1116. * be cleaned up.
  1117. */
  1118. static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
  1119. {
  1120. struct srpt_rdma_ch *ch = cq->cq_context;
  1121. struct srpt_send_ioctx *ioctx =
  1122. container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
  1123. WARN_ON(ioctx->n_rdma <= 0);
  1124. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1125. ioctx->n_rdma = 0;
  1126. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  1127. pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
  1128. ioctx, wc->status);
  1129. srpt_abort_cmd(ioctx);
  1130. return;
  1131. }
  1132. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1133. SRPT_STATE_DATA_IN))
  1134. target_execute_cmd(&ioctx->cmd);
  1135. else
  1136. pr_err("%s[%d]: wrong state = %d\n", __func__,
  1137. __LINE__, ioctx->state);
  1138. }
  1139. /**
  1140. * srpt_build_cmd_rsp - build a SRP_RSP response
  1141. * @ch: RDMA channel through which the request has been received.
  1142. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1143. * be built in the buffer ioctx->buf points at and hence this function will
  1144. * overwrite the request data.
  1145. * @tag: tag of the request for which this response is being generated.
  1146. * @status: value for the STATUS field of the SRP_RSP information unit.
  1147. *
  1148. * Returns the size in bytes of the SRP_RSP response.
  1149. *
  1150. * An SRP_RSP response contains a SCSI status or service response. See also
  1151. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1152. * response. See also SPC-2 for more information about sense data.
  1153. */
  1154. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1155. struct srpt_send_ioctx *ioctx, u64 tag,
  1156. int status)
  1157. {
  1158. struct srp_rsp *srp_rsp;
  1159. const u8 *sense_data;
  1160. int sense_data_len, max_sense_len;
  1161. /*
  1162. * The lowest bit of all SAM-3 status codes is zero (see also
  1163. * paragraph 5.3 in SAM-3).
  1164. */
  1165. WARN_ON(status & 1);
  1166. srp_rsp = ioctx->ioctx.buf;
  1167. BUG_ON(!srp_rsp);
  1168. sense_data = ioctx->sense_data;
  1169. sense_data_len = ioctx->cmd.scsi_sense_length;
  1170. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1171. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1172. srp_rsp->opcode = SRP_RSP;
  1173. srp_rsp->req_lim_delta =
  1174. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1175. srp_rsp->tag = tag;
  1176. srp_rsp->status = status;
  1177. if (sense_data_len) {
  1178. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1179. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1180. if (sense_data_len > max_sense_len) {
  1181. pr_warn("truncated sense data from %d to %d"
  1182. " bytes\n", sense_data_len, max_sense_len);
  1183. sense_data_len = max_sense_len;
  1184. }
  1185. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1186. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1187. memcpy(srp_rsp + 1, sense_data, sense_data_len);
  1188. }
  1189. return sizeof(*srp_rsp) + sense_data_len;
  1190. }
  1191. /**
  1192. * srpt_build_tskmgmt_rsp - build a task management response
  1193. * @ch: RDMA channel through which the request has been received.
  1194. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1195. * @rsp_code: RSP_CODE that will be stored in the response.
  1196. * @tag: Tag of the request for which this response is being generated.
  1197. *
  1198. * Returns the size in bytes of the SRP_RSP response.
  1199. *
  1200. * An SRP_RSP response contains a SCSI status or service response. See also
  1201. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1202. * response.
  1203. */
  1204. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1205. struct srpt_send_ioctx *ioctx,
  1206. u8 rsp_code, u64 tag)
  1207. {
  1208. struct srp_rsp *srp_rsp;
  1209. int resp_data_len;
  1210. int resp_len;
  1211. resp_data_len = 4;
  1212. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1213. srp_rsp = ioctx->ioctx.buf;
  1214. BUG_ON(!srp_rsp);
  1215. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1216. srp_rsp->opcode = SRP_RSP;
  1217. srp_rsp->req_lim_delta =
  1218. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1219. srp_rsp->tag = tag;
  1220. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1221. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1222. srp_rsp->data[3] = rsp_code;
  1223. return resp_len;
  1224. }
  1225. static int srpt_check_stop_free(struct se_cmd *cmd)
  1226. {
  1227. struct srpt_send_ioctx *ioctx = container_of(cmd,
  1228. struct srpt_send_ioctx, cmd);
  1229. return target_put_sess_cmd(&ioctx->cmd);
  1230. }
  1231. /**
  1232. * srpt_handle_cmd - process a SRP_CMD information unit
  1233. * @ch: SRPT RDMA channel.
  1234. * @recv_ioctx: Receive I/O context.
  1235. * @send_ioctx: Send I/O context.
  1236. */
  1237. static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1238. struct srpt_recv_ioctx *recv_ioctx,
  1239. struct srpt_send_ioctx *send_ioctx)
  1240. {
  1241. struct se_cmd *cmd;
  1242. struct srp_cmd *srp_cmd;
  1243. struct scatterlist *sg = NULL;
  1244. unsigned sg_cnt = 0;
  1245. u64 data_len;
  1246. enum dma_data_direction dir;
  1247. int rc;
  1248. BUG_ON(!send_ioctx);
  1249. srp_cmd = recv_ioctx->ioctx.buf;
  1250. cmd = &send_ioctx->cmd;
  1251. cmd->tag = srp_cmd->tag;
  1252. switch (srp_cmd->task_attr) {
  1253. case SRP_CMD_SIMPLE_Q:
  1254. cmd->sam_task_attr = TCM_SIMPLE_TAG;
  1255. break;
  1256. case SRP_CMD_ORDERED_Q:
  1257. default:
  1258. cmd->sam_task_attr = TCM_ORDERED_TAG;
  1259. break;
  1260. case SRP_CMD_HEAD_OF_Q:
  1261. cmd->sam_task_attr = TCM_HEAD_TAG;
  1262. break;
  1263. case SRP_CMD_ACA:
  1264. cmd->sam_task_attr = TCM_ACA_TAG;
  1265. break;
  1266. }
  1267. rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
  1268. &data_len);
  1269. if (rc) {
  1270. if (rc != -EAGAIN) {
  1271. pr_err("0x%llx: parsing SRP descriptor table failed.\n",
  1272. srp_cmd->tag);
  1273. }
  1274. goto release_ioctx;
  1275. }
  1276. rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
  1277. &send_ioctx->sense_data[0],
  1278. scsilun_to_int(&srp_cmd->lun), data_len,
  1279. TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
  1280. sg, sg_cnt, NULL, 0, NULL, 0);
  1281. if (rc != 0) {
  1282. pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
  1283. srp_cmd->tag);
  1284. goto release_ioctx;
  1285. }
  1286. return;
  1287. release_ioctx:
  1288. send_ioctx->state = SRPT_STATE_DONE;
  1289. srpt_release_cmd(cmd);
  1290. }
  1291. static int srp_tmr_to_tcm(int fn)
  1292. {
  1293. switch (fn) {
  1294. case SRP_TSK_ABORT_TASK:
  1295. return TMR_ABORT_TASK;
  1296. case SRP_TSK_ABORT_TASK_SET:
  1297. return TMR_ABORT_TASK_SET;
  1298. case SRP_TSK_CLEAR_TASK_SET:
  1299. return TMR_CLEAR_TASK_SET;
  1300. case SRP_TSK_LUN_RESET:
  1301. return TMR_LUN_RESET;
  1302. case SRP_TSK_CLEAR_ACA:
  1303. return TMR_CLEAR_ACA;
  1304. default:
  1305. return -1;
  1306. }
  1307. }
  1308. /**
  1309. * srpt_handle_tsk_mgmt - process a SRP_TSK_MGMT information unit
  1310. * @ch: SRPT RDMA channel.
  1311. * @recv_ioctx: Receive I/O context.
  1312. * @send_ioctx: Send I/O context.
  1313. *
  1314. * Returns 0 if and only if the request will be processed by the target core.
  1315. *
  1316. * For more information about SRP_TSK_MGMT information units, see also section
  1317. * 6.7 in the SRP r16a document.
  1318. */
  1319. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1320. struct srpt_recv_ioctx *recv_ioctx,
  1321. struct srpt_send_ioctx *send_ioctx)
  1322. {
  1323. struct srp_tsk_mgmt *srp_tsk;
  1324. struct se_cmd *cmd;
  1325. struct se_session *sess = ch->sess;
  1326. int tcm_tmr;
  1327. int rc;
  1328. BUG_ON(!send_ioctx);
  1329. srp_tsk = recv_ioctx->ioctx.buf;
  1330. cmd = &send_ioctx->cmd;
  1331. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld ch %p sess %p\n",
  1332. srp_tsk->tsk_mgmt_func, srp_tsk->task_tag, srp_tsk->tag, ch,
  1333. ch->sess);
  1334. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1335. send_ioctx->cmd.tag = srp_tsk->tag;
  1336. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1337. rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
  1338. scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
  1339. GFP_KERNEL, srp_tsk->task_tag,
  1340. TARGET_SCF_ACK_KREF);
  1341. if (rc != 0) {
  1342. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1343. goto fail;
  1344. }
  1345. return;
  1346. fail:
  1347. transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
  1348. }
  1349. /**
  1350. * srpt_handle_new_iu - process a newly received information unit
  1351. * @ch: RDMA channel through which the information unit has been received.
  1352. * @recv_ioctx: Receive I/O context associated with the information unit.
  1353. */
  1354. static bool
  1355. srpt_handle_new_iu(struct srpt_rdma_ch *ch, struct srpt_recv_ioctx *recv_ioctx)
  1356. {
  1357. struct srpt_send_ioctx *send_ioctx = NULL;
  1358. struct srp_cmd *srp_cmd;
  1359. bool res = false;
  1360. u8 opcode;
  1361. BUG_ON(!ch);
  1362. BUG_ON(!recv_ioctx);
  1363. if (unlikely(ch->state == CH_CONNECTING))
  1364. goto push;
  1365. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1366. recv_ioctx->ioctx.dma, srp_max_req_size,
  1367. DMA_FROM_DEVICE);
  1368. srp_cmd = recv_ioctx->ioctx.buf;
  1369. opcode = srp_cmd->opcode;
  1370. if (opcode == SRP_CMD || opcode == SRP_TSK_MGMT) {
  1371. send_ioctx = srpt_get_send_ioctx(ch);
  1372. if (unlikely(!send_ioctx))
  1373. goto push;
  1374. }
  1375. if (!list_empty(&recv_ioctx->wait_list)) {
  1376. WARN_ON_ONCE(!ch->processing_wait_list);
  1377. list_del_init(&recv_ioctx->wait_list);
  1378. }
  1379. switch (opcode) {
  1380. case SRP_CMD:
  1381. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1382. break;
  1383. case SRP_TSK_MGMT:
  1384. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1385. break;
  1386. case SRP_I_LOGOUT:
  1387. pr_err("Not yet implemented: SRP_I_LOGOUT\n");
  1388. break;
  1389. case SRP_CRED_RSP:
  1390. pr_debug("received SRP_CRED_RSP\n");
  1391. break;
  1392. case SRP_AER_RSP:
  1393. pr_debug("received SRP_AER_RSP\n");
  1394. break;
  1395. case SRP_RSP:
  1396. pr_err("Received SRP_RSP\n");
  1397. break;
  1398. default:
  1399. pr_err("received IU with unknown opcode 0x%x\n", opcode);
  1400. break;
  1401. }
  1402. srpt_post_recv(ch->sport->sdev, ch, recv_ioctx);
  1403. res = true;
  1404. out:
  1405. return res;
  1406. push:
  1407. if (list_empty(&recv_ioctx->wait_list)) {
  1408. WARN_ON_ONCE(ch->processing_wait_list);
  1409. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1410. }
  1411. goto out;
  1412. }
  1413. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  1414. {
  1415. struct srpt_rdma_ch *ch = cq->cq_context;
  1416. struct srpt_recv_ioctx *ioctx =
  1417. container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
  1418. if (wc->status == IB_WC_SUCCESS) {
  1419. int req_lim;
  1420. req_lim = atomic_dec_return(&ch->req_lim);
  1421. if (unlikely(req_lim < 0))
  1422. pr_err("req_lim = %d < 0\n", req_lim);
  1423. srpt_handle_new_iu(ch, ioctx);
  1424. } else {
  1425. pr_info_ratelimited("receiving failed for ioctx %p with status %d\n",
  1426. ioctx, wc->status);
  1427. }
  1428. }
  1429. /*
  1430. * This function must be called from the context in which RDMA completions are
  1431. * processed because it accesses the wait list without protection against
  1432. * access from other threads.
  1433. */
  1434. static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
  1435. {
  1436. struct srpt_recv_ioctx *recv_ioctx, *tmp;
  1437. WARN_ON_ONCE(ch->state == CH_CONNECTING);
  1438. if (list_empty(&ch->cmd_wait_list))
  1439. return;
  1440. WARN_ON_ONCE(ch->processing_wait_list);
  1441. ch->processing_wait_list = true;
  1442. list_for_each_entry_safe(recv_ioctx, tmp, &ch->cmd_wait_list,
  1443. wait_list) {
  1444. if (!srpt_handle_new_iu(ch, recv_ioctx))
  1445. break;
  1446. }
  1447. ch->processing_wait_list = false;
  1448. }
  1449. /**
  1450. * srpt_send_done - send completion callback
  1451. * @cq: Completion queue.
  1452. * @wc: Work completion.
  1453. *
  1454. * Note: Although this has not yet been observed during tests, at least in
  1455. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1456. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1457. * value in each response is set to one, and it is possible that this response
  1458. * makes the initiator send a new request before the send completion for that
  1459. * response has been processed. This could e.g. happen if the call to
  1460. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1461. * if IB retransmission causes generation of the send completion to be
  1462. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1463. * are queued on cmd_wait_list. The code below processes these delayed
  1464. * requests one at a time.
  1465. */
  1466. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
  1467. {
  1468. struct srpt_rdma_ch *ch = cq->cq_context;
  1469. struct srpt_send_ioctx *ioctx =
  1470. container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
  1471. enum srpt_command_state state;
  1472. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1473. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
  1474. state != SRPT_STATE_MGMT_RSP_SENT);
  1475. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  1476. if (wc->status != IB_WC_SUCCESS)
  1477. pr_info("sending response for ioctx 0x%p failed"
  1478. " with status %d\n", ioctx, wc->status);
  1479. if (state != SRPT_STATE_DONE) {
  1480. transport_generic_free_cmd(&ioctx->cmd, 0);
  1481. } else {
  1482. pr_err("IB completion has been received too late for"
  1483. " wr_id = %u.\n", ioctx->ioctx.index);
  1484. }
  1485. srpt_process_wait_list(ch);
  1486. }
  1487. /**
  1488. * srpt_create_ch_ib - create receive and send completion queues
  1489. * @ch: SRPT RDMA channel.
  1490. */
  1491. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1492. {
  1493. struct ib_qp_init_attr *qp_init;
  1494. struct srpt_port *sport = ch->sport;
  1495. struct srpt_device *sdev = sport->sdev;
  1496. const struct ib_device_attr *attrs = &sdev->device->attrs;
  1497. int sq_size = sport->port_attrib.srp_sq_size;
  1498. int i, ret;
  1499. WARN_ON(ch->rq_size < 1);
  1500. ret = -ENOMEM;
  1501. qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
  1502. if (!qp_init)
  1503. goto out;
  1504. retry:
  1505. ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + sq_size,
  1506. 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
  1507. if (IS_ERR(ch->cq)) {
  1508. ret = PTR_ERR(ch->cq);
  1509. pr_err("failed to create CQ cqe= %d ret= %d\n",
  1510. ch->rq_size + sq_size, ret);
  1511. goto out;
  1512. }
  1513. qp_init->qp_context = (void *)ch;
  1514. qp_init->event_handler
  1515. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1516. qp_init->send_cq = ch->cq;
  1517. qp_init->recv_cq = ch->cq;
  1518. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1519. qp_init->qp_type = IB_QPT_RC;
  1520. /*
  1521. * We divide up our send queue size into half SEND WRs to send the
  1522. * completions, and half R/W contexts to actually do the RDMA
  1523. * READ/WRITE transfers. Note that we need to allocate CQ slots for
  1524. * both both, as RDMA contexts will also post completions for the
  1525. * RDMA READ case.
  1526. */
  1527. qp_init->cap.max_send_wr = min(sq_size / 2, attrs->max_qp_wr);
  1528. qp_init->cap.max_rdma_ctxs = sq_size / 2;
  1529. qp_init->cap.max_send_sge = min(attrs->max_sge, SRPT_MAX_SG_PER_WQE);
  1530. qp_init->port_num = ch->sport->port;
  1531. if (sdev->use_srq) {
  1532. qp_init->srq = sdev->srq;
  1533. } else {
  1534. qp_init->cap.max_recv_wr = ch->rq_size;
  1535. qp_init->cap.max_recv_sge = qp_init->cap.max_send_sge;
  1536. }
  1537. if (ch->using_rdma_cm) {
  1538. ret = rdma_create_qp(ch->rdma_cm.cm_id, sdev->pd, qp_init);
  1539. ch->qp = ch->rdma_cm.cm_id->qp;
  1540. } else {
  1541. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1542. if (!IS_ERR(ch->qp)) {
  1543. ret = srpt_init_ch_qp(ch, ch->qp);
  1544. if (ret)
  1545. ib_destroy_qp(ch->qp);
  1546. } else {
  1547. ret = PTR_ERR(ch->qp);
  1548. }
  1549. }
  1550. if (ret) {
  1551. bool retry = sq_size > MIN_SRPT_SQ_SIZE;
  1552. if (retry) {
  1553. pr_debug("failed to create queue pair with sq_size = %d (%d) - retrying\n",
  1554. sq_size, ret);
  1555. ib_free_cq(ch->cq);
  1556. sq_size = max(sq_size / 2, MIN_SRPT_SQ_SIZE);
  1557. goto retry;
  1558. } else {
  1559. pr_err("failed to create queue pair with sq_size = %d (%d)\n",
  1560. sq_size, ret);
  1561. goto err_destroy_cq;
  1562. }
  1563. }
  1564. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1565. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d ch= %p\n",
  1566. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1567. qp_init->cap.max_send_wr, ch);
  1568. if (!sdev->use_srq)
  1569. for (i = 0; i < ch->rq_size; i++)
  1570. srpt_post_recv(sdev, ch, ch->ioctx_recv_ring[i]);
  1571. out:
  1572. kfree(qp_init);
  1573. return ret;
  1574. err_destroy_cq:
  1575. ch->qp = NULL;
  1576. ib_free_cq(ch->cq);
  1577. goto out;
  1578. }
  1579. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1580. {
  1581. ib_destroy_qp(ch->qp);
  1582. ib_free_cq(ch->cq);
  1583. }
  1584. /**
  1585. * srpt_close_ch - close a RDMA channel
  1586. * @ch: SRPT RDMA channel.
  1587. *
  1588. * Make sure all resources associated with the channel will be deallocated at
  1589. * an appropriate time.
  1590. *
  1591. * Returns true if and only if the channel state has been modified into
  1592. * CH_DRAINING.
  1593. */
  1594. static bool srpt_close_ch(struct srpt_rdma_ch *ch)
  1595. {
  1596. int ret;
  1597. if (!srpt_set_ch_state(ch, CH_DRAINING)) {
  1598. pr_debug("%s-%d: already closed\n", ch->sess_name,
  1599. ch->qp->qp_num);
  1600. return false;
  1601. }
  1602. kref_get(&ch->kref);
  1603. ret = srpt_ch_qp_err(ch);
  1604. if (ret < 0)
  1605. pr_err("%s-%d: changing queue pair into error state failed: %d\n",
  1606. ch->sess_name, ch->qp->qp_num, ret);
  1607. ret = srpt_zerolength_write(ch);
  1608. if (ret < 0) {
  1609. pr_err("%s-%d: queuing zero-length write failed: %d\n",
  1610. ch->sess_name, ch->qp->qp_num, ret);
  1611. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  1612. schedule_work(&ch->release_work);
  1613. else
  1614. WARN_ON_ONCE(true);
  1615. }
  1616. kref_put(&ch->kref, srpt_free_ch);
  1617. return true;
  1618. }
  1619. /*
  1620. * Change the channel state into CH_DISCONNECTING. If a channel has not yet
  1621. * reached the connected state, close it. If a channel is in the connected
  1622. * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
  1623. * the responsibility of the caller to ensure that this function is not
  1624. * invoked concurrently with the code that accepts a connection. This means
  1625. * that this function must either be invoked from inside a CM callback
  1626. * function or that it must be invoked with the srpt_port.mutex held.
  1627. */
  1628. static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
  1629. {
  1630. int ret;
  1631. if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
  1632. return -ENOTCONN;
  1633. if (ch->using_rdma_cm) {
  1634. ret = rdma_disconnect(ch->rdma_cm.cm_id);
  1635. } else {
  1636. ret = ib_send_cm_dreq(ch->ib_cm.cm_id, NULL, 0);
  1637. if (ret < 0)
  1638. ret = ib_send_cm_drep(ch->ib_cm.cm_id, NULL, 0);
  1639. }
  1640. if (ret < 0 && srpt_close_ch(ch))
  1641. ret = 0;
  1642. return ret;
  1643. }
  1644. static bool srpt_ch_closed(struct srpt_port *sport, struct srpt_rdma_ch *ch)
  1645. {
  1646. struct srpt_nexus *nexus;
  1647. struct srpt_rdma_ch *ch2;
  1648. bool res = true;
  1649. rcu_read_lock();
  1650. list_for_each_entry(nexus, &sport->nexus_list, entry) {
  1651. list_for_each_entry(ch2, &nexus->ch_list, list) {
  1652. if (ch2 == ch) {
  1653. res = false;
  1654. goto done;
  1655. }
  1656. }
  1657. }
  1658. done:
  1659. rcu_read_unlock();
  1660. return res;
  1661. }
  1662. /* Send DREQ and wait for DREP. */
  1663. static void srpt_disconnect_ch_sync(struct srpt_rdma_ch *ch)
  1664. {
  1665. struct srpt_port *sport = ch->sport;
  1666. pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
  1667. ch->state);
  1668. mutex_lock(&sport->mutex);
  1669. srpt_disconnect_ch(ch);
  1670. mutex_unlock(&sport->mutex);
  1671. while (wait_event_timeout(sport->ch_releaseQ, srpt_ch_closed(sport, ch),
  1672. 5 * HZ) == 0)
  1673. pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
  1674. ch->sess_name, ch->qp->qp_num, ch->state);
  1675. }
  1676. static void __srpt_close_all_ch(struct srpt_port *sport)
  1677. {
  1678. struct srpt_nexus *nexus;
  1679. struct srpt_rdma_ch *ch;
  1680. lockdep_assert_held(&sport->mutex);
  1681. list_for_each_entry(nexus, &sport->nexus_list, entry) {
  1682. list_for_each_entry(ch, &nexus->ch_list, list) {
  1683. if (srpt_disconnect_ch(ch) >= 0)
  1684. pr_info("Closing channel %s-%d because target %s_%d has been disabled\n",
  1685. ch->sess_name, ch->qp->qp_num,
  1686. sport->sdev->device->name, sport->port);
  1687. srpt_close_ch(ch);
  1688. }
  1689. }
  1690. }
  1691. /*
  1692. * Look up (i_port_id, t_port_id) in sport->nexus_list. Create an entry if
  1693. * it does not yet exist.
  1694. */
  1695. static struct srpt_nexus *srpt_get_nexus(struct srpt_port *sport,
  1696. const u8 i_port_id[16],
  1697. const u8 t_port_id[16])
  1698. {
  1699. struct srpt_nexus *nexus = NULL, *tmp_nexus = NULL, *n;
  1700. for (;;) {
  1701. mutex_lock(&sport->mutex);
  1702. list_for_each_entry(n, &sport->nexus_list, entry) {
  1703. if (memcmp(n->i_port_id, i_port_id, 16) == 0 &&
  1704. memcmp(n->t_port_id, t_port_id, 16) == 0) {
  1705. nexus = n;
  1706. break;
  1707. }
  1708. }
  1709. if (!nexus && tmp_nexus) {
  1710. list_add_tail_rcu(&tmp_nexus->entry,
  1711. &sport->nexus_list);
  1712. swap(nexus, tmp_nexus);
  1713. }
  1714. mutex_unlock(&sport->mutex);
  1715. if (nexus)
  1716. break;
  1717. tmp_nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
  1718. if (!tmp_nexus) {
  1719. nexus = ERR_PTR(-ENOMEM);
  1720. break;
  1721. }
  1722. INIT_LIST_HEAD(&tmp_nexus->ch_list);
  1723. memcpy(tmp_nexus->i_port_id, i_port_id, 16);
  1724. memcpy(tmp_nexus->t_port_id, t_port_id, 16);
  1725. }
  1726. kfree(tmp_nexus);
  1727. return nexus;
  1728. }
  1729. static void srpt_set_enabled(struct srpt_port *sport, bool enabled)
  1730. __must_hold(&sport->mutex)
  1731. {
  1732. lockdep_assert_held(&sport->mutex);
  1733. if (sport->enabled == enabled)
  1734. return;
  1735. sport->enabled = enabled;
  1736. if (!enabled)
  1737. __srpt_close_all_ch(sport);
  1738. }
  1739. static void srpt_free_ch(struct kref *kref)
  1740. {
  1741. struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
  1742. kfree_rcu(ch, rcu);
  1743. }
  1744. static void srpt_release_channel_work(struct work_struct *w)
  1745. {
  1746. struct srpt_rdma_ch *ch;
  1747. struct srpt_device *sdev;
  1748. struct srpt_port *sport;
  1749. struct se_session *se_sess;
  1750. ch = container_of(w, struct srpt_rdma_ch, release_work);
  1751. pr_debug("%s-%d\n", ch->sess_name, ch->qp->qp_num);
  1752. sdev = ch->sport->sdev;
  1753. BUG_ON(!sdev);
  1754. se_sess = ch->sess;
  1755. BUG_ON(!se_sess);
  1756. target_sess_cmd_list_set_waiting(se_sess);
  1757. target_wait_for_sess_cmds(se_sess);
  1758. target_remove_session(se_sess);
  1759. ch->sess = NULL;
  1760. if (ch->using_rdma_cm)
  1761. rdma_destroy_id(ch->rdma_cm.cm_id);
  1762. else
  1763. ib_destroy_cm_id(ch->ib_cm.cm_id);
  1764. srpt_destroy_ch_ib(ch);
  1765. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  1766. ch->sport->sdev, ch->rq_size,
  1767. ch->max_rsp_size, DMA_TO_DEVICE);
  1768. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
  1769. sdev, ch->rq_size,
  1770. srp_max_req_size, DMA_FROM_DEVICE);
  1771. sport = ch->sport;
  1772. mutex_lock(&sport->mutex);
  1773. list_del_rcu(&ch->list);
  1774. mutex_unlock(&sport->mutex);
  1775. wake_up(&sport->ch_releaseQ);
  1776. kref_put(&ch->kref, srpt_free_ch);
  1777. }
  1778. /**
  1779. * srpt_cm_req_recv - process the event IB_CM_REQ_RECEIVED
  1780. * @sdev: HCA through which the login request was received.
  1781. * @ib_cm_id: IB/CM connection identifier in case of IB/CM.
  1782. * @rdma_cm_id: RDMA/CM connection identifier in case of RDMA/CM.
  1783. * @port_num: Port through which the REQ message was received.
  1784. * @pkey: P_Key of the incoming connection.
  1785. * @req: SRP login request.
  1786. * @src_addr: GID (IB/CM) or IP address (RDMA/CM) of the port that submitted
  1787. * the login request.
  1788. *
  1789. * Ownership of the cm_id is transferred to the target session if this
  1790. * function returns zero. Otherwise the caller remains the owner of cm_id.
  1791. */
  1792. static int srpt_cm_req_recv(struct srpt_device *const sdev,
  1793. struct ib_cm_id *ib_cm_id,
  1794. struct rdma_cm_id *rdma_cm_id,
  1795. u8 port_num, __be16 pkey,
  1796. const struct srp_login_req *req,
  1797. const char *src_addr)
  1798. {
  1799. struct srpt_port *sport = &sdev->port[port_num - 1];
  1800. struct srpt_nexus *nexus;
  1801. struct srp_login_rsp *rsp = NULL;
  1802. struct srp_login_rej *rej = NULL;
  1803. union {
  1804. struct rdma_conn_param rdma_cm;
  1805. struct ib_cm_rep_param ib_cm;
  1806. } *rep_param = NULL;
  1807. struct srpt_rdma_ch *ch;
  1808. char i_port_id[36];
  1809. u32 it_iu_len;
  1810. int i, ret;
  1811. WARN_ON_ONCE(irqs_disabled());
  1812. if (WARN_ON(!sdev || !req))
  1813. return -EINVAL;
  1814. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  1815. pr_info("Received SRP_LOGIN_REQ with i_port_id %pI6, t_port_id %pI6 and it_iu_len %d on port %d (guid=%pI6); pkey %#04x\n",
  1816. req->initiator_port_id, req->target_port_id, it_iu_len,
  1817. port_num, &sport->gid, be16_to_cpu(pkey));
  1818. nexus = srpt_get_nexus(sport, req->initiator_port_id,
  1819. req->target_port_id);
  1820. if (IS_ERR(nexus)) {
  1821. ret = PTR_ERR(nexus);
  1822. goto out;
  1823. }
  1824. ret = -ENOMEM;
  1825. rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
  1826. rej = kzalloc(sizeof(*rej), GFP_KERNEL);
  1827. rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
  1828. if (!rsp || !rej || !rep_param)
  1829. goto out;
  1830. ret = -EINVAL;
  1831. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  1832. rej->reason = cpu_to_be32(
  1833. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  1834. pr_err("rejected SRP_LOGIN_REQ because its length (%d bytes) is out of range (%d .. %d)\n",
  1835. it_iu_len, 64, srp_max_req_size);
  1836. goto reject;
  1837. }
  1838. if (!sport->enabled) {
  1839. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1840. pr_info("rejected SRP_LOGIN_REQ because target port %s_%d has not yet been enabled\n",
  1841. sport->sdev->device->name, port_num);
  1842. goto reject;
  1843. }
  1844. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  1845. || *(__be64 *)(req->target_port_id + 8) !=
  1846. cpu_to_be64(srpt_service_guid)) {
  1847. rej->reason = cpu_to_be32(
  1848. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  1849. pr_err("rejected SRP_LOGIN_REQ because it has an invalid target port identifier.\n");
  1850. goto reject;
  1851. }
  1852. ret = -ENOMEM;
  1853. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1854. if (!ch) {
  1855. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1856. pr_err("rejected SRP_LOGIN_REQ because out of memory.\n");
  1857. goto reject;
  1858. }
  1859. kref_init(&ch->kref);
  1860. ch->pkey = be16_to_cpu(pkey);
  1861. ch->nexus = nexus;
  1862. ch->zw_cqe.done = srpt_zerolength_write_done;
  1863. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  1864. ch->sport = sport;
  1865. if (ib_cm_id) {
  1866. ch->ib_cm.cm_id = ib_cm_id;
  1867. ib_cm_id->context = ch;
  1868. } else {
  1869. ch->using_rdma_cm = true;
  1870. ch->rdma_cm.cm_id = rdma_cm_id;
  1871. rdma_cm_id->context = ch;
  1872. }
  1873. /*
  1874. * ch->rq_size should be at least as large as the initiator queue
  1875. * depth to avoid that the initiator driver has to report QUEUE_FULL
  1876. * to the SCSI mid-layer.
  1877. */
  1878. ch->rq_size = min(MAX_SRPT_RQ_SIZE, sdev->device->attrs.max_qp_wr);
  1879. spin_lock_init(&ch->spinlock);
  1880. ch->state = CH_CONNECTING;
  1881. INIT_LIST_HEAD(&ch->cmd_wait_list);
  1882. ch->max_rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  1883. ch->ioctx_ring = (struct srpt_send_ioctx **)
  1884. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  1885. sizeof(*ch->ioctx_ring[0]),
  1886. ch->max_rsp_size, DMA_TO_DEVICE);
  1887. if (!ch->ioctx_ring) {
  1888. pr_err("rejected SRP_LOGIN_REQ because creating a new QP SQ ring failed.\n");
  1889. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1890. goto free_ch;
  1891. }
  1892. INIT_LIST_HEAD(&ch->free_list);
  1893. for (i = 0; i < ch->rq_size; i++) {
  1894. ch->ioctx_ring[i]->ch = ch;
  1895. list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
  1896. }
  1897. if (!sdev->use_srq) {
  1898. ch->ioctx_recv_ring = (struct srpt_recv_ioctx **)
  1899. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  1900. sizeof(*ch->ioctx_recv_ring[0]),
  1901. srp_max_req_size,
  1902. DMA_FROM_DEVICE);
  1903. if (!ch->ioctx_recv_ring) {
  1904. pr_err("rejected SRP_LOGIN_REQ because creating a new QP RQ ring failed.\n");
  1905. rej->reason =
  1906. cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1907. goto free_ring;
  1908. }
  1909. for (i = 0; i < ch->rq_size; i++)
  1910. INIT_LIST_HEAD(&ch->ioctx_recv_ring[i]->wait_list);
  1911. }
  1912. ret = srpt_create_ch_ib(ch);
  1913. if (ret) {
  1914. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1915. pr_err("rejected SRP_LOGIN_REQ because creating a new RDMA channel failed.\n");
  1916. goto free_recv_ring;
  1917. }
  1918. strlcpy(ch->sess_name, src_addr, sizeof(ch->sess_name));
  1919. snprintf(i_port_id, sizeof(i_port_id), "0x%016llx%016llx",
  1920. be64_to_cpu(*(__be64 *)nexus->i_port_id),
  1921. be64_to_cpu(*(__be64 *)(nexus->i_port_id + 8)));
  1922. pr_debug("registering session %s\n", ch->sess_name);
  1923. if (sport->port_guid_tpg.se_tpg_wwn)
  1924. ch->sess = target_setup_session(&sport->port_guid_tpg, 0, 0,
  1925. TARGET_PROT_NORMAL,
  1926. ch->sess_name, ch, NULL);
  1927. if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
  1928. ch->sess = target_setup_session(&sport->port_gid_tpg, 0, 0,
  1929. TARGET_PROT_NORMAL, i_port_id, ch,
  1930. NULL);
  1931. /* Retry without leading "0x" */
  1932. if (sport->port_gid_tpg.se_tpg_wwn && IS_ERR_OR_NULL(ch->sess))
  1933. ch->sess = target_setup_session(&sport->port_gid_tpg, 0, 0,
  1934. TARGET_PROT_NORMAL,
  1935. i_port_id + 2, ch, NULL);
  1936. if (IS_ERR_OR_NULL(ch->sess)) {
  1937. ret = PTR_ERR(ch->sess);
  1938. pr_info("Rejected login for initiator %s: ret = %d.\n",
  1939. ch->sess_name, ret);
  1940. rej->reason = cpu_to_be32(ret == -ENOMEM ?
  1941. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
  1942. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  1943. goto reject;
  1944. }
  1945. mutex_lock(&sport->mutex);
  1946. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  1947. struct srpt_rdma_ch *ch2;
  1948. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
  1949. list_for_each_entry(ch2, &nexus->ch_list, list) {
  1950. if (srpt_disconnect_ch(ch2) < 0)
  1951. continue;
  1952. pr_info("Relogin - closed existing channel %s\n",
  1953. ch2->sess_name);
  1954. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  1955. }
  1956. } else {
  1957. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  1958. }
  1959. list_add_tail_rcu(&ch->list, &nexus->ch_list);
  1960. if (!sport->enabled) {
  1961. rej->reason = cpu_to_be32(
  1962. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1963. pr_info("rejected SRP_LOGIN_REQ because target %s_%d is not enabled\n",
  1964. sdev->device->name, port_num);
  1965. mutex_unlock(&sport->mutex);
  1966. goto reject;
  1967. }
  1968. mutex_unlock(&sport->mutex);
  1969. ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rtr(ch, ch->qp);
  1970. if (ret) {
  1971. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1972. pr_err("rejected SRP_LOGIN_REQ because enabling RTR failed (error code = %d)\n",
  1973. ret);
  1974. goto destroy_ib;
  1975. }
  1976. pr_debug("Establish connection sess=%p name=%s ch=%p\n", ch->sess,
  1977. ch->sess_name, ch);
  1978. /* create srp_login_response */
  1979. rsp->opcode = SRP_LOGIN_RSP;
  1980. rsp->tag = req->tag;
  1981. rsp->max_it_iu_len = req->req_it_iu_len;
  1982. rsp->max_ti_iu_len = req->req_it_iu_len;
  1983. ch->max_ti_iu_len = it_iu_len;
  1984. rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  1985. SRP_BUF_FORMAT_INDIRECT);
  1986. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  1987. atomic_set(&ch->req_lim, ch->rq_size);
  1988. atomic_set(&ch->req_lim_delta, 0);
  1989. /* create cm reply */
  1990. if (ch->using_rdma_cm) {
  1991. rep_param->rdma_cm.private_data = (void *)rsp;
  1992. rep_param->rdma_cm.private_data_len = sizeof(*rsp);
  1993. rep_param->rdma_cm.rnr_retry_count = 7;
  1994. rep_param->rdma_cm.flow_control = 1;
  1995. rep_param->rdma_cm.responder_resources = 4;
  1996. rep_param->rdma_cm.initiator_depth = 4;
  1997. } else {
  1998. rep_param->ib_cm.qp_num = ch->qp->qp_num;
  1999. rep_param->ib_cm.private_data = (void *)rsp;
  2000. rep_param->ib_cm.private_data_len = sizeof(*rsp);
  2001. rep_param->ib_cm.rnr_retry_count = 7;
  2002. rep_param->ib_cm.flow_control = 1;
  2003. rep_param->ib_cm.failover_accepted = 0;
  2004. rep_param->ib_cm.srq = 1;
  2005. rep_param->ib_cm.responder_resources = 4;
  2006. rep_param->ib_cm.initiator_depth = 4;
  2007. }
  2008. /*
  2009. * Hold the sport mutex while accepting a connection to avoid that
  2010. * srpt_disconnect_ch() is invoked concurrently with this code.
  2011. */
  2012. mutex_lock(&sport->mutex);
  2013. if (sport->enabled && ch->state == CH_CONNECTING) {
  2014. if (ch->using_rdma_cm)
  2015. ret = rdma_accept(rdma_cm_id, &rep_param->rdma_cm);
  2016. else
  2017. ret = ib_send_cm_rep(ib_cm_id, &rep_param->ib_cm);
  2018. } else {
  2019. ret = -EINVAL;
  2020. }
  2021. mutex_unlock(&sport->mutex);
  2022. switch (ret) {
  2023. case 0:
  2024. break;
  2025. case -EINVAL:
  2026. goto reject;
  2027. default:
  2028. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2029. pr_err("sending SRP_LOGIN_REQ response failed (error code = %d)\n",
  2030. ret);
  2031. goto reject;
  2032. }
  2033. goto out;
  2034. destroy_ib:
  2035. srpt_destroy_ch_ib(ch);
  2036. free_recv_ring:
  2037. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_recv_ring,
  2038. ch->sport->sdev, ch->rq_size,
  2039. srp_max_req_size, DMA_FROM_DEVICE);
  2040. free_ring:
  2041. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2042. ch->sport->sdev, ch->rq_size,
  2043. ch->max_rsp_size, DMA_TO_DEVICE);
  2044. free_ch:
  2045. if (ib_cm_id)
  2046. ib_cm_id->context = NULL;
  2047. kfree(ch);
  2048. ch = NULL;
  2049. WARN_ON_ONCE(ret == 0);
  2050. reject:
  2051. pr_info("Rejecting login with reason %#x\n", be32_to_cpu(rej->reason));
  2052. rej->opcode = SRP_LOGIN_REJ;
  2053. rej->tag = req->tag;
  2054. rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  2055. SRP_BUF_FORMAT_INDIRECT);
  2056. if (rdma_cm_id)
  2057. rdma_reject(rdma_cm_id, rej, sizeof(*rej));
  2058. else
  2059. ib_send_cm_rej(ib_cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  2060. rej, sizeof(*rej));
  2061. out:
  2062. kfree(rep_param);
  2063. kfree(rsp);
  2064. kfree(rej);
  2065. return ret;
  2066. }
  2067. static int srpt_ib_cm_req_recv(struct ib_cm_id *cm_id,
  2068. struct ib_cm_req_event_param *param,
  2069. void *private_data)
  2070. {
  2071. char sguid[40];
  2072. srpt_format_guid(sguid, sizeof(sguid),
  2073. &param->primary_path->dgid.global.interface_id);
  2074. return srpt_cm_req_recv(cm_id->context, cm_id, NULL, param->port,
  2075. param->primary_path->pkey,
  2076. private_data, sguid);
  2077. }
  2078. static int srpt_rdma_cm_req_recv(struct rdma_cm_id *cm_id,
  2079. struct rdma_cm_event *event)
  2080. {
  2081. struct srpt_device *sdev;
  2082. struct srp_login_req req;
  2083. const struct srp_login_req_rdma *req_rdma;
  2084. char src_addr[40];
  2085. sdev = ib_get_client_data(cm_id->device, &srpt_client);
  2086. if (!sdev)
  2087. return -ECONNREFUSED;
  2088. if (event->param.conn.private_data_len < sizeof(*req_rdma))
  2089. return -EINVAL;
  2090. /* Transform srp_login_req_rdma into srp_login_req. */
  2091. req_rdma = event->param.conn.private_data;
  2092. memset(&req, 0, sizeof(req));
  2093. req.opcode = req_rdma->opcode;
  2094. req.tag = req_rdma->tag;
  2095. req.req_it_iu_len = req_rdma->req_it_iu_len;
  2096. req.req_buf_fmt = req_rdma->req_buf_fmt;
  2097. req.req_flags = req_rdma->req_flags;
  2098. memcpy(req.initiator_port_id, req_rdma->initiator_port_id, 16);
  2099. memcpy(req.target_port_id, req_rdma->target_port_id, 16);
  2100. snprintf(src_addr, sizeof(src_addr), "%pIS",
  2101. &cm_id->route.addr.src_addr);
  2102. return srpt_cm_req_recv(sdev, NULL, cm_id, cm_id->port_num,
  2103. cm_id->route.path_rec->pkey, &req, src_addr);
  2104. }
  2105. static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
  2106. enum ib_cm_rej_reason reason,
  2107. const u8 *private_data,
  2108. u8 private_data_len)
  2109. {
  2110. char *priv = NULL;
  2111. int i;
  2112. if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
  2113. GFP_KERNEL))) {
  2114. for (i = 0; i < private_data_len; i++)
  2115. sprintf(priv + 3 * i, " %02x", private_data[i]);
  2116. }
  2117. pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
  2118. ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
  2119. "; private data" : "", priv ? priv : " (?)");
  2120. kfree(priv);
  2121. }
  2122. /**
  2123. * srpt_cm_rtu_recv - process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event
  2124. * @ch: SRPT RDMA channel.
  2125. *
  2126. * An RTU (ready to use) message indicates that the connection has been
  2127. * established and that the recipient may begin transmitting.
  2128. */
  2129. static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
  2130. {
  2131. int ret;
  2132. ret = ch->using_rdma_cm ? 0 : srpt_ch_qp_rts(ch, ch->qp);
  2133. if (ret < 0) {
  2134. pr_err("%s-%d: QP transition to RTS failed\n", ch->sess_name,
  2135. ch->qp->qp_num);
  2136. srpt_close_ch(ch);
  2137. return;
  2138. }
  2139. /*
  2140. * Note: calling srpt_close_ch() if the transition to the LIVE state
  2141. * fails is not necessary since that means that that function has
  2142. * already been invoked from another thread.
  2143. */
  2144. if (!srpt_set_ch_state(ch, CH_LIVE)) {
  2145. pr_err("%s-%d: channel transition to LIVE state failed\n",
  2146. ch->sess_name, ch->qp->qp_num);
  2147. return;
  2148. }
  2149. /* Trigger wait list processing. */
  2150. ret = srpt_zerolength_write(ch);
  2151. WARN_ONCE(ret < 0, "%d\n", ret);
  2152. }
  2153. /**
  2154. * srpt_cm_handler - IB connection manager callback function
  2155. * @cm_id: IB/CM connection identifier.
  2156. * @event: IB/CM event.
  2157. *
  2158. * A non-zero return value will cause the caller destroy the CM ID.
  2159. *
  2160. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  2161. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  2162. * a non-zero value in any other case will trigger a race with the
  2163. * ib_destroy_cm_id() call in srpt_release_channel().
  2164. */
  2165. static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  2166. {
  2167. struct srpt_rdma_ch *ch = cm_id->context;
  2168. int ret;
  2169. ret = 0;
  2170. switch (event->event) {
  2171. case IB_CM_REQ_RECEIVED:
  2172. ret = srpt_ib_cm_req_recv(cm_id, &event->param.req_rcvd,
  2173. event->private_data);
  2174. break;
  2175. case IB_CM_REJ_RECEIVED:
  2176. srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
  2177. event->private_data,
  2178. IB_CM_REJ_PRIVATE_DATA_SIZE);
  2179. break;
  2180. case IB_CM_RTU_RECEIVED:
  2181. case IB_CM_USER_ESTABLISHED:
  2182. srpt_cm_rtu_recv(ch);
  2183. break;
  2184. case IB_CM_DREQ_RECEIVED:
  2185. srpt_disconnect_ch(ch);
  2186. break;
  2187. case IB_CM_DREP_RECEIVED:
  2188. pr_info("Received CM DREP message for ch %s-%d.\n",
  2189. ch->sess_name, ch->qp->qp_num);
  2190. srpt_close_ch(ch);
  2191. break;
  2192. case IB_CM_TIMEWAIT_EXIT:
  2193. pr_info("Received CM TimeWait exit for ch %s-%d.\n",
  2194. ch->sess_name, ch->qp->qp_num);
  2195. srpt_close_ch(ch);
  2196. break;
  2197. case IB_CM_REP_ERROR:
  2198. pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
  2199. ch->qp->qp_num);
  2200. break;
  2201. case IB_CM_DREQ_ERROR:
  2202. pr_info("Received CM DREQ ERROR event.\n");
  2203. break;
  2204. case IB_CM_MRA_RECEIVED:
  2205. pr_info("Received CM MRA event\n");
  2206. break;
  2207. default:
  2208. pr_err("received unrecognized CM event %d\n", event->event);
  2209. break;
  2210. }
  2211. return ret;
  2212. }
  2213. static int srpt_rdma_cm_handler(struct rdma_cm_id *cm_id,
  2214. struct rdma_cm_event *event)
  2215. {
  2216. struct srpt_rdma_ch *ch = cm_id->context;
  2217. int ret = 0;
  2218. switch (event->event) {
  2219. case RDMA_CM_EVENT_CONNECT_REQUEST:
  2220. ret = srpt_rdma_cm_req_recv(cm_id, event);
  2221. break;
  2222. case RDMA_CM_EVENT_REJECTED:
  2223. srpt_cm_rej_recv(ch, event->status,
  2224. event->param.conn.private_data,
  2225. event->param.conn.private_data_len);
  2226. break;
  2227. case RDMA_CM_EVENT_ESTABLISHED:
  2228. srpt_cm_rtu_recv(ch);
  2229. break;
  2230. case RDMA_CM_EVENT_DISCONNECTED:
  2231. if (ch->state < CH_DISCONNECTING)
  2232. srpt_disconnect_ch(ch);
  2233. else
  2234. srpt_close_ch(ch);
  2235. break;
  2236. case RDMA_CM_EVENT_TIMEWAIT_EXIT:
  2237. srpt_close_ch(ch);
  2238. break;
  2239. case RDMA_CM_EVENT_UNREACHABLE:
  2240. pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
  2241. ch->qp->qp_num);
  2242. break;
  2243. case RDMA_CM_EVENT_DEVICE_REMOVAL:
  2244. case RDMA_CM_EVENT_ADDR_CHANGE:
  2245. break;
  2246. default:
  2247. pr_err("received unrecognized RDMA CM event %d\n",
  2248. event->event);
  2249. break;
  2250. }
  2251. return ret;
  2252. }
  2253. static int srpt_write_pending_status(struct se_cmd *se_cmd)
  2254. {
  2255. struct srpt_send_ioctx *ioctx;
  2256. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2257. return ioctx->state == SRPT_STATE_NEED_DATA;
  2258. }
  2259. /*
  2260. * srpt_write_pending - Start data transfer from initiator to target (write).
  2261. */
  2262. static int srpt_write_pending(struct se_cmd *se_cmd)
  2263. {
  2264. struct srpt_send_ioctx *ioctx =
  2265. container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2266. struct srpt_rdma_ch *ch = ioctx->ch;
  2267. struct ib_send_wr *first_wr = NULL, *bad_wr;
  2268. struct ib_cqe *cqe = &ioctx->rdma_cqe;
  2269. enum srpt_command_state new_state;
  2270. int ret, i;
  2271. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  2272. WARN_ON(new_state == SRPT_STATE_DONE);
  2273. if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
  2274. pr_warn("%s: IB send queue full (needed %d)\n",
  2275. __func__, ioctx->n_rdma);
  2276. ret = -ENOMEM;
  2277. goto out_undo;
  2278. }
  2279. cqe->done = srpt_rdma_read_done;
  2280. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  2281. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  2282. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
  2283. cqe, first_wr);
  2284. cqe = NULL;
  2285. }
  2286. ret = ib_post_send(ch->qp, first_wr, &bad_wr);
  2287. if (ret) {
  2288. pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
  2289. __func__, ret, ioctx->n_rdma,
  2290. atomic_read(&ch->sq_wr_avail));
  2291. goto out_undo;
  2292. }
  2293. return 0;
  2294. out_undo:
  2295. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  2296. return ret;
  2297. }
  2298. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  2299. {
  2300. switch (tcm_mgmt_status) {
  2301. case TMR_FUNCTION_COMPLETE:
  2302. return SRP_TSK_MGMT_SUCCESS;
  2303. case TMR_FUNCTION_REJECTED:
  2304. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  2305. }
  2306. return SRP_TSK_MGMT_FAILED;
  2307. }
  2308. /**
  2309. * srpt_queue_response - transmit the response to a SCSI command
  2310. * @cmd: SCSI target command.
  2311. *
  2312. * Callback function called by the TCM core. Must not block since it can be
  2313. * invoked on the context of the IB completion handler.
  2314. */
  2315. static void srpt_queue_response(struct se_cmd *cmd)
  2316. {
  2317. struct srpt_send_ioctx *ioctx =
  2318. container_of(cmd, struct srpt_send_ioctx, cmd);
  2319. struct srpt_rdma_ch *ch = ioctx->ch;
  2320. struct srpt_device *sdev = ch->sport->sdev;
  2321. struct ib_send_wr send_wr, *first_wr = &send_wr, *bad_wr;
  2322. struct ib_sge sge;
  2323. enum srpt_command_state state;
  2324. int resp_len, ret, i;
  2325. u8 srp_tm_status;
  2326. BUG_ON(!ch);
  2327. state = ioctx->state;
  2328. switch (state) {
  2329. case SRPT_STATE_NEW:
  2330. case SRPT_STATE_DATA_IN:
  2331. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  2332. break;
  2333. case SRPT_STATE_MGMT:
  2334. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  2335. break;
  2336. default:
  2337. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  2338. ch, ioctx->ioctx.index, ioctx->state);
  2339. break;
  2340. }
  2341. if (unlikely(WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)))
  2342. return;
  2343. /* For read commands, transfer the data to the initiator. */
  2344. if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
  2345. ioctx->cmd.data_length &&
  2346. !ioctx->queue_status_only) {
  2347. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  2348. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  2349. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
  2350. ch->sport->port, NULL, first_wr);
  2351. }
  2352. }
  2353. if (state != SRPT_STATE_MGMT)
  2354. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
  2355. cmd->scsi_status);
  2356. else {
  2357. srp_tm_status
  2358. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2359. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2360. ioctx->cmd.tag);
  2361. }
  2362. atomic_inc(&ch->req_lim);
  2363. if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
  2364. &ch->sq_wr_avail) < 0)) {
  2365. pr_warn("%s: IB send queue full (needed %d)\n",
  2366. __func__, ioctx->n_rdma);
  2367. ret = -ENOMEM;
  2368. goto out;
  2369. }
  2370. ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
  2371. DMA_TO_DEVICE);
  2372. sge.addr = ioctx->ioctx.dma;
  2373. sge.length = resp_len;
  2374. sge.lkey = sdev->lkey;
  2375. ioctx->ioctx.cqe.done = srpt_send_done;
  2376. send_wr.next = NULL;
  2377. send_wr.wr_cqe = &ioctx->ioctx.cqe;
  2378. send_wr.sg_list = &sge;
  2379. send_wr.num_sge = 1;
  2380. send_wr.opcode = IB_WR_SEND;
  2381. send_wr.send_flags = IB_SEND_SIGNALED;
  2382. ret = ib_post_send(ch->qp, first_wr, &bad_wr);
  2383. if (ret < 0) {
  2384. pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
  2385. __func__, ioctx->cmd.tag, ret);
  2386. goto out;
  2387. }
  2388. return;
  2389. out:
  2390. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  2391. atomic_dec(&ch->req_lim);
  2392. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2393. target_put_sess_cmd(&ioctx->cmd);
  2394. }
  2395. static int srpt_queue_data_in(struct se_cmd *cmd)
  2396. {
  2397. srpt_queue_response(cmd);
  2398. return 0;
  2399. }
  2400. static void srpt_queue_tm_rsp(struct se_cmd *cmd)
  2401. {
  2402. srpt_queue_response(cmd);
  2403. }
  2404. static void srpt_aborted_task(struct se_cmd *cmd)
  2405. {
  2406. }
  2407. static int srpt_queue_status(struct se_cmd *cmd)
  2408. {
  2409. struct srpt_send_ioctx *ioctx;
  2410. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2411. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2412. if (cmd->se_cmd_flags &
  2413. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2414. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2415. ioctx->queue_status_only = true;
  2416. srpt_queue_response(cmd);
  2417. return 0;
  2418. }
  2419. static void srpt_refresh_port_work(struct work_struct *work)
  2420. {
  2421. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2422. srpt_refresh_port(sport);
  2423. }
  2424. static bool srpt_ch_list_empty(struct srpt_port *sport)
  2425. {
  2426. struct srpt_nexus *nexus;
  2427. bool res = true;
  2428. rcu_read_lock();
  2429. list_for_each_entry(nexus, &sport->nexus_list, entry)
  2430. if (!list_empty(&nexus->ch_list))
  2431. res = false;
  2432. rcu_read_unlock();
  2433. return res;
  2434. }
  2435. /**
  2436. * srpt_release_sport - disable login and wait for associated channels
  2437. * @sport: SRPT HCA port.
  2438. */
  2439. static int srpt_release_sport(struct srpt_port *sport)
  2440. {
  2441. struct srpt_nexus *nexus, *next_n;
  2442. struct srpt_rdma_ch *ch;
  2443. WARN_ON_ONCE(irqs_disabled());
  2444. mutex_lock(&sport->mutex);
  2445. srpt_set_enabled(sport, false);
  2446. mutex_unlock(&sport->mutex);
  2447. while (wait_event_timeout(sport->ch_releaseQ,
  2448. srpt_ch_list_empty(sport), 5 * HZ) <= 0) {
  2449. pr_info("%s_%d: waiting for session unregistration ...\n",
  2450. sport->sdev->device->name, sport->port);
  2451. rcu_read_lock();
  2452. list_for_each_entry(nexus, &sport->nexus_list, entry) {
  2453. list_for_each_entry(ch, &nexus->ch_list, list) {
  2454. pr_info("%s-%d: state %s\n",
  2455. ch->sess_name, ch->qp->qp_num,
  2456. get_ch_state_name(ch->state));
  2457. }
  2458. }
  2459. rcu_read_unlock();
  2460. }
  2461. mutex_lock(&sport->mutex);
  2462. list_for_each_entry_safe(nexus, next_n, &sport->nexus_list, entry) {
  2463. list_del(&nexus->entry);
  2464. kfree_rcu(nexus, rcu);
  2465. }
  2466. mutex_unlock(&sport->mutex);
  2467. return 0;
  2468. }
  2469. static struct se_wwn *__srpt_lookup_wwn(const char *name)
  2470. {
  2471. struct ib_device *dev;
  2472. struct srpt_device *sdev;
  2473. struct srpt_port *sport;
  2474. int i;
  2475. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2476. dev = sdev->device;
  2477. if (!dev)
  2478. continue;
  2479. for (i = 0; i < dev->phys_port_cnt; i++) {
  2480. sport = &sdev->port[i];
  2481. if (strcmp(sport->port_guid, name) == 0)
  2482. return &sport->port_guid_wwn;
  2483. if (strcmp(sport->port_gid, name) == 0)
  2484. return &sport->port_gid_wwn;
  2485. }
  2486. }
  2487. return NULL;
  2488. }
  2489. static struct se_wwn *srpt_lookup_wwn(const char *name)
  2490. {
  2491. struct se_wwn *wwn;
  2492. spin_lock(&srpt_dev_lock);
  2493. wwn = __srpt_lookup_wwn(name);
  2494. spin_unlock(&srpt_dev_lock);
  2495. return wwn;
  2496. }
  2497. static void srpt_free_srq(struct srpt_device *sdev)
  2498. {
  2499. if (!sdev->srq)
  2500. return;
  2501. ib_destroy_srq(sdev->srq);
  2502. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2503. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2504. sdev->srq = NULL;
  2505. }
  2506. static int srpt_alloc_srq(struct srpt_device *sdev)
  2507. {
  2508. struct ib_srq_init_attr srq_attr = {
  2509. .event_handler = srpt_srq_event,
  2510. .srq_context = (void *)sdev,
  2511. .attr.max_wr = sdev->srq_size,
  2512. .attr.max_sge = 1,
  2513. .srq_type = IB_SRQT_BASIC,
  2514. };
  2515. struct ib_device *device = sdev->device;
  2516. struct ib_srq *srq;
  2517. int i;
  2518. WARN_ON_ONCE(sdev->srq);
  2519. srq = ib_create_srq(sdev->pd, &srq_attr);
  2520. if (IS_ERR(srq)) {
  2521. pr_debug("ib_create_srq() failed: %ld\n", PTR_ERR(srq));
  2522. return PTR_ERR(srq);
  2523. }
  2524. pr_debug("create SRQ #wr= %d max_allow=%d dev= %s\n", sdev->srq_size,
  2525. sdev->device->attrs.max_srq_wr, device->name);
  2526. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2527. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2528. sizeof(*sdev->ioctx_ring[0]),
  2529. srp_max_req_size, DMA_FROM_DEVICE);
  2530. if (!sdev->ioctx_ring) {
  2531. ib_destroy_srq(srq);
  2532. return -ENOMEM;
  2533. }
  2534. sdev->use_srq = true;
  2535. sdev->srq = srq;
  2536. for (i = 0; i < sdev->srq_size; ++i) {
  2537. INIT_LIST_HEAD(&sdev->ioctx_ring[i]->wait_list);
  2538. srpt_post_recv(sdev, NULL, sdev->ioctx_ring[i]);
  2539. }
  2540. return 0;
  2541. }
  2542. static int srpt_use_srq(struct srpt_device *sdev, bool use_srq)
  2543. {
  2544. struct ib_device *device = sdev->device;
  2545. int ret = 0;
  2546. if (!use_srq) {
  2547. srpt_free_srq(sdev);
  2548. sdev->use_srq = false;
  2549. } else if (use_srq && !sdev->srq) {
  2550. ret = srpt_alloc_srq(sdev);
  2551. }
  2552. pr_debug("%s(%s): use_srq = %d; ret = %d\n", __func__, device->name,
  2553. sdev->use_srq, ret);
  2554. return ret;
  2555. }
  2556. /**
  2557. * srpt_add_one - InfiniBand device addition callback function
  2558. * @device: Describes a HCA.
  2559. */
  2560. static void srpt_add_one(struct ib_device *device)
  2561. {
  2562. struct srpt_device *sdev;
  2563. struct srpt_port *sport;
  2564. int i, ret;
  2565. pr_debug("device = %p\n", device);
  2566. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  2567. if (!sdev)
  2568. goto err;
  2569. sdev->device = device;
  2570. mutex_init(&sdev->sdev_mutex);
  2571. sdev->pd = ib_alloc_pd(device, 0);
  2572. if (IS_ERR(sdev->pd))
  2573. goto free_dev;
  2574. sdev->lkey = sdev->pd->local_dma_lkey;
  2575. sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
  2576. srpt_use_srq(sdev, sdev->port[0].port_attrib.use_srq);
  2577. if (!srpt_service_guid)
  2578. srpt_service_guid = be64_to_cpu(device->node_guid);
  2579. if (rdma_port_get_link_layer(device, 1) == IB_LINK_LAYER_INFINIBAND)
  2580. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2581. if (IS_ERR(sdev->cm_id)) {
  2582. pr_info("ib_create_cm_id() failed: %ld\n",
  2583. PTR_ERR(sdev->cm_id));
  2584. sdev->cm_id = NULL;
  2585. if (!rdma_cm_id)
  2586. goto err_ring;
  2587. }
  2588. /* print out target login information */
  2589. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2590. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2591. srpt_service_guid, srpt_service_guid);
  2592. /*
  2593. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2594. * to identify this target. We currently use the guid of the first HCA
  2595. * in the system as service_id; therefore, the target_id will change
  2596. * if this HCA is gone bad and replaced by different HCA
  2597. */
  2598. ret = sdev->cm_id ?
  2599. ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0) :
  2600. 0;
  2601. if (ret < 0) {
  2602. pr_err("ib_cm_listen() failed: %d (cm_id state = %d)\n", ret,
  2603. sdev->cm_id->state);
  2604. goto err_cm;
  2605. }
  2606. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2607. srpt_event_handler);
  2608. ib_register_event_handler(&sdev->event_handler);
  2609. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2610. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2611. sport = &sdev->port[i - 1];
  2612. INIT_LIST_HEAD(&sport->nexus_list);
  2613. init_waitqueue_head(&sport->ch_releaseQ);
  2614. mutex_init(&sport->mutex);
  2615. sport->sdev = sdev;
  2616. sport->port = i;
  2617. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2618. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2619. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2620. sport->port_attrib.use_srq = false;
  2621. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2622. if (srpt_refresh_port(sport)) {
  2623. pr_err("MAD registration failed for %s-%d.\n",
  2624. sdev->device->name, i);
  2625. goto err_event;
  2626. }
  2627. }
  2628. spin_lock(&srpt_dev_lock);
  2629. list_add_tail(&sdev->list, &srpt_dev_list);
  2630. spin_unlock(&srpt_dev_lock);
  2631. out:
  2632. ib_set_client_data(device, &srpt_client, sdev);
  2633. pr_debug("added %s.\n", device->name);
  2634. return;
  2635. err_event:
  2636. ib_unregister_event_handler(&sdev->event_handler);
  2637. err_cm:
  2638. if (sdev->cm_id)
  2639. ib_destroy_cm_id(sdev->cm_id);
  2640. err_ring:
  2641. srpt_free_srq(sdev);
  2642. ib_dealloc_pd(sdev->pd);
  2643. free_dev:
  2644. kfree(sdev);
  2645. err:
  2646. sdev = NULL;
  2647. pr_info("%s(%s) failed.\n", __func__, device->name);
  2648. goto out;
  2649. }
  2650. /**
  2651. * srpt_remove_one - InfiniBand device removal callback function
  2652. * @device: Describes a HCA.
  2653. * @client_data: The value passed as the third argument to ib_set_client_data().
  2654. */
  2655. static void srpt_remove_one(struct ib_device *device, void *client_data)
  2656. {
  2657. struct srpt_device *sdev = client_data;
  2658. int i;
  2659. if (!sdev) {
  2660. pr_info("%s(%s): nothing to do.\n", __func__, device->name);
  2661. return;
  2662. }
  2663. srpt_unregister_mad_agent(sdev);
  2664. ib_unregister_event_handler(&sdev->event_handler);
  2665. /* Cancel any work queued by the just unregistered IB event handler. */
  2666. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2667. cancel_work_sync(&sdev->port[i].work);
  2668. if (sdev->cm_id)
  2669. ib_destroy_cm_id(sdev->cm_id);
  2670. ib_set_client_data(device, &srpt_client, NULL);
  2671. /*
  2672. * Unregistering a target must happen after destroying sdev->cm_id
  2673. * such that no new SRP_LOGIN_REQ information units can arrive while
  2674. * destroying the target.
  2675. */
  2676. spin_lock(&srpt_dev_lock);
  2677. list_del(&sdev->list);
  2678. spin_unlock(&srpt_dev_lock);
  2679. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2680. srpt_release_sport(&sdev->port[i]);
  2681. srpt_free_srq(sdev);
  2682. ib_dealloc_pd(sdev->pd);
  2683. kfree(sdev);
  2684. }
  2685. static struct ib_client srpt_client = {
  2686. .name = DRV_NAME,
  2687. .add = srpt_add_one,
  2688. .remove = srpt_remove_one
  2689. };
  2690. static int srpt_check_true(struct se_portal_group *se_tpg)
  2691. {
  2692. return 1;
  2693. }
  2694. static int srpt_check_false(struct se_portal_group *se_tpg)
  2695. {
  2696. return 0;
  2697. }
  2698. static char *srpt_get_fabric_name(void)
  2699. {
  2700. return "srpt";
  2701. }
  2702. static struct srpt_port *srpt_tpg_to_sport(struct se_portal_group *tpg)
  2703. {
  2704. return tpg->se_tpg_wwn->priv;
  2705. }
  2706. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2707. {
  2708. struct srpt_port *sport = srpt_tpg_to_sport(tpg);
  2709. WARN_ON_ONCE(tpg != &sport->port_guid_tpg &&
  2710. tpg != &sport->port_gid_tpg);
  2711. return tpg == &sport->port_guid_tpg ? sport->port_guid :
  2712. sport->port_gid;
  2713. }
  2714. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2715. {
  2716. return 1;
  2717. }
  2718. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2719. {
  2720. return 1;
  2721. }
  2722. static void srpt_release_cmd(struct se_cmd *se_cmd)
  2723. {
  2724. struct srpt_send_ioctx *ioctx = container_of(se_cmd,
  2725. struct srpt_send_ioctx, cmd);
  2726. struct srpt_rdma_ch *ch = ioctx->ch;
  2727. unsigned long flags;
  2728. WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
  2729. !(ioctx->cmd.transport_state & CMD_T_ABORTED));
  2730. if (ioctx->n_rw_ctx) {
  2731. srpt_free_rw_ctxs(ch, ioctx);
  2732. ioctx->n_rw_ctx = 0;
  2733. }
  2734. spin_lock_irqsave(&ch->spinlock, flags);
  2735. list_add(&ioctx->free_list, &ch->free_list);
  2736. spin_unlock_irqrestore(&ch->spinlock, flags);
  2737. }
  2738. /**
  2739. * srpt_close_session - forcibly close a session
  2740. * @se_sess: SCSI target session.
  2741. *
  2742. * Callback function invoked by the TCM core to clean up sessions associated
  2743. * with a node ACL when the user invokes
  2744. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2745. */
  2746. static void srpt_close_session(struct se_session *se_sess)
  2747. {
  2748. struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
  2749. srpt_disconnect_ch_sync(ch);
  2750. }
  2751. /**
  2752. * srpt_sess_get_index - return the value of scsiAttIntrPortIndex (SCSI-MIB)
  2753. * @se_sess: SCSI target session.
  2754. *
  2755. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  2756. * This object represents an arbitrary integer used to uniquely identify a
  2757. * particular attached remote initiator port to a particular SCSI target port
  2758. * within a particular SCSI target device within a particular SCSI instance.
  2759. */
  2760. static u32 srpt_sess_get_index(struct se_session *se_sess)
  2761. {
  2762. return 0;
  2763. }
  2764. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  2765. {
  2766. }
  2767. /* Note: only used from inside debug printk's by the TCM core. */
  2768. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  2769. {
  2770. struct srpt_send_ioctx *ioctx;
  2771. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2772. return ioctx->state;
  2773. }
  2774. static int srpt_parse_guid(u64 *guid, const char *name)
  2775. {
  2776. u16 w[4];
  2777. int ret = -EINVAL;
  2778. if (sscanf(name, "%hx:%hx:%hx:%hx", &w[0], &w[1], &w[2], &w[3]) != 4)
  2779. goto out;
  2780. *guid = get_unaligned_be64(w);
  2781. ret = 0;
  2782. out:
  2783. return ret;
  2784. }
  2785. /**
  2786. * srpt_parse_i_port_id - parse an initiator port ID
  2787. * @name: ASCII representation of a 128-bit initiator port ID.
  2788. * @i_port_id: Binary 128-bit port ID.
  2789. */
  2790. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  2791. {
  2792. const char *p;
  2793. unsigned len, count, leading_zero_bytes;
  2794. int ret;
  2795. p = name;
  2796. if (strncasecmp(p, "0x", 2) == 0)
  2797. p += 2;
  2798. ret = -EINVAL;
  2799. len = strlen(p);
  2800. if (len % 2)
  2801. goto out;
  2802. count = min(len / 2, 16U);
  2803. leading_zero_bytes = 16 - count;
  2804. memset(i_port_id, 0, leading_zero_bytes);
  2805. ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
  2806. out:
  2807. return ret;
  2808. }
  2809. /*
  2810. * configfs callback function invoked for mkdir
  2811. * /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2812. *
  2813. * i_port_id must be an initiator port GUID, GID or IP address. See also the
  2814. * target_alloc_session() calls in this driver. Examples of valid initiator
  2815. * port IDs:
  2816. * 0x0000000000000000505400fffe4a0b7b
  2817. * 0000000000000000505400fffe4a0b7b
  2818. * 5054:00ff:fe4a:0b7b
  2819. * 192.168.122.76
  2820. */
  2821. static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
  2822. {
  2823. struct sockaddr_storage sa;
  2824. u64 guid;
  2825. u8 i_port_id[16];
  2826. int ret;
  2827. ret = srpt_parse_guid(&guid, name);
  2828. if (ret < 0)
  2829. ret = srpt_parse_i_port_id(i_port_id, name);
  2830. if (ret < 0)
  2831. ret = inet_pton_with_scope(&init_net, AF_UNSPEC, name, NULL,
  2832. &sa);
  2833. if (ret < 0)
  2834. pr_err("invalid initiator port ID %s\n", name);
  2835. return ret;
  2836. }
  2837. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
  2838. char *page)
  2839. {
  2840. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2841. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2842. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  2843. }
  2844. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
  2845. const char *page, size_t count)
  2846. {
  2847. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2848. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2849. unsigned long val;
  2850. int ret;
  2851. ret = kstrtoul(page, 0, &val);
  2852. if (ret < 0) {
  2853. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2854. return -EINVAL;
  2855. }
  2856. if (val > MAX_SRPT_RDMA_SIZE) {
  2857. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  2858. MAX_SRPT_RDMA_SIZE);
  2859. return -EINVAL;
  2860. }
  2861. if (val < DEFAULT_MAX_RDMA_SIZE) {
  2862. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  2863. val, DEFAULT_MAX_RDMA_SIZE);
  2864. return -EINVAL;
  2865. }
  2866. sport->port_attrib.srp_max_rdma_size = val;
  2867. return count;
  2868. }
  2869. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
  2870. char *page)
  2871. {
  2872. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2873. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2874. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  2875. }
  2876. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
  2877. const char *page, size_t count)
  2878. {
  2879. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2880. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2881. unsigned long val;
  2882. int ret;
  2883. ret = kstrtoul(page, 0, &val);
  2884. if (ret < 0) {
  2885. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2886. return -EINVAL;
  2887. }
  2888. if (val > MAX_SRPT_RSP_SIZE) {
  2889. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  2890. MAX_SRPT_RSP_SIZE);
  2891. return -EINVAL;
  2892. }
  2893. if (val < MIN_MAX_RSP_SIZE) {
  2894. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  2895. MIN_MAX_RSP_SIZE);
  2896. return -EINVAL;
  2897. }
  2898. sport->port_attrib.srp_max_rsp_size = val;
  2899. return count;
  2900. }
  2901. static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
  2902. char *page)
  2903. {
  2904. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2905. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2906. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  2907. }
  2908. static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
  2909. const char *page, size_t count)
  2910. {
  2911. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2912. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2913. unsigned long val;
  2914. int ret;
  2915. ret = kstrtoul(page, 0, &val);
  2916. if (ret < 0) {
  2917. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2918. return -EINVAL;
  2919. }
  2920. if (val > MAX_SRPT_SRQ_SIZE) {
  2921. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  2922. MAX_SRPT_SRQ_SIZE);
  2923. return -EINVAL;
  2924. }
  2925. if (val < MIN_SRPT_SRQ_SIZE) {
  2926. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  2927. MIN_SRPT_SRQ_SIZE);
  2928. return -EINVAL;
  2929. }
  2930. sport->port_attrib.srp_sq_size = val;
  2931. return count;
  2932. }
  2933. static ssize_t srpt_tpg_attrib_use_srq_show(struct config_item *item,
  2934. char *page)
  2935. {
  2936. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2937. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2938. return sprintf(page, "%d\n", sport->port_attrib.use_srq);
  2939. }
  2940. static ssize_t srpt_tpg_attrib_use_srq_store(struct config_item *item,
  2941. const char *page, size_t count)
  2942. {
  2943. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2944. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  2945. struct srpt_device *sdev = sport->sdev;
  2946. unsigned long val;
  2947. bool enabled;
  2948. int ret;
  2949. ret = kstrtoul(page, 0, &val);
  2950. if (ret < 0)
  2951. return ret;
  2952. if (val != !!val)
  2953. return -EINVAL;
  2954. ret = mutex_lock_interruptible(&sdev->sdev_mutex);
  2955. if (ret < 0)
  2956. return ret;
  2957. ret = mutex_lock_interruptible(&sport->mutex);
  2958. if (ret < 0)
  2959. goto unlock_sdev;
  2960. enabled = sport->enabled;
  2961. /* Log out all initiator systems before changing 'use_srq'. */
  2962. srpt_set_enabled(sport, false);
  2963. sport->port_attrib.use_srq = val;
  2964. srpt_use_srq(sdev, sport->port_attrib.use_srq);
  2965. srpt_set_enabled(sport, enabled);
  2966. ret = count;
  2967. mutex_unlock(&sport->mutex);
  2968. unlock_sdev:
  2969. mutex_unlock(&sdev->sdev_mutex);
  2970. return ret;
  2971. }
  2972. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
  2973. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
  2974. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
  2975. CONFIGFS_ATTR(srpt_tpg_attrib_, use_srq);
  2976. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  2977. &srpt_tpg_attrib_attr_srp_max_rdma_size,
  2978. &srpt_tpg_attrib_attr_srp_max_rsp_size,
  2979. &srpt_tpg_attrib_attr_srp_sq_size,
  2980. &srpt_tpg_attrib_attr_use_srq,
  2981. NULL,
  2982. };
  2983. static struct rdma_cm_id *srpt_create_rdma_id(struct sockaddr *listen_addr)
  2984. {
  2985. struct rdma_cm_id *rdma_cm_id;
  2986. int ret;
  2987. rdma_cm_id = rdma_create_id(&init_net, srpt_rdma_cm_handler,
  2988. NULL, RDMA_PS_TCP, IB_QPT_RC);
  2989. if (IS_ERR(rdma_cm_id)) {
  2990. pr_err("RDMA/CM ID creation failed: %ld\n",
  2991. PTR_ERR(rdma_cm_id));
  2992. goto out;
  2993. }
  2994. ret = rdma_bind_addr(rdma_cm_id, listen_addr);
  2995. if (ret) {
  2996. char addr_str[64];
  2997. snprintf(addr_str, sizeof(addr_str), "%pISp", listen_addr);
  2998. pr_err("Binding RDMA/CM ID to address %s failed: %d\n",
  2999. addr_str, ret);
  3000. rdma_destroy_id(rdma_cm_id);
  3001. rdma_cm_id = ERR_PTR(ret);
  3002. goto out;
  3003. }
  3004. ret = rdma_listen(rdma_cm_id, 128);
  3005. if (ret) {
  3006. pr_err("rdma_listen() failed: %d\n", ret);
  3007. rdma_destroy_id(rdma_cm_id);
  3008. rdma_cm_id = ERR_PTR(ret);
  3009. }
  3010. out:
  3011. return rdma_cm_id;
  3012. }
  3013. static ssize_t srpt_rdma_cm_port_show(struct config_item *item, char *page)
  3014. {
  3015. return sprintf(page, "%d\n", rdma_cm_port);
  3016. }
  3017. static ssize_t srpt_rdma_cm_port_store(struct config_item *item,
  3018. const char *page, size_t count)
  3019. {
  3020. struct sockaddr_in addr4 = { .sin_family = AF_INET };
  3021. struct sockaddr_in6 addr6 = { .sin6_family = AF_INET6 };
  3022. struct rdma_cm_id *new_id = NULL;
  3023. u16 val;
  3024. int ret;
  3025. ret = kstrtou16(page, 0, &val);
  3026. if (ret < 0)
  3027. return ret;
  3028. ret = count;
  3029. if (rdma_cm_port == val)
  3030. goto out;
  3031. if (val) {
  3032. addr6.sin6_port = cpu_to_be16(val);
  3033. new_id = srpt_create_rdma_id((struct sockaddr *)&addr6);
  3034. if (IS_ERR(new_id)) {
  3035. addr4.sin_port = cpu_to_be16(val);
  3036. new_id = srpt_create_rdma_id((struct sockaddr *)&addr4);
  3037. if (IS_ERR(new_id)) {
  3038. ret = PTR_ERR(new_id);
  3039. goto out;
  3040. }
  3041. }
  3042. }
  3043. mutex_lock(&rdma_cm_mutex);
  3044. rdma_cm_port = val;
  3045. swap(rdma_cm_id, new_id);
  3046. mutex_unlock(&rdma_cm_mutex);
  3047. if (new_id)
  3048. rdma_destroy_id(new_id);
  3049. ret = count;
  3050. out:
  3051. return ret;
  3052. }
  3053. CONFIGFS_ATTR(srpt_, rdma_cm_port);
  3054. static struct configfs_attribute *srpt_da_attrs[] = {
  3055. &srpt_attr_rdma_cm_port,
  3056. NULL,
  3057. };
  3058. static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
  3059. {
  3060. struct se_portal_group *se_tpg = to_tpg(item);
  3061. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3062. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  3063. }
  3064. static ssize_t srpt_tpg_enable_store(struct config_item *item,
  3065. const char *page, size_t count)
  3066. {
  3067. struct se_portal_group *se_tpg = to_tpg(item);
  3068. struct srpt_port *sport = srpt_tpg_to_sport(se_tpg);
  3069. unsigned long tmp;
  3070. int ret;
  3071. ret = kstrtoul(page, 0, &tmp);
  3072. if (ret < 0) {
  3073. pr_err("Unable to extract srpt_tpg_store_enable\n");
  3074. return -EINVAL;
  3075. }
  3076. if ((tmp != 0) && (tmp != 1)) {
  3077. pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  3078. return -EINVAL;
  3079. }
  3080. mutex_lock(&sport->mutex);
  3081. srpt_set_enabled(sport, tmp);
  3082. mutex_unlock(&sport->mutex);
  3083. return count;
  3084. }
  3085. CONFIGFS_ATTR(srpt_tpg_, enable);
  3086. static struct configfs_attribute *srpt_tpg_attrs[] = {
  3087. &srpt_tpg_attr_enable,
  3088. NULL,
  3089. };
  3090. /**
  3091. * srpt_make_tpg - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port/$tpg
  3092. * @wwn: Corresponds to $driver/$port.
  3093. * @name: $tpg.
  3094. */
  3095. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  3096. const char *name)
  3097. {
  3098. struct srpt_port *sport = wwn->priv;
  3099. static struct se_portal_group *tpg;
  3100. int res;
  3101. WARN_ON_ONCE(wwn != &sport->port_guid_wwn &&
  3102. wwn != &sport->port_gid_wwn);
  3103. tpg = wwn == &sport->port_guid_wwn ? &sport->port_guid_tpg :
  3104. &sport->port_gid_tpg;
  3105. res = core_tpg_register(wwn, tpg, SCSI_PROTOCOL_SRP);
  3106. if (res)
  3107. return ERR_PTR(res);
  3108. return tpg;
  3109. }
  3110. /**
  3111. * srpt_drop_tpg - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port/$tpg
  3112. * @tpg: Target portal group to deregister.
  3113. */
  3114. static void srpt_drop_tpg(struct se_portal_group *tpg)
  3115. {
  3116. struct srpt_port *sport = srpt_tpg_to_sport(tpg);
  3117. sport->enabled = false;
  3118. core_tpg_deregister(tpg);
  3119. }
  3120. /**
  3121. * srpt_make_tport - configfs callback invoked for mkdir /sys/kernel/config/target/$driver/$port
  3122. * @tf: Not used.
  3123. * @group: Not used.
  3124. * @name: $port.
  3125. */
  3126. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  3127. struct config_group *group,
  3128. const char *name)
  3129. {
  3130. return srpt_lookup_wwn(name) ? : ERR_PTR(-EINVAL);
  3131. }
  3132. /**
  3133. * srpt_drop_tport - configfs callback invoked for rmdir /sys/kernel/config/target/$driver/$port
  3134. * @wwn: $port.
  3135. */
  3136. static void srpt_drop_tport(struct se_wwn *wwn)
  3137. {
  3138. }
  3139. static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
  3140. {
  3141. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  3142. }
  3143. CONFIGFS_ATTR_RO(srpt_wwn_, version);
  3144. static struct configfs_attribute *srpt_wwn_attrs[] = {
  3145. &srpt_wwn_attr_version,
  3146. NULL,
  3147. };
  3148. static const struct target_core_fabric_ops srpt_template = {
  3149. .module = THIS_MODULE,
  3150. .name = "srpt",
  3151. .get_fabric_name = srpt_get_fabric_name,
  3152. .tpg_get_wwn = srpt_get_fabric_wwn,
  3153. .tpg_get_tag = srpt_get_tag,
  3154. .tpg_check_demo_mode = srpt_check_false,
  3155. .tpg_check_demo_mode_cache = srpt_check_true,
  3156. .tpg_check_demo_mode_write_protect = srpt_check_true,
  3157. .tpg_check_prod_mode_write_protect = srpt_check_false,
  3158. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  3159. .release_cmd = srpt_release_cmd,
  3160. .check_stop_free = srpt_check_stop_free,
  3161. .close_session = srpt_close_session,
  3162. .sess_get_index = srpt_sess_get_index,
  3163. .sess_get_initiator_sid = NULL,
  3164. .write_pending = srpt_write_pending,
  3165. .write_pending_status = srpt_write_pending_status,
  3166. .set_default_node_attributes = srpt_set_default_node_attrs,
  3167. .get_cmd_state = srpt_get_tcm_cmd_state,
  3168. .queue_data_in = srpt_queue_data_in,
  3169. .queue_status = srpt_queue_status,
  3170. .queue_tm_rsp = srpt_queue_tm_rsp,
  3171. .aborted_task = srpt_aborted_task,
  3172. /*
  3173. * Setup function pointers for generic logic in
  3174. * target_core_fabric_configfs.c
  3175. */
  3176. .fabric_make_wwn = srpt_make_tport,
  3177. .fabric_drop_wwn = srpt_drop_tport,
  3178. .fabric_make_tpg = srpt_make_tpg,
  3179. .fabric_drop_tpg = srpt_drop_tpg,
  3180. .fabric_init_nodeacl = srpt_init_nodeacl,
  3181. .tfc_discovery_attrs = srpt_da_attrs,
  3182. .tfc_wwn_attrs = srpt_wwn_attrs,
  3183. .tfc_tpg_base_attrs = srpt_tpg_attrs,
  3184. .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
  3185. };
  3186. /**
  3187. * srpt_init_module - kernel module initialization
  3188. *
  3189. * Note: Since ib_register_client() registers callback functions, and since at
  3190. * least one of these callback functions (srpt_add_one()) calls target core
  3191. * functions, this driver must be registered with the target core before
  3192. * ib_register_client() is called.
  3193. */
  3194. static int __init srpt_init_module(void)
  3195. {
  3196. int ret;
  3197. ret = -EINVAL;
  3198. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  3199. pr_err("invalid value %d for kernel module parameter"
  3200. " srp_max_req_size -- must be at least %d.\n",
  3201. srp_max_req_size, MIN_MAX_REQ_SIZE);
  3202. goto out;
  3203. }
  3204. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  3205. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  3206. pr_err("invalid value %d for kernel module parameter"
  3207. " srpt_srq_size -- must be in the range [%d..%d].\n",
  3208. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  3209. goto out;
  3210. }
  3211. ret = target_register_template(&srpt_template);
  3212. if (ret)
  3213. goto out;
  3214. ret = ib_register_client(&srpt_client);
  3215. if (ret) {
  3216. pr_err("couldn't register IB client\n");
  3217. goto out_unregister_target;
  3218. }
  3219. return 0;
  3220. out_unregister_target:
  3221. target_unregister_template(&srpt_template);
  3222. out:
  3223. return ret;
  3224. }
  3225. static void __exit srpt_cleanup_module(void)
  3226. {
  3227. if (rdma_cm_id)
  3228. rdma_destroy_id(rdma_cm_id);
  3229. ib_unregister_client(&srpt_client);
  3230. target_unregister_template(&srpt_template);
  3231. }
  3232. module_init(srpt_init_module);
  3233. module_exit(srpt_cleanup_module);