ib_srpt.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132
  1. /*
  2. * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
  3. * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/err.h>
  38. #include <linux/ctype.h>
  39. #include <linux/kthread.h>
  40. #include <linux/string.h>
  41. #include <linux/delay.h>
  42. #include <linux/atomic.h>
  43. #include <scsi/scsi_proto.h>
  44. #include <scsi/scsi_tcq.h>
  45. #include <target/target_core_base.h>
  46. #include <target/target_core_fabric.h>
  47. #include "ib_srpt.h"
  48. /* Name of this kernel module. */
  49. #define DRV_NAME "ib_srpt"
  50. #define DRV_VERSION "2.0.0"
  51. #define DRV_RELDATE "2011-02-14"
  52. #define SRPT_ID_STRING "Linux SRP target"
  53. #undef pr_fmt
  54. #define pr_fmt(fmt) DRV_NAME " " fmt
  55. MODULE_AUTHOR("Vu Pham and Bart Van Assche");
  56. MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
  57. "v" DRV_VERSION " (" DRV_RELDATE ")");
  58. MODULE_LICENSE("Dual BSD/GPL");
  59. /*
  60. * Global Variables
  61. */
  62. static u64 srpt_service_guid;
  63. static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
  64. static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
  65. static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
  66. module_param(srp_max_req_size, int, 0444);
  67. MODULE_PARM_DESC(srp_max_req_size,
  68. "Maximum size of SRP request messages in bytes.");
  69. static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
  70. module_param(srpt_srq_size, int, 0444);
  71. MODULE_PARM_DESC(srpt_srq_size,
  72. "Shared receive queue (SRQ) size.");
  73. static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
  74. {
  75. return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
  76. }
  77. module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
  78. 0444);
  79. MODULE_PARM_DESC(srpt_service_guid,
  80. "Using this value for ioc_guid, id_ext, and cm_listen_id"
  81. " instead of using the node_guid of the first HCA.");
  82. static struct ib_client srpt_client;
  83. static void srpt_release_cmd(struct se_cmd *se_cmd);
  84. static void srpt_free_ch(struct kref *kref);
  85. static int srpt_queue_status(struct se_cmd *cmd);
  86. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc);
  87. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc);
  88. static void srpt_process_wait_list(struct srpt_rdma_ch *ch);
  89. /*
  90. * The only allowed channel state changes are those that change the channel
  91. * state into a state with a higher numerical value. Hence the new > prev test.
  92. */
  93. static bool srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new)
  94. {
  95. unsigned long flags;
  96. enum rdma_ch_state prev;
  97. bool changed = false;
  98. spin_lock_irqsave(&ch->spinlock, flags);
  99. prev = ch->state;
  100. if (new > prev) {
  101. ch->state = new;
  102. changed = true;
  103. }
  104. spin_unlock_irqrestore(&ch->spinlock, flags);
  105. return changed;
  106. }
  107. /**
  108. * srpt_event_handler() - Asynchronous IB event callback function.
  109. *
  110. * Callback function called by the InfiniBand core when an asynchronous IB
  111. * event occurs. This callback may occur in interrupt context. See also
  112. * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
  113. * Architecture Specification.
  114. */
  115. static void srpt_event_handler(struct ib_event_handler *handler,
  116. struct ib_event *event)
  117. {
  118. struct srpt_device *sdev;
  119. struct srpt_port *sport;
  120. sdev = ib_get_client_data(event->device, &srpt_client);
  121. if (!sdev || sdev->device != event->device)
  122. return;
  123. pr_debug("ASYNC event= %d on device= %s\n", event->event,
  124. sdev->device->name);
  125. switch (event->event) {
  126. case IB_EVENT_PORT_ERR:
  127. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  128. sport = &sdev->port[event->element.port_num - 1];
  129. sport->lid = 0;
  130. sport->sm_lid = 0;
  131. }
  132. break;
  133. case IB_EVENT_PORT_ACTIVE:
  134. case IB_EVENT_LID_CHANGE:
  135. case IB_EVENT_PKEY_CHANGE:
  136. case IB_EVENT_SM_CHANGE:
  137. case IB_EVENT_CLIENT_REREGISTER:
  138. case IB_EVENT_GID_CHANGE:
  139. /* Refresh port data asynchronously. */
  140. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  141. sport = &sdev->port[event->element.port_num - 1];
  142. if (!sport->lid && !sport->sm_lid)
  143. schedule_work(&sport->work);
  144. }
  145. break;
  146. default:
  147. pr_err("received unrecognized IB event %d\n",
  148. event->event);
  149. break;
  150. }
  151. }
  152. /**
  153. * srpt_srq_event() - SRQ event callback function.
  154. */
  155. static void srpt_srq_event(struct ib_event *event, void *ctx)
  156. {
  157. pr_info("SRQ event %d\n", event->event);
  158. }
  159. static const char *get_ch_state_name(enum rdma_ch_state s)
  160. {
  161. switch (s) {
  162. case CH_CONNECTING:
  163. return "connecting";
  164. case CH_LIVE:
  165. return "live";
  166. case CH_DISCONNECTING:
  167. return "disconnecting";
  168. case CH_DRAINING:
  169. return "draining";
  170. case CH_DISCONNECTED:
  171. return "disconnected";
  172. }
  173. return "???";
  174. }
  175. /**
  176. * srpt_qp_event() - QP event callback function.
  177. */
  178. static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
  179. {
  180. pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
  181. event->event, ch->cm_id, ch->sess_name, ch->state);
  182. switch (event->event) {
  183. case IB_EVENT_COMM_EST:
  184. ib_cm_notify(ch->cm_id, event->event);
  185. break;
  186. case IB_EVENT_QP_LAST_WQE_REACHED:
  187. pr_debug("%s-%d, state %s: received Last WQE event.\n",
  188. ch->sess_name, ch->qp->qp_num,
  189. get_ch_state_name(ch->state));
  190. break;
  191. default:
  192. pr_err("received unrecognized IB QP event %d\n", event->event);
  193. break;
  194. }
  195. }
  196. /**
  197. * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
  198. *
  199. * @slot: one-based slot number.
  200. * @value: four-bit value.
  201. *
  202. * Copies the lowest four bits of value in element slot of the array of four
  203. * bit elements called c_list (controller list). The index slot is one-based.
  204. */
  205. static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
  206. {
  207. u16 id;
  208. u8 tmp;
  209. id = (slot - 1) / 2;
  210. if (slot & 0x1) {
  211. tmp = c_list[id] & 0xf;
  212. c_list[id] = (value << 4) | tmp;
  213. } else {
  214. tmp = c_list[id] & 0xf0;
  215. c_list[id] = (value & 0xf) | tmp;
  216. }
  217. }
  218. /**
  219. * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
  220. *
  221. * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
  222. * Specification.
  223. */
  224. static void srpt_get_class_port_info(struct ib_dm_mad *mad)
  225. {
  226. struct ib_class_port_info *cif;
  227. cif = (struct ib_class_port_info *)mad->data;
  228. memset(cif, 0, sizeof(*cif));
  229. cif->base_version = 1;
  230. cif->class_version = 1;
  231. ib_set_cpi_resp_time(cif, 20);
  232. mad->mad_hdr.status = 0;
  233. }
  234. /**
  235. * srpt_get_iou() - Write IOUnitInfo to a management datagram.
  236. *
  237. * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
  238. * Specification. See also section B.7, table B.6 in the SRP r16a document.
  239. */
  240. static void srpt_get_iou(struct ib_dm_mad *mad)
  241. {
  242. struct ib_dm_iou_info *ioui;
  243. u8 slot;
  244. int i;
  245. ioui = (struct ib_dm_iou_info *)mad->data;
  246. ioui->change_id = cpu_to_be16(1);
  247. ioui->max_controllers = 16;
  248. /* set present for slot 1 and empty for the rest */
  249. srpt_set_ioc(ioui->controller_list, 1, 1);
  250. for (i = 1, slot = 2; i < 16; i++, slot++)
  251. srpt_set_ioc(ioui->controller_list, slot, 0);
  252. mad->mad_hdr.status = 0;
  253. }
  254. /**
  255. * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
  256. *
  257. * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
  258. * Architecture Specification. See also section B.7, table B.7 in the SRP
  259. * r16a document.
  260. */
  261. static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
  262. struct ib_dm_mad *mad)
  263. {
  264. struct srpt_device *sdev = sport->sdev;
  265. struct ib_dm_ioc_profile *iocp;
  266. iocp = (struct ib_dm_ioc_profile *)mad->data;
  267. if (!slot || slot > 16) {
  268. mad->mad_hdr.status
  269. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  270. return;
  271. }
  272. if (slot > 2) {
  273. mad->mad_hdr.status
  274. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  275. return;
  276. }
  277. memset(iocp, 0, sizeof(*iocp));
  278. strcpy(iocp->id_string, SRPT_ID_STRING);
  279. iocp->guid = cpu_to_be64(srpt_service_guid);
  280. iocp->vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  281. iocp->device_id = cpu_to_be32(sdev->device->attrs.vendor_part_id);
  282. iocp->device_version = cpu_to_be16(sdev->device->attrs.hw_ver);
  283. iocp->subsys_vendor_id = cpu_to_be32(sdev->device->attrs.vendor_id);
  284. iocp->subsys_device_id = 0x0;
  285. iocp->io_class = cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
  286. iocp->io_subclass = cpu_to_be16(SRP_IO_SUBCLASS);
  287. iocp->protocol = cpu_to_be16(SRP_PROTOCOL);
  288. iocp->protocol_version = cpu_to_be16(SRP_PROTOCOL_VERSION);
  289. iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
  290. iocp->rdma_read_depth = 4;
  291. iocp->send_size = cpu_to_be32(srp_max_req_size);
  292. iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
  293. 1U << 24));
  294. iocp->num_svc_entries = 1;
  295. iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
  296. SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
  297. mad->mad_hdr.status = 0;
  298. }
  299. /**
  300. * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
  301. *
  302. * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
  303. * Specification. See also section B.7, table B.8 in the SRP r16a document.
  304. */
  305. static void srpt_get_svc_entries(u64 ioc_guid,
  306. u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
  307. {
  308. struct ib_dm_svc_entries *svc_entries;
  309. WARN_ON(!ioc_guid);
  310. if (!slot || slot > 16) {
  311. mad->mad_hdr.status
  312. = cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  313. return;
  314. }
  315. if (slot > 2 || lo > hi || hi > 1) {
  316. mad->mad_hdr.status
  317. = cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  318. return;
  319. }
  320. svc_entries = (struct ib_dm_svc_entries *)mad->data;
  321. memset(svc_entries, 0, sizeof(*svc_entries));
  322. svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
  323. snprintf(svc_entries->service_entries[0].name,
  324. sizeof(svc_entries->service_entries[0].name),
  325. "%s%016llx",
  326. SRP_SERVICE_NAME_PREFIX,
  327. ioc_guid);
  328. mad->mad_hdr.status = 0;
  329. }
  330. /**
  331. * srpt_mgmt_method_get() - Process a received management datagram.
  332. * @sp: source port through which the MAD has been received.
  333. * @rq_mad: received MAD.
  334. * @rsp_mad: response MAD.
  335. */
  336. static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
  337. struct ib_dm_mad *rsp_mad)
  338. {
  339. u16 attr_id;
  340. u32 slot;
  341. u8 hi, lo;
  342. attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
  343. switch (attr_id) {
  344. case DM_ATTR_CLASS_PORT_INFO:
  345. srpt_get_class_port_info(rsp_mad);
  346. break;
  347. case DM_ATTR_IOU_INFO:
  348. srpt_get_iou(rsp_mad);
  349. break;
  350. case DM_ATTR_IOC_PROFILE:
  351. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  352. srpt_get_ioc(sp, slot, rsp_mad);
  353. break;
  354. case DM_ATTR_SVC_ENTRIES:
  355. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  356. hi = (u8) ((slot >> 8) & 0xff);
  357. lo = (u8) (slot & 0xff);
  358. slot = (u16) ((slot >> 16) & 0xffff);
  359. srpt_get_svc_entries(srpt_service_guid,
  360. slot, hi, lo, rsp_mad);
  361. break;
  362. default:
  363. rsp_mad->mad_hdr.status =
  364. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  365. break;
  366. }
  367. }
  368. /**
  369. * srpt_mad_send_handler() - Post MAD-send callback function.
  370. */
  371. static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
  372. struct ib_mad_send_wc *mad_wc)
  373. {
  374. ib_destroy_ah(mad_wc->send_buf->ah);
  375. ib_free_send_mad(mad_wc->send_buf);
  376. }
  377. /**
  378. * srpt_mad_recv_handler() - MAD reception callback function.
  379. */
  380. static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
  381. struct ib_mad_send_buf *send_buf,
  382. struct ib_mad_recv_wc *mad_wc)
  383. {
  384. struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
  385. struct ib_ah *ah;
  386. struct ib_mad_send_buf *rsp;
  387. struct ib_dm_mad *dm_mad;
  388. if (!mad_wc || !mad_wc->recv_buf.mad)
  389. return;
  390. ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
  391. mad_wc->recv_buf.grh, mad_agent->port_num);
  392. if (IS_ERR(ah))
  393. goto err;
  394. BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
  395. rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
  396. mad_wc->wc->pkey_index, 0,
  397. IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
  398. GFP_KERNEL,
  399. IB_MGMT_BASE_VERSION);
  400. if (IS_ERR(rsp))
  401. goto err_rsp;
  402. rsp->ah = ah;
  403. dm_mad = rsp->mad;
  404. memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof(*dm_mad));
  405. dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  406. dm_mad->mad_hdr.status = 0;
  407. switch (mad_wc->recv_buf.mad->mad_hdr.method) {
  408. case IB_MGMT_METHOD_GET:
  409. srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
  410. break;
  411. case IB_MGMT_METHOD_SET:
  412. dm_mad->mad_hdr.status =
  413. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  414. break;
  415. default:
  416. dm_mad->mad_hdr.status =
  417. cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
  418. break;
  419. }
  420. if (!ib_post_send_mad(rsp, NULL)) {
  421. ib_free_recv_mad(mad_wc);
  422. /* will destroy_ah & free_send_mad in send completion */
  423. return;
  424. }
  425. ib_free_send_mad(rsp);
  426. err_rsp:
  427. ib_destroy_ah(ah);
  428. err:
  429. ib_free_recv_mad(mad_wc);
  430. }
  431. /**
  432. * srpt_refresh_port() - Configure a HCA port.
  433. *
  434. * Enable InfiniBand management datagram processing, update the cached sm_lid,
  435. * lid and gid values, and register a callback function for processing MADs
  436. * on the specified port.
  437. *
  438. * Note: It is safe to call this function more than once for the same port.
  439. */
  440. static int srpt_refresh_port(struct srpt_port *sport)
  441. {
  442. struct ib_mad_reg_req reg_req;
  443. struct ib_port_modify port_modify;
  444. struct ib_port_attr port_attr;
  445. int ret;
  446. memset(&port_modify, 0, sizeof(port_modify));
  447. port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  448. port_modify.clr_port_cap_mask = 0;
  449. ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  450. if (ret)
  451. goto err_mod_port;
  452. ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
  453. if (ret)
  454. goto err_query_port;
  455. sport->sm_lid = port_attr.sm_lid;
  456. sport->lid = port_attr.lid;
  457. ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid,
  458. NULL);
  459. if (ret)
  460. goto err_query_port;
  461. snprintf(sport->port_guid, sizeof(sport->port_guid),
  462. "0x%016llx%016llx",
  463. be64_to_cpu(sport->gid.global.subnet_prefix),
  464. be64_to_cpu(sport->gid.global.interface_id));
  465. if (!sport->mad_agent) {
  466. memset(&reg_req, 0, sizeof(reg_req));
  467. reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
  468. reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
  469. set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
  470. set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
  471. sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
  472. sport->port,
  473. IB_QPT_GSI,
  474. &reg_req, 0,
  475. srpt_mad_send_handler,
  476. srpt_mad_recv_handler,
  477. sport, 0);
  478. if (IS_ERR(sport->mad_agent)) {
  479. ret = PTR_ERR(sport->mad_agent);
  480. sport->mad_agent = NULL;
  481. goto err_query_port;
  482. }
  483. }
  484. return 0;
  485. err_query_port:
  486. port_modify.set_port_cap_mask = 0;
  487. port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  488. ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  489. err_mod_port:
  490. return ret;
  491. }
  492. /**
  493. * srpt_unregister_mad_agent() - Unregister MAD callback functions.
  494. *
  495. * Note: It is safe to call this function more than once for the same device.
  496. */
  497. static void srpt_unregister_mad_agent(struct srpt_device *sdev)
  498. {
  499. struct ib_port_modify port_modify = {
  500. .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
  501. };
  502. struct srpt_port *sport;
  503. int i;
  504. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  505. sport = &sdev->port[i - 1];
  506. WARN_ON(sport->port != i);
  507. if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
  508. pr_err("disabling MAD processing failed.\n");
  509. if (sport->mad_agent) {
  510. ib_unregister_mad_agent(sport->mad_agent);
  511. sport->mad_agent = NULL;
  512. }
  513. }
  514. }
  515. /**
  516. * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
  517. */
  518. static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
  519. int ioctx_size, int dma_size,
  520. enum dma_data_direction dir)
  521. {
  522. struct srpt_ioctx *ioctx;
  523. ioctx = kmalloc(ioctx_size, GFP_KERNEL);
  524. if (!ioctx)
  525. goto err;
  526. ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
  527. if (!ioctx->buf)
  528. goto err_free_ioctx;
  529. ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
  530. if (ib_dma_mapping_error(sdev->device, ioctx->dma))
  531. goto err_free_buf;
  532. return ioctx;
  533. err_free_buf:
  534. kfree(ioctx->buf);
  535. err_free_ioctx:
  536. kfree(ioctx);
  537. err:
  538. return NULL;
  539. }
  540. /**
  541. * srpt_free_ioctx() - Free an SRPT I/O context structure.
  542. */
  543. static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
  544. int dma_size, enum dma_data_direction dir)
  545. {
  546. if (!ioctx)
  547. return;
  548. ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
  549. kfree(ioctx->buf);
  550. kfree(ioctx);
  551. }
  552. /**
  553. * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
  554. * @sdev: Device to allocate the I/O context ring for.
  555. * @ring_size: Number of elements in the I/O context ring.
  556. * @ioctx_size: I/O context size.
  557. * @dma_size: DMA buffer size.
  558. * @dir: DMA data direction.
  559. */
  560. static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
  561. int ring_size, int ioctx_size,
  562. int dma_size, enum dma_data_direction dir)
  563. {
  564. struct srpt_ioctx **ring;
  565. int i;
  566. WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
  567. && ioctx_size != sizeof(struct srpt_send_ioctx));
  568. ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
  569. if (!ring)
  570. goto out;
  571. for (i = 0; i < ring_size; ++i) {
  572. ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
  573. if (!ring[i])
  574. goto err;
  575. ring[i]->index = i;
  576. }
  577. goto out;
  578. err:
  579. while (--i >= 0)
  580. srpt_free_ioctx(sdev, ring[i], dma_size, dir);
  581. kfree(ring);
  582. ring = NULL;
  583. out:
  584. return ring;
  585. }
  586. /**
  587. * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
  588. */
  589. static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
  590. struct srpt_device *sdev, int ring_size,
  591. int dma_size, enum dma_data_direction dir)
  592. {
  593. int i;
  594. for (i = 0; i < ring_size; ++i)
  595. srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
  596. kfree(ioctx_ring);
  597. }
  598. /**
  599. * srpt_get_cmd_state() - Get the state of a SCSI command.
  600. */
  601. static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
  602. {
  603. enum srpt_command_state state;
  604. unsigned long flags;
  605. BUG_ON(!ioctx);
  606. spin_lock_irqsave(&ioctx->spinlock, flags);
  607. state = ioctx->state;
  608. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  609. return state;
  610. }
  611. /**
  612. * srpt_set_cmd_state() - Set the state of a SCSI command.
  613. *
  614. * Does not modify the state of aborted commands. Returns the previous command
  615. * state.
  616. */
  617. static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
  618. enum srpt_command_state new)
  619. {
  620. enum srpt_command_state previous;
  621. unsigned long flags;
  622. BUG_ON(!ioctx);
  623. spin_lock_irqsave(&ioctx->spinlock, flags);
  624. previous = ioctx->state;
  625. if (previous != SRPT_STATE_DONE)
  626. ioctx->state = new;
  627. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  628. return previous;
  629. }
  630. /**
  631. * srpt_test_and_set_cmd_state() - Test and set the state of a command.
  632. *
  633. * Returns true if and only if the previous command state was equal to 'old'.
  634. */
  635. static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
  636. enum srpt_command_state old,
  637. enum srpt_command_state new)
  638. {
  639. enum srpt_command_state previous;
  640. unsigned long flags;
  641. WARN_ON(!ioctx);
  642. WARN_ON(old == SRPT_STATE_DONE);
  643. WARN_ON(new == SRPT_STATE_NEW);
  644. spin_lock_irqsave(&ioctx->spinlock, flags);
  645. previous = ioctx->state;
  646. if (previous == old)
  647. ioctx->state = new;
  648. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  649. return previous == old;
  650. }
  651. /**
  652. * srpt_post_recv() - Post an IB receive request.
  653. */
  654. static int srpt_post_recv(struct srpt_device *sdev,
  655. struct srpt_recv_ioctx *ioctx)
  656. {
  657. struct ib_sge list;
  658. struct ib_recv_wr wr, *bad_wr;
  659. BUG_ON(!sdev);
  660. list.addr = ioctx->ioctx.dma;
  661. list.length = srp_max_req_size;
  662. list.lkey = sdev->pd->local_dma_lkey;
  663. ioctx->ioctx.cqe.done = srpt_recv_done;
  664. wr.wr_cqe = &ioctx->ioctx.cqe;
  665. wr.next = NULL;
  666. wr.sg_list = &list;
  667. wr.num_sge = 1;
  668. return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
  669. }
  670. /**
  671. * srpt_zerolength_write() - Perform a zero-length RDMA write.
  672. *
  673. * A quote from the InfiniBand specification: C9-88: For an HCA responder
  674. * using Reliable Connection service, for each zero-length RDMA READ or WRITE
  675. * request, the R_Key shall not be validated, even if the request includes
  676. * Immediate data.
  677. */
  678. static int srpt_zerolength_write(struct srpt_rdma_ch *ch)
  679. {
  680. struct ib_send_wr wr, *bad_wr;
  681. memset(&wr, 0, sizeof(wr));
  682. wr.opcode = IB_WR_RDMA_WRITE;
  683. wr.wr_cqe = &ch->zw_cqe;
  684. wr.send_flags = IB_SEND_SIGNALED;
  685. return ib_post_send(ch->qp, &wr, &bad_wr);
  686. }
  687. static void srpt_zerolength_write_done(struct ib_cq *cq, struct ib_wc *wc)
  688. {
  689. struct srpt_rdma_ch *ch = cq->cq_context;
  690. if (wc->status == IB_WC_SUCCESS) {
  691. srpt_process_wait_list(ch);
  692. } else {
  693. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  694. schedule_work(&ch->release_work);
  695. else
  696. WARN_ONCE(1, "%s-%d\n", ch->sess_name, ch->qp->qp_num);
  697. }
  698. }
  699. static int srpt_alloc_rw_ctxs(struct srpt_send_ioctx *ioctx,
  700. struct srp_direct_buf *db, int nbufs, struct scatterlist **sg,
  701. unsigned *sg_cnt)
  702. {
  703. enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
  704. struct srpt_rdma_ch *ch = ioctx->ch;
  705. struct scatterlist *prev = NULL;
  706. unsigned prev_nents;
  707. int ret, i;
  708. if (nbufs == 1) {
  709. ioctx->rw_ctxs = &ioctx->s_rw_ctx;
  710. } else {
  711. ioctx->rw_ctxs = kmalloc_array(nbufs, sizeof(*ioctx->rw_ctxs),
  712. GFP_KERNEL);
  713. if (!ioctx->rw_ctxs)
  714. return -ENOMEM;
  715. }
  716. for (i = ioctx->n_rw_ctx; i < nbufs; i++, db++) {
  717. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  718. u64 remote_addr = be64_to_cpu(db->va);
  719. u32 size = be32_to_cpu(db->len);
  720. u32 rkey = be32_to_cpu(db->key);
  721. ret = target_alloc_sgl(&ctx->sg, &ctx->nents, size, false,
  722. i < nbufs - 1);
  723. if (ret)
  724. goto unwind;
  725. ret = rdma_rw_ctx_init(&ctx->rw, ch->qp, ch->sport->port,
  726. ctx->sg, ctx->nents, 0, remote_addr, rkey, dir);
  727. if (ret < 0) {
  728. target_free_sgl(ctx->sg, ctx->nents);
  729. goto unwind;
  730. }
  731. ioctx->n_rdma += ret;
  732. ioctx->n_rw_ctx++;
  733. if (prev) {
  734. sg_unmark_end(&prev[prev_nents - 1]);
  735. sg_chain(prev, prev_nents + 1, ctx->sg);
  736. } else {
  737. *sg = ctx->sg;
  738. }
  739. prev = ctx->sg;
  740. prev_nents = ctx->nents;
  741. *sg_cnt += ctx->nents;
  742. }
  743. return 0;
  744. unwind:
  745. while (--i >= 0) {
  746. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  747. rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
  748. ctx->sg, ctx->nents, dir);
  749. target_free_sgl(ctx->sg, ctx->nents);
  750. }
  751. if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
  752. kfree(ioctx->rw_ctxs);
  753. return ret;
  754. }
  755. static void srpt_free_rw_ctxs(struct srpt_rdma_ch *ch,
  756. struct srpt_send_ioctx *ioctx)
  757. {
  758. enum dma_data_direction dir = target_reverse_dma_direction(&ioctx->cmd);
  759. int i;
  760. for (i = 0; i < ioctx->n_rw_ctx; i++) {
  761. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  762. rdma_rw_ctx_destroy(&ctx->rw, ch->qp, ch->sport->port,
  763. ctx->sg, ctx->nents, dir);
  764. target_free_sgl(ctx->sg, ctx->nents);
  765. }
  766. if (ioctx->rw_ctxs != &ioctx->s_rw_ctx)
  767. kfree(ioctx->rw_ctxs);
  768. }
  769. static inline void *srpt_get_desc_buf(struct srp_cmd *srp_cmd)
  770. {
  771. /*
  772. * The pointer computations below will only be compiled correctly
  773. * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
  774. * whether srp_cmd::add_data has been declared as a byte pointer.
  775. */
  776. BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0) &&
  777. !__same_type(srp_cmd->add_data[0], (u8)0));
  778. /*
  779. * According to the SRP spec, the lower two bits of the 'ADDITIONAL
  780. * CDB LENGTH' field are reserved and the size in bytes of this field
  781. * is four times the value specified in bits 3..7. Hence the "& ~3".
  782. */
  783. return srp_cmd->add_data + (srp_cmd->add_cdb_len & ~3);
  784. }
  785. /**
  786. * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
  787. * @ioctx: Pointer to the I/O context associated with the request.
  788. * @srp_cmd: Pointer to the SRP_CMD request data.
  789. * @dir: Pointer to the variable to which the transfer direction will be
  790. * written.
  791. * @data_len: Pointer to the variable to which the total data length of all
  792. * descriptors in the SRP_CMD request will be written.
  793. *
  794. * This function initializes ioctx->nrbuf and ioctx->r_bufs.
  795. *
  796. * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
  797. * -ENOMEM when memory allocation fails and zero upon success.
  798. */
  799. static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
  800. struct srp_cmd *srp_cmd, enum dma_data_direction *dir,
  801. struct scatterlist **sg, unsigned *sg_cnt, u64 *data_len)
  802. {
  803. BUG_ON(!dir);
  804. BUG_ON(!data_len);
  805. /*
  806. * The lower four bits of the buffer format field contain the DATA-IN
  807. * buffer descriptor format, and the highest four bits contain the
  808. * DATA-OUT buffer descriptor format.
  809. */
  810. if (srp_cmd->buf_fmt & 0xf)
  811. /* DATA-IN: transfer data from target to initiator (read). */
  812. *dir = DMA_FROM_DEVICE;
  813. else if (srp_cmd->buf_fmt >> 4)
  814. /* DATA-OUT: transfer data from initiator to target (write). */
  815. *dir = DMA_TO_DEVICE;
  816. else
  817. *dir = DMA_NONE;
  818. /* initialize data_direction early as srpt_alloc_rw_ctxs needs it */
  819. ioctx->cmd.data_direction = *dir;
  820. if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
  821. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
  822. struct srp_direct_buf *db = srpt_get_desc_buf(srp_cmd);
  823. *data_len = be32_to_cpu(db->len);
  824. return srpt_alloc_rw_ctxs(ioctx, db, 1, sg, sg_cnt);
  825. } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
  826. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
  827. struct srp_indirect_buf *idb = srpt_get_desc_buf(srp_cmd);
  828. int nbufs = be32_to_cpu(idb->table_desc.len) /
  829. sizeof(struct srp_direct_buf);
  830. if (nbufs >
  831. (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
  832. pr_err("received unsupported SRP_CMD request"
  833. " type (%u out + %u in != %u / %zu)\n",
  834. srp_cmd->data_out_desc_cnt,
  835. srp_cmd->data_in_desc_cnt,
  836. be32_to_cpu(idb->table_desc.len),
  837. sizeof(struct srp_direct_buf));
  838. return -EINVAL;
  839. }
  840. *data_len = be32_to_cpu(idb->len);
  841. return srpt_alloc_rw_ctxs(ioctx, idb->desc_list, nbufs,
  842. sg, sg_cnt);
  843. } else {
  844. *data_len = 0;
  845. return 0;
  846. }
  847. }
  848. /**
  849. * srpt_init_ch_qp() - Initialize queue pair attributes.
  850. *
  851. * Initialized the attributes of queue pair 'qp' by allowing local write,
  852. * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
  853. */
  854. static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  855. {
  856. struct ib_qp_attr *attr;
  857. int ret;
  858. attr = kzalloc(sizeof(*attr), GFP_KERNEL);
  859. if (!attr)
  860. return -ENOMEM;
  861. attr->qp_state = IB_QPS_INIT;
  862. attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE;
  863. attr->port_num = ch->sport->port;
  864. attr->pkey_index = 0;
  865. ret = ib_modify_qp(qp, attr,
  866. IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
  867. IB_QP_PKEY_INDEX);
  868. kfree(attr);
  869. return ret;
  870. }
  871. /**
  872. * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
  873. * @ch: channel of the queue pair.
  874. * @qp: queue pair to change the state of.
  875. *
  876. * Returns zero upon success and a negative value upon failure.
  877. *
  878. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  879. * If this structure ever becomes larger, it might be necessary to allocate
  880. * it dynamically instead of on the stack.
  881. */
  882. static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  883. {
  884. struct ib_qp_attr qp_attr;
  885. int attr_mask;
  886. int ret;
  887. qp_attr.qp_state = IB_QPS_RTR;
  888. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  889. if (ret)
  890. goto out;
  891. qp_attr.max_dest_rd_atomic = 4;
  892. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  893. out:
  894. return ret;
  895. }
  896. /**
  897. * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
  898. * @ch: channel of the queue pair.
  899. * @qp: queue pair to change the state of.
  900. *
  901. * Returns zero upon success and a negative value upon failure.
  902. *
  903. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  904. * If this structure ever becomes larger, it might be necessary to allocate
  905. * it dynamically instead of on the stack.
  906. */
  907. static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  908. {
  909. struct ib_qp_attr qp_attr;
  910. int attr_mask;
  911. int ret;
  912. qp_attr.qp_state = IB_QPS_RTS;
  913. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  914. if (ret)
  915. goto out;
  916. qp_attr.max_rd_atomic = 4;
  917. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  918. out:
  919. return ret;
  920. }
  921. /**
  922. * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
  923. */
  924. static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
  925. {
  926. struct ib_qp_attr qp_attr;
  927. qp_attr.qp_state = IB_QPS_ERR;
  928. return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
  929. }
  930. /**
  931. * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
  932. */
  933. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  934. {
  935. struct srpt_send_ioctx *ioctx;
  936. unsigned long flags;
  937. BUG_ON(!ch);
  938. ioctx = NULL;
  939. spin_lock_irqsave(&ch->spinlock, flags);
  940. if (!list_empty(&ch->free_list)) {
  941. ioctx = list_first_entry(&ch->free_list,
  942. struct srpt_send_ioctx, free_list);
  943. list_del(&ioctx->free_list);
  944. }
  945. spin_unlock_irqrestore(&ch->spinlock, flags);
  946. if (!ioctx)
  947. return ioctx;
  948. BUG_ON(ioctx->ch != ch);
  949. spin_lock_init(&ioctx->spinlock);
  950. ioctx->state = SRPT_STATE_NEW;
  951. ioctx->n_rdma = 0;
  952. ioctx->n_rw_ctx = 0;
  953. init_completion(&ioctx->tx_done);
  954. ioctx->queue_status_only = false;
  955. /*
  956. * transport_init_se_cmd() does not initialize all fields, so do it
  957. * here.
  958. */
  959. memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
  960. memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
  961. return ioctx;
  962. }
  963. /**
  964. * srpt_abort_cmd() - Abort a SCSI command.
  965. * @ioctx: I/O context associated with the SCSI command.
  966. * @context: Preferred execution context.
  967. */
  968. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  969. {
  970. enum srpt_command_state state;
  971. unsigned long flags;
  972. BUG_ON(!ioctx);
  973. /*
  974. * If the command is in a state where the target core is waiting for
  975. * the ib_srpt driver, change the state to the next state.
  976. */
  977. spin_lock_irqsave(&ioctx->spinlock, flags);
  978. state = ioctx->state;
  979. switch (state) {
  980. case SRPT_STATE_NEED_DATA:
  981. ioctx->state = SRPT_STATE_DATA_IN;
  982. break;
  983. case SRPT_STATE_CMD_RSP_SENT:
  984. case SRPT_STATE_MGMT_RSP_SENT:
  985. ioctx->state = SRPT_STATE_DONE;
  986. break;
  987. default:
  988. WARN_ONCE(true, "%s: unexpected I/O context state %d\n",
  989. __func__, state);
  990. break;
  991. }
  992. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  993. pr_debug("Aborting cmd with state %d and tag %lld\n", state,
  994. ioctx->cmd.tag);
  995. switch (state) {
  996. case SRPT_STATE_NEW:
  997. case SRPT_STATE_DATA_IN:
  998. case SRPT_STATE_MGMT:
  999. case SRPT_STATE_DONE:
  1000. /*
  1001. * Do nothing - defer abort processing until
  1002. * srpt_queue_response() is invoked.
  1003. */
  1004. break;
  1005. case SRPT_STATE_NEED_DATA:
  1006. pr_debug("tag %#llx: RDMA read error\n", ioctx->cmd.tag);
  1007. transport_generic_request_failure(&ioctx->cmd,
  1008. TCM_CHECK_CONDITION_ABORT_CMD);
  1009. break;
  1010. case SRPT_STATE_CMD_RSP_SENT:
  1011. /*
  1012. * SRP_RSP sending failed or the SRP_RSP send completion has
  1013. * not been received in time.
  1014. */
  1015. transport_generic_free_cmd(&ioctx->cmd, 0);
  1016. break;
  1017. case SRPT_STATE_MGMT_RSP_SENT:
  1018. transport_generic_free_cmd(&ioctx->cmd, 0);
  1019. break;
  1020. default:
  1021. WARN(1, "Unexpected command state (%d)", state);
  1022. break;
  1023. }
  1024. return state;
  1025. }
  1026. /**
  1027. * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
  1028. * the data that has been transferred via IB RDMA had to be postponed until the
  1029. * check_stop_free() callback. None of this is necessary anymore and needs to
  1030. * be cleaned up.
  1031. */
  1032. static void srpt_rdma_read_done(struct ib_cq *cq, struct ib_wc *wc)
  1033. {
  1034. struct srpt_rdma_ch *ch = cq->cq_context;
  1035. struct srpt_send_ioctx *ioctx =
  1036. container_of(wc->wr_cqe, struct srpt_send_ioctx, rdma_cqe);
  1037. WARN_ON(ioctx->n_rdma <= 0);
  1038. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1039. ioctx->n_rdma = 0;
  1040. if (unlikely(wc->status != IB_WC_SUCCESS)) {
  1041. pr_info("RDMA_READ for ioctx 0x%p failed with status %d\n",
  1042. ioctx, wc->status);
  1043. srpt_abort_cmd(ioctx);
  1044. return;
  1045. }
  1046. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1047. SRPT_STATE_DATA_IN))
  1048. target_execute_cmd(&ioctx->cmd);
  1049. else
  1050. pr_err("%s[%d]: wrong state = %d\n", __func__,
  1051. __LINE__, srpt_get_cmd_state(ioctx));
  1052. }
  1053. /**
  1054. * srpt_build_cmd_rsp() - Build an SRP_RSP response.
  1055. * @ch: RDMA channel through which the request has been received.
  1056. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1057. * be built in the buffer ioctx->buf points at and hence this function will
  1058. * overwrite the request data.
  1059. * @tag: tag of the request for which this response is being generated.
  1060. * @status: value for the STATUS field of the SRP_RSP information unit.
  1061. *
  1062. * Returns the size in bytes of the SRP_RSP response.
  1063. *
  1064. * An SRP_RSP response contains a SCSI status or service response. See also
  1065. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1066. * response. See also SPC-2 for more information about sense data.
  1067. */
  1068. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1069. struct srpt_send_ioctx *ioctx, u64 tag,
  1070. int status)
  1071. {
  1072. struct srp_rsp *srp_rsp;
  1073. const u8 *sense_data;
  1074. int sense_data_len, max_sense_len;
  1075. /*
  1076. * The lowest bit of all SAM-3 status codes is zero (see also
  1077. * paragraph 5.3 in SAM-3).
  1078. */
  1079. WARN_ON(status & 1);
  1080. srp_rsp = ioctx->ioctx.buf;
  1081. BUG_ON(!srp_rsp);
  1082. sense_data = ioctx->sense_data;
  1083. sense_data_len = ioctx->cmd.scsi_sense_length;
  1084. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1085. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1086. srp_rsp->opcode = SRP_RSP;
  1087. srp_rsp->req_lim_delta =
  1088. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1089. srp_rsp->tag = tag;
  1090. srp_rsp->status = status;
  1091. if (sense_data_len) {
  1092. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1093. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1094. if (sense_data_len > max_sense_len) {
  1095. pr_warn("truncated sense data from %d to %d"
  1096. " bytes\n", sense_data_len, max_sense_len);
  1097. sense_data_len = max_sense_len;
  1098. }
  1099. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1100. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1101. memcpy(srp_rsp + 1, sense_data, sense_data_len);
  1102. }
  1103. return sizeof(*srp_rsp) + sense_data_len;
  1104. }
  1105. /**
  1106. * srpt_build_tskmgmt_rsp() - Build a task management response.
  1107. * @ch: RDMA channel through which the request has been received.
  1108. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1109. * @rsp_code: RSP_CODE that will be stored in the response.
  1110. * @tag: Tag of the request for which this response is being generated.
  1111. *
  1112. * Returns the size in bytes of the SRP_RSP response.
  1113. *
  1114. * An SRP_RSP response contains a SCSI status or service response. See also
  1115. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1116. * response.
  1117. */
  1118. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1119. struct srpt_send_ioctx *ioctx,
  1120. u8 rsp_code, u64 tag)
  1121. {
  1122. struct srp_rsp *srp_rsp;
  1123. int resp_data_len;
  1124. int resp_len;
  1125. resp_data_len = 4;
  1126. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1127. srp_rsp = ioctx->ioctx.buf;
  1128. BUG_ON(!srp_rsp);
  1129. memset(srp_rsp, 0, sizeof(*srp_rsp));
  1130. srp_rsp->opcode = SRP_RSP;
  1131. srp_rsp->req_lim_delta =
  1132. cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1133. srp_rsp->tag = tag;
  1134. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1135. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1136. srp_rsp->data[3] = rsp_code;
  1137. return resp_len;
  1138. }
  1139. static int srpt_check_stop_free(struct se_cmd *cmd)
  1140. {
  1141. struct srpt_send_ioctx *ioctx = container_of(cmd,
  1142. struct srpt_send_ioctx, cmd);
  1143. return target_put_sess_cmd(&ioctx->cmd);
  1144. }
  1145. /**
  1146. * srpt_handle_cmd() - Process SRP_CMD.
  1147. */
  1148. static void srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1149. struct srpt_recv_ioctx *recv_ioctx,
  1150. struct srpt_send_ioctx *send_ioctx)
  1151. {
  1152. struct se_cmd *cmd;
  1153. struct srp_cmd *srp_cmd;
  1154. struct scatterlist *sg = NULL;
  1155. unsigned sg_cnt = 0;
  1156. u64 data_len;
  1157. enum dma_data_direction dir;
  1158. int rc;
  1159. BUG_ON(!send_ioctx);
  1160. srp_cmd = recv_ioctx->ioctx.buf;
  1161. cmd = &send_ioctx->cmd;
  1162. cmd->tag = srp_cmd->tag;
  1163. switch (srp_cmd->task_attr) {
  1164. case SRP_CMD_SIMPLE_Q:
  1165. cmd->sam_task_attr = TCM_SIMPLE_TAG;
  1166. break;
  1167. case SRP_CMD_ORDERED_Q:
  1168. default:
  1169. cmd->sam_task_attr = TCM_ORDERED_TAG;
  1170. break;
  1171. case SRP_CMD_HEAD_OF_Q:
  1172. cmd->sam_task_attr = TCM_HEAD_TAG;
  1173. break;
  1174. case SRP_CMD_ACA:
  1175. cmd->sam_task_attr = TCM_ACA_TAG;
  1176. break;
  1177. }
  1178. rc = srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &sg, &sg_cnt,
  1179. &data_len);
  1180. if (rc) {
  1181. if (rc != -EAGAIN) {
  1182. pr_err("0x%llx: parsing SRP descriptor table failed.\n",
  1183. srp_cmd->tag);
  1184. }
  1185. goto release_ioctx;
  1186. }
  1187. rc = target_submit_cmd_map_sgls(cmd, ch->sess, srp_cmd->cdb,
  1188. &send_ioctx->sense_data[0],
  1189. scsilun_to_int(&srp_cmd->lun), data_len,
  1190. TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF,
  1191. sg, sg_cnt, NULL, 0, NULL, 0);
  1192. if (rc != 0) {
  1193. pr_debug("target_submit_cmd() returned %d for tag %#llx\n", rc,
  1194. srp_cmd->tag);
  1195. goto release_ioctx;
  1196. }
  1197. return;
  1198. release_ioctx:
  1199. send_ioctx->state = SRPT_STATE_DONE;
  1200. srpt_release_cmd(cmd);
  1201. }
  1202. static int srp_tmr_to_tcm(int fn)
  1203. {
  1204. switch (fn) {
  1205. case SRP_TSK_ABORT_TASK:
  1206. return TMR_ABORT_TASK;
  1207. case SRP_TSK_ABORT_TASK_SET:
  1208. return TMR_ABORT_TASK_SET;
  1209. case SRP_TSK_CLEAR_TASK_SET:
  1210. return TMR_CLEAR_TASK_SET;
  1211. case SRP_TSK_LUN_RESET:
  1212. return TMR_LUN_RESET;
  1213. case SRP_TSK_CLEAR_ACA:
  1214. return TMR_CLEAR_ACA;
  1215. default:
  1216. return -1;
  1217. }
  1218. }
  1219. /**
  1220. * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
  1221. *
  1222. * Returns 0 if and only if the request will be processed by the target core.
  1223. *
  1224. * For more information about SRP_TSK_MGMT information units, see also section
  1225. * 6.7 in the SRP r16a document.
  1226. */
  1227. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1228. struct srpt_recv_ioctx *recv_ioctx,
  1229. struct srpt_send_ioctx *send_ioctx)
  1230. {
  1231. struct srp_tsk_mgmt *srp_tsk;
  1232. struct se_cmd *cmd;
  1233. struct se_session *sess = ch->sess;
  1234. int tcm_tmr;
  1235. int rc;
  1236. BUG_ON(!send_ioctx);
  1237. srp_tsk = recv_ioctx->ioctx.buf;
  1238. cmd = &send_ioctx->cmd;
  1239. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
  1240. " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
  1241. srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
  1242. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1243. send_ioctx->cmd.tag = srp_tsk->tag;
  1244. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1245. rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL,
  1246. scsilun_to_int(&srp_tsk->lun), srp_tsk, tcm_tmr,
  1247. GFP_KERNEL, srp_tsk->task_tag,
  1248. TARGET_SCF_ACK_KREF);
  1249. if (rc != 0) {
  1250. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1251. goto fail;
  1252. }
  1253. return;
  1254. fail:
  1255. transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
  1256. }
  1257. /**
  1258. * srpt_handle_new_iu() - Process a newly received information unit.
  1259. * @ch: RDMA channel through which the information unit has been received.
  1260. * @ioctx: SRPT I/O context associated with the information unit.
  1261. */
  1262. static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
  1263. struct srpt_recv_ioctx *recv_ioctx,
  1264. struct srpt_send_ioctx *send_ioctx)
  1265. {
  1266. struct srp_cmd *srp_cmd;
  1267. BUG_ON(!ch);
  1268. BUG_ON(!recv_ioctx);
  1269. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1270. recv_ioctx->ioctx.dma, srp_max_req_size,
  1271. DMA_FROM_DEVICE);
  1272. if (unlikely(ch->state == CH_CONNECTING))
  1273. goto out_wait;
  1274. if (unlikely(ch->state != CH_LIVE))
  1275. return;
  1276. srp_cmd = recv_ioctx->ioctx.buf;
  1277. if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
  1278. if (!send_ioctx) {
  1279. if (!list_empty(&ch->cmd_wait_list))
  1280. goto out_wait;
  1281. send_ioctx = srpt_get_send_ioctx(ch);
  1282. }
  1283. if (unlikely(!send_ioctx))
  1284. goto out_wait;
  1285. }
  1286. switch (srp_cmd->opcode) {
  1287. case SRP_CMD:
  1288. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1289. break;
  1290. case SRP_TSK_MGMT:
  1291. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1292. break;
  1293. case SRP_I_LOGOUT:
  1294. pr_err("Not yet implemented: SRP_I_LOGOUT\n");
  1295. break;
  1296. case SRP_CRED_RSP:
  1297. pr_debug("received SRP_CRED_RSP\n");
  1298. break;
  1299. case SRP_AER_RSP:
  1300. pr_debug("received SRP_AER_RSP\n");
  1301. break;
  1302. case SRP_RSP:
  1303. pr_err("Received SRP_RSP\n");
  1304. break;
  1305. default:
  1306. pr_err("received IU with unknown opcode 0x%x\n",
  1307. srp_cmd->opcode);
  1308. break;
  1309. }
  1310. srpt_post_recv(ch->sport->sdev, recv_ioctx);
  1311. return;
  1312. out_wait:
  1313. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1314. }
  1315. static void srpt_recv_done(struct ib_cq *cq, struct ib_wc *wc)
  1316. {
  1317. struct srpt_rdma_ch *ch = cq->cq_context;
  1318. struct srpt_recv_ioctx *ioctx =
  1319. container_of(wc->wr_cqe, struct srpt_recv_ioctx, ioctx.cqe);
  1320. if (wc->status == IB_WC_SUCCESS) {
  1321. int req_lim;
  1322. req_lim = atomic_dec_return(&ch->req_lim);
  1323. if (unlikely(req_lim < 0))
  1324. pr_err("req_lim = %d < 0\n", req_lim);
  1325. srpt_handle_new_iu(ch, ioctx, NULL);
  1326. } else {
  1327. pr_info("receiving failed for ioctx %p with status %d\n",
  1328. ioctx, wc->status);
  1329. }
  1330. }
  1331. /*
  1332. * This function must be called from the context in which RDMA completions are
  1333. * processed because it accesses the wait list without protection against
  1334. * access from other threads.
  1335. */
  1336. static void srpt_process_wait_list(struct srpt_rdma_ch *ch)
  1337. {
  1338. struct srpt_send_ioctx *ioctx;
  1339. while (!list_empty(&ch->cmd_wait_list) &&
  1340. ch->state >= CH_LIVE &&
  1341. (ioctx = srpt_get_send_ioctx(ch)) != NULL) {
  1342. struct srpt_recv_ioctx *recv_ioctx;
  1343. recv_ioctx = list_first_entry(&ch->cmd_wait_list,
  1344. struct srpt_recv_ioctx,
  1345. wait_list);
  1346. list_del(&recv_ioctx->wait_list);
  1347. srpt_handle_new_iu(ch, recv_ioctx, ioctx);
  1348. }
  1349. }
  1350. /**
  1351. * Note: Although this has not yet been observed during tests, at least in
  1352. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1353. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1354. * value in each response is set to one, and it is possible that this response
  1355. * makes the initiator send a new request before the send completion for that
  1356. * response has been processed. This could e.g. happen if the call to
  1357. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1358. * if IB retransmission causes generation of the send completion to be
  1359. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1360. * are queued on cmd_wait_list. The code below processes these delayed
  1361. * requests one at a time.
  1362. */
  1363. static void srpt_send_done(struct ib_cq *cq, struct ib_wc *wc)
  1364. {
  1365. struct srpt_rdma_ch *ch = cq->cq_context;
  1366. struct srpt_send_ioctx *ioctx =
  1367. container_of(wc->wr_cqe, struct srpt_send_ioctx, ioctx.cqe);
  1368. enum srpt_command_state state;
  1369. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1370. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT &&
  1371. state != SRPT_STATE_MGMT_RSP_SENT);
  1372. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  1373. if (wc->status != IB_WC_SUCCESS)
  1374. pr_info("sending response for ioctx 0x%p failed"
  1375. " with status %d\n", ioctx, wc->status);
  1376. if (state != SRPT_STATE_DONE) {
  1377. transport_generic_free_cmd(&ioctx->cmd, 0);
  1378. } else {
  1379. pr_err("IB completion has been received too late for"
  1380. " wr_id = %u.\n", ioctx->ioctx.index);
  1381. }
  1382. srpt_process_wait_list(ch);
  1383. }
  1384. /**
  1385. * srpt_create_ch_ib() - Create receive and send completion queues.
  1386. */
  1387. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1388. {
  1389. struct ib_qp_init_attr *qp_init;
  1390. struct srpt_port *sport = ch->sport;
  1391. struct srpt_device *sdev = sport->sdev;
  1392. const struct ib_device_attr *attrs = &sdev->device->attrs;
  1393. u32 srp_sq_size = sport->port_attrib.srp_sq_size;
  1394. int ret;
  1395. WARN_ON(ch->rq_size < 1);
  1396. ret = -ENOMEM;
  1397. qp_init = kzalloc(sizeof(*qp_init), GFP_KERNEL);
  1398. if (!qp_init)
  1399. goto out;
  1400. retry:
  1401. ch->cq = ib_alloc_cq(sdev->device, ch, ch->rq_size + srp_sq_size,
  1402. 0 /* XXX: spread CQs */, IB_POLL_WORKQUEUE);
  1403. if (IS_ERR(ch->cq)) {
  1404. ret = PTR_ERR(ch->cq);
  1405. pr_err("failed to create CQ cqe= %d ret= %d\n",
  1406. ch->rq_size + srp_sq_size, ret);
  1407. goto out;
  1408. }
  1409. qp_init->qp_context = (void *)ch;
  1410. qp_init->event_handler
  1411. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1412. qp_init->send_cq = ch->cq;
  1413. qp_init->recv_cq = ch->cq;
  1414. qp_init->srq = sdev->srq;
  1415. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1416. qp_init->qp_type = IB_QPT_RC;
  1417. /*
  1418. * We divide up our send queue size into half SEND WRs to send the
  1419. * completions, and half R/W contexts to actually do the RDMA
  1420. * READ/WRITE transfers. Note that we need to allocate CQ slots for
  1421. * both both, as RDMA contexts will also post completions for the
  1422. * RDMA READ case.
  1423. */
  1424. qp_init->cap.max_send_wr = srp_sq_size / 2;
  1425. qp_init->cap.max_rdma_ctxs = srp_sq_size / 2;
  1426. qp_init->cap.max_send_sge = min(attrs->max_sge, SRPT_MAX_SG_PER_WQE);
  1427. qp_init->port_num = ch->sport->port;
  1428. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1429. if (IS_ERR(ch->qp)) {
  1430. ret = PTR_ERR(ch->qp);
  1431. if (ret == -ENOMEM) {
  1432. srp_sq_size /= 2;
  1433. if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
  1434. ib_destroy_cq(ch->cq);
  1435. goto retry;
  1436. }
  1437. }
  1438. pr_err("failed to create_qp ret= %d\n", ret);
  1439. goto err_destroy_cq;
  1440. }
  1441. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1442. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  1443. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1444. qp_init->cap.max_send_wr, ch->cm_id);
  1445. ret = srpt_init_ch_qp(ch, ch->qp);
  1446. if (ret)
  1447. goto err_destroy_qp;
  1448. out:
  1449. kfree(qp_init);
  1450. return ret;
  1451. err_destroy_qp:
  1452. ib_destroy_qp(ch->qp);
  1453. err_destroy_cq:
  1454. ib_free_cq(ch->cq);
  1455. goto out;
  1456. }
  1457. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1458. {
  1459. ib_destroy_qp(ch->qp);
  1460. ib_free_cq(ch->cq);
  1461. }
  1462. /**
  1463. * srpt_close_ch() - Close an RDMA channel.
  1464. *
  1465. * Make sure all resources associated with the channel will be deallocated at
  1466. * an appropriate time.
  1467. *
  1468. * Returns true if and only if the channel state has been modified into
  1469. * CH_DRAINING.
  1470. */
  1471. static bool srpt_close_ch(struct srpt_rdma_ch *ch)
  1472. {
  1473. int ret;
  1474. if (!srpt_set_ch_state(ch, CH_DRAINING)) {
  1475. pr_debug("%s-%d: already closed\n", ch->sess_name,
  1476. ch->qp->qp_num);
  1477. return false;
  1478. }
  1479. kref_get(&ch->kref);
  1480. ret = srpt_ch_qp_err(ch);
  1481. if (ret < 0)
  1482. pr_err("%s-%d: changing queue pair into error state failed: %d\n",
  1483. ch->sess_name, ch->qp->qp_num, ret);
  1484. pr_debug("%s-%d: queued zerolength write\n", ch->sess_name,
  1485. ch->qp->qp_num);
  1486. ret = srpt_zerolength_write(ch);
  1487. if (ret < 0) {
  1488. pr_err("%s-%d: queuing zero-length write failed: %d\n",
  1489. ch->sess_name, ch->qp->qp_num, ret);
  1490. if (srpt_set_ch_state(ch, CH_DISCONNECTED))
  1491. schedule_work(&ch->release_work);
  1492. else
  1493. WARN_ON_ONCE(true);
  1494. }
  1495. kref_put(&ch->kref, srpt_free_ch);
  1496. return true;
  1497. }
  1498. /*
  1499. * Change the channel state into CH_DISCONNECTING. If a channel has not yet
  1500. * reached the connected state, close it. If a channel is in the connected
  1501. * state, send a DREQ. If a DREQ has been received, send a DREP. Note: it is
  1502. * the responsibility of the caller to ensure that this function is not
  1503. * invoked concurrently with the code that accepts a connection. This means
  1504. * that this function must either be invoked from inside a CM callback
  1505. * function or that it must be invoked with the srpt_port.mutex held.
  1506. */
  1507. static int srpt_disconnect_ch(struct srpt_rdma_ch *ch)
  1508. {
  1509. int ret;
  1510. if (!srpt_set_ch_state(ch, CH_DISCONNECTING))
  1511. return -ENOTCONN;
  1512. ret = ib_send_cm_dreq(ch->cm_id, NULL, 0);
  1513. if (ret < 0)
  1514. ret = ib_send_cm_drep(ch->cm_id, NULL, 0);
  1515. if (ret < 0 && srpt_close_ch(ch))
  1516. ret = 0;
  1517. return ret;
  1518. }
  1519. static void __srpt_close_all_ch(struct srpt_device *sdev)
  1520. {
  1521. struct srpt_rdma_ch *ch;
  1522. lockdep_assert_held(&sdev->mutex);
  1523. list_for_each_entry(ch, &sdev->rch_list, list) {
  1524. if (srpt_disconnect_ch(ch) >= 0)
  1525. pr_info("Closing channel %s-%d because target %s has been disabled\n",
  1526. ch->sess_name, ch->qp->qp_num,
  1527. sdev->device->name);
  1528. srpt_close_ch(ch);
  1529. }
  1530. }
  1531. static void srpt_free_ch(struct kref *kref)
  1532. {
  1533. struct srpt_rdma_ch *ch = container_of(kref, struct srpt_rdma_ch, kref);
  1534. kfree(ch);
  1535. }
  1536. static void srpt_release_channel_work(struct work_struct *w)
  1537. {
  1538. struct srpt_rdma_ch *ch;
  1539. struct srpt_device *sdev;
  1540. struct se_session *se_sess;
  1541. ch = container_of(w, struct srpt_rdma_ch, release_work);
  1542. pr_debug("%s: %s-%d; release_done = %p\n", __func__, ch->sess_name,
  1543. ch->qp->qp_num, ch->release_done);
  1544. sdev = ch->sport->sdev;
  1545. BUG_ON(!sdev);
  1546. se_sess = ch->sess;
  1547. BUG_ON(!se_sess);
  1548. target_sess_cmd_list_set_waiting(se_sess);
  1549. target_wait_for_sess_cmds(se_sess);
  1550. transport_deregister_session_configfs(se_sess);
  1551. transport_deregister_session(se_sess);
  1552. ch->sess = NULL;
  1553. ib_destroy_cm_id(ch->cm_id);
  1554. srpt_destroy_ch_ib(ch);
  1555. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  1556. ch->sport->sdev, ch->rq_size,
  1557. ch->rsp_size, DMA_TO_DEVICE);
  1558. mutex_lock(&sdev->mutex);
  1559. list_del_init(&ch->list);
  1560. if (ch->release_done)
  1561. complete(ch->release_done);
  1562. mutex_unlock(&sdev->mutex);
  1563. wake_up(&sdev->ch_releaseQ);
  1564. kref_put(&ch->kref, srpt_free_ch);
  1565. }
  1566. /**
  1567. * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
  1568. *
  1569. * Ownership of the cm_id is transferred to the target session if this
  1570. * functions returns zero. Otherwise the caller remains the owner of cm_id.
  1571. */
  1572. static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
  1573. struct ib_cm_req_event_param *param,
  1574. void *private_data)
  1575. {
  1576. struct srpt_device *sdev = cm_id->context;
  1577. struct srpt_port *sport = &sdev->port[param->port - 1];
  1578. struct srp_login_req *req;
  1579. struct srp_login_rsp *rsp;
  1580. struct srp_login_rej *rej;
  1581. struct ib_cm_rep_param *rep_param;
  1582. struct srpt_rdma_ch *ch, *tmp_ch;
  1583. u32 it_iu_len;
  1584. int i, ret = 0;
  1585. unsigned char *p;
  1586. WARN_ON_ONCE(irqs_disabled());
  1587. if (WARN_ON(!sdev || !private_data))
  1588. return -EINVAL;
  1589. req = (struct srp_login_req *)private_data;
  1590. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  1591. pr_info("Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
  1592. " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
  1593. " (guid=0x%llx:0x%llx)\n",
  1594. be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
  1595. be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
  1596. be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
  1597. be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
  1598. it_iu_len,
  1599. param->port,
  1600. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
  1601. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
  1602. rsp = kzalloc(sizeof(*rsp), GFP_KERNEL);
  1603. rej = kzalloc(sizeof(*rej), GFP_KERNEL);
  1604. rep_param = kzalloc(sizeof(*rep_param), GFP_KERNEL);
  1605. if (!rsp || !rej || !rep_param) {
  1606. ret = -ENOMEM;
  1607. goto out;
  1608. }
  1609. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  1610. rej->reason = cpu_to_be32(
  1611. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  1612. ret = -EINVAL;
  1613. pr_err("rejected SRP_LOGIN_REQ because its"
  1614. " length (%d bytes) is out of range (%d .. %d)\n",
  1615. it_iu_len, 64, srp_max_req_size);
  1616. goto reject;
  1617. }
  1618. if (!sport->enabled) {
  1619. rej->reason = cpu_to_be32(
  1620. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1621. ret = -EINVAL;
  1622. pr_err("rejected SRP_LOGIN_REQ because the target port"
  1623. " has not yet been enabled\n");
  1624. goto reject;
  1625. }
  1626. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  1627. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
  1628. mutex_lock(&sdev->mutex);
  1629. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
  1630. if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
  1631. && !memcmp(ch->t_port_id, req->target_port_id, 16)
  1632. && param->port == ch->sport->port
  1633. && param->listen_id == ch->sport->sdev->cm_id
  1634. && ch->cm_id) {
  1635. if (srpt_disconnect_ch(ch) < 0)
  1636. continue;
  1637. pr_info("Relogin - closed existing channel %s\n",
  1638. ch->sess_name);
  1639. rsp->rsp_flags =
  1640. SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  1641. }
  1642. }
  1643. mutex_unlock(&sdev->mutex);
  1644. } else
  1645. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  1646. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  1647. || *(__be64 *)(req->target_port_id + 8) !=
  1648. cpu_to_be64(srpt_service_guid)) {
  1649. rej->reason = cpu_to_be32(
  1650. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  1651. ret = -ENOMEM;
  1652. pr_err("rejected SRP_LOGIN_REQ because it"
  1653. " has an invalid target port identifier.\n");
  1654. goto reject;
  1655. }
  1656. ch = kzalloc(sizeof(*ch), GFP_KERNEL);
  1657. if (!ch) {
  1658. rej->reason = cpu_to_be32(
  1659. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1660. pr_err("rejected SRP_LOGIN_REQ because no memory.\n");
  1661. ret = -ENOMEM;
  1662. goto reject;
  1663. }
  1664. kref_init(&ch->kref);
  1665. ch->zw_cqe.done = srpt_zerolength_write_done;
  1666. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  1667. memcpy(ch->i_port_id, req->initiator_port_id, 16);
  1668. memcpy(ch->t_port_id, req->target_port_id, 16);
  1669. ch->sport = &sdev->port[param->port - 1];
  1670. ch->cm_id = cm_id;
  1671. cm_id->context = ch;
  1672. /*
  1673. * Avoid QUEUE_FULL conditions by limiting the number of buffers used
  1674. * for the SRP protocol to the command queue size.
  1675. */
  1676. ch->rq_size = SRPT_RQ_SIZE;
  1677. spin_lock_init(&ch->spinlock);
  1678. ch->state = CH_CONNECTING;
  1679. INIT_LIST_HEAD(&ch->cmd_wait_list);
  1680. ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  1681. ch->ioctx_ring = (struct srpt_send_ioctx **)
  1682. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  1683. sizeof(*ch->ioctx_ring[0]),
  1684. ch->rsp_size, DMA_TO_DEVICE);
  1685. if (!ch->ioctx_ring)
  1686. goto free_ch;
  1687. INIT_LIST_HEAD(&ch->free_list);
  1688. for (i = 0; i < ch->rq_size; i++) {
  1689. ch->ioctx_ring[i]->ch = ch;
  1690. list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
  1691. }
  1692. ret = srpt_create_ch_ib(ch);
  1693. if (ret) {
  1694. rej->reason = cpu_to_be32(
  1695. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1696. pr_err("rejected SRP_LOGIN_REQ because creating"
  1697. " a new RDMA channel failed.\n");
  1698. goto free_ring;
  1699. }
  1700. ret = srpt_ch_qp_rtr(ch, ch->qp);
  1701. if (ret) {
  1702. rej->reason = cpu_to_be32(SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  1703. pr_err("rejected SRP_LOGIN_REQ because enabling"
  1704. " RTR failed (error code = %d)\n", ret);
  1705. goto destroy_ib;
  1706. }
  1707. /*
  1708. * Use the initator port identifier as the session name, when
  1709. * checking against se_node_acl->initiatorname[] this can be
  1710. * with or without preceeding '0x'.
  1711. */
  1712. snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
  1713. be64_to_cpu(*(__be64 *)ch->i_port_id),
  1714. be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
  1715. pr_debug("registering session %s\n", ch->sess_name);
  1716. p = &ch->sess_name[0];
  1717. try_again:
  1718. ch->sess = target_alloc_session(&sport->port_tpg_1, 0, 0,
  1719. TARGET_PROT_NORMAL, p, ch, NULL);
  1720. if (IS_ERR(ch->sess)) {
  1721. pr_info("Rejected login because no ACL has been"
  1722. " configured yet for initiator %s.\n", p);
  1723. /*
  1724. * XXX: Hack to retry of ch->i_port_id without leading '0x'
  1725. */
  1726. if (p == &ch->sess_name[0]) {
  1727. p += 2;
  1728. goto try_again;
  1729. }
  1730. rej->reason = cpu_to_be32((PTR_ERR(ch->sess) == -ENOMEM) ?
  1731. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES :
  1732. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  1733. goto destroy_ib;
  1734. }
  1735. pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
  1736. ch->sess_name, ch->cm_id);
  1737. /* create srp_login_response */
  1738. rsp->opcode = SRP_LOGIN_RSP;
  1739. rsp->tag = req->tag;
  1740. rsp->max_it_iu_len = req->req_it_iu_len;
  1741. rsp->max_ti_iu_len = req->req_it_iu_len;
  1742. ch->max_ti_iu_len = it_iu_len;
  1743. rsp->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  1744. | SRP_BUF_FORMAT_INDIRECT);
  1745. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  1746. atomic_set(&ch->req_lim, ch->rq_size);
  1747. atomic_set(&ch->req_lim_delta, 0);
  1748. /* create cm reply */
  1749. rep_param->qp_num = ch->qp->qp_num;
  1750. rep_param->private_data = (void *)rsp;
  1751. rep_param->private_data_len = sizeof(*rsp);
  1752. rep_param->rnr_retry_count = 7;
  1753. rep_param->flow_control = 1;
  1754. rep_param->failover_accepted = 0;
  1755. rep_param->srq = 1;
  1756. rep_param->responder_resources = 4;
  1757. rep_param->initiator_depth = 4;
  1758. ret = ib_send_cm_rep(cm_id, rep_param);
  1759. if (ret) {
  1760. pr_err("sending SRP_LOGIN_REQ response failed"
  1761. " (error code = %d)\n", ret);
  1762. goto release_channel;
  1763. }
  1764. mutex_lock(&sdev->mutex);
  1765. list_add_tail(&ch->list, &sdev->rch_list);
  1766. mutex_unlock(&sdev->mutex);
  1767. goto out;
  1768. release_channel:
  1769. srpt_disconnect_ch(ch);
  1770. transport_deregister_session_configfs(ch->sess);
  1771. transport_deregister_session(ch->sess);
  1772. ch->sess = NULL;
  1773. destroy_ib:
  1774. srpt_destroy_ch_ib(ch);
  1775. free_ring:
  1776. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  1777. ch->sport->sdev, ch->rq_size,
  1778. ch->rsp_size, DMA_TO_DEVICE);
  1779. free_ch:
  1780. kfree(ch);
  1781. reject:
  1782. rej->opcode = SRP_LOGIN_REJ;
  1783. rej->tag = req->tag;
  1784. rej->buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  1785. | SRP_BUF_FORMAT_INDIRECT);
  1786. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  1787. (void *)rej, sizeof(*rej));
  1788. out:
  1789. kfree(rep_param);
  1790. kfree(rsp);
  1791. kfree(rej);
  1792. return ret;
  1793. }
  1794. static void srpt_cm_rej_recv(struct srpt_rdma_ch *ch,
  1795. enum ib_cm_rej_reason reason,
  1796. const u8 *private_data,
  1797. u8 private_data_len)
  1798. {
  1799. char *priv = NULL;
  1800. int i;
  1801. if (private_data_len && (priv = kmalloc(private_data_len * 3 + 1,
  1802. GFP_KERNEL))) {
  1803. for (i = 0; i < private_data_len; i++)
  1804. sprintf(priv + 3 * i, " %02x", private_data[i]);
  1805. }
  1806. pr_info("Received CM REJ for ch %s-%d; reason %d%s%s.\n",
  1807. ch->sess_name, ch->qp->qp_num, reason, private_data_len ?
  1808. "; private data" : "", priv ? priv : " (?)");
  1809. kfree(priv);
  1810. }
  1811. /**
  1812. * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
  1813. *
  1814. * An IB_CM_RTU_RECEIVED message indicates that the connection is established
  1815. * and that the recipient may begin transmitting (RTU = ready to use).
  1816. */
  1817. static void srpt_cm_rtu_recv(struct srpt_rdma_ch *ch)
  1818. {
  1819. int ret;
  1820. if (srpt_set_ch_state(ch, CH_LIVE)) {
  1821. ret = srpt_ch_qp_rts(ch, ch->qp);
  1822. if (ret == 0) {
  1823. /* Trigger wait list processing. */
  1824. ret = srpt_zerolength_write(ch);
  1825. WARN_ONCE(ret < 0, "%d\n", ret);
  1826. } else {
  1827. srpt_close_ch(ch);
  1828. }
  1829. }
  1830. }
  1831. /**
  1832. * srpt_cm_handler() - IB connection manager callback function.
  1833. *
  1834. * A non-zero return value will cause the caller destroy the CM ID.
  1835. *
  1836. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  1837. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  1838. * a non-zero value in any other case will trigger a race with the
  1839. * ib_destroy_cm_id() call in srpt_release_channel().
  1840. */
  1841. static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  1842. {
  1843. struct srpt_rdma_ch *ch = cm_id->context;
  1844. int ret;
  1845. ret = 0;
  1846. switch (event->event) {
  1847. case IB_CM_REQ_RECEIVED:
  1848. ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
  1849. event->private_data);
  1850. break;
  1851. case IB_CM_REJ_RECEIVED:
  1852. srpt_cm_rej_recv(ch, event->param.rej_rcvd.reason,
  1853. event->private_data,
  1854. IB_CM_REJ_PRIVATE_DATA_SIZE);
  1855. break;
  1856. case IB_CM_RTU_RECEIVED:
  1857. case IB_CM_USER_ESTABLISHED:
  1858. srpt_cm_rtu_recv(ch);
  1859. break;
  1860. case IB_CM_DREQ_RECEIVED:
  1861. srpt_disconnect_ch(ch);
  1862. break;
  1863. case IB_CM_DREP_RECEIVED:
  1864. pr_info("Received CM DREP message for ch %s-%d.\n",
  1865. ch->sess_name, ch->qp->qp_num);
  1866. srpt_close_ch(ch);
  1867. break;
  1868. case IB_CM_TIMEWAIT_EXIT:
  1869. pr_info("Received CM TimeWait exit for ch %s-%d.\n",
  1870. ch->sess_name, ch->qp->qp_num);
  1871. srpt_close_ch(ch);
  1872. break;
  1873. case IB_CM_REP_ERROR:
  1874. pr_info("Received CM REP error for ch %s-%d.\n", ch->sess_name,
  1875. ch->qp->qp_num);
  1876. break;
  1877. case IB_CM_DREQ_ERROR:
  1878. pr_info("Received CM DREQ ERROR event.\n");
  1879. break;
  1880. case IB_CM_MRA_RECEIVED:
  1881. pr_info("Received CM MRA event\n");
  1882. break;
  1883. default:
  1884. pr_err("received unrecognized CM event %d\n", event->event);
  1885. break;
  1886. }
  1887. return ret;
  1888. }
  1889. static int srpt_write_pending_status(struct se_cmd *se_cmd)
  1890. {
  1891. struct srpt_send_ioctx *ioctx;
  1892. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  1893. return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
  1894. }
  1895. /*
  1896. * srpt_write_pending() - Start data transfer from initiator to target (write).
  1897. */
  1898. static int srpt_write_pending(struct se_cmd *se_cmd)
  1899. {
  1900. struct srpt_send_ioctx *ioctx =
  1901. container_of(se_cmd, struct srpt_send_ioctx, cmd);
  1902. struct srpt_rdma_ch *ch = ioctx->ch;
  1903. struct ib_send_wr *first_wr = NULL, *bad_wr;
  1904. struct ib_cqe *cqe = &ioctx->rdma_cqe;
  1905. enum srpt_command_state new_state;
  1906. int ret, i;
  1907. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  1908. WARN_ON(new_state == SRPT_STATE_DONE);
  1909. if (atomic_sub_return(ioctx->n_rdma, &ch->sq_wr_avail) < 0) {
  1910. pr_warn("%s: IB send queue full (needed %d)\n",
  1911. __func__, ioctx->n_rdma);
  1912. ret = -ENOMEM;
  1913. goto out_undo;
  1914. }
  1915. cqe->done = srpt_rdma_read_done;
  1916. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  1917. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  1918. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp, ch->sport->port,
  1919. cqe, first_wr);
  1920. cqe = NULL;
  1921. }
  1922. ret = ib_post_send(ch->qp, first_wr, &bad_wr);
  1923. if (ret) {
  1924. pr_err("%s: ib_post_send() returned %d for %d (avail: %d)\n",
  1925. __func__, ret, ioctx->n_rdma,
  1926. atomic_read(&ch->sq_wr_avail));
  1927. goto out_undo;
  1928. }
  1929. return 0;
  1930. out_undo:
  1931. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1932. return ret;
  1933. }
  1934. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  1935. {
  1936. switch (tcm_mgmt_status) {
  1937. case TMR_FUNCTION_COMPLETE:
  1938. return SRP_TSK_MGMT_SUCCESS;
  1939. case TMR_FUNCTION_REJECTED:
  1940. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  1941. }
  1942. return SRP_TSK_MGMT_FAILED;
  1943. }
  1944. /**
  1945. * srpt_queue_response() - Transmits the response to a SCSI command.
  1946. *
  1947. * Callback function called by the TCM core. Must not block since it can be
  1948. * invoked on the context of the IB completion handler.
  1949. */
  1950. static void srpt_queue_response(struct se_cmd *cmd)
  1951. {
  1952. struct srpt_send_ioctx *ioctx =
  1953. container_of(cmd, struct srpt_send_ioctx, cmd);
  1954. struct srpt_rdma_ch *ch = ioctx->ch;
  1955. struct srpt_device *sdev = ch->sport->sdev;
  1956. struct ib_send_wr send_wr, *first_wr = &send_wr, *bad_wr;
  1957. struct ib_sge sge;
  1958. enum srpt_command_state state;
  1959. unsigned long flags;
  1960. int resp_len, ret, i;
  1961. u8 srp_tm_status;
  1962. BUG_ON(!ch);
  1963. spin_lock_irqsave(&ioctx->spinlock, flags);
  1964. state = ioctx->state;
  1965. switch (state) {
  1966. case SRPT_STATE_NEW:
  1967. case SRPT_STATE_DATA_IN:
  1968. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  1969. break;
  1970. case SRPT_STATE_MGMT:
  1971. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  1972. break;
  1973. default:
  1974. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  1975. ch, ioctx->ioctx.index, ioctx->state);
  1976. break;
  1977. }
  1978. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  1979. if (unlikely(WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT)))
  1980. return;
  1981. /* For read commands, transfer the data to the initiator. */
  1982. if (ioctx->cmd.data_direction == DMA_FROM_DEVICE &&
  1983. ioctx->cmd.data_length &&
  1984. !ioctx->queue_status_only) {
  1985. for (i = ioctx->n_rw_ctx - 1; i >= 0; i--) {
  1986. struct srpt_rw_ctx *ctx = &ioctx->rw_ctxs[i];
  1987. first_wr = rdma_rw_ctx_wrs(&ctx->rw, ch->qp,
  1988. ch->sport->port, NULL, first_wr);
  1989. }
  1990. }
  1991. if (state != SRPT_STATE_MGMT)
  1992. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->cmd.tag,
  1993. cmd->scsi_status);
  1994. else {
  1995. srp_tm_status
  1996. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  1997. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  1998. ioctx->cmd.tag);
  1999. }
  2000. atomic_inc(&ch->req_lim);
  2001. if (unlikely(atomic_sub_return(1 + ioctx->n_rdma,
  2002. &ch->sq_wr_avail) < 0)) {
  2003. pr_warn("%s: IB send queue full (needed %d)\n",
  2004. __func__, ioctx->n_rdma);
  2005. ret = -ENOMEM;
  2006. goto out;
  2007. }
  2008. ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, resp_len,
  2009. DMA_TO_DEVICE);
  2010. sge.addr = ioctx->ioctx.dma;
  2011. sge.length = resp_len;
  2012. sge.lkey = sdev->pd->local_dma_lkey;
  2013. ioctx->ioctx.cqe.done = srpt_send_done;
  2014. send_wr.next = NULL;
  2015. send_wr.wr_cqe = &ioctx->ioctx.cqe;
  2016. send_wr.sg_list = &sge;
  2017. send_wr.num_sge = 1;
  2018. send_wr.opcode = IB_WR_SEND;
  2019. send_wr.send_flags = IB_SEND_SIGNALED;
  2020. ret = ib_post_send(ch->qp, first_wr, &bad_wr);
  2021. if (ret < 0) {
  2022. pr_err("%s: sending cmd response failed for tag %llu (%d)\n",
  2023. __func__, ioctx->cmd.tag, ret);
  2024. goto out;
  2025. }
  2026. return;
  2027. out:
  2028. atomic_add(1 + ioctx->n_rdma, &ch->sq_wr_avail);
  2029. atomic_dec(&ch->req_lim);
  2030. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2031. target_put_sess_cmd(&ioctx->cmd);
  2032. }
  2033. static int srpt_queue_data_in(struct se_cmd *cmd)
  2034. {
  2035. srpt_queue_response(cmd);
  2036. return 0;
  2037. }
  2038. static void srpt_queue_tm_rsp(struct se_cmd *cmd)
  2039. {
  2040. srpt_queue_response(cmd);
  2041. }
  2042. static void srpt_aborted_task(struct se_cmd *cmd)
  2043. {
  2044. }
  2045. static int srpt_queue_status(struct se_cmd *cmd)
  2046. {
  2047. struct srpt_send_ioctx *ioctx;
  2048. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2049. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2050. if (cmd->se_cmd_flags &
  2051. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2052. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2053. ioctx->queue_status_only = true;
  2054. srpt_queue_response(cmd);
  2055. return 0;
  2056. }
  2057. static void srpt_refresh_port_work(struct work_struct *work)
  2058. {
  2059. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2060. srpt_refresh_port(sport);
  2061. }
  2062. /**
  2063. * srpt_release_sdev() - Free the channel resources associated with a target.
  2064. */
  2065. static int srpt_release_sdev(struct srpt_device *sdev)
  2066. {
  2067. int i, res;
  2068. WARN_ON_ONCE(irqs_disabled());
  2069. BUG_ON(!sdev);
  2070. mutex_lock(&sdev->mutex);
  2071. for (i = 0; i < ARRAY_SIZE(sdev->port); i++)
  2072. sdev->port[i].enabled = false;
  2073. __srpt_close_all_ch(sdev);
  2074. mutex_unlock(&sdev->mutex);
  2075. res = wait_event_interruptible(sdev->ch_releaseQ,
  2076. list_empty_careful(&sdev->rch_list));
  2077. if (res)
  2078. pr_err("%s: interrupted.\n", __func__);
  2079. return 0;
  2080. }
  2081. static struct srpt_port *__srpt_lookup_port(const char *name)
  2082. {
  2083. struct ib_device *dev;
  2084. struct srpt_device *sdev;
  2085. struct srpt_port *sport;
  2086. int i;
  2087. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2088. dev = sdev->device;
  2089. if (!dev)
  2090. continue;
  2091. for (i = 0; i < dev->phys_port_cnt; i++) {
  2092. sport = &sdev->port[i];
  2093. if (!strcmp(sport->port_guid, name))
  2094. return sport;
  2095. }
  2096. }
  2097. return NULL;
  2098. }
  2099. static struct srpt_port *srpt_lookup_port(const char *name)
  2100. {
  2101. struct srpt_port *sport;
  2102. spin_lock(&srpt_dev_lock);
  2103. sport = __srpt_lookup_port(name);
  2104. spin_unlock(&srpt_dev_lock);
  2105. return sport;
  2106. }
  2107. /**
  2108. * srpt_add_one() - Infiniband device addition callback function.
  2109. */
  2110. static void srpt_add_one(struct ib_device *device)
  2111. {
  2112. struct srpt_device *sdev;
  2113. struct srpt_port *sport;
  2114. struct ib_srq_init_attr srq_attr;
  2115. int i;
  2116. pr_debug("device = %p, device->dma_ops = %p\n", device,
  2117. device->dma_ops);
  2118. sdev = kzalloc(sizeof(*sdev), GFP_KERNEL);
  2119. if (!sdev)
  2120. goto err;
  2121. sdev->device = device;
  2122. INIT_LIST_HEAD(&sdev->rch_list);
  2123. init_waitqueue_head(&sdev->ch_releaseQ);
  2124. mutex_init(&sdev->mutex);
  2125. sdev->pd = ib_alloc_pd(device, 0);
  2126. if (IS_ERR(sdev->pd))
  2127. goto free_dev;
  2128. sdev->srq_size = min(srpt_srq_size, sdev->device->attrs.max_srq_wr);
  2129. srq_attr.event_handler = srpt_srq_event;
  2130. srq_attr.srq_context = (void *)sdev;
  2131. srq_attr.attr.max_wr = sdev->srq_size;
  2132. srq_attr.attr.max_sge = 1;
  2133. srq_attr.attr.srq_limit = 0;
  2134. srq_attr.srq_type = IB_SRQT_BASIC;
  2135. sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
  2136. if (IS_ERR(sdev->srq))
  2137. goto err_pd;
  2138. pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
  2139. __func__, sdev->srq_size, sdev->device->attrs.max_srq_wr,
  2140. device->name);
  2141. if (!srpt_service_guid)
  2142. srpt_service_guid = be64_to_cpu(device->node_guid);
  2143. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2144. if (IS_ERR(sdev->cm_id))
  2145. goto err_srq;
  2146. /* print out target login information */
  2147. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2148. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2149. srpt_service_guid, srpt_service_guid);
  2150. /*
  2151. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2152. * to identify this target. We currently use the guid of the first HCA
  2153. * in the system as service_id; therefore, the target_id will change
  2154. * if this HCA is gone bad and replaced by different HCA
  2155. */
  2156. if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0))
  2157. goto err_cm;
  2158. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2159. srpt_event_handler);
  2160. if (ib_register_event_handler(&sdev->event_handler))
  2161. goto err_cm;
  2162. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2163. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2164. sizeof(*sdev->ioctx_ring[0]),
  2165. srp_max_req_size, DMA_FROM_DEVICE);
  2166. if (!sdev->ioctx_ring)
  2167. goto err_event;
  2168. for (i = 0; i < sdev->srq_size; ++i)
  2169. srpt_post_recv(sdev, sdev->ioctx_ring[i]);
  2170. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2171. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2172. sport = &sdev->port[i - 1];
  2173. sport->sdev = sdev;
  2174. sport->port = i;
  2175. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2176. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2177. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2178. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2179. if (srpt_refresh_port(sport)) {
  2180. pr_err("MAD registration failed for %s-%d.\n",
  2181. sdev->device->name, i);
  2182. goto err_ring;
  2183. }
  2184. }
  2185. spin_lock(&srpt_dev_lock);
  2186. list_add_tail(&sdev->list, &srpt_dev_list);
  2187. spin_unlock(&srpt_dev_lock);
  2188. out:
  2189. ib_set_client_data(device, &srpt_client, sdev);
  2190. pr_debug("added %s.\n", device->name);
  2191. return;
  2192. err_ring:
  2193. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2194. sdev->srq_size, srp_max_req_size,
  2195. DMA_FROM_DEVICE);
  2196. err_event:
  2197. ib_unregister_event_handler(&sdev->event_handler);
  2198. err_cm:
  2199. ib_destroy_cm_id(sdev->cm_id);
  2200. err_srq:
  2201. ib_destroy_srq(sdev->srq);
  2202. err_pd:
  2203. ib_dealloc_pd(sdev->pd);
  2204. free_dev:
  2205. kfree(sdev);
  2206. err:
  2207. sdev = NULL;
  2208. pr_info("%s(%s) failed.\n", __func__, device->name);
  2209. goto out;
  2210. }
  2211. /**
  2212. * srpt_remove_one() - InfiniBand device removal callback function.
  2213. */
  2214. static void srpt_remove_one(struct ib_device *device, void *client_data)
  2215. {
  2216. struct srpt_device *sdev = client_data;
  2217. int i;
  2218. if (!sdev) {
  2219. pr_info("%s(%s): nothing to do.\n", __func__, device->name);
  2220. return;
  2221. }
  2222. srpt_unregister_mad_agent(sdev);
  2223. ib_unregister_event_handler(&sdev->event_handler);
  2224. /* Cancel any work queued by the just unregistered IB event handler. */
  2225. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2226. cancel_work_sync(&sdev->port[i].work);
  2227. ib_destroy_cm_id(sdev->cm_id);
  2228. /*
  2229. * Unregistering a target must happen after destroying sdev->cm_id
  2230. * such that no new SRP_LOGIN_REQ information units can arrive while
  2231. * destroying the target.
  2232. */
  2233. spin_lock(&srpt_dev_lock);
  2234. list_del(&sdev->list);
  2235. spin_unlock(&srpt_dev_lock);
  2236. srpt_release_sdev(sdev);
  2237. ib_destroy_srq(sdev->srq);
  2238. ib_dealloc_pd(sdev->pd);
  2239. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2240. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2241. sdev->ioctx_ring = NULL;
  2242. kfree(sdev);
  2243. }
  2244. static struct ib_client srpt_client = {
  2245. .name = DRV_NAME,
  2246. .add = srpt_add_one,
  2247. .remove = srpt_remove_one
  2248. };
  2249. static int srpt_check_true(struct se_portal_group *se_tpg)
  2250. {
  2251. return 1;
  2252. }
  2253. static int srpt_check_false(struct se_portal_group *se_tpg)
  2254. {
  2255. return 0;
  2256. }
  2257. static char *srpt_get_fabric_name(void)
  2258. {
  2259. return "srpt";
  2260. }
  2261. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2262. {
  2263. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  2264. return sport->port_guid;
  2265. }
  2266. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2267. {
  2268. return 1;
  2269. }
  2270. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2271. {
  2272. return 1;
  2273. }
  2274. static void srpt_release_cmd(struct se_cmd *se_cmd)
  2275. {
  2276. struct srpt_send_ioctx *ioctx = container_of(se_cmd,
  2277. struct srpt_send_ioctx, cmd);
  2278. struct srpt_rdma_ch *ch = ioctx->ch;
  2279. unsigned long flags;
  2280. WARN_ON_ONCE(ioctx->state != SRPT_STATE_DONE &&
  2281. !(ioctx->cmd.transport_state & CMD_T_ABORTED));
  2282. if (ioctx->n_rw_ctx) {
  2283. srpt_free_rw_ctxs(ch, ioctx);
  2284. ioctx->n_rw_ctx = 0;
  2285. }
  2286. spin_lock_irqsave(&ch->spinlock, flags);
  2287. list_add(&ioctx->free_list, &ch->free_list);
  2288. spin_unlock_irqrestore(&ch->spinlock, flags);
  2289. }
  2290. /**
  2291. * srpt_close_session() - Forcibly close a session.
  2292. *
  2293. * Callback function invoked by the TCM core to clean up sessions associated
  2294. * with a node ACL when the user invokes
  2295. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2296. */
  2297. static void srpt_close_session(struct se_session *se_sess)
  2298. {
  2299. DECLARE_COMPLETION_ONSTACK(release_done);
  2300. struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
  2301. struct srpt_device *sdev = ch->sport->sdev;
  2302. bool wait;
  2303. pr_debug("ch %s-%d state %d\n", ch->sess_name, ch->qp->qp_num,
  2304. ch->state);
  2305. mutex_lock(&sdev->mutex);
  2306. BUG_ON(ch->release_done);
  2307. ch->release_done = &release_done;
  2308. wait = !list_empty(&ch->list);
  2309. srpt_disconnect_ch(ch);
  2310. mutex_unlock(&sdev->mutex);
  2311. if (!wait)
  2312. return;
  2313. while (wait_for_completion_timeout(&release_done, 180 * HZ) == 0)
  2314. pr_info("%s(%s-%d state %d): still waiting ...\n", __func__,
  2315. ch->sess_name, ch->qp->qp_num, ch->state);
  2316. }
  2317. /**
  2318. * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
  2319. *
  2320. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  2321. * This object represents an arbitrary integer used to uniquely identify a
  2322. * particular attached remote initiator port to a particular SCSI target port
  2323. * within a particular SCSI target device within a particular SCSI instance.
  2324. */
  2325. static u32 srpt_sess_get_index(struct se_session *se_sess)
  2326. {
  2327. return 0;
  2328. }
  2329. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  2330. {
  2331. }
  2332. /* Note: only used from inside debug printk's by the TCM core. */
  2333. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  2334. {
  2335. struct srpt_send_ioctx *ioctx;
  2336. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2337. return srpt_get_cmd_state(ioctx);
  2338. }
  2339. /**
  2340. * srpt_parse_i_port_id() - Parse an initiator port ID.
  2341. * @name: ASCII representation of a 128-bit initiator port ID.
  2342. * @i_port_id: Binary 128-bit port ID.
  2343. */
  2344. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  2345. {
  2346. const char *p;
  2347. unsigned len, count, leading_zero_bytes;
  2348. int ret;
  2349. p = name;
  2350. if (strncasecmp(p, "0x", 2) == 0)
  2351. p += 2;
  2352. ret = -EINVAL;
  2353. len = strlen(p);
  2354. if (len % 2)
  2355. goto out;
  2356. count = min(len / 2, 16U);
  2357. leading_zero_bytes = 16 - count;
  2358. memset(i_port_id, 0, leading_zero_bytes);
  2359. ret = hex2bin(i_port_id + leading_zero_bytes, p, count);
  2360. if (ret < 0)
  2361. pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", ret);
  2362. out:
  2363. return ret;
  2364. }
  2365. /*
  2366. * configfs callback function invoked for
  2367. * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  2368. */
  2369. static int srpt_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
  2370. {
  2371. u8 i_port_id[16];
  2372. if (srpt_parse_i_port_id(i_port_id, name) < 0) {
  2373. pr_err("invalid initiator port ID %s\n", name);
  2374. return -EINVAL;
  2375. }
  2376. return 0;
  2377. }
  2378. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_show(struct config_item *item,
  2379. char *page)
  2380. {
  2381. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2382. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2383. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  2384. }
  2385. static ssize_t srpt_tpg_attrib_srp_max_rdma_size_store(struct config_item *item,
  2386. const char *page, size_t count)
  2387. {
  2388. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2389. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2390. unsigned long val;
  2391. int ret;
  2392. ret = kstrtoul(page, 0, &val);
  2393. if (ret < 0) {
  2394. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2395. return -EINVAL;
  2396. }
  2397. if (val > MAX_SRPT_RDMA_SIZE) {
  2398. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  2399. MAX_SRPT_RDMA_SIZE);
  2400. return -EINVAL;
  2401. }
  2402. if (val < DEFAULT_MAX_RDMA_SIZE) {
  2403. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  2404. val, DEFAULT_MAX_RDMA_SIZE);
  2405. return -EINVAL;
  2406. }
  2407. sport->port_attrib.srp_max_rdma_size = val;
  2408. return count;
  2409. }
  2410. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_show(struct config_item *item,
  2411. char *page)
  2412. {
  2413. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2414. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2415. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  2416. }
  2417. static ssize_t srpt_tpg_attrib_srp_max_rsp_size_store(struct config_item *item,
  2418. const char *page, size_t count)
  2419. {
  2420. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2421. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2422. unsigned long val;
  2423. int ret;
  2424. ret = kstrtoul(page, 0, &val);
  2425. if (ret < 0) {
  2426. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2427. return -EINVAL;
  2428. }
  2429. if (val > MAX_SRPT_RSP_SIZE) {
  2430. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  2431. MAX_SRPT_RSP_SIZE);
  2432. return -EINVAL;
  2433. }
  2434. if (val < MIN_MAX_RSP_SIZE) {
  2435. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  2436. MIN_MAX_RSP_SIZE);
  2437. return -EINVAL;
  2438. }
  2439. sport->port_attrib.srp_max_rsp_size = val;
  2440. return count;
  2441. }
  2442. static ssize_t srpt_tpg_attrib_srp_sq_size_show(struct config_item *item,
  2443. char *page)
  2444. {
  2445. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2446. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2447. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  2448. }
  2449. static ssize_t srpt_tpg_attrib_srp_sq_size_store(struct config_item *item,
  2450. const char *page, size_t count)
  2451. {
  2452. struct se_portal_group *se_tpg = attrib_to_tpg(item);
  2453. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2454. unsigned long val;
  2455. int ret;
  2456. ret = kstrtoul(page, 0, &val);
  2457. if (ret < 0) {
  2458. pr_err("kstrtoul() failed with ret: %d\n", ret);
  2459. return -EINVAL;
  2460. }
  2461. if (val > MAX_SRPT_SRQ_SIZE) {
  2462. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  2463. MAX_SRPT_SRQ_SIZE);
  2464. return -EINVAL;
  2465. }
  2466. if (val < MIN_SRPT_SRQ_SIZE) {
  2467. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  2468. MIN_SRPT_SRQ_SIZE);
  2469. return -EINVAL;
  2470. }
  2471. sport->port_attrib.srp_sq_size = val;
  2472. return count;
  2473. }
  2474. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rdma_size);
  2475. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_max_rsp_size);
  2476. CONFIGFS_ATTR(srpt_tpg_attrib_, srp_sq_size);
  2477. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  2478. &srpt_tpg_attrib_attr_srp_max_rdma_size,
  2479. &srpt_tpg_attrib_attr_srp_max_rsp_size,
  2480. &srpt_tpg_attrib_attr_srp_sq_size,
  2481. NULL,
  2482. };
  2483. static ssize_t srpt_tpg_enable_show(struct config_item *item, char *page)
  2484. {
  2485. struct se_portal_group *se_tpg = to_tpg(item);
  2486. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2487. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  2488. }
  2489. static ssize_t srpt_tpg_enable_store(struct config_item *item,
  2490. const char *page, size_t count)
  2491. {
  2492. struct se_portal_group *se_tpg = to_tpg(item);
  2493. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  2494. struct srpt_device *sdev = sport->sdev;
  2495. struct srpt_rdma_ch *ch;
  2496. unsigned long tmp;
  2497. int ret;
  2498. ret = kstrtoul(page, 0, &tmp);
  2499. if (ret < 0) {
  2500. pr_err("Unable to extract srpt_tpg_store_enable\n");
  2501. return -EINVAL;
  2502. }
  2503. if ((tmp != 0) && (tmp != 1)) {
  2504. pr_err("Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  2505. return -EINVAL;
  2506. }
  2507. if (sport->enabled == tmp)
  2508. goto out;
  2509. sport->enabled = tmp;
  2510. if (sport->enabled)
  2511. goto out;
  2512. mutex_lock(&sdev->mutex);
  2513. list_for_each_entry(ch, &sdev->rch_list, list) {
  2514. if (ch->sport == sport) {
  2515. pr_debug("%s: ch %p %s-%d\n", __func__, ch,
  2516. ch->sess_name, ch->qp->qp_num);
  2517. srpt_disconnect_ch(ch);
  2518. srpt_close_ch(ch);
  2519. }
  2520. }
  2521. mutex_unlock(&sdev->mutex);
  2522. out:
  2523. return count;
  2524. }
  2525. CONFIGFS_ATTR(srpt_tpg_, enable);
  2526. static struct configfs_attribute *srpt_tpg_attrs[] = {
  2527. &srpt_tpg_attr_enable,
  2528. NULL,
  2529. };
  2530. /**
  2531. * configfs callback invoked for
  2532. * mkdir /sys/kernel/config/target/$driver/$port/$tpg
  2533. */
  2534. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  2535. struct config_group *group,
  2536. const char *name)
  2537. {
  2538. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  2539. int res;
  2540. /* Initialize sport->port_wwn and sport->port_tpg_1 */
  2541. res = core_tpg_register(&sport->port_wwn, &sport->port_tpg_1, SCSI_PROTOCOL_SRP);
  2542. if (res)
  2543. return ERR_PTR(res);
  2544. return &sport->port_tpg_1;
  2545. }
  2546. /**
  2547. * configfs callback invoked for
  2548. * rmdir /sys/kernel/config/target/$driver/$port/$tpg
  2549. */
  2550. static void srpt_drop_tpg(struct se_portal_group *tpg)
  2551. {
  2552. struct srpt_port *sport = container_of(tpg,
  2553. struct srpt_port, port_tpg_1);
  2554. sport->enabled = false;
  2555. core_tpg_deregister(&sport->port_tpg_1);
  2556. }
  2557. /**
  2558. * configfs callback invoked for
  2559. * mkdir /sys/kernel/config/target/$driver/$port
  2560. */
  2561. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  2562. struct config_group *group,
  2563. const char *name)
  2564. {
  2565. struct srpt_port *sport;
  2566. int ret;
  2567. sport = srpt_lookup_port(name);
  2568. pr_debug("make_tport(%s)\n", name);
  2569. ret = -EINVAL;
  2570. if (!sport)
  2571. goto err;
  2572. return &sport->port_wwn;
  2573. err:
  2574. return ERR_PTR(ret);
  2575. }
  2576. /**
  2577. * configfs callback invoked for
  2578. * rmdir /sys/kernel/config/target/$driver/$port
  2579. */
  2580. static void srpt_drop_tport(struct se_wwn *wwn)
  2581. {
  2582. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  2583. pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
  2584. }
  2585. static ssize_t srpt_wwn_version_show(struct config_item *item, char *buf)
  2586. {
  2587. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  2588. }
  2589. CONFIGFS_ATTR_RO(srpt_wwn_, version);
  2590. static struct configfs_attribute *srpt_wwn_attrs[] = {
  2591. &srpt_wwn_attr_version,
  2592. NULL,
  2593. };
  2594. static const struct target_core_fabric_ops srpt_template = {
  2595. .module = THIS_MODULE,
  2596. .name = "srpt",
  2597. .get_fabric_name = srpt_get_fabric_name,
  2598. .tpg_get_wwn = srpt_get_fabric_wwn,
  2599. .tpg_get_tag = srpt_get_tag,
  2600. .tpg_check_demo_mode = srpt_check_false,
  2601. .tpg_check_demo_mode_cache = srpt_check_true,
  2602. .tpg_check_demo_mode_write_protect = srpt_check_true,
  2603. .tpg_check_prod_mode_write_protect = srpt_check_false,
  2604. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  2605. .release_cmd = srpt_release_cmd,
  2606. .check_stop_free = srpt_check_stop_free,
  2607. .close_session = srpt_close_session,
  2608. .sess_get_index = srpt_sess_get_index,
  2609. .sess_get_initiator_sid = NULL,
  2610. .write_pending = srpt_write_pending,
  2611. .write_pending_status = srpt_write_pending_status,
  2612. .set_default_node_attributes = srpt_set_default_node_attrs,
  2613. .get_cmd_state = srpt_get_tcm_cmd_state,
  2614. .queue_data_in = srpt_queue_data_in,
  2615. .queue_status = srpt_queue_status,
  2616. .queue_tm_rsp = srpt_queue_tm_rsp,
  2617. .aborted_task = srpt_aborted_task,
  2618. /*
  2619. * Setup function pointers for generic logic in
  2620. * target_core_fabric_configfs.c
  2621. */
  2622. .fabric_make_wwn = srpt_make_tport,
  2623. .fabric_drop_wwn = srpt_drop_tport,
  2624. .fabric_make_tpg = srpt_make_tpg,
  2625. .fabric_drop_tpg = srpt_drop_tpg,
  2626. .fabric_init_nodeacl = srpt_init_nodeacl,
  2627. .tfc_wwn_attrs = srpt_wwn_attrs,
  2628. .tfc_tpg_base_attrs = srpt_tpg_attrs,
  2629. .tfc_tpg_attrib_attrs = srpt_tpg_attrib_attrs,
  2630. };
  2631. /**
  2632. * srpt_init_module() - Kernel module initialization.
  2633. *
  2634. * Note: Since ib_register_client() registers callback functions, and since at
  2635. * least one of these callback functions (srpt_add_one()) calls target core
  2636. * functions, this driver must be registered with the target core before
  2637. * ib_register_client() is called.
  2638. */
  2639. static int __init srpt_init_module(void)
  2640. {
  2641. int ret;
  2642. ret = -EINVAL;
  2643. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  2644. pr_err("invalid value %d for kernel module parameter"
  2645. " srp_max_req_size -- must be at least %d.\n",
  2646. srp_max_req_size, MIN_MAX_REQ_SIZE);
  2647. goto out;
  2648. }
  2649. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  2650. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  2651. pr_err("invalid value %d for kernel module parameter"
  2652. " srpt_srq_size -- must be in the range [%d..%d].\n",
  2653. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  2654. goto out;
  2655. }
  2656. ret = target_register_template(&srpt_template);
  2657. if (ret)
  2658. goto out;
  2659. ret = ib_register_client(&srpt_client);
  2660. if (ret) {
  2661. pr_err("couldn't register IB client\n");
  2662. goto out_unregister_target;
  2663. }
  2664. return 0;
  2665. out_unregister_target:
  2666. target_unregister_template(&srpt_template);
  2667. out:
  2668. return ret;
  2669. }
  2670. static void __exit srpt_cleanup_module(void)
  2671. {
  2672. ib_unregister_client(&srpt_client);
  2673. target_unregister_template(&srpt_template);
  2674. }
  2675. module_init(srpt_init_module);
  2676. module_exit(srpt_cleanup_module);