ib_srpt.c 101 KB

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