ib_srpt.c 106 KB

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