ib_srpt.c 99 KB

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