ib_srpt.c 104 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051
  1. /*
  2. * Copyright (c) 2006 - 2009 Mellanox Technology Inc. All rights reserved.
  3. * Copyright (C) 2008 - 2011 Bart Van Assche <bvanassche@acm.org>.
  4. *
  5. * This software is available to you under a choice of one of two
  6. * licenses. You may choose to be licensed under the terms of the GNU
  7. * General Public License (GPL) Version 2, available from the file
  8. * COPYING in the main directory of this source tree, or the
  9. * OpenIB.org BSD license below:
  10. *
  11. * Redistribution and use in source and binary forms, with or
  12. * without modification, are permitted provided that the following
  13. * conditions are met:
  14. *
  15. * - Redistributions of source code must retain the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials
  22. * provided with the distribution.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  28. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  29. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  30. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  31. * SOFTWARE.
  32. *
  33. */
  34. #include <linux/module.h>
  35. #include <linux/init.h>
  36. #include <linux/slab.h>
  37. #include <linux/err.h>
  38. #include <linux/ctype.h>
  39. #include <linux/kthread.h>
  40. #include <linux/string.h>
  41. #include <linux/delay.h>
  42. #include <linux/atomic.h>
  43. #include <scsi/scsi_tcq.h>
  44. #include <target/configfs_macros.h>
  45. #include <target/target_core_base.h>
  46. #include <target/target_core_fabric_configfs.h>
  47. #include <target/target_core_fabric.h>
  48. #include <target/target_core_configfs.h>
  49. #include "ib_srpt.h"
  50. /* Name of this kernel module. */
  51. #define DRV_NAME "ib_srpt"
  52. #define DRV_VERSION "2.0.0"
  53. #define DRV_RELDATE "2011-02-14"
  54. #define SRPT_ID_STRING "Linux SRP target"
  55. #undef pr_fmt
  56. #define pr_fmt(fmt) DRV_NAME " " fmt
  57. MODULE_AUTHOR("Vu Pham and Bart Van Assche");
  58. MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol target "
  59. "v" DRV_VERSION " (" DRV_RELDATE ")");
  60. MODULE_LICENSE("Dual BSD/GPL");
  61. /*
  62. * Global Variables
  63. */
  64. static u64 srpt_service_guid;
  65. static DEFINE_SPINLOCK(srpt_dev_lock); /* Protects srpt_dev_list. */
  66. static LIST_HEAD(srpt_dev_list); /* List of srpt_device structures. */
  67. static unsigned srp_max_req_size = DEFAULT_MAX_REQ_SIZE;
  68. module_param(srp_max_req_size, int, 0444);
  69. MODULE_PARM_DESC(srp_max_req_size,
  70. "Maximum size of SRP request messages in bytes.");
  71. static int srpt_srq_size = DEFAULT_SRPT_SRQ_SIZE;
  72. module_param(srpt_srq_size, int, 0444);
  73. MODULE_PARM_DESC(srpt_srq_size,
  74. "Shared receive queue (SRQ) size.");
  75. static int srpt_get_u64_x(char *buffer, struct kernel_param *kp)
  76. {
  77. return sprintf(buffer, "0x%016llx", *(u64 *)kp->arg);
  78. }
  79. module_param_call(srpt_service_guid, NULL, srpt_get_u64_x, &srpt_service_guid,
  80. 0444);
  81. MODULE_PARM_DESC(srpt_service_guid,
  82. "Using this value for ioc_guid, id_ext, and cm_listen_id"
  83. " instead of using the node_guid of the first HCA.");
  84. static struct ib_client srpt_client;
  85. static struct target_fabric_configfs *srpt_target;
  86. static void srpt_release_channel(struct srpt_rdma_ch *ch);
  87. static int srpt_queue_status(struct se_cmd *cmd);
  88. /**
  89. * opposite_dma_dir() - Swap DMA_TO_DEVICE and DMA_FROM_DEVICE.
  90. */
  91. static inline
  92. enum dma_data_direction opposite_dma_dir(enum dma_data_direction dir)
  93. {
  94. switch (dir) {
  95. case DMA_TO_DEVICE: return DMA_FROM_DEVICE;
  96. case DMA_FROM_DEVICE: return DMA_TO_DEVICE;
  97. default: return dir;
  98. }
  99. }
  100. /**
  101. * srpt_sdev_name() - Return the name associated with the HCA.
  102. *
  103. * Examples are ib0, ib1, ...
  104. */
  105. static inline const char *srpt_sdev_name(struct srpt_device *sdev)
  106. {
  107. return sdev->device->name;
  108. }
  109. static enum rdma_ch_state srpt_get_ch_state(struct srpt_rdma_ch *ch)
  110. {
  111. unsigned long flags;
  112. enum rdma_ch_state state;
  113. spin_lock_irqsave(&ch->spinlock, flags);
  114. state = ch->state;
  115. spin_unlock_irqrestore(&ch->spinlock, flags);
  116. return state;
  117. }
  118. static enum rdma_ch_state
  119. srpt_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state new_state)
  120. {
  121. unsigned long flags;
  122. enum rdma_ch_state prev;
  123. spin_lock_irqsave(&ch->spinlock, flags);
  124. prev = ch->state;
  125. ch->state = new_state;
  126. spin_unlock_irqrestore(&ch->spinlock, flags);
  127. return prev;
  128. }
  129. /**
  130. * srpt_test_and_set_ch_state() - Test and set the channel state.
  131. *
  132. * Returns true if and only if the channel state has been set to the new state.
  133. */
  134. static bool
  135. srpt_test_and_set_ch_state(struct srpt_rdma_ch *ch, enum rdma_ch_state old,
  136. enum rdma_ch_state new)
  137. {
  138. unsigned long flags;
  139. enum rdma_ch_state prev;
  140. spin_lock_irqsave(&ch->spinlock, flags);
  141. prev = ch->state;
  142. if (prev == old)
  143. ch->state = new;
  144. spin_unlock_irqrestore(&ch->spinlock, flags);
  145. return prev == old;
  146. }
  147. /**
  148. * srpt_event_handler() - Asynchronous IB event callback function.
  149. *
  150. * Callback function called by the InfiniBand core when an asynchronous IB
  151. * event occurs. This callback may occur in interrupt context. See also
  152. * section 11.5.2, Set Asynchronous Event Handler in the InfiniBand
  153. * Architecture Specification.
  154. */
  155. static void srpt_event_handler(struct ib_event_handler *handler,
  156. struct ib_event *event)
  157. {
  158. struct srpt_device *sdev;
  159. struct srpt_port *sport;
  160. sdev = ib_get_client_data(event->device, &srpt_client);
  161. if (!sdev || sdev->device != event->device)
  162. return;
  163. pr_debug("ASYNC event= %d on device= %s\n", event->event,
  164. srpt_sdev_name(sdev));
  165. switch (event->event) {
  166. case IB_EVENT_PORT_ERR:
  167. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  168. sport = &sdev->port[event->element.port_num - 1];
  169. sport->lid = 0;
  170. sport->sm_lid = 0;
  171. }
  172. break;
  173. case IB_EVENT_PORT_ACTIVE:
  174. case IB_EVENT_LID_CHANGE:
  175. case IB_EVENT_PKEY_CHANGE:
  176. case IB_EVENT_SM_CHANGE:
  177. case IB_EVENT_CLIENT_REREGISTER:
  178. case IB_EVENT_GID_CHANGE:
  179. /* Refresh port data asynchronously. */
  180. if (event->element.port_num <= sdev->device->phys_port_cnt) {
  181. sport = &sdev->port[event->element.port_num - 1];
  182. if (!sport->lid && !sport->sm_lid)
  183. schedule_work(&sport->work);
  184. }
  185. break;
  186. default:
  187. printk(KERN_ERR "received unrecognized IB event %d\n",
  188. event->event);
  189. break;
  190. }
  191. }
  192. /**
  193. * srpt_srq_event() - SRQ event callback function.
  194. */
  195. static void srpt_srq_event(struct ib_event *event, void *ctx)
  196. {
  197. printk(KERN_INFO "SRQ event %d\n", event->event);
  198. }
  199. /**
  200. * srpt_qp_event() - QP event callback function.
  201. */
  202. static void srpt_qp_event(struct ib_event *event, struct srpt_rdma_ch *ch)
  203. {
  204. pr_debug("QP event %d on cm_id=%p sess_name=%s state=%d\n",
  205. event->event, ch->cm_id, ch->sess_name, srpt_get_ch_state(ch));
  206. switch (event->event) {
  207. case IB_EVENT_COMM_EST:
  208. ib_cm_notify(ch->cm_id, event->event);
  209. break;
  210. case IB_EVENT_QP_LAST_WQE_REACHED:
  211. if (srpt_test_and_set_ch_state(ch, CH_DRAINING,
  212. CH_RELEASING))
  213. srpt_release_channel(ch);
  214. else
  215. pr_debug("%s: state %d - ignored LAST_WQE.\n",
  216. ch->sess_name, srpt_get_ch_state(ch));
  217. break;
  218. default:
  219. printk(KERN_ERR "received unrecognized IB QP event %d\n",
  220. event->event);
  221. break;
  222. }
  223. }
  224. /**
  225. * srpt_set_ioc() - Helper function for initializing an IOUnitInfo structure.
  226. *
  227. * @slot: one-based slot number.
  228. * @value: four-bit value.
  229. *
  230. * Copies the lowest four bits of value in element slot of the array of four
  231. * bit elements called c_list (controller list). The index slot is one-based.
  232. */
  233. static void srpt_set_ioc(u8 *c_list, u32 slot, u8 value)
  234. {
  235. u16 id;
  236. u8 tmp;
  237. id = (slot - 1) / 2;
  238. if (slot & 0x1) {
  239. tmp = c_list[id] & 0xf;
  240. c_list[id] = (value << 4) | tmp;
  241. } else {
  242. tmp = c_list[id] & 0xf0;
  243. c_list[id] = (value & 0xf) | tmp;
  244. }
  245. }
  246. /**
  247. * srpt_get_class_port_info() - Copy ClassPortInfo to a management datagram.
  248. *
  249. * See also section 16.3.3.1 ClassPortInfo in the InfiniBand Architecture
  250. * Specification.
  251. */
  252. static void srpt_get_class_port_info(struct ib_dm_mad *mad)
  253. {
  254. struct ib_class_port_info *cif;
  255. cif = (struct ib_class_port_info *)mad->data;
  256. memset(cif, 0, sizeof *cif);
  257. cif->base_version = 1;
  258. cif->class_version = 1;
  259. cif->resp_time_value = 20;
  260. mad->mad_hdr.status = 0;
  261. }
  262. /**
  263. * srpt_get_iou() - Write IOUnitInfo to a management datagram.
  264. *
  265. * See also section 16.3.3.3 IOUnitInfo in the InfiniBand Architecture
  266. * Specification. See also section B.7, table B.6 in the SRP r16a document.
  267. */
  268. static void srpt_get_iou(struct ib_dm_mad *mad)
  269. {
  270. struct ib_dm_iou_info *ioui;
  271. u8 slot;
  272. int i;
  273. ioui = (struct ib_dm_iou_info *)mad->data;
  274. ioui->change_id = __constant_cpu_to_be16(1);
  275. ioui->max_controllers = 16;
  276. /* set present for slot 1 and empty for the rest */
  277. srpt_set_ioc(ioui->controller_list, 1, 1);
  278. for (i = 1, slot = 2; i < 16; i++, slot++)
  279. srpt_set_ioc(ioui->controller_list, slot, 0);
  280. mad->mad_hdr.status = 0;
  281. }
  282. /**
  283. * srpt_get_ioc() - Write IOControllerprofile to a management datagram.
  284. *
  285. * See also section 16.3.3.4 IOControllerProfile in the InfiniBand
  286. * Architecture Specification. See also section B.7, table B.7 in the SRP
  287. * r16a document.
  288. */
  289. static void srpt_get_ioc(struct srpt_port *sport, u32 slot,
  290. struct ib_dm_mad *mad)
  291. {
  292. struct srpt_device *sdev = sport->sdev;
  293. struct ib_dm_ioc_profile *iocp;
  294. iocp = (struct ib_dm_ioc_profile *)mad->data;
  295. if (!slot || slot > 16) {
  296. mad->mad_hdr.status
  297. = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  298. return;
  299. }
  300. if (slot > 2) {
  301. mad->mad_hdr.status
  302. = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  303. return;
  304. }
  305. memset(iocp, 0, sizeof *iocp);
  306. strcpy(iocp->id_string, SRPT_ID_STRING);
  307. iocp->guid = cpu_to_be64(srpt_service_guid);
  308. iocp->vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
  309. iocp->device_id = cpu_to_be32(sdev->dev_attr.vendor_part_id);
  310. iocp->device_version = cpu_to_be16(sdev->dev_attr.hw_ver);
  311. iocp->subsys_vendor_id = cpu_to_be32(sdev->dev_attr.vendor_id);
  312. iocp->subsys_device_id = 0x0;
  313. iocp->io_class = __constant_cpu_to_be16(SRP_REV16A_IB_IO_CLASS);
  314. iocp->io_subclass = __constant_cpu_to_be16(SRP_IO_SUBCLASS);
  315. iocp->protocol = __constant_cpu_to_be16(SRP_PROTOCOL);
  316. iocp->protocol_version = __constant_cpu_to_be16(SRP_PROTOCOL_VERSION);
  317. iocp->send_queue_depth = cpu_to_be16(sdev->srq_size);
  318. iocp->rdma_read_depth = 4;
  319. iocp->send_size = cpu_to_be32(srp_max_req_size);
  320. iocp->rdma_size = cpu_to_be32(min(sport->port_attrib.srp_max_rdma_size,
  321. 1U << 24));
  322. iocp->num_svc_entries = 1;
  323. iocp->op_cap_mask = SRP_SEND_TO_IOC | SRP_SEND_FROM_IOC |
  324. SRP_RDMA_READ_FROM_IOC | SRP_RDMA_WRITE_FROM_IOC;
  325. mad->mad_hdr.status = 0;
  326. }
  327. /**
  328. * srpt_get_svc_entries() - Write ServiceEntries to a management datagram.
  329. *
  330. * See also section 16.3.3.5 ServiceEntries in the InfiniBand Architecture
  331. * Specification. See also section B.7, table B.8 in the SRP r16a document.
  332. */
  333. static void srpt_get_svc_entries(u64 ioc_guid,
  334. u16 slot, u8 hi, u8 lo, struct ib_dm_mad *mad)
  335. {
  336. struct ib_dm_svc_entries *svc_entries;
  337. WARN_ON(!ioc_guid);
  338. if (!slot || slot > 16) {
  339. mad->mad_hdr.status
  340. = __constant_cpu_to_be16(DM_MAD_STATUS_INVALID_FIELD);
  341. return;
  342. }
  343. if (slot > 2 || lo > hi || hi > 1) {
  344. mad->mad_hdr.status
  345. = __constant_cpu_to_be16(DM_MAD_STATUS_NO_IOC);
  346. return;
  347. }
  348. svc_entries = (struct ib_dm_svc_entries *)mad->data;
  349. memset(svc_entries, 0, sizeof *svc_entries);
  350. svc_entries->service_entries[0].id = cpu_to_be64(ioc_guid);
  351. snprintf(svc_entries->service_entries[0].name,
  352. sizeof(svc_entries->service_entries[0].name),
  353. "%s%016llx",
  354. SRP_SERVICE_NAME_PREFIX,
  355. ioc_guid);
  356. mad->mad_hdr.status = 0;
  357. }
  358. /**
  359. * srpt_mgmt_method_get() - Process a received management datagram.
  360. * @sp: source port through which the MAD has been received.
  361. * @rq_mad: received MAD.
  362. * @rsp_mad: response MAD.
  363. */
  364. static void srpt_mgmt_method_get(struct srpt_port *sp, struct ib_mad *rq_mad,
  365. struct ib_dm_mad *rsp_mad)
  366. {
  367. u16 attr_id;
  368. u32 slot;
  369. u8 hi, lo;
  370. attr_id = be16_to_cpu(rq_mad->mad_hdr.attr_id);
  371. switch (attr_id) {
  372. case DM_ATTR_CLASS_PORT_INFO:
  373. srpt_get_class_port_info(rsp_mad);
  374. break;
  375. case DM_ATTR_IOU_INFO:
  376. srpt_get_iou(rsp_mad);
  377. break;
  378. case DM_ATTR_IOC_PROFILE:
  379. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  380. srpt_get_ioc(sp, slot, rsp_mad);
  381. break;
  382. case DM_ATTR_SVC_ENTRIES:
  383. slot = be32_to_cpu(rq_mad->mad_hdr.attr_mod);
  384. hi = (u8) ((slot >> 8) & 0xff);
  385. lo = (u8) (slot & 0xff);
  386. slot = (u16) ((slot >> 16) & 0xffff);
  387. srpt_get_svc_entries(srpt_service_guid,
  388. slot, hi, lo, rsp_mad);
  389. break;
  390. default:
  391. rsp_mad->mad_hdr.status =
  392. __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  393. break;
  394. }
  395. }
  396. /**
  397. * srpt_mad_send_handler() - Post MAD-send callback function.
  398. */
  399. static void srpt_mad_send_handler(struct ib_mad_agent *mad_agent,
  400. struct ib_mad_send_wc *mad_wc)
  401. {
  402. ib_destroy_ah(mad_wc->send_buf->ah);
  403. ib_free_send_mad(mad_wc->send_buf);
  404. }
  405. /**
  406. * srpt_mad_recv_handler() - MAD reception callback function.
  407. */
  408. static void srpt_mad_recv_handler(struct ib_mad_agent *mad_agent,
  409. struct ib_mad_recv_wc *mad_wc)
  410. {
  411. struct srpt_port *sport = (struct srpt_port *)mad_agent->context;
  412. struct ib_ah *ah;
  413. struct ib_mad_send_buf *rsp;
  414. struct ib_dm_mad *dm_mad;
  415. if (!mad_wc || !mad_wc->recv_buf.mad)
  416. return;
  417. ah = ib_create_ah_from_wc(mad_agent->qp->pd, mad_wc->wc,
  418. mad_wc->recv_buf.grh, mad_agent->port_num);
  419. if (IS_ERR(ah))
  420. goto err;
  421. BUILD_BUG_ON(offsetof(struct ib_dm_mad, data) != IB_MGMT_DEVICE_HDR);
  422. rsp = ib_create_send_mad(mad_agent, mad_wc->wc->src_qp,
  423. mad_wc->wc->pkey_index, 0,
  424. IB_MGMT_DEVICE_HDR, IB_MGMT_DEVICE_DATA,
  425. GFP_KERNEL);
  426. if (IS_ERR(rsp))
  427. goto err_rsp;
  428. rsp->ah = ah;
  429. dm_mad = rsp->mad;
  430. memcpy(dm_mad, mad_wc->recv_buf.mad, sizeof *dm_mad);
  431. dm_mad->mad_hdr.method = IB_MGMT_METHOD_GET_RESP;
  432. dm_mad->mad_hdr.status = 0;
  433. switch (mad_wc->recv_buf.mad->mad_hdr.method) {
  434. case IB_MGMT_METHOD_GET:
  435. srpt_mgmt_method_get(sport, mad_wc->recv_buf.mad, dm_mad);
  436. break;
  437. case IB_MGMT_METHOD_SET:
  438. dm_mad->mad_hdr.status =
  439. __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD_ATTR);
  440. break;
  441. default:
  442. dm_mad->mad_hdr.status =
  443. __constant_cpu_to_be16(DM_MAD_STATUS_UNSUP_METHOD);
  444. break;
  445. }
  446. if (!ib_post_send_mad(rsp, NULL)) {
  447. ib_free_recv_mad(mad_wc);
  448. /* will destroy_ah & free_send_mad in send completion */
  449. return;
  450. }
  451. ib_free_send_mad(rsp);
  452. err_rsp:
  453. ib_destroy_ah(ah);
  454. err:
  455. ib_free_recv_mad(mad_wc);
  456. }
  457. /**
  458. * srpt_refresh_port() - Configure a HCA port.
  459. *
  460. * Enable InfiniBand management datagram processing, update the cached sm_lid,
  461. * lid and gid values, and register a callback function for processing MADs
  462. * on the specified port.
  463. *
  464. * Note: It is safe to call this function more than once for the same port.
  465. */
  466. static int srpt_refresh_port(struct srpt_port *sport)
  467. {
  468. struct ib_mad_reg_req reg_req;
  469. struct ib_port_modify port_modify;
  470. struct ib_port_attr port_attr;
  471. int ret;
  472. memset(&port_modify, 0, sizeof port_modify);
  473. port_modify.set_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  474. port_modify.clr_port_cap_mask = 0;
  475. ret = ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  476. if (ret)
  477. goto err_mod_port;
  478. ret = ib_query_port(sport->sdev->device, sport->port, &port_attr);
  479. if (ret)
  480. goto err_query_port;
  481. sport->sm_lid = port_attr.sm_lid;
  482. sport->lid = port_attr.lid;
  483. ret = ib_query_gid(sport->sdev->device, sport->port, 0, &sport->gid);
  484. if (ret)
  485. goto err_query_port;
  486. if (!sport->mad_agent) {
  487. memset(&reg_req, 0, sizeof reg_req);
  488. reg_req.mgmt_class = IB_MGMT_CLASS_DEVICE_MGMT;
  489. reg_req.mgmt_class_version = IB_MGMT_BASE_VERSION;
  490. set_bit(IB_MGMT_METHOD_GET, reg_req.method_mask);
  491. set_bit(IB_MGMT_METHOD_SET, reg_req.method_mask);
  492. sport->mad_agent = ib_register_mad_agent(sport->sdev->device,
  493. sport->port,
  494. IB_QPT_GSI,
  495. &reg_req, 0,
  496. srpt_mad_send_handler,
  497. srpt_mad_recv_handler,
  498. sport, 0);
  499. if (IS_ERR(sport->mad_agent)) {
  500. ret = PTR_ERR(sport->mad_agent);
  501. sport->mad_agent = NULL;
  502. goto err_query_port;
  503. }
  504. }
  505. return 0;
  506. err_query_port:
  507. port_modify.set_port_cap_mask = 0;
  508. port_modify.clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP;
  509. ib_modify_port(sport->sdev->device, sport->port, 0, &port_modify);
  510. err_mod_port:
  511. return ret;
  512. }
  513. /**
  514. * srpt_unregister_mad_agent() - Unregister MAD callback functions.
  515. *
  516. * Note: It is safe to call this function more than once for the same device.
  517. */
  518. static void srpt_unregister_mad_agent(struct srpt_device *sdev)
  519. {
  520. struct ib_port_modify port_modify = {
  521. .clr_port_cap_mask = IB_PORT_DEVICE_MGMT_SUP,
  522. };
  523. struct srpt_port *sport;
  524. int i;
  525. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  526. sport = &sdev->port[i - 1];
  527. WARN_ON(sport->port != i);
  528. if (ib_modify_port(sdev->device, i, 0, &port_modify) < 0)
  529. printk(KERN_ERR "disabling MAD processing failed.\n");
  530. if (sport->mad_agent) {
  531. ib_unregister_mad_agent(sport->mad_agent);
  532. sport->mad_agent = NULL;
  533. }
  534. }
  535. }
  536. /**
  537. * srpt_alloc_ioctx() - Allocate an SRPT I/O context structure.
  538. */
  539. static struct srpt_ioctx *srpt_alloc_ioctx(struct srpt_device *sdev,
  540. int ioctx_size, int dma_size,
  541. enum dma_data_direction dir)
  542. {
  543. struct srpt_ioctx *ioctx;
  544. ioctx = kmalloc(ioctx_size, GFP_KERNEL);
  545. if (!ioctx)
  546. goto err;
  547. ioctx->buf = kmalloc(dma_size, GFP_KERNEL);
  548. if (!ioctx->buf)
  549. goto err_free_ioctx;
  550. ioctx->dma = ib_dma_map_single(sdev->device, ioctx->buf, dma_size, dir);
  551. if (ib_dma_mapping_error(sdev->device, ioctx->dma))
  552. goto err_free_buf;
  553. return ioctx;
  554. err_free_buf:
  555. kfree(ioctx->buf);
  556. err_free_ioctx:
  557. kfree(ioctx);
  558. err:
  559. return NULL;
  560. }
  561. /**
  562. * srpt_free_ioctx() - Free an SRPT I/O context structure.
  563. */
  564. static void srpt_free_ioctx(struct srpt_device *sdev, struct srpt_ioctx *ioctx,
  565. int dma_size, enum dma_data_direction dir)
  566. {
  567. if (!ioctx)
  568. return;
  569. ib_dma_unmap_single(sdev->device, ioctx->dma, dma_size, dir);
  570. kfree(ioctx->buf);
  571. kfree(ioctx);
  572. }
  573. /**
  574. * srpt_alloc_ioctx_ring() - Allocate a ring of SRPT I/O context structures.
  575. * @sdev: Device to allocate the I/O context ring for.
  576. * @ring_size: Number of elements in the I/O context ring.
  577. * @ioctx_size: I/O context size.
  578. * @dma_size: DMA buffer size.
  579. * @dir: DMA data direction.
  580. */
  581. static struct srpt_ioctx **srpt_alloc_ioctx_ring(struct srpt_device *sdev,
  582. int ring_size, int ioctx_size,
  583. int dma_size, enum dma_data_direction dir)
  584. {
  585. struct srpt_ioctx **ring;
  586. int i;
  587. WARN_ON(ioctx_size != sizeof(struct srpt_recv_ioctx)
  588. && ioctx_size != sizeof(struct srpt_send_ioctx));
  589. ring = kmalloc(ring_size * sizeof(ring[0]), GFP_KERNEL);
  590. if (!ring)
  591. goto out;
  592. for (i = 0; i < ring_size; ++i) {
  593. ring[i] = srpt_alloc_ioctx(sdev, ioctx_size, dma_size, dir);
  594. if (!ring[i])
  595. goto err;
  596. ring[i]->index = i;
  597. }
  598. goto out;
  599. err:
  600. while (--i >= 0)
  601. srpt_free_ioctx(sdev, ring[i], dma_size, dir);
  602. kfree(ring);
  603. ring = NULL;
  604. out:
  605. return ring;
  606. }
  607. /**
  608. * srpt_free_ioctx_ring() - Free the ring of SRPT I/O context structures.
  609. */
  610. static void srpt_free_ioctx_ring(struct srpt_ioctx **ioctx_ring,
  611. struct srpt_device *sdev, int ring_size,
  612. int dma_size, enum dma_data_direction dir)
  613. {
  614. int i;
  615. for (i = 0; i < ring_size; ++i)
  616. srpt_free_ioctx(sdev, ioctx_ring[i], dma_size, dir);
  617. kfree(ioctx_ring);
  618. }
  619. /**
  620. * srpt_get_cmd_state() - Get the state of a SCSI command.
  621. */
  622. static enum srpt_command_state srpt_get_cmd_state(struct srpt_send_ioctx *ioctx)
  623. {
  624. enum srpt_command_state state;
  625. unsigned long flags;
  626. BUG_ON(!ioctx);
  627. spin_lock_irqsave(&ioctx->spinlock, flags);
  628. state = ioctx->state;
  629. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  630. return state;
  631. }
  632. /**
  633. * srpt_set_cmd_state() - Set the state of a SCSI command.
  634. *
  635. * Does not modify the state of aborted commands. Returns the previous command
  636. * state.
  637. */
  638. static enum srpt_command_state srpt_set_cmd_state(struct srpt_send_ioctx *ioctx,
  639. enum srpt_command_state new)
  640. {
  641. enum srpt_command_state previous;
  642. unsigned long flags;
  643. BUG_ON(!ioctx);
  644. spin_lock_irqsave(&ioctx->spinlock, flags);
  645. previous = ioctx->state;
  646. if (previous != SRPT_STATE_DONE)
  647. ioctx->state = new;
  648. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  649. return previous;
  650. }
  651. /**
  652. * srpt_test_and_set_cmd_state() - Test and set the state of a command.
  653. *
  654. * Returns true if and only if the previous command state was equal to 'old'.
  655. */
  656. static bool srpt_test_and_set_cmd_state(struct srpt_send_ioctx *ioctx,
  657. enum srpt_command_state old,
  658. enum srpt_command_state new)
  659. {
  660. enum srpt_command_state previous;
  661. unsigned long flags;
  662. WARN_ON(!ioctx);
  663. WARN_ON(old == SRPT_STATE_DONE);
  664. WARN_ON(new == SRPT_STATE_NEW);
  665. spin_lock_irqsave(&ioctx->spinlock, flags);
  666. previous = ioctx->state;
  667. if (previous == old)
  668. ioctx->state = new;
  669. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  670. return previous == old;
  671. }
  672. /**
  673. * srpt_post_recv() - Post an IB receive request.
  674. */
  675. static int srpt_post_recv(struct srpt_device *sdev,
  676. struct srpt_recv_ioctx *ioctx)
  677. {
  678. struct ib_sge list;
  679. struct ib_recv_wr wr, *bad_wr;
  680. BUG_ON(!sdev);
  681. wr.wr_id = encode_wr_id(SRPT_RECV, ioctx->ioctx.index);
  682. list.addr = ioctx->ioctx.dma;
  683. list.length = srp_max_req_size;
  684. list.lkey = sdev->mr->lkey;
  685. wr.next = NULL;
  686. wr.sg_list = &list;
  687. wr.num_sge = 1;
  688. return ib_post_srq_recv(sdev->srq, &wr, &bad_wr);
  689. }
  690. /**
  691. * srpt_post_send() - Post an IB send request.
  692. *
  693. * Returns zero upon success and a non-zero value upon failure.
  694. */
  695. static int srpt_post_send(struct srpt_rdma_ch *ch,
  696. struct srpt_send_ioctx *ioctx, int len)
  697. {
  698. struct ib_sge list;
  699. struct ib_send_wr wr, *bad_wr;
  700. struct srpt_device *sdev = ch->sport->sdev;
  701. int ret;
  702. atomic_inc(&ch->req_lim);
  703. ret = -ENOMEM;
  704. if (unlikely(atomic_dec_return(&ch->sq_wr_avail) < 0)) {
  705. printk(KERN_WARNING "IB send queue full (needed 1)\n");
  706. goto out;
  707. }
  708. ib_dma_sync_single_for_device(sdev->device, ioctx->ioctx.dma, len,
  709. DMA_TO_DEVICE);
  710. list.addr = ioctx->ioctx.dma;
  711. list.length = len;
  712. list.lkey = sdev->mr->lkey;
  713. wr.next = NULL;
  714. wr.wr_id = encode_wr_id(SRPT_SEND, ioctx->ioctx.index);
  715. wr.sg_list = &list;
  716. wr.num_sge = 1;
  717. wr.opcode = IB_WR_SEND;
  718. wr.send_flags = IB_SEND_SIGNALED;
  719. ret = ib_post_send(ch->qp, &wr, &bad_wr);
  720. out:
  721. if (ret < 0) {
  722. atomic_inc(&ch->sq_wr_avail);
  723. atomic_dec(&ch->req_lim);
  724. }
  725. return ret;
  726. }
  727. /**
  728. * srpt_get_desc_tbl() - Parse the data descriptors of an SRP_CMD request.
  729. * @ioctx: Pointer to the I/O context associated with the request.
  730. * @srp_cmd: Pointer to the SRP_CMD request data.
  731. * @dir: Pointer to the variable to which the transfer direction will be
  732. * written.
  733. * @data_len: Pointer to the variable to which the total data length of all
  734. * descriptors in the SRP_CMD request will be written.
  735. *
  736. * This function initializes ioctx->nrbuf and ioctx->r_bufs.
  737. *
  738. * Returns -EINVAL when the SRP_CMD request contains inconsistent descriptors;
  739. * -ENOMEM when memory allocation fails and zero upon success.
  740. */
  741. static int srpt_get_desc_tbl(struct srpt_send_ioctx *ioctx,
  742. struct srp_cmd *srp_cmd,
  743. enum dma_data_direction *dir, u64 *data_len)
  744. {
  745. struct srp_indirect_buf *idb;
  746. struct srp_direct_buf *db;
  747. unsigned add_cdb_offset;
  748. int ret;
  749. /*
  750. * The pointer computations below will only be compiled correctly
  751. * if srp_cmd::add_data is declared as s8*, u8*, s8[] or u8[], so check
  752. * whether srp_cmd::add_data has been declared as a byte pointer.
  753. */
  754. BUILD_BUG_ON(!__same_type(srp_cmd->add_data[0], (s8)0)
  755. && !__same_type(srp_cmd->add_data[0], (u8)0));
  756. BUG_ON(!dir);
  757. BUG_ON(!data_len);
  758. ret = 0;
  759. *data_len = 0;
  760. /*
  761. * The lower four bits of the buffer format field contain the DATA-IN
  762. * buffer descriptor format, and the highest four bits contain the
  763. * DATA-OUT buffer descriptor format.
  764. */
  765. *dir = DMA_NONE;
  766. if (srp_cmd->buf_fmt & 0xf)
  767. /* DATA-IN: transfer data from target to initiator (read). */
  768. *dir = DMA_FROM_DEVICE;
  769. else if (srp_cmd->buf_fmt >> 4)
  770. /* DATA-OUT: transfer data from initiator to target (write). */
  771. *dir = DMA_TO_DEVICE;
  772. /*
  773. * According to the SRP spec, the lower two bits of the 'ADDITIONAL
  774. * CDB LENGTH' field are reserved and the size in bytes of this field
  775. * is four times the value specified in bits 3..7. Hence the "& ~3".
  776. */
  777. add_cdb_offset = srp_cmd->add_cdb_len & ~3;
  778. if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_DIRECT) ||
  779. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_DIRECT)) {
  780. ioctx->n_rbuf = 1;
  781. ioctx->rbufs = &ioctx->single_rbuf;
  782. db = (struct srp_direct_buf *)(srp_cmd->add_data
  783. + add_cdb_offset);
  784. memcpy(ioctx->rbufs, db, sizeof *db);
  785. *data_len = be32_to_cpu(db->len);
  786. } else if (((srp_cmd->buf_fmt & 0xf) == SRP_DATA_DESC_INDIRECT) ||
  787. ((srp_cmd->buf_fmt >> 4) == SRP_DATA_DESC_INDIRECT)) {
  788. idb = (struct srp_indirect_buf *)(srp_cmd->add_data
  789. + add_cdb_offset);
  790. ioctx->n_rbuf = be32_to_cpu(idb->table_desc.len) / sizeof *db;
  791. if (ioctx->n_rbuf >
  792. (srp_cmd->data_out_desc_cnt + srp_cmd->data_in_desc_cnt)) {
  793. printk(KERN_ERR "received unsupported SRP_CMD request"
  794. " type (%u out + %u in != %u / %zu)\n",
  795. srp_cmd->data_out_desc_cnt,
  796. srp_cmd->data_in_desc_cnt,
  797. be32_to_cpu(idb->table_desc.len),
  798. sizeof(*db));
  799. ioctx->n_rbuf = 0;
  800. ret = -EINVAL;
  801. goto out;
  802. }
  803. if (ioctx->n_rbuf == 1)
  804. ioctx->rbufs = &ioctx->single_rbuf;
  805. else {
  806. ioctx->rbufs =
  807. kmalloc(ioctx->n_rbuf * sizeof *db, GFP_ATOMIC);
  808. if (!ioctx->rbufs) {
  809. ioctx->n_rbuf = 0;
  810. ret = -ENOMEM;
  811. goto out;
  812. }
  813. }
  814. db = idb->desc_list;
  815. memcpy(ioctx->rbufs, db, ioctx->n_rbuf * sizeof *db);
  816. *data_len = be32_to_cpu(idb->len);
  817. }
  818. out:
  819. return ret;
  820. }
  821. /**
  822. * srpt_init_ch_qp() - Initialize queue pair attributes.
  823. *
  824. * Initialized the attributes of queue pair 'qp' by allowing local write,
  825. * remote read and remote write. Also transitions 'qp' to state IB_QPS_INIT.
  826. */
  827. static int srpt_init_ch_qp(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  828. {
  829. struct ib_qp_attr *attr;
  830. int ret;
  831. attr = kzalloc(sizeof *attr, GFP_KERNEL);
  832. if (!attr)
  833. return -ENOMEM;
  834. attr->qp_state = IB_QPS_INIT;
  835. attr->qp_access_flags = IB_ACCESS_LOCAL_WRITE | IB_ACCESS_REMOTE_READ |
  836. IB_ACCESS_REMOTE_WRITE;
  837. attr->port_num = ch->sport->port;
  838. attr->pkey_index = 0;
  839. ret = ib_modify_qp(qp, attr,
  840. IB_QP_STATE | IB_QP_ACCESS_FLAGS | IB_QP_PORT |
  841. IB_QP_PKEY_INDEX);
  842. kfree(attr);
  843. return ret;
  844. }
  845. /**
  846. * srpt_ch_qp_rtr() - Change the state of a channel to 'ready to receive' (RTR).
  847. * @ch: channel of the queue pair.
  848. * @qp: queue pair to change the state of.
  849. *
  850. * Returns zero upon success and a negative value upon failure.
  851. *
  852. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  853. * If this structure ever becomes larger, it might be necessary to allocate
  854. * it dynamically instead of on the stack.
  855. */
  856. static int srpt_ch_qp_rtr(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  857. {
  858. struct ib_qp_attr qp_attr;
  859. int attr_mask;
  860. int ret;
  861. qp_attr.qp_state = IB_QPS_RTR;
  862. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  863. if (ret)
  864. goto out;
  865. qp_attr.max_dest_rd_atomic = 4;
  866. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  867. out:
  868. return ret;
  869. }
  870. /**
  871. * srpt_ch_qp_rts() - Change the state of a channel to 'ready to send' (RTS).
  872. * @ch: channel of the queue pair.
  873. * @qp: queue pair to change the state of.
  874. *
  875. * Returns zero upon success and a negative value upon failure.
  876. *
  877. * Note: currently a struct ib_qp_attr takes 136 bytes on a 64-bit system.
  878. * If this structure ever becomes larger, it might be necessary to allocate
  879. * it dynamically instead of on the stack.
  880. */
  881. static int srpt_ch_qp_rts(struct srpt_rdma_ch *ch, struct ib_qp *qp)
  882. {
  883. struct ib_qp_attr qp_attr;
  884. int attr_mask;
  885. int ret;
  886. qp_attr.qp_state = IB_QPS_RTS;
  887. ret = ib_cm_init_qp_attr(ch->cm_id, &qp_attr, &attr_mask);
  888. if (ret)
  889. goto out;
  890. qp_attr.max_rd_atomic = 4;
  891. ret = ib_modify_qp(qp, &qp_attr, attr_mask);
  892. out:
  893. return ret;
  894. }
  895. /**
  896. * srpt_ch_qp_err() - Set the channel queue pair state to 'error'.
  897. */
  898. static int srpt_ch_qp_err(struct srpt_rdma_ch *ch)
  899. {
  900. struct ib_qp_attr qp_attr;
  901. qp_attr.qp_state = IB_QPS_ERR;
  902. return ib_modify_qp(ch->qp, &qp_attr, IB_QP_STATE);
  903. }
  904. /**
  905. * srpt_unmap_sg_to_ib_sge() - Unmap an IB SGE list.
  906. */
  907. static void srpt_unmap_sg_to_ib_sge(struct srpt_rdma_ch *ch,
  908. struct srpt_send_ioctx *ioctx)
  909. {
  910. struct scatterlist *sg;
  911. enum dma_data_direction dir;
  912. BUG_ON(!ch);
  913. BUG_ON(!ioctx);
  914. BUG_ON(ioctx->n_rdma && !ioctx->rdma_ius);
  915. while (ioctx->n_rdma)
  916. kfree(ioctx->rdma_ius[--ioctx->n_rdma].sge);
  917. kfree(ioctx->rdma_ius);
  918. ioctx->rdma_ius = NULL;
  919. if (ioctx->mapped_sg_count) {
  920. sg = ioctx->sg;
  921. WARN_ON(!sg);
  922. dir = ioctx->cmd.data_direction;
  923. BUG_ON(dir == DMA_NONE);
  924. ib_dma_unmap_sg(ch->sport->sdev->device, sg, ioctx->sg_cnt,
  925. opposite_dma_dir(dir));
  926. ioctx->mapped_sg_count = 0;
  927. }
  928. }
  929. /**
  930. * srpt_map_sg_to_ib_sge() - Map an SG list to an IB SGE list.
  931. */
  932. static int srpt_map_sg_to_ib_sge(struct srpt_rdma_ch *ch,
  933. struct srpt_send_ioctx *ioctx)
  934. {
  935. struct ib_device *dev = ch->sport->sdev->device;
  936. struct se_cmd *cmd;
  937. struct scatterlist *sg, *sg_orig;
  938. int sg_cnt;
  939. enum dma_data_direction dir;
  940. struct rdma_iu *riu;
  941. struct srp_direct_buf *db;
  942. dma_addr_t dma_addr;
  943. struct ib_sge *sge;
  944. u64 raddr;
  945. u32 rsize;
  946. u32 tsize;
  947. u32 dma_len;
  948. int count, nrdma;
  949. int i, j, k;
  950. BUG_ON(!ch);
  951. BUG_ON(!ioctx);
  952. cmd = &ioctx->cmd;
  953. dir = cmd->data_direction;
  954. BUG_ON(dir == DMA_NONE);
  955. ioctx->sg = sg = sg_orig = cmd->t_data_sg;
  956. ioctx->sg_cnt = sg_cnt = cmd->t_data_nents;
  957. count = ib_dma_map_sg(ch->sport->sdev->device, sg, sg_cnt,
  958. opposite_dma_dir(dir));
  959. if (unlikely(!count))
  960. return -EAGAIN;
  961. ioctx->mapped_sg_count = count;
  962. if (ioctx->rdma_ius && ioctx->n_rdma_ius)
  963. nrdma = ioctx->n_rdma_ius;
  964. else {
  965. nrdma = (count + SRPT_DEF_SG_PER_WQE - 1) / SRPT_DEF_SG_PER_WQE
  966. + ioctx->n_rbuf;
  967. ioctx->rdma_ius = kzalloc(nrdma * sizeof *riu, GFP_KERNEL);
  968. if (!ioctx->rdma_ius)
  969. goto free_mem;
  970. ioctx->n_rdma_ius = nrdma;
  971. }
  972. db = ioctx->rbufs;
  973. tsize = cmd->data_length;
  974. dma_len = ib_sg_dma_len(dev, &sg[0]);
  975. riu = ioctx->rdma_ius;
  976. /*
  977. * For each remote desc - calculate the #ib_sge.
  978. * If #ib_sge < SRPT_DEF_SG_PER_WQE per rdma operation then
  979. * each remote desc rdma_iu is required a rdma wr;
  980. * else
  981. * we need to allocate extra rdma_iu to carry extra #ib_sge in
  982. * another rdma wr
  983. */
  984. for (i = 0, j = 0;
  985. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  986. rsize = be32_to_cpu(db->len);
  987. raddr = be64_to_cpu(db->va);
  988. riu->raddr = raddr;
  989. riu->rkey = be32_to_cpu(db->key);
  990. riu->sge_cnt = 0;
  991. /* calculate how many sge required for this remote_buf */
  992. while (rsize > 0 && tsize > 0) {
  993. if (rsize >= dma_len) {
  994. tsize -= dma_len;
  995. rsize -= dma_len;
  996. raddr += dma_len;
  997. if (tsize > 0) {
  998. ++j;
  999. if (j < count) {
  1000. sg = sg_next(sg);
  1001. dma_len = ib_sg_dma_len(
  1002. dev, sg);
  1003. }
  1004. }
  1005. } else {
  1006. tsize -= rsize;
  1007. dma_len -= rsize;
  1008. rsize = 0;
  1009. }
  1010. ++riu->sge_cnt;
  1011. if (rsize > 0 && riu->sge_cnt == SRPT_DEF_SG_PER_WQE) {
  1012. ++ioctx->n_rdma;
  1013. riu->sge =
  1014. kmalloc(riu->sge_cnt * sizeof *riu->sge,
  1015. GFP_KERNEL);
  1016. if (!riu->sge)
  1017. goto free_mem;
  1018. ++riu;
  1019. riu->sge_cnt = 0;
  1020. riu->raddr = raddr;
  1021. riu->rkey = be32_to_cpu(db->key);
  1022. }
  1023. }
  1024. ++ioctx->n_rdma;
  1025. riu->sge = kmalloc(riu->sge_cnt * sizeof *riu->sge,
  1026. GFP_KERNEL);
  1027. if (!riu->sge)
  1028. goto free_mem;
  1029. }
  1030. db = ioctx->rbufs;
  1031. tsize = cmd->data_length;
  1032. riu = ioctx->rdma_ius;
  1033. sg = sg_orig;
  1034. dma_len = ib_sg_dma_len(dev, &sg[0]);
  1035. dma_addr = ib_sg_dma_address(dev, &sg[0]);
  1036. /* this second loop is really mapped sg_addres to rdma_iu->ib_sge */
  1037. for (i = 0, j = 0;
  1038. j < count && i < ioctx->n_rbuf && tsize > 0; ++i, ++riu, ++db) {
  1039. rsize = be32_to_cpu(db->len);
  1040. sge = riu->sge;
  1041. k = 0;
  1042. while (rsize > 0 && tsize > 0) {
  1043. sge->addr = dma_addr;
  1044. sge->lkey = ch->sport->sdev->mr->lkey;
  1045. if (rsize >= dma_len) {
  1046. sge->length =
  1047. (tsize < dma_len) ? tsize : dma_len;
  1048. tsize -= dma_len;
  1049. rsize -= dma_len;
  1050. if (tsize > 0) {
  1051. ++j;
  1052. if (j < count) {
  1053. sg = sg_next(sg);
  1054. dma_len = ib_sg_dma_len(
  1055. dev, sg);
  1056. dma_addr = ib_sg_dma_address(
  1057. dev, sg);
  1058. }
  1059. }
  1060. } else {
  1061. sge->length = (tsize < rsize) ? tsize : rsize;
  1062. tsize -= rsize;
  1063. dma_len -= rsize;
  1064. dma_addr += rsize;
  1065. rsize = 0;
  1066. }
  1067. ++k;
  1068. if (k == riu->sge_cnt && rsize > 0 && tsize > 0) {
  1069. ++riu;
  1070. sge = riu->sge;
  1071. k = 0;
  1072. } else if (rsize > 0 && tsize > 0)
  1073. ++sge;
  1074. }
  1075. }
  1076. return 0;
  1077. free_mem:
  1078. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  1079. return -ENOMEM;
  1080. }
  1081. /**
  1082. * srpt_get_send_ioctx() - Obtain an I/O context for sending to the initiator.
  1083. */
  1084. static struct srpt_send_ioctx *srpt_get_send_ioctx(struct srpt_rdma_ch *ch)
  1085. {
  1086. struct srpt_send_ioctx *ioctx;
  1087. unsigned long flags;
  1088. BUG_ON(!ch);
  1089. ioctx = NULL;
  1090. spin_lock_irqsave(&ch->spinlock, flags);
  1091. if (!list_empty(&ch->free_list)) {
  1092. ioctx = list_first_entry(&ch->free_list,
  1093. struct srpt_send_ioctx, free_list);
  1094. list_del(&ioctx->free_list);
  1095. }
  1096. spin_unlock_irqrestore(&ch->spinlock, flags);
  1097. if (!ioctx)
  1098. return ioctx;
  1099. BUG_ON(ioctx->ch != ch);
  1100. spin_lock_init(&ioctx->spinlock);
  1101. ioctx->state = SRPT_STATE_NEW;
  1102. ioctx->n_rbuf = 0;
  1103. ioctx->rbufs = NULL;
  1104. ioctx->n_rdma = 0;
  1105. ioctx->n_rdma_ius = 0;
  1106. ioctx->rdma_ius = NULL;
  1107. ioctx->mapped_sg_count = 0;
  1108. init_completion(&ioctx->tx_done);
  1109. ioctx->queue_status_only = false;
  1110. /*
  1111. * transport_init_se_cmd() does not initialize all fields, so do it
  1112. * here.
  1113. */
  1114. memset(&ioctx->cmd, 0, sizeof(ioctx->cmd));
  1115. memset(&ioctx->sense_data, 0, sizeof(ioctx->sense_data));
  1116. return ioctx;
  1117. }
  1118. /**
  1119. * srpt_abort_cmd() - Abort a SCSI command.
  1120. * @ioctx: I/O context associated with the SCSI command.
  1121. * @context: Preferred execution context.
  1122. */
  1123. static int srpt_abort_cmd(struct srpt_send_ioctx *ioctx)
  1124. {
  1125. enum srpt_command_state state;
  1126. unsigned long flags;
  1127. BUG_ON(!ioctx);
  1128. /*
  1129. * If the command is in a state where the target core is waiting for
  1130. * the ib_srpt driver, change the state to the next state. Changing
  1131. * the state of the command from SRPT_STATE_NEED_DATA to
  1132. * SRPT_STATE_DATA_IN ensures that srpt_xmit_response() will call this
  1133. * function a second time.
  1134. */
  1135. spin_lock_irqsave(&ioctx->spinlock, flags);
  1136. state = ioctx->state;
  1137. switch (state) {
  1138. case SRPT_STATE_NEED_DATA:
  1139. ioctx->state = SRPT_STATE_DATA_IN;
  1140. break;
  1141. case SRPT_STATE_DATA_IN:
  1142. case SRPT_STATE_CMD_RSP_SENT:
  1143. case SRPT_STATE_MGMT_RSP_SENT:
  1144. ioctx->state = SRPT_STATE_DONE;
  1145. break;
  1146. default:
  1147. break;
  1148. }
  1149. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  1150. if (state == SRPT_STATE_DONE) {
  1151. struct srpt_rdma_ch *ch = ioctx->ch;
  1152. BUG_ON(ch->sess == NULL);
  1153. target_put_sess_cmd(ch->sess, &ioctx->cmd);
  1154. goto out;
  1155. }
  1156. pr_debug("Aborting cmd with state %d and tag %lld\n", state,
  1157. ioctx->tag);
  1158. switch (state) {
  1159. case SRPT_STATE_NEW:
  1160. case SRPT_STATE_DATA_IN:
  1161. case SRPT_STATE_MGMT:
  1162. /*
  1163. * Do nothing - defer abort processing until
  1164. * srpt_queue_response() is invoked.
  1165. */
  1166. WARN_ON(!transport_check_aborted_status(&ioctx->cmd, false));
  1167. break;
  1168. case SRPT_STATE_NEED_DATA:
  1169. /* DMA_TO_DEVICE (write) - RDMA read error. */
  1170. /* XXX(hch): this is a horrible layering violation.. */
  1171. spin_lock_irqsave(&ioctx->cmd.t_state_lock, flags);
  1172. ioctx->cmd.transport_state &= ~CMD_T_ACTIVE;
  1173. spin_unlock_irqrestore(&ioctx->cmd.t_state_lock, flags);
  1174. break;
  1175. case SRPT_STATE_CMD_RSP_SENT:
  1176. /*
  1177. * SRP_RSP sending failed or the SRP_RSP send completion has
  1178. * not been received in time.
  1179. */
  1180. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  1181. target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
  1182. break;
  1183. case SRPT_STATE_MGMT_RSP_SENT:
  1184. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1185. target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
  1186. break;
  1187. default:
  1188. WARN(1, "Unexpected command state (%d)", state);
  1189. break;
  1190. }
  1191. out:
  1192. return state;
  1193. }
  1194. /**
  1195. * srpt_handle_send_err_comp() - Process an IB_WC_SEND error completion.
  1196. */
  1197. static void srpt_handle_send_err_comp(struct srpt_rdma_ch *ch, u64 wr_id)
  1198. {
  1199. struct srpt_send_ioctx *ioctx;
  1200. enum srpt_command_state state;
  1201. struct se_cmd *cmd;
  1202. u32 index;
  1203. atomic_inc(&ch->sq_wr_avail);
  1204. index = idx_from_wr_id(wr_id);
  1205. ioctx = ch->ioctx_ring[index];
  1206. state = srpt_get_cmd_state(ioctx);
  1207. cmd = &ioctx->cmd;
  1208. WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
  1209. && state != SRPT_STATE_MGMT_RSP_SENT
  1210. && state != SRPT_STATE_NEED_DATA
  1211. && state != SRPT_STATE_DONE);
  1212. /* If SRP_RSP sending failed, undo the ch->req_lim change. */
  1213. if (state == SRPT_STATE_CMD_RSP_SENT
  1214. || state == SRPT_STATE_MGMT_RSP_SENT)
  1215. atomic_dec(&ch->req_lim);
  1216. srpt_abort_cmd(ioctx);
  1217. }
  1218. /**
  1219. * srpt_handle_send_comp() - Process an IB send completion notification.
  1220. */
  1221. static void srpt_handle_send_comp(struct srpt_rdma_ch *ch,
  1222. struct srpt_send_ioctx *ioctx)
  1223. {
  1224. enum srpt_command_state state;
  1225. atomic_inc(&ch->sq_wr_avail);
  1226. state = srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  1227. if (WARN_ON(state != SRPT_STATE_CMD_RSP_SENT
  1228. && state != SRPT_STATE_MGMT_RSP_SENT
  1229. && state != SRPT_STATE_DONE))
  1230. pr_debug("state = %d\n", state);
  1231. if (state != SRPT_STATE_DONE) {
  1232. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  1233. transport_generic_free_cmd(&ioctx->cmd, 0);
  1234. } else {
  1235. printk(KERN_ERR "IB completion has been received too late for"
  1236. " wr_id = %u.\n", ioctx->ioctx.index);
  1237. }
  1238. }
  1239. /**
  1240. * srpt_handle_rdma_comp() - Process an IB RDMA completion notification.
  1241. *
  1242. * XXX: what is now target_execute_cmd used to be asynchronous, and unmapping
  1243. * the data that has been transferred via IB RDMA had to be postponed until the
  1244. * check_stop_free() callback. None of this is necessary anymore and needs to
  1245. * be cleaned up.
  1246. */
  1247. static void srpt_handle_rdma_comp(struct srpt_rdma_ch *ch,
  1248. struct srpt_send_ioctx *ioctx,
  1249. enum srpt_opcode opcode)
  1250. {
  1251. WARN_ON(ioctx->n_rdma <= 0);
  1252. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1253. if (opcode == SRPT_RDMA_READ_LAST) {
  1254. if (srpt_test_and_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA,
  1255. SRPT_STATE_DATA_IN))
  1256. target_execute_cmd(&ioctx->cmd);
  1257. else
  1258. printk(KERN_ERR "%s[%d]: wrong state = %d\n", __func__,
  1259. __LINE__, srpt_get_cmd_state(ioctx));
  1260. } else if (opcode == SRPT_RDMA_ABORT) {
  1261. ioctx->rdma_aborted = true;
  1262. } else {
  1263. WARN(true, "unexpected opcode %d\n", opcode);
  1264. }
  1265. }
  1266. /**
  1267. * srpt_handle_rdma_err_comp() - Process an IB RDMA error completion.
  1268. */
  1269. static void srpt_handle_rdma_err_comp(struct srpt_rdma_ch *ch,
  1270. struct srpt_send_ioctx *ioctx,
  1271. enum srpt_opcode opcode)
  1272. {
  1273. struct se_cmd *cmd;
  1274. enum srpt_command_state state;
  1275. cmd = &ioctx->cmd;
  1276. state = srpt_get_cmd_state(ioctx);
  1277. switch (opcode) {
  1278. case SRPT_RDMA_READ_LAST:
  1279. if (ioctx->n_rdma <= 0) {
  1280. printk(KERN_ERR "Received invalid RDMA read"
  1281. " error completion with idx %d\n",
  1282. ioctx->ioctx.index);
  1283. break;
  1284. }
  1285. atomic_add(ioctx->n_rdma, &ch->sq_wr_avail);
  1286. if (state == SRPT_STATE_NEED_DATA)
  1287. srpt_abort_cmd(ioctx);
  1288. else
  1289. printk(KERN_ERR "%s[%d]: wrong state = %d\n",
  1290. __func__, __LINE__, state);
  1291. break;
  1292. case SRPT_RDMA_WRITE_LAST:
  1293. break;
  1294. default:
  1295. printk(KERN_ERR "%s[%d]: opcode = %u\n", __func__,
  1296. __LINE__, opcode);
  1297. break;
  1298. }
  1299. }
  1300. /**
  1301. * srpt_build_cmd_rsp() - Build an SRP_RSP response.
  1302. * @ch: RDMA channel through which the request has been received.
  1303. * @ioctx: I/O context associated with the SRP_CMD request. The response will
  1304. * be built in the buffer ioctx->buf points at and hence this function will
  1305. * overwrite the request data.
  1306. * @tag: tag of the request for which this response is being generated.
  1307. * @status: value for the STATUS field of the SRP_RSP information unit.
  1308. *
  1309. * Returns the size in bytes of the SRP_RSP response.
  1310. *
  1311. * An SRP_RSP response contains a SCSI status or service response. See also
  1312. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1313. * response. See also SPC-2 for more information about sense data.
  1314. */
  1315. static int srpt_build_cmd_rsp(struct srpt_rdma_ch *ch,
  1316. struct srpt_send_ioctx *ioctx, u64 tag,
  1317. int status)
  1318. {
  1319. struct srp_rsp *srp_rsp;
  1320. const u8 *sense_data;
  1321. int sense_data_len, max_sense_len;
  1322. /*
  1323. * The lowest bit of all SAM-3 status codes is zero (see also
  1324. * paragraph 5.3 in SAM-3).
  1325. */
  1326. WARN_ON(status & 1);
  1327. srp_rsp = ioctx->ioctx.buf;
  1328. BUG_ON(!srp_rsp);
  1329. sense_data = ioctx->sense_data;
  1330. sense_data_len = ioctx->cmd.scsi_sense_length;
  1331. WARN_ON(sense_data_len > sizeof(ioctx->sense_data));
  1332. memset(srp_rsp, 0, sizeof *srp_rsp);
  1333. srp_rsp->opcode = SRP_RSP;
  1334. srp_rsp->req_lim_delta =
  1335. __constant_cpu_to_be32(1 + atomic_xchg(&ch->req_lim_delta, 0));
  1336. srp_rsp->tag = tag;
  1337. srp_rsp->status = status;
  1338. if (sense_data_len) {
  1339. BUILD_BUG_ON(MIN_MAX_RSP_SIZE <= sizeof(*srp_rsp));
  1340. max_sense_len = ch->max_ti_iu_len - sizeof(*srp_rsp);
  1341. if (sense_data_len > max_sense_len) {
  1342. printk(KERN_WARNING "truncated sense data from %d to %d"
  1343. " bytes\n", sense_data_len, max_sense_len);
  1344. sense_data_len = max_sense_len;
  1345. }
  1346. srp_rsp->flags |= SRP_RSP_FLAG_SNSVALID;
  1347. srp_rsp->sense_data_len = cpu_to_be32(sense_data_len);
  1348. memcpy(srp_rsp + 1, sense_data, sense_data_len);
  1349. }
  1350. return sizeof(*srp_rsp) + sense_data_len;
  1351. }
  1352. /**
  1353. * srpt_build_tskmgmt_rsp() - Build a task management response.
  1354. * @ch: RDMA channel through which the request has been received.
  1355. * @ioctx: I/O context in which the SRP_RSP response will be built.
  1356. * @rsp_code: RSP_CODE that will be stored in the response.
  1357. * @tag: Tag of the request for which this response is being generated.
  1358. *
  1359. * Returns the size in bytes of the SRP_RSP response.
  1360. *
  1361. * An SRP_RSP response contains a SCSI status or service response. See also
  1362. * section 6.9 in the SRP r16a document for the format of an SRP_RSP
  1363. * response.
  1364. */
  1365. static int srpt_build_tskmgmt_rsp(struct srpt_rdma_ch *ch,
  1366. struct srpt_send_ioctx *ioctx,
  1367. u8 rsp_code, u64 tag)
  1368. {
  1369. struct srp_rsp *srp_rsp;
  1370. int resp_data_len;
  1371. int resp_len;
  1372. resp_data_len = 4;
  1373. resp_len = sizeof(*srp_rsp) + resp_data_len;
  1374. srp_rsp = ioctx->ioctx.buf;
  1375. BUG_ON(!srp_rsp);
  1376. memset(srp_rsp, 0, sizeof *srp_rsp);
  1377. srp_rsp->opcode = SRP_RSP;
  1378. srp_rsp->req_lim_delta = __constant_cpu_to_be32(1
  1379. + atomic_xchg(&ch->req_lim_delta, 0));
  1380. srp_rsp->tag = tag;
  1381. srp_rsp->flags |= SRP_RSP_FLAG_RSPVALID;
  1382. srp_rsp->resp_data_len = cpu_to_be32(resp_data_len);
  1383. srp_rsp->data[3] = rsp_code;
  1384. return resp_len;
  1385. }
  1386. #define NO_SUCH_LUN ((uint64_t)-1LL)
  1387. /*
  1388. * SCSI LUN addressing method. See also SAM-2 and the section about
  1389. * eight byte LUNs.
  1390. */
  1391. enum scsi_lun_addr_method {
  1392. SCSI_LUN_ADDR_METHOD_PERIPHERAL = 0,
  1393. SCSI_LUN_ADDR_METHOD_FLAT = 1,
  1394. SCSI_LUN_ADDR_METHOD_LUN = 2,
  1395. SCSI_LUN_ADDR_METHOD_EXTENDED_LUN = 3,
  1396. };
  1397. /*
  1398. * srpt_unpack_lun() - Convert from network LUN to linear LUN.
  1399. *
  1400. * Convert an 2-byte, 4-byte, 6-byte or 8-byte LUN structure in network byte
  1401. * order (big endian) to a linear LUN. Supports three LUN addressing methods:
  1402. * peripheral, flat and logical unit. See also SAM-2, section 4.9.4 (page 40).
  1403. */
  1404. static uint64_t srpt_unpack_lun(const uint8_t *lun, int len)
  1405. {
  1406. uint64_t res = NO_SUCH_LUN;
  1407. int addressing_method;
  1408. if (unlikely(len < 2)) {
  1409. printk(KERN_ERR "Illegal LUN length %d, expected 2 bytes or "
  1410. "more", len);
  1411. goto out;
  1412. }
  1413. switch (len) {
  1414. case 8:
  1415. if ((*((__be64 *)lun) &
  1416. __constant_cpu_to_be64(0x0000FFFFFFFFFFFFLL)) != 0)
  1417. goto out_err;
  1418. break;
  1419. case 4:
  1420. if (*((__be16 *)&lun[2]) != 0)
  1421. goto out_err;
  1422. break;
  1423. case 6:
  1424. if (*((__be32 *)&lun[2]) != 0)
  1425. goto out_err;
  1426. break;
  1427. case 2:
  1428. break;
  1429. default:
  1430. goto out_err;
  1431. }
  1432. addressing_method = (*lun) >> 6; /* highest two bits of byte 0 */
  1433. switch (addressing_method) {
  1434. case SCSI_LUN_ADDR_METHOD_PERIPHERAL:
  1435. case SCSI_LUN_ADDR_METHOD_FLAT:
  1436. case SCSI_LUN_ADDR_METHOD_LUN:
  1437. res = *(lun + 1) | (((*lun) & 0x3f) << 8);
  1438. break;
  1439. case SCSI_LUN_ADDR_METHOD_EXTENDED_LUN:
  1440. default:
  1441. printk(KERN_ERR "Unimplemented LUN addressing method %u",
  1442. addressing_method);
  1443. break;
  1444. }
  1445. out:
  1446. return res;
  1447. out_err:
  1448. printk(KERN_ERR "Support for multi-level LUNs has not yet been"
  1449. " implemented");
  1450. goto out;
  1451. }
  1452. static int srpt_check_stop_free(struct se_cmd *cmd)
  1453. {
  1454. struct srpt_send_ioctx *ioctx = container_of(cmd,
  1455. struct srpt_send_ioctx, cmd);
  1456. return target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
  1457. }
  1458. /**
  1459. * srpt_handle_cmd() - Process SRP_CMD.
  1460. */
  1461. static int srpt_handle_cmd(struct srpt_rdma_ch *ch,
  1462. struct srpt_recv_ioctx *recv_ioctx,
  1463. struct srpt_send_ioctx *send_ioctx)
  1464. {
  1465. struct se_cmd *cmd;
  1466. struct srp_cmd *srp_cmd;
  1467. uint64_t unpacked_lun;
  1468. u64 data_len;
  1469. enum dma_data_direction dir;
  1470. sense_reason_t ret;
  1471. int rc;
  1472. BUG_ON(!send_ioctx);
  1473. srp_cmd = recv_ioctx->ioctx.buf;
  1474. cmd = &send_ioctx->cmd;
  1475. send_ioctx->tag = srp_cmd->tag;
  1476. switch (srp_cmd->task_attr) {
  1477. case SRP_CMD_SIMPLE_Q:
  1478. cmd->sam_task_attr = TCM_SIMPLE_TAG;
  1479. break;
  1480. case SRP_CMD_ORDERED_Q:
  1481. default:
  1482. cmd->sam_task_attr = TCM_ORDERED_TAG;
  1483. break;
  1484. case SRP_CMD_HEAD_OF_Q:
  1485. cmd->sam_task_attr = TCM_HEAD_TAG;
  1486. break;
  1487. case SRP_CMD_ACA:
  1488. cmd->sam_task_attr = TCM_ACA_TAG;
  1489. break;
  1490. }
  1491. if (srpt_get_desc_tbl(send_ioctx, srp_cmd, &dir, &data_len)) {
  1492. printk(KERN_ERR "0x%llx: parsing SRP descriptor table failed.\n",
  1493. srp_cmd->tag);
  1494. ret = TCM_INVALID_CDB_FIELD;
  1495. goto send_sense;
  1496. }
  1497. unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_cmd->lun,
  1498. sizeof(srp_cmd->lun));
  1499. rc = target_submit_cmd(cmd, ch->sess, srp_cmd->cdb,
  1500. &send_ioctx->sense_data[0], unpacked_lun, data_len,
  1501. TCM_SIMPLE_TAG, dir, TARGET_SCF_ACK_KREF);
  1502. if (rc != 0) {
  1503. ret = TCM_LOGICAL_UNIT_COMMUNICATION_FAILURE;
  1504. goto send_sense;
  1505. }
  1506. return 0;
  1507. send_sense:
  1508. transport_send_check_condition_and_sense(cmd, ret, 0);
  1509. return -1;
  1510. }
  1511. /**
  1512. * srpt_rx_mgmt_fn_tag() - Process a task management function by tag.
  1513. * @ch: RDMA channel of the task management request.
  1514. * @fn: Task management function to perform.
  1515. * @req_tag: Tag of the SRP task management request.
  1516. * @mgmt_ioctx: I/O context of the task management request.
  1517. *
  1518. * Returns zero if the target core will process the task management
  1519. * request asynchronously.
  1520. *
  1521. * Note: It is assumed that the initiator serializes tag-based task management
  1522. * requests.
  1523. */
  1524. static int srpt_rx_mgmt_fn_tag(struct srpt_send_ioctx *ioctx, u64 tag)
  1525. {
  1526. struct srpt_device *sdev;
  1527. struct srpt_rdma_ch *ch;
  1528. struct srpt_send_ioctx *target;
  1529. int ret, i;
  1530. ret = -EINVAL;
  1531. ch = ioctx->ch;
  1532. BUG_ON(!ch);
  1533. BUG_ON(!ch->sport);
  1534. sdev = ch->sport->sdev;
  1535. BUG_ON(!sdev);
  1536. spin_lock_irq(&sdev->spinlock);
  1537. for (i = 0; i < ch->rq_size; ++i) {
  1538. target = ch->ioctx_ring[i];
  1539. if (target->cmd.se_lun == ioctx->cmd.se_lun &&
  1540. target->tag == tag &&
  1541. srpt_get_cmd_state(target) != SRPT_STATE_DONE) {
  1542. ret = 0;
  1543. /* now let the target core abort &target->cmd; */
  1544. break;
  1545. }
  1546. }
  1547. spin_unlock_irq(&sdev->spinlock);
  1548. return ret;
  1549. }
  1550. static int srp_tmr_to_tcm(int fn)
  1551. {
  1552. switch (fn) {
  1553. case SRP_TSK_ABORT_TASK:
  1554. return TMR_ABORT_TASK;
  1555. case SRP_TSK_ABORT_TASK_SET:
  1556. return TMR_ABORT_TASK_SET;
  1557. case SRP_TSK_CLEAR_TASK_SET:
  1558. return TMR_CLEAR_TASK_SET;
  1559. case SRP_TSK_LUN_RESET:
  1560. return TMR_LUN_RESET;
  1561. case SRP_TSK_CLEAR_ACA:
  1562. return TMR_CLEAR_ACA;
  1563. default:
  1564. return -1;
  1565. }
  1566. }
  1567. /**
  1568. * srpt_handle_tsk_mgmt() - Process an SRP_TSK_MGMT information unit.
  1569. *
  1570. * Returns 0 if and only if the request will be processed by the target core.
  1571. *
  1572. * For more information about SRP_TSK_MGMT information units, see also section
  1573. * 6.7 in the SRP r16a document.
  1574. */
  1575. static void srpt_handle_tsk_mgmt(struct srpt_rdma_ch *ch,
  1576. struct srpt_recv_ioctx *recv_ioctx,
  1577. struct srpt_send_ioctx *send_ioctx)
  1578. {
  1579. struct srp_tsk_mgmt *srp_tsk;
  1580. struct se_cmd *cmd;
  1581. struct se_session *sess = ch->sess;
  1582. uint64_t unpacked_lun;
  1583. uint32_t tag = 0;
  1584. int tcm_tmr;
  1585. int rc;
  1586. BUG_ON(!send_ioctx);
  1587. srp_tsk = recv_ioctx->ioctx.buf;
  1588. cmd = &send_ioctx->cmd;
  1589. pr_debug("recv tsk_mgmt fn %d for task_tag %lld and cmd tag %lld"
  1590. " cm_id %p sess %p\n", srp_tsk->tsk_mgmt_func,
  1591. srp_tsk->task_tag, srp_tsk->tag, ch->cm_id, ch->sess);
  1592. srpt_set_cmd_state(send_ioctx, SRPT_STATE_MGMT);
  1593. send_ioctx->tag = srp_tsk->tag;
  1594. tcm_tmr = srp_tmr_to_tcm(srp_tsk->tsk_mgmt_func);
  1595. if (tcm_tmr < 0) {
  1596. send_ioctx->cmd.se_tmr_req->response =
  1597. TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
  1598. goto fail;
  1599. }
  1600. unpacked_lun = srpt_unpack_lun((uint8_t *)&srp_tsk->lun,
  1601. sizeof(srp_tsk->lun));
  1602. if (srp_tsk->tsk_mgmt_func == SRP_TSK_ABORT_TASK) {
  1603. rc = srpt_rx_mgmt_fn_tag(send_ioctx, srp_tsk->task_tag);
  1604. if (rc < 0) {
  1605. send_ioctx->cmd.se_tmr_req->response =
  1606. TMR_TASK_DOES_NOT_EXIST;
  1607. goto fail;
  1608. }
  1609. tag = srp_tsk->task_tag;
  1610. }
  1611. rc = target_submit_tmr(&send_ioctx->cmd, sess, NULL, unpacked_lun,
  1612. srp_tsk, tcm_tmr, GFP_KERNEL, tag,
  1613. TARGET_SCF_ACK_KREF);
  1614. if (rc != 0) {
  1615. send_ioctx->cmd.se_tmr_req->response = TMR_FUNCTION_REJECTED;
  1616. goto fail;
  1617. }
  1618. return;
  1619. fail:
  1620. transport_send_check_condition_and_sense(cmd, 0, 0); // XXX:
  1621. }
  1622. /**
  1623. * srpt_handle_new_iu() - Process a newly received information unit.
  1624. * @ch: RDMA channel through which the information unit has been received.
  1625. * @ioctx: SRPT I/O context associated with the information unit.
  1626. */
  1627. static void srpt_handle_new_iu(struct srpt_rdma_ch *ch,
  1628. struct srpt_recv_ioctx *recv_ioctx,
  1629. struct srpt_send_ioctx *send_ioctx)
  1630. {
  1631. struct srp_cmd *srp_cmd;
  1632. enum rdma_ch_state ch_state;
  1633. BUG_ON(!ch);
  1634. BUG_ON(!recv_ioctx);
  1635. ib_dma_sync_single_for_cpu(ch->sport->sdev->device,
  1636. recv_ioctx->ioctx.dma, srp_max_req_size,
  1637. DMA_FROM_DEVICE);
  1638. ch_state = srpt_get_ch_state(ch);
  1639. if (unlikely(ch_state == CH_CONNECTING)) {
  1640. list_add_tail(&recv_ioctx->wait_list, &ch->cmd_wait_list);
  1641. goto out;
  1642. }
  1643. if (unlikely(ch_state != CH_LIVE))
  1644. goto out;
  1645. srp_cmd = recv_ioctx->ioctx.buf;
  1646. if (srp_cmd->opcode == SRP_CMD || srp_cmd->opcode == SRP_TSK_MGMT) {
  1647. if (!send_ioctx)
  1648. send_ioctx = srpt_get_send_ioctx(ch);
  1649. if (unlikely(!send_ioctx)) {
  1650. list_add_tail(&recv_ioctx->wait_list,
  1651. &ch->cmd_wait_list);
  1652. goto out;
  1653. }
  1654. }
  1655. switch (srp_cmd->opcode) {
  1656. case SRP_CMD:
  1657. srpt_handle_cmd(ch, recv_ioctx, send_ioctx);
  1658. break;
  1659. case SRP_TSK_MGMT:
  1660. srpt_handle_tsk_mgmt(ch, recv_ioctx, send_ioctx);
  1661. break;
  1662. case SRP_I_LOGOUT:
  1663. printk(KERN_ERR "Not yet implemented: SRP_I_LOGOUT\n");
  1664. break;
  1665. case SRP_CRED_RSP:
  1666. pr_debug("received SRP_CRED_RSP\n");
  1667. break;
  1668. case SRP_AER_RSP:
  1669. pr_debug("received SRP_AER_RSP\n");
  1670. break;
  1671. case SRP_RSP:
  1672. printk(KERN_ERR "Received SRP_RSP\n");
  1673. break;
  1674. default:
  1675. printk(KERN_ERR "received IU with unknown opcode 0x%x\n",
  1676. srp_cmd->opcode);
  1677. break;
  1678. }
  1679. srpt_post_recv(ch->sport->sdev, recv_ioctx);
  1680. out:
  1681. return;
  1682. }
  1683. static void srpt_process_rcv_completion(struct ib_cq *cq,
  1684. struct srpt_rdma_ch *ch,
  1685. struct ib_wc *wc)
  1686. {
  1687. struct srpt_device *sdev = ch->sport->sdev;
  1688. struct srpt_recv_ioctx *ioctx;
  1689. u32 index;
  1690. index = idx_from_wr_id(wc->wr_id);
  1691. if (wc->status == IB_WC_SUCCESS) {
  1692. int req_lim;
  1693. req_lim = atomic_dec_return(&ch->req_lim);
  1694. if (unlikely(req_lim < 0))
  1695. printk(KERN_ERR "req_lim = %d < 0\n", req_lim);
  1696. ioctx = sdev->ioctx_ring[index];
  1697. srpt_handle_new_iu(ch, ioctx, NULL);
  1698. } else {
  1699. printk(KERN_INFO "receiving failed for idx %u with status %d\n",
  1700. index, wc->status);
  1701. }
  1702. }
  1703. /**
  1704. * srpt_process_send_completion() - Process an IB send completion.
  1705. *
  1706. * Note: Although this has not yet been observed during tests, at least in
  1707. * theory it is possible that the srpt_get_send_ioctx() call invoked by
  1708. * srpt_handle_new_iu() fails. This is possible because the req_lim_delta
  1709. * value in each response is set to one, and it is possible that this response
  1710. * makes the initiator send a new request before the send completion for that
  1711. * response has been processed. This could e.g. happen if the call to
  1712. * srpt_put_send_iotcx() is delayed because of a higher priority interrupt or
  1713. * if IB retransmission causes generation of the send completion to be
  1714. * delayed. Incoming information units for which srpt_get_send_ioctx() fails
  1715. * are queued on cmd_wait_list. The code below processes these delayed
  1716. * requests one at a time.
  1717. */
  1718. static void srpt_process_send_completion(struct ib_cq *cq,
  1719. struct srpt_rdma_ch *ch,
  1720. struct ib_wc *wc)
  1721. {
  1722. struct srpt_send_ioctx *send_ioctx;
  1723. uint32_t index;
  1724. enum srpt_opcode opcode;
  1725. index = idx_from_wr_id(wc->wr_id);
  1726. opcode = opcode_from_wr_id(wc->wr_id);
  1727. send_ioctx = ch->ioctx_ring[index];
  1728. if (wc->status == IB_WC_SUCCESS) {
  1729. if (opcode == SRPT_SEND)
  1730. srpt_handle_send_comp(ch, send_ioctx);
  1731. else {
  1732. WARN_ON(opcode != SRPT_RDMA_ABORT &&
  1733. wc->opcode != IB_WC_RDMA_READ);
  1734. srpt_handle_rdma_comp(ch, send_ioctx, opcode);
  1735. }
  1736. } else {
  1737. if (opcode == SRPT_SEND) {
  1738. printk(KERN_INFO "sending response for idx %u failed"
  1739. " with status %d\n", index, wc->status);
  1740. srpt_handle_send_err_comp(ch, wc->wr_id);
  1741. } else if (opcode != SRPT_RDMA_MID) {
  1742. printk(KERN_INFO "RDMA t %d for idx %u failed with"
  1743. " status %d", opcode, index, wc->status);
  1744. srpt_handle_rdma_err_comp(ch, send_ioctx, opcode);
  1745. }
  1746. }
  1747. while (unlikely(opcode == SRPT_SEND
  1748. && !list_empty(&ch->cmd_wait_list)
  1749. && srpt_get_ch_state(ch) == CH_LIVE
  1750. && (send_ioctx = srpt_get_send_ioctx(ch)) != NULL)) {
  1751. struct srpt_recv_ioctx *recv_ioctx;
  1752. recv_ioctx = list_first_entry(&ch->cmd_wait_list,
  1753. struct srpt_recv_ioctx,
  1754. wait_list);
  1755. list_del(&recv_ioctx->wait_list);
  1756. srpt_handle_new_iu(ch, recv_ioctx, send_ioctx);
  1757. }
  1758. }
  1759. static void srpt_process_completion(struct ib_cq *cq, struct srpt_rdma_ch *ch)
  1760. {
  1761. struct ib_wc *const wc = ch->wc;
  1762. int i, n;
  1763. WARN_ON(cq != ch->cq);
  1764. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  1765. while ((n = ib_poll_cq(cq, ARRAY_SIZE(ch->wc), wc)) > 0) {
  1766. for (i = 0; i < n; i++) {
  1767. if (opcode_from_wr_id(wc[i].wr_id) == SRPT_RECV)
  1768. srpt_process_rcv_completion(cq, ch, &wc[i]);
  1769. else
  1770. srpt_process_send_completion(cq, ch, &wc[i]);
  1771. }
  1772. }
  1773. }
  1774. /**
  1775. * srpt_completion() - IB completion queue callback function.
  1776. *
  1777. * Notes:
  1778. * - It is guaranteed that a completion handler will never be invoked
  1779. * concurrently on two different CPUs for the same completion queue. See also
  1780. * Documentation/infiniband/core_locking.txt and the implementation of
  1781. * handle_edge_irq() in kernel/irq/chip.c.
  1782. * - When threaded IRQs are enabled, completion handlers are invoked in thread
  1783. * context instead of interrupt context.
  1784. */
  1785. static void srpt_completion(struct ib_cq *cq, void *ctx)
  1786. {
  1787. struct srpt_rdma_ch *ch = ctx;
  1788. wake_up_interruptible(&ch->wait_queue);
  1789. }
  1790. static int srpt_compl_thread(void *arg)
  1791. {
  1792. struct srpt_rdma_ch *ch;
  1793. /* Hibernation / freezing of the SRPT kernel thread is not supported. */
  1794. current->flags |= PF_NOFREEZE;
  1795. ch = arg;
  1796. BUG_ON(!ch);
  1797. printk(KERN_INFO "Session %s: kernel thread %s (PID %d) started\n",
  1798. ch->sess_name, ch->thread->comm, current->pid);
  1799. while (!kthread_should_stop()) {
  1800. wait_event_interruptible(ch->wait_queue,
  1801. (srpt_process_completion(ch->cq, ch),
  1802. kthread_should_stop()));
  1803. }
  1804. printk(KERN_INFO "Session %s: kernel thread %s (PID %d) stopped\n",
  1805. ch->sess_name, ch->thread->comm, current->pid);
  1806. return 0;
  1807. }
  1808. /**
  1809. * srpt_create_ch_ib() - Create receive and send completion queues.
  1810. */
  1811. static int srpt_create_ch_ib(struct srpt_rdma_ch *ch)
  1812. {
  1813. struct ib_qp_init_attr *qp_init;
  1814. struct srpt_port *sport = ch->sport;
  1815. struct srpt_device *sdev = sport->sdev;
  1816. u32 srp_sq_size = sport->port_attrib.srp_sq_size;
  1817. int ret;
  1818. WARN_ON(ch->rq_size < 1);
  1819. ret = -ENOMEM;
  1820. qp_init = kzalloc(sizeof *qp_init, GFP_KERNEL);
  1821. if (!qp_init)
  1822. goto out;
  1823. retry:
  1824. ch->cq = ib_create_cq(sdev->device, srpt_completion, NULL, ch,
  1825. ch->rq_size + srp_sq_size, 0);
  1826. if (IS_ERR(ch->cq)) {
  1827. ret = PTR_ERR(ch->cq);
  1828. printk(KERN_ERR "failed to create CQ cqe= %d ret= %d\n",
  1829. ch->rq_size + srp_sq_size, ret);
  1830. goto out;
  1831. }
  1832. qp_init->qp_context = (void *)ch;
  1833. qp_init->event_handler
  1834. = (void(*)(struct ib_event *, void*))srpt_qp_event;
  1835. qp_init->send_cq = ch->cq;
  1836. qp_init->recv_cq = ch->cq;
  1837. qp_init->srq = sdev->srq;
  1838. qp_init->sq_sig_type = IB_SIGNAL_REQ_WR;
  1839. qp_init->qp_type = IB_QPT_RC;
  1840. qp_init->cap.max_send_wr = srp_sq_size;
  1841. qp_init->cap.max_send_sge = SRPT_DEF_SG_PER_WQE;
  1842. ch->qp = ib_create_qp(sdev->pd, qp_init);
  1843. if (IS_ERR(ch->qp)) {
  1844. ret = PTR_ERR(ch->qp);
  1845. if (ret == -ENOMEM) {
  1846. srp_sq_size /= 2;
  1847. if (srp_sq_size >= MIN_SRPT_SQ_SIZE) {
  1848. ib_destroy_cq(ch->cq);
  1849. goto retry;
  1850. }
  1851. }
  1852. printk(KERN_ERR "failed to create_qp ret= %d\n", ret);
  1853. goto err_destroy_cq;
  1854. }
  1855. atomic_set(&ch->sq_wr_avail, qp_init->cap.max_send_wr);
  1856. pr_debug("%s: max_cqe= %d max_sge= %d sq_size = %d cm_id= %p\n",
  1857. __func__, ch->cq->cqe, qp_init->cap.max_send_sge,
  1858. qp_init->cap.max_send_wr, ch->cm_id);
  1859. ret = srpt_init_ch_qp(ch, ch->qp);
  1860. if (ret)
  1861. goto err_destroy_qp;
  1862. init_waitqueue_head(&ch->wait_queue);
  1863. pr_debug("creating thread for session %s\n", ch->sess_name);
  1864. ch->thread = kthread_run(srpt_compl_thread, ch, "ib_srpt_compl");
  1865. if (IS_ERR(ch->thread)) {
  1866. printk(KERN_ERR "failed to create kernel thread %ld\n",
  1867. PTR_ERR(ch->thread));
  1868. ch->thread = NULL;
  1869. goto err_destroy_qp;
  1870. }
  1871. out:
  1872. kfree(qp_init);
  1873. return ret;
  1874. err_destroy_qp:
  1875. ib_destroy_qp(ch->qp);
  1876. err_destroy_cq:
  1877. ib_destroy_cq(ch->cq);
  1878. goto out;
  1879. }
  1880. static void srpt_destroy_ch_ib(struct srpt_rdma_ch *ch)
  1881. {
  1882. if (ch->thread)
  1883. kthread_stop(ch->thread);
  1884. ib_destroy_qp(ch->qp);
  1885. ib_destroy_cq(ch->cq);
  1886. }
  1887. /**
  1888. * __srpt_close_ch() - Close an RDMA channel by setting the QP error state.
  1889. *
  1890. * Reset the QP and make sure all resources associated with the channel will
  1891. * be deallocated at an appropriate time.
  1892. *
  1893. * Note: The caller must hold ch->sport->sdev->spinlock.
  1894. */
  1895. static void __srpt_close_ch(struct srpt_rdma_ch *ch)
  1896. {
  1897. struct srpt_device *sdev;
  1898. enum rdma_ch_state prev_state;
  1899. unsigned long flags;
  1900. sdev = ch->sport->sdev;
  1901. spin_lock_irqsave(&ch->spinlock, flags);
  1902. prev_state = ch->state;
  1903. switch (prev_state) {
  1904. case CH_CONNECTING:
  1905. case CH_LIVE:
  1906. ch->state = CH_DISCONNECTING;
  1907. break;
  1908. default:
  1909. break;
  1910. }
  1911. spin_unlock_irqrestore(&ch->spinlock, flags);
  1912. switch (prev_state) {
  1913. case CH_CONNECTING:
  1914. ib_send_cm_rej(ch->cm_id, IB_CM_REJ_NO_RESOURCES, NULL, 0,
  1915. NULL, 0);
  1916. /* fall through */
  1917. case CH_LIVE:
  1918. if (ib_send_cm_dreq(ch->cm_id, NULL, 0) < 0)
  1919. printk(KERN_ERR "sending CM DREQ failed.\n");
  1920. break;
  1921. case CH_DISCONNECTING:
  1922. break;
  1923. case CH_DRAINING:
  1924. case CH_RELEASING:
  1925. break;
  1926. }
  1927. }
  1928. /**
  1929. * srpt_close_ch() - Close an RDMA channel.
  1930. */
  1931. static void srpt_close_ch(struct srpt_rdma_ch *ch)
  1932. {
  1933. struct srpt_device *sdev;
  1934. sdev = ch->sport->sdev;
  1935. spin_lock_irq(&sdev->spinlock);
  1936. __srpt_close_ch(ch);
  1937. spin_unlock_irq(&sdev->spinlock);
  1938. }
  1939. /**
  1940. * srpt_shutdown_session() - Whether or not a session may be shut down.
  1941. */
  1942. static int srpt_shutdown_session(struct se_session *se_sess)
  1943. {
  1944. struct srpt_rdma_ch *ch = se_sess->fabric_sess_ptr;
  1945. unsigned long flags;
  1946. spin_lock_irqsave(&ch->spinlock, flags);
  1947. if (ch->in_shutdown) {
  1948. spin_unlock_irqrestore(&ch->spinlock, flags);
  1949. return true;
  1950. }
  1951. ch->in_shutdown = true;
  1952. target_sess_cmd_list_set_waiting(se_sess);
  1953. spin_unlock_irqrestore(&ch->spinlock, flags);
  1954. return true;
  1955. }
  1956. /**
  1957. * srpt_drain_channel() - Drain a channel by resetting the IB queue pair.
  1958. * @cm_id: Pointer to the CM ID of the channel to be drained.
  1959. *
  1960. * Note: Must be called from inside srpt_cm_handler to avoid a race between
  1961. * accessing sdev->spinlock and the call to kfree(sdev) in srpt_remove_one()
  1962. * (the caller of srpt_cm_handler holds the cm_id spinlock; srpt_remove_one()
  1963. * waits until all target sessions for the associated IB device have been
  1964. * unregistered and target session registration involves a call to
  1965. * ib_destroy_cm_id(), which locks the cm_id spinlock and hence waits until
  1966. * this function has finished).
  1967. */
  1968. static void srpt_drain_channel(struct ib_cm_id *cm_id)
  1969. {
  1970. struct srpt_device *sdev;
  1971. struct srpt_rdma_ch *ch;
  1972. int ret;
  1973. bool do_reset = false;
  1974. WARN_ON_ONCE(irqs_disabled());
  1975. sdev = cm_id->context;
  1976. BUG_ON(!sdev);
  1977. spin_lock_irq(&sdev->spinlock);
  1978. list_for_each_entry(ch, &sdev->rch_list, list) {
  1979. if (ch->cm_id == cm_id) {
  1980. do_reset = srpt_test_and_set_ch_state(ch,
  1981. CH_CONNECTING, CH_DRAINING) ||
  1982. srpt_test_and_set_ch_state(ch,
  1983. CH_LIVE, CH_DRAINING) ||
  1984. srpt_test_and_set_ch_state(ch,
  1985. CH_DISCONNECTING, CH_DRAINING);
  1986. break;
  1987. }
  1988. }
  1989. spin_unlock_irq(&sdev->spinlock);
  1990. if (do_reset) {
  1991. if (ch->sess)
  1992. srpt_shutdown_session(ch->sess);
  1993. ret = srpt_ch_qp_err(ch);
  1994. if (ret < 0)
  1995. printk(KERN_ERR "Setting queue pair in error state"
  1996. " failed: %d\n", ret);
  1997. }
  1998. }
  1999. /**
  2000. * srpt_find_channel() - Look up an RDMA channel.
  2001. * @cm_id: Pointer to the CM ID of the channel to be looked up.
  2002. *
  2003. * Return NULL if no matching RDMA channel has been found.
  2004. */
  2005. static struct srpt_rdma_ch *srpt_find_channel(struct srpt_device *sdev,
  2006. struct ib_cm_id *cm_id)
  2007. {
  2008. struct srpt_rdma_ch *ch;
  2009. bool found;
  2010. WARN_ON_ONCE(irqs_disabled());
  2011. BUG_ON(!sdev);
  2012. found = false;
  2013. spin_lock_irq(&sdev->spinlock);
  2014. list_for_each_entry(ch, &sdev->rch_list, list) {
  2015. if (ch->cm_id == cm_id) {
  2016. found = true;
  2017. break;
  2018. }
  2019. }
  2020. spin_unlock_irq(&sdev->spinlock);
  2021. return found ? ch : NULL;
  2022. }
  2023. /**
  2024. * srpt_release_channel() - Release channel resources.
  2025. *
  2026. * Schedules the actual release because:
  2027. * - Calling the ib_destroy_cm_id() call from inside an IB CM callback would
  2028. * trigger a deadlock.
  2029. * - It is not safe to call TCM transport_* functions from interrupt context.
  2030. */
  2031. static void srpt_release_channel(struct srpt_rdma_ch *ch)
  2032. {
  2033. schedule_work(&ch->release_work);
  2034. }
  2035. static void srpt_release_channel_work(struct work_struct *w)
  2036. {
  2037. struct srpt_rdma_ch *ch;
  2038. struct srpt_device *sdev;
  2039. struct se_session *se_sess;
  2040. ch = container_of(w, struct srpt_rdma_ch, release_work);
  2041. pr_debug("ch = %p; ch->sess = %p; release_done = %p\n", ch, ch->sess,
  2042. ch->release_done);
  2043. sdev = ch->sport->sdev;
  2044. BUG_ON(!sdev);
  2045. se_sess = ch->sess;
  2046. BUG_ON(!se_sess);
  2047. target_wait_for_sess_cmds(se_sess);
  2048. transport_deregister_session_configfs(se_sess);
  2049. transport_deregister_session(se_sess);
  2050. ch->sess = NULL;
  2051. ib_destroy_cm_id(ch->cm_id);
  2052. srpt_destroy_ch_ib(ch);
  2053. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2054. ch->sport->sdev, ch->rq_size,
  2055. ch->rsp_size, DMA_TO_DEVICE);
  2056. spin_lock_irq(&sdev->spinlock);
  2057. list_del(&ch->list);
  2058. spin_unlock_irq(&sdev->spinlock);
  2059. if (ch->release_done)
  2060. complete(ch->release_done);
  2061. wake_up(&sdev->ch_releaseQ);
  2062. kfree(ch);
  2063. }
  2064. static struct srpt_node_acl *__srpt_lookup_acl(struct srpt_port *sport,
  2065. u8 i_port_id[16])
  2066. {
  2067. struct srpt_node_acl *nacl;
  2068. list_for_each_entry(nacl, &sport->port_acl_list, list)
  2069. if (memcmp(nacl->i_port_id, i_port_id,
  2070. sizeof(nacl->i_port_id)) == 0)
  2071. return nacl;
  2072. return NULL;
  2073. }
  2074. static struct srpt_node_acl *srpt_lookup_acl(struct srpt_port *sport,
  2075. u8 i_port_id[16])
  2076. {
  2077. struct srpt_node_acl *nacl;
  2078. spin_lock_irq(&sport->port_acl_lock);
  2079. nacl = __srpt_lookup_acl(sport, i_port_id);
  2080. spin_unlock_irq(&sport->port_acl_lock);
  2081. return nacl;
  2082. }
  2083. /**
  2084. * srpt_cm_req_recv() - Process the event IB_CM_REQ_RECEIVED.
  2085. *
  2086. * Ownership of the cm_id is transferred to the target session if this
  2087. * functions returns zero. Otherwise the caller remains the owner of cm_id.
  2088. */
  2089. static int srpt_cm_req_recv(struct ib_cm_id *cm_id,
  2090. struct ib_cm_req_event_param *param,
  2091. void *private_data)
  2092. {
  2093. struct srpt_device *sdev = cm_id->context;
  2094. struct srpt_port *sport = &sdev->port[param->port - 1];
  2095. struct srp_login_req *req;
  2096. struct srp_login_rsp *rsp;
  2097. struct srp_login_rej *rej;
  2098. struct ib_cm_rep_param *rep_param;
  2099. struct srpt_rdma_ch *ch, *tmp_ch;
  2100. struct srpt_node_acl *nacl;
  2101. u32 it_iu_len;
  2102. int i;
  2103. int ret = 0;
  2104. WARN_ON_ONCE(irqs_disabled());
  2105. if (WARN_ON(!sdev || !private_data))
  2106. return -EINVAL;
  2107. req = (struct srp_login_req *)private_data;
  2108. it_iu_len = be32_to_cpu(req->req_it_iu_len);
  2109. printk(KERN_INFO "Received SRP_LOGIN_REQ with i_port_id 0x%llx:0x%llx,"
  2110. " t_port_id 0x%llx:0x%llx and it_iu_len %d on port %d"
  2111. " (guid=0x%llx:0x%llx)\n",
  2112. be64_to_cpu(*(__be64 *)&req->initiator_port_id[0]),
  2113. be64_to_cpu(*(__be64 *)&req->initiator_port_id[8]),
  2114. be64_to_cpu(*(__be64 *)&req->target_port_id[0]),
  2115. be64_to_cpu(*(__be64 *)&req->target_port_id[8]),
  2116. it_iu_len,
  2117. param->port,
  2118. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[0]),
  2119. be64_to_cpu(*(__be64 *)&sdev->port[param->port - 1].gid.raw[8]));
  2120. rsp = kzalloc(sizeof *rsp, GFP_KERNEL);
  2121. rej = kzalloc(sizeof *rej, GFP_KERNEL);
  2122. rep_param = kzalloc(sizeof *rep_param, GFP_KERNEL);
  2123. if (!rsp || !rej || !rep_param) {
  2124. ret = -ENOMEM;
  2125. goto out;
  2126. }
  2127. if (it_iu_len > srp_max_req_size || it_iu_len < 64) {
  2128. rej->reason = __constant_cpu_to_be32(
  2129. SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE);
  2130. ret = -EINVAL;
  2131. printk(KERN_ERR "rejected SRP_LOGIN_REQ because its"
  2132. " length (%d bytes) is out of range (%d .. %d)\n",
  2133. it_iu_len, 64, srp_max_req_size);
  2134. goto reject;
  2135. }
  2136. if (!sport->enabled) {
  2137. rej->reason = __constant_cpu_to_be32(
  2138. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2139. ret = -EINVAL;
  2140. printk(KERN_ERR "rejected SRP_LOGIN_REQ because the target port"
  2141. " has not yet been enabled\n");
  2142. goto reject;
  2143. }
  2144. if ((req->req_flags & SRP_MTCH_ACTION) == SRP_MULTICHAN_SINGLE) {
  2145. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_NO_CHAN;
  2146. spin_lock_irq(&sdev->spinlock);
  2147. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list) {
  2148. if (!memcmp(ch->i_port_id, req->initiator_port_id, 16)
  2149. && !memcmp(ch->t_port_id, req->target_port_id, 16)
  2150. && param->port == ch->sport->port
  2151. && param->listen_id == ch->sport->sdev->cm_id
  2152. && ch->cm_id) {
  2153. enum rdma_ch_state ch_state;
  2154. ch_state = srpt_get_ch_state(ch);
  2155. if (ch_state != CH_CONNECTING
  2156. && ch_state != CH_LIVE)
  2157. continue;
  2158. /* found an existing channel */
  2159. pr_debug("Found existing channel %s"
  2160. " cm_id= %p state= %d\n",
  2161. ch->sess_name, ch->cm_id, ch_state);
  2162. __srpt_close_ch(ch);
  2163. rsp->rsp_flags =
  2164. SRP_LOGIN_RSP_MULTICHAN_TERMINATED;
  2165. }
  2166. }
  2167. spin_unlock_irq(&sdev->spinlock);
  2168. } else
  2169. rsp->rsp_flags = SRP_LOGIN_RSP_MULTICHAN_MAINTAINED;
  2170. if (*(__be64 *)req->target_port_id != cpu_to_be64(srpt_service_guid)
  2171. || *(__be64 *)(req->target_port_id + 8) !=
  2172. cpu_to_be64(srpt_service_guid)) {
  2173. rej->reason = __constant_cpu_to_be32(
  2174. SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL);
  2175. ret = -ENOMEM;
  2176. printk(KERN_ERR "rejected SRP_LOGIN_REQ because it"
  2177. " has an invalid target port identifier.\n");
  2178. goto reject;
  2179. }
  2180. ch = kzalloc(sizeof *ch, GFP_KERNEL);
  2181. if (!ch) {
  2182. rej->reason = __constant_cpu_to_be32(
  2183. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2184. printk(KERN_ERR "rejected SRP_LOGIN_REQ because no memory.\n");
  2185. ret = -ENOMEM;
  2186. goto reject;
  2187. }
  2188. INIT_WORK(&ch->release_work, srpt_release_channel_work);
  2189. memcpy(ch->i_port_id, req->initiator_port_id, 16);
  2190. memcpy(ch->t_port_id, req->target_port_id, 16);
  2191. ch->sport = &sdev->port[param->port - 1];
  2192. ch->cm_id = cm_id;
  2193. /*
  2194. * Avoid QUEUE_FULL conditions by limiting the number of buffers used
  2195. * for the SRP protocol to the command queue size.
  2196. */
  2197. ch->rq_size = SRPT_RQ_SIZE;
  2198. spin_lock_init(&ch->spinlock);
  2199. ch->state = CH_CONNECTING;
  2200. INIT_LIST_HEAD(&ch->cmd_wait_list);
  2201. ch->rsp_size = ch->sport->port_attrib.srp_max_rsp_size;
  2202. ch->ioctx_ring = (struct srpt_send_ioctx **)
  2203. srpt_alloc_ioctx_ring(ch->sport->sdev, ch->rq_size,
  2204. sizeof(*ch->ioctx_ring[0]),
  2205. ch->rsp_size, DMA_TO_DEVICE);
  2206. if (!ch->ioctx_ring)
  2207. goto free_ch;
  2208. INIT_LIST_HEAD(&ch->free_list);
  2209. for (i = 0; i < ch->rq_size; i++) {
  2210. ch->ioctx_ring[i]->ch = ch;
  2211. list_add_tail(&ch->ioctx_ring[i]->free_list, &ch->free_list);
  2212. }
  2213. ret = srpt_create_ch_ib(ch);
  2214. if (ret) {
  2215. rej->reason = __constant_cpu_to_be32(
  2216. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2217. printk(KERN_ERR "rejected SRP_LOGIN_REQ because creating"
  2218. " a new RDMA channel failed.\n");
  2219. goto free_ring;
  2220. }
  2221. ret = srpt_ch_qp_rtr(ch, ch->qp);
  2222. if (ret) {
  2223. rej->reason = __constant_cpu_to_be32(
  2224. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2225. printk(KERN_ERR "rejected SRP_LOGIN_REQ because enabling"
  2226. " RTR failed (error code = %d)\n", ret);
  2227. goto destroy_ib;
  2228. }
  2229. /*
  2230. * Use the initator port identifier as the session name.
  2231. */
  2232. snprintf(ch->sess_name, sizeof(ch->sess_name), "0x%016llx%016llx",
  2233. be64_to_cpu(*(__be64 *)ch->i_port_id),
  2234. be64_to_cpu(*(__be64 *)(ch->i_port_id + 8)));
  2235. pr_debug("registering session %s\n", ch->sess_name);
  2236. nacl = srpt_lookup_acl(sport, ch->i_port_id);
  2237. if (!nacl) {
  2238. printk(KERN_INFO "Rejected login because no ACL has been"
  2239. " configured yet for initiator %s.\n", ch->sess_name);
  2240. rej->reason = __constant_cpu_to_be32(
  2241. SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED);
  2242. goto destroy_ib;
  2243. }
  2244. ch->sess = transport_init_session(TARGET_PROT_NORMAL);
  2245. if (IS_ERR(ch->sess)) {
  2246. rej->reason = __constant_cpu_to_be32(
  2247. SRP_LOGIN_REJ_INSUFFICIENT_RESOURCES);
  2248. pr_debug("Failed to create session\n");
  2249. goto deregister_session;
  2250. }
  2251. ch->sess->se_node_acl = &nacl->nacl;
  2252. transport_register_session(&sport->port_tpg_1, &nacl->nacl, ch->sess, ch);
  2253. pr_debug("Establish connection sess=%p name=%s cm_id=%p\n", ch->sess,
  2254. ch->sess_name, ch->cm_id);
  2255. /* create srp_login_response */
  2256. rsp->opcode = SRP_LOGIN_RSP;
  2257. rsp->tag = req->tag;
  2258. rsp->max_it_iu_len = req->req_it_iu_len;
  2259. rsp->max_ti_iu_len = req->req_it_iu_len;
  2260. ch->max_ti_iu_len = it_iu_len;
  2261. rsp->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  2262. | SRP_BUF_FORMAT_INDIRECT);
  2263. rsp->req_lim_delta = cpu_to_be32(ch->rq_size);
  2264. atomic_set(&ch->req_lim, ch->rq_size);
  2265. atomic_set(&ch->req_lim_delta, 0);
  2266. /* create cm reply */
  2267. rep_param->qp_num = ch->qp->qp_num;
  2268. rep_param->private_data = (void *)rsp;
  2269. rep_param->private_data_len = sizeof *rsp;
  2270. rep_param->rnr_retry_count = 7;
  2271. rep_param->flow_control = 1;
  2272. rep_param->failover_accepted = 0;
  2273. rep_param->srq = 1;
  2274. rep_param->responder_resources = 4;
  2275. rep_param->initiator_depth = 4;
  2276. ret = ib_send_cm_rep(cm_id, rep_param);
  2277. if (ret) {
  2278. printk(KERN_ERR "sending SRP_LOGIN_REQ response failed"
  2279. " (error code = %d)\n", ret);
  2280. goto release_channel;
  2281. }
  2282. spin_lock_irq(&sdev->spinlock);
  2283. list_add_tail(&ch->list, &sdev->rch_list);
  2284. spin_unlock_irq(&sdev->spinlock);
  2285. goto out;
  2286. release_channel:
  2287. srpt_set_ch_state(ch, CH_RELEASING);
  2288. transport_deregister_session_configfs(ch->sess);
  2289. deregister_session:
  2290. transport_deregister_session(ch->sess);
  2291. ch->sess = NULL;
  2292. destroy_ib:
  2293. srpt_destroy_ch_ib(ch);
  2294. free_ring:
  2295. srpt_free_ioctx_ring((struct srpt_ioctx **)ch->ioctx_ring,
  2296. ch->sport->sdev, ch->rq_size,
  2297. ch->rsp_size, DMA_TO_DEVICE);
  2298. free_ch:
  2299. kfree(ch);
  2300. reject:
  2301. rej->opcode = SRP_LOGIN_REJ;
  2302. rej->tag = req->tag;
  2303. rej->buf_fmt = __constant_cpu_to_be16(SRP_BUF_FORMAT_DIRECT
  2304. | SRP_BUF_FORMAT_INDIRECT);
  2305. ib_send_cm_rej(cm_id, IB_CM_REJ_CONSUMER_DEFINED, NULL, 0,
  2306. (void *)rej, sizeof *rej);
  2307. out:
  2308. kfree(rep_param);
  2309. kfree(rsp);
  2310. kfree(rej);
  2311. return ret;
  2312. }
  2313. static void srpt_cm_rej_recv(struct ib_cm_id *cm_id)
  2314. {
  2315. printk(KERN_INFO "Received IB REJ for cm_id %p.\n", cm_id);
  2316. srpt_drain_channel(cm_id);
  2317. }
  2318. /**
  2319. * srpt_cm_rtu_recv() - Process an IB_CM_RTU_RECEIVED or USER_ESTABLISHED event.
  2320. *
  2321. * An IB_CM_RTU_RECEIVED message indicates that the connection is established
  2322. * and that the recipient may begin transmitting (RTU = ready to use).
  2323. */
  2324. static void srpt_cm_rtu_recv(struct ib_cm_id *cm_id)
  2325. {
  2326. struct srpt_rdma_ch *ch;
  2327. int ret;
  2328. ch = srpt_find_channel(cm_id->context, cm_id);
  2329. BUG_ON(!ch);
  2330. if (srpt_test_and_set_ch_state(ch, CH_CONNECTING, CH_LIVE)) {
  2331. struct srpt_recv_ioctx *ioctx, *ioctx_tmp;
  2332. ret = srpt_ch_qp_rts(ch, ch->qp);
  2333. list_for_each_entry_safe(ioctx, ioctx_tmp, &ch->cmd_wait_list,
  2334. wait_list) {
  2335. list_del(&ioctx->wait_list);
  2336. srpt_handle_new_iu(ch, ioctx, NULL);
  2337. }
  2338. if (ret)
  2339. srpt_close_ch(ch);
  2340. }
  2341. }
  2342. static void srpt_cm_timewait_exit(struct ib_cm_id *cm_id)
  2343. {
  2344. printk(KERN_INFO "Received IB TimeWait exit for cm_id %p.\n", cm_id);
  2345. srpt_drain_channel(cm_id);
  2346. }
  2347. static void srpt_cm_rep_error(struct ib_cm_id *cm_id)
  2348. {
  2349. printk(KERN_INFO "Received IB REP error for cm_id %p.\n", cm_id);
  2350. srpt_drain_channel(cm_id);
  2351. }
  2352. /**
  2353. * srpt_cm_dreq_recv() - Process reception of a DREQ message.
  2354. */
  2355. static void srpt_cm_dreq_recv(struct ib_cm_id *cm_id)
  2356. {
  2357. struct srpt_rdma_ch *ch;
  2358. unsigned long flags;
  2359. bool send_drep = false;
  2360. ch = srpt_find_channel(cm_id->context, cm_id);
  2361. BUG_ON(!ch);
  2362. pr_debug("cm_id= %p ch->state= %d\n", cm_id, srpt_get_ch_state(ch));
  2363. spin_lock_irqsave(&ch->spinlock, flags);
  2364. switch (ch->state) {
  2365. case CH_CONNECTING:
  2366. case CH_LIVE:
  2367. send_drep = true;
  2368. ch->state = CH_DISCONNECTING;
  2369. break;
  2370. case CH_DISCONNECTING:
  2371. case CH_DRAINING:
  2372. case CH_RELEASING:
  2373. WARN(true, "unexpected channel state %d\n", ch->state);
  2374. break;
  2375. }
  2376. spin_unlock_irqrestore(&ch->spinlock, flags);
  2377. if (send_drep) {
  2378. if (ib_send_cm_drep(ch->cm_id, NULL, 0) < 0)
  2379. printk(KERN_ERR "Sending IB DREP failed.\n");
  2380. printk(KERN_INFO "Received DREQ and sent DREP for session %s.\n",
  2381. ch->sess_name);
  2382. }
  2383. }
  2384. /**
  2385. * srpt_cm_drep_recv() - Process reception of a DREP message.
  2386. */
  2387. static void srpt_cm_drep_recv(struct ib_cm_id *cm_id)
  2388. {
  2389. printk(KERN_INFO "Received InfiniBand DREP message for cm_id %p.\n",
  2390. cm_id);
  2391. srpt_drain_channel(cm_id);
  2392. }
  2393. /**
  2394. * srpt_cm_handler() - IB connection manager callback function.
  2395. *
  2396. * A non-zero return value will cause the caller destroy the CM ID.
  2397. *
  2398. * Note: srpt_cm_handler() must only return a non-zero value when transferring
  2399. * ownership of the cm_id to a channel by srpt_cm_req_recv() failed. Returning
  2400. * a non-zero value in any other case will trigger a race with the
  2401. * ib_destroy_cm_id() call in srpt_release_channel().
  2402. */
  2403. static int srpt_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  2404. {
  2405. int ret;
  2406. ret = 0;
  2407. switch (event->event) {
  2408. case IB_CM_REQ_RECEIVED:
  2409. ret = srpt_cm_req_recv(cm_id, &event->param.req_rcvd,
  2410. event->private_data);
  2411. break;
  2412. case IB_CM_REJ_RECEIVED:
  2413. srpt_cm_rej_recv(cm_id);
  2414. break;
  2415. case IB_CM_RTU_RECEIVED:
  2416. case IB_CM_USER_ESTABLISHED:
  2417. srpt_cm_rtu_recv(cm_id);
  2418. break;
  2419. case IB_CM_DREQ_RECEIVED:
  2420. srpt_cm_dreq_recv(cm_id);
  2421. break;
  2422. case IB_CM_DREP_RECEIVED:
  2423. srpt_cm_drep_recv(cm_id);
  2424. break;
  2425. case IB_CM_TIMEWAIT_EXIT:
  2426. srpt_cm_timewait_exit(cm_id);
  2427. break;
  2428. case IB_CM_REP_ERROR:
  2429. srpt_cm_rep_error(cm_id);
  2430. break;
  2431. case IB_CM_DREQ_ERROR:
  2432. printk(KERN_INFO "Received IB DREQ ERROR event.\n");
  2433. break;
  2434. case IB_CM_MRA_RECEIVED:
  2435. printk(KERN_INFO "Received IB MRA event\n");
  2436. break;
  2437. default:
  2438. printk(KERN_ERR "received unrecognized IB CM event %d\n",
  2439. event->event);
  2440. break;
  2441. }
  2442. return ret;
  2443. }
  2444. /**
  2445. * srpt_perform_rdmas() - Perform IB RDMA.
  2446. *
  2447. * Returns zero upon success or a negative number upon failure.
  2448. */
  2449. static int srpt_perform_rdmas(struct srpt_rdma_ch *ch,
  2450. struct srpt_send_ioctx *ioctx)
  2451. {
  2452. struct ib_send_wr wr;
  2453. struct ib_send_wr *bad_wr;
  2454. struct rdma_iu *riu;
  2455. int i;
  2456. int ret;
  2457. int sq_wr_avail;
  2458. enum dma_data_direction dir;
  2459. const int n_rdma = ioctx->n_rdma;
  2460. dir = ioctx->cmd.data_direction;
  2461. if (dir == DMA_TO_DEVICE) {
  2462. /* write */
  2463. ret = -ENOMEM;
  2464. sq_wr_avail = atomic_sub_return(n_rdma, &ch->sq_wr_avail);
  2465. if (sq_wr_avail < 0) {
  2466. printk(KERN_WARNING "IB send queue full (needed %d)\n",
  2467. n_rdma);
  2468. goto out;
  2469. }
  2470. }
  2471. ioctx->rdma_aborted = false;
  2472. ret = 0;
  2473. riu = ioctx->rdma_ius;
  2474. memset(&wr, 0, sizeof wr);
  2475. for (i = 0; i < n_rdma; ++i, ++riu) {
  2476. if (dir == DMA_FROM_DEVICE) {
  2477. wr.opcode = IB_WR_RDMA_WRITE;
  2478. wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
  2479. SRPT_RDMA_WRITE_LAST :
  2480. SRPT_RDMA_MID,
  2481. ioctx->ioctx.index);
  2482. } else {
  2483. wr.opcode = IB_WR_RDMA_READ;
  2484. wr.wr_id = encode_wr_id(i == n_rdma - 1 ?
  2485. SRPT_RDMA_READ_LAST :
  2486. SRPT_RDMA_MID,
  2487. ioctx->ioctx.index);
  2488. }
  2489. wr.next = NULL;
  2490. wr.wr.rdma.remote_addr = riu->raddr;
  2491. wr.wr.rdma.rkey = riu->rkey;
  2492. wr.num_sge = riu->sge_cnt;
  2493. wr.sg_list = riu->sge;
  2494. /* only get completion event for the last rdma write */
  2495. if (i == (n_rdma - 1) && dir == DMA_TO_DEVICE)
  2496. wr.send_flags = IB_SEND_SIGNALED;
  2497. ret = ib_post_send(ch->qp, &wr, &bad_wr);
  2498. if (ret)
  2499. break;
  2500. }
  2501. if (ret)
  2502. printk(KERN_ERR "%s[%d]: ib_post_send() returned %d for %d/%d",
  2503. __func__, __LINE__, ret, i, n_rdma);
  2504. if (ret && i > 0) {
  2505. wr.num_sge = 0;
  2506. wr.wr_id = encode_wr_id(SRPT_RDMA_ABORT, ioctx->ioctx.index);
  2507. wr.send_flags = IB_SEND_SIGNALED;
  2508. while (ch->state == CH_LIVE &&
  2509. ib_post_send(ch->qp, &wr, &bad_wr) != 0) {
  2510. printk(KERN_INFO "Trying to abort failed RDMA transfer [%d]",
  2511. ioctx->ioctx.index);
  2512. msleep(1000);
  2513. }
  2514. while (ch->state != CH_RELEASING && !ioctx->rdma_aborted) {
  2515. printk(KERN_INFO "Waiting until RDMA abort finished [%d]",
  2516. ioctx->ioctx.index);
  2517. msleep(1000);
  2518. }
  2519. }
  2520. out:
  2521. if (unlikely(dir == DMA_TO_DEVICE && ret < 0))
  2522. atomic_add(n_rdma, &ch->sq_wr_avail);
  2523. return ret;
  2524. }
  2525. /**
  2526. * srpt_xfer_data() - Start data transfer from initiator to target.
  2527. */
  2528. static int srpt_xfer_data(struct srpt_rdma_ch *ch,
  2529. struct srpt_send_ioctx *ioctx)
  2530. {
  2531. int ret;
  2532. ret = srpt_map_sg_to_ib_sge(ch, ioctx);
  2533. if (ret) {
  2534. printk(KERN_ERR "%s[%d] ret=%d\n", __func__, __LINE__, ret);
  2535. goto out;
  2536. }
  2537. ret = srpt_perform_rdmas(ch, ioctx);
  2538. if (ret) {
  2539. if (ret == -EAGAIN || ret == -ENOMEM)
  2540. printk(KERN_INFO "%s[%d] queue full -- ret=%d\n",
  2541. __func__, __LINE__, ret);
  2542. else
  2543. printk(KERN_ERR "%s[%d] fatal error -- ret=%d\n",
  2544. __func__, __LINE__, ret);
  2545. goto out_unmap;
  2546. }
  2547. out:
  2548. return ret;
  2549. out_unmap:
  2550. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2551. goto out;
  2552. }
  2553. static int srpt_write_pending_status(struct se_cmd *se_cmd)
  2554. {
  2555. struct srpt_send_ioctx *ioctx;
  2556. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2557. return srpt_get_cmd_state(ioctx) == SRPT_STATE_NEED_DATA;
  2558. }
  2559. /*
  2560. * srpt_write_pending() - Start data transfer from initiator to target (write).
  2561. */
  2562. static int srpt_write_pending(struct se_cmd *se_cmd)
  2563. {
  2564. struct srpt_rdma_ch *ch;
  2565. struct srpt_send_ioctx *ioctx;
  2566. enum srpt_command_state new_state;
  2567. enum rdma_ch_state ch_state;
  2568. int ret;
  2569. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  2570. new_state = srpt_set_cmd_state(ioctx, SRPT_STATE_NEED_DATA);
  2571. WARN_ON(new_state == SRPT_STATE_DONE);
  2572. ch = ioctx->ch;
  2573. BUG_ON(!ch);
  2574. ch_state = srpt_get_ch_state(ch);
  2575. switch (ch_state) {
  2576. case CH_CONNECTING:
  2577. WARN(true, "unexpected channel state %d\n", ch_state);
  2578. ret = -EINVAL;
  2579. goto out;
  2580. case CH_LIVE:
  2581. break;
  2582. case CH_DISCONNECTING:
  2583. case CH_DRAINING:
  2584. case CH_RELEASING:
  2585. pr_debug("cmd with tag %lld: channel disconnecting\n",
  2586. ioctx->tag);
  2587. srpt_set_cmd_state(ioctx, SRPT_STATE_DATA_IN);
  2588. ret = -EINVAL;
  2589. goto out;
  2590. }
  2591. ret = srpt_xfer_data(ch, ioctx);
  2592. out:
  2593. return ret;
  2594. }
  2595. static u8 tcm_to_srp_tsk_mgmt_status(const int tcm_mgmt_status)
  2596. {
  2597. switch (tcm_mgmt_status) {
  2598. case TMR_FUNCTION_COMPLETE:
  2599. return SRP_TSK_MGMT_SUCCESS;
  2600. case TMR_FUNCTION_REJECTED:
  2601. return SRP_TSK_MGMT_FUNC_NOT_SUPP;
  2602. }
  2603. return SRP_TSK_MGMT_FAILED;
  2604. }
  2605. /**
  2606. * srpt_queue_response() - Transmits the response to a SCSI command.
  2607. *
  2608. * Callback function called by the TCM core. Must not block since it can be
  2609. * invoked on the context of the IB completion handler.
  2610. */
  2611. static void srpt_queue_response(struct se_cmd *cmd)
  2612. {
  2613. struct srpt_rdma_ch *ch;
  2614. struct srpt_send_ioctx *ioctx;
  2615. enum srpt_command_state state;
  2616. unsigned long flags;
  2617. int ret;
  2618. enum dma_data_direction dir;
  2619. int resp_len;
  2620. u8 srp_tm_status;
  2621. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2622. ch = ioctx->ch;
  2623. BUG_ON(!ch);
  2624. spin_lock_irqsave(&ioctx->spinlock, flags);
  2625. state = ioctx->state;
  2626. switch (state) {
  2627. case SRPT_STATE_NEW:
  2628. case SRPT_STATE_DATA_IN:
  2629. ioctx->state = SRPT_STATE_CMD_RSP_SENT;
  2630. break;
  2631. case SRPT_STATE_MGMT:
  2632. ioctx->state = SRPT_STATE_MGMT_RSP_SENT;
  2633. break;
  2634. default:
  2635. WARN(true, "ch %p; cmd %d: unexpected command state %d\n",
  2636. ch, ioctx->ioctx.index, ioctx->state);
  2637. break;
  2638. }
  2639. spin_unlock_irqrestore(&ioctx->spinlock, flags);
  2640. if (unlikely(transport_check_aborted_status(&ioctx->cmd, false)
  2641. || WARN_ON_ONCE(state == SRPT_STATE_CMD_RSP_SENT))) {
  2642. atomic_inc(&ch->req_lim_delta);
  2643. srpt_abort_cmd(ioctx);
  2644. return;
  2645. }
  2646. dir = ioctx->cmd.data_direction;
  2647. /* For read commands, transfer the data to the initiator. */
  2648. if (dir == DMA_FROM_DEVICE && ioctx->cmd.data_length &&
  2649. !ioctx->queue_status_only) {
  2650. ret = srpt_xfer_data(ch, ioctx);
  2651. if (ret) {
  2652. printk(KERN_ERR "xfer_data failed for tag %llu\n",
  2653. ioctx->tag);
  2654. return;
  2655. }
  2656. }
  2657. if (state != SRPT_STATE_MGMT)
  2658. resp_len = srpt_build_cmd_rsp(ch, ioctx, ioctx->tag,
  2659. cmd->scsi_status);
  2660. else {
  2661. srp_tm_status
  2662. = tcm_to_srp_tsk_mgmt_status(cmd->se_tmr_req->response);
  2663. resp_len = srpt_build_tskmgmt_rsp(ch, ioctx, srp_tm_status,
  2664. ioctx->tag);
  2665. }
  2666. ret = srpt_post_send(ch, ioctx, resp_len);
  2667. if (ret) {
  2668. printk(KERN_ERR "sending cmd response failed for tag %llu\n",
  2669. ioctx->tag);
  2670. srpt_unmap_sg_to_ib_sge(ch, ioctx);
  2671. srpt_set_cmd_state(ioctx, SRPT_STATE_DONE);
  2672. target_put_sess_cmd(ioctx->ch->sess, &ioctx->cmd);
  2673. }
  2674. }
  2675. static int srpt_queue_data_in(struct se_cmd *cmd)
  2676. {
  2677. srpt_queue_response(cmd);
  2678. return 0;
  2679. }
  2680. static void srpt_queue_tm_rsp(struct se_cmd *cmd)
  2681. {
  2682. srpt_queue_response(cmd);
  2683. }
  2684. static void srpt_aborted_task(struct se_cmd *cmd)
  2685. {
  2686. struct srpt_send_ioctx *ioctx = container_of(cmd,
  2687. struct srpt_send_ioctx, cmd);
  2688. srpt_unmap_sg_to_ib_sge(ioctx->ch, ioctx);
  2689. }
  2690. static int srpt_queue_status(struct se_cmd *cmd)
  2691. {
  2692. struct srpt_send_ioctx *ioctx;
  2693. ioctx = container_of(cmd, struct srpt_send_ioctx, cmd);
  2694. BUG_ON(ioctx->sense_data != cmd->sense_buffer);
  2695. if (cmd->se_cmd_flags &
  2696. (SCF_TRANSPORT_TASK_SENSE | SCF_EMULATED_TASK_SENSE))
  2697. WARN_ON(cmd->scsi_status != SAM_STAT_CHECK_CONDITION);
  2698. ioctx->queue_status_only = true;
  2699. srpt_queue_response(cmd);
  2700. return 0;
  2701. }
  2702. static void srpt_refresh_port_work(struct work_struct *work)
  2703. {
  2704. struct srpt_port *sport = container_of(work, struct srpt_port, work);
  2705. srpt_refresh_port(sport);
  2706. }
  2707. static int srpt_ch_list_empty(struct srpt_device *sdev)
  2708. {
  2709. int res;
  2710. spin_lock_irq(&sdev->spinlock);
  2711. res = list_empty(&sdev->rch_list);
  2712. spin_unlock_irq(&sdev->spinlock);
  2713. return res;
  2714. }
  2715. /**
  2716. * srpt_release_sdev() - Free the channel resources associated with a target.
  2717. */
  2718. static int srpt_release_sdev(struct srpt_device *sdev)
  2719. {
  2720. struct srpt_rdma_ch *ch, *tmp_ch;
  2721. int res;
  2722. WARN_ON_ONCE(irqs_disabled());
  2723. BUG_ON(!sdev);
  2724. spin_lock_irq(&sdev->spinlock);
  2725. list_for_each_entry_safe(ch, tmp_ch, &sdev->rch_list, list)
  2726. __srpt_close_ch(ch);
  2727. spin_unlock_irq(&sdev->spinlock);
  2728. res = wait_event_interruptible(sdev->ch_releaseQ,
  2729. srpt_ch_list_empty(sdev));
  2730. if (res)
  2731. printk(KERN_ERR "%s: interrupted.\n", __func__);
  2732. return 0;
  2733. }
  2734. static struct srpt_port *__srpt_lookup_port(const char *name)
  2735. {
  2736. struct ib_device *dev;
  2737. struct srpt_device *sdev;
  2738. struct srpt_port *sport;
  2739. int i;
  2740. list_for_each_entry(sdev, &srpt_dev_list, list) {
  2741. dev = sdev->device;
  2742. if (!dev)
  2743. continue;
  2744. for (i = 0; i < dev->phys_port_cnt; i++) {
  2745. sport = &sdev->port[i];
  2746. if (!strcmp(sport->port_guid, name))
  2747. return sport;
  2748. }
  2749. }
  2750. return NULL;
  2751. }
  2752. static struct srpt_port *srpt_lookup_port(const char *name)
  2753. {
  2754. struct srpt_port *sport;
  2755. spin_lock(&srpt_dev_lock);
  2756. sport = __srpt_lookup_port(name);
  2757. spin_unlock(&srpt_dev_lock);
  2758. return sport;
  2759. }
  2760. /**
  2761. * srpt_add_one() - Infiniband device addition callback function.
  2762. */
  2763. static void srpt_add_one(struct ib_device *device)
  2764. {
  2765. struct srpt_device *sdev;
  2766. struct srpt_port *sport;
  2767. struct ib_srq_init_attr srq_attr;
  2768. int i;
  2769. pr_debug("device = %p, device->dma_ops = %p\n", device,
  2770. device->dma_ops);
  2771. sdev = kzalloc(sizeof *sdev, GFP_KERNEL);
  2772. if (!sdev)
  2773. goto err;
  2774. sdev->device = device;
  2775. INIT_LIST_HEAD(&sdev->rch_list);
  2776. init_waitqueue_head(&sdev->ch_releaseQ);
  2777. spin_lock_init(&sdev->spinlock);
  2778. if (ib_query_device(device, &sdev->dev_attr))
  2779. goto free_dev;
  2780. sdev->pd = ib_alloc_pd(device);
  2781. if (IS_ERR(sdev->pd))
  2782. goto free_dev;
  2783. sdev->mr = ib_get_dma_mr(sdev->pd, IB_ACCESS_LOCAL_WRITE);
  2784. if (IS_ERR(sdev->mr))
  2785. goto err_pd;
  2786. sdev->srq_size = min(srpt_srq_size, sdev->dev_attr.max_srq_wr);
  2787. srq_attr.event_handler = srpt_srq_event;
  2788. srq_attr.srq_context = (void *)sdev;
  2789. srq_attr.attr.max_wr = sdev->srq_size;
  2790. srq_attr.attr.max_sge = 1;
  2791. srq_attr.attr.srq_limit = 0;
  2792. srq_attr.srq_type = IB_SRQT_BASIC;
  2793. sdev->srq = ib_create_srq(sdev->pd, &srq_attr);
  2794. if (IS_ERR(sdev->srq))
  2795. goto err_mr;
  2796. pr_debug("%s: create SRQ #wr= %d max_allow=%d dev= %s\n",
  2797. __func__, sdev->srq_size, sdev->dev_attr.max_srq_wr,
  2798. device->name);
  2799. if (!srpt_service_guid)
  2800. srpt_service_guid = be64_to_cpu(device->node_guid);
  2801. sdev->cm_id = ib_create_cm_id(device, srpt_cm_handler, sdev);
  2802. if (IS_ERR(sdev->cm_id))
  2803. goto err_srq;
  2804. /* print out target login information */
  2805. pr_debug("Target login info: id_ext=%016llx,ioc_guid=%016llx,"
  2806. "pkey=ffff,service_id=%016llx\n", srpt_service_guid,
  2807. srpt_service_guid, srpt_service_guid);
  2808. /*
  2809. * We do not have a consistent service_id (ie. also id_ext of target_id)
  2810. * to identify this target. We currently use the guid of the first HCA
  2811. * in the system as service_id; therefore, the target_id will change
  2812. * if this HCA is gone bad and replaced by different HCA
  2813. */
  2814. if (ib_cm_listen(sdev->cm_id, cpu_to_be64(srpt_service_guid), 0, NULL))
  2815. goto err_cm;
  2816. INIT_IB_EVENT_HANDLER(&sdev->event_handler, sdev->device,
  2817. srpt_event_handler);
  2818. if (ib_register_event_handler(&sdev->event_handler))
  2819. goto err_cm;
  2820. sdev->ioctx_ring = (struct srpt_recv_ioctx **)
  2821. srpt_alloc_ioctx_ring(sdev, sdev->srq_size,
  2822. sizeof(*sdev->ioctx_ring[0]),
  2823. srp_max_req_size, DMA_FROM_DEVICE);
  2824. if (!sdev->ioctx_ring)
  2825. goto err_event;
  2826. for (i = 0; i < sdev->srq_size; ++i)
  2827. srpt_post_recv(sdev, sdev->ioctx_ring[i]);
  2828. WARN_ON(sdev->device->phys_port_cnt > ARRAY_SIZE(sdev->port));
  2829. for (i = 1; i <= sdev->device->phys_port_cnt; i++) {
  2830. sport = &sdev->port[i - 1];
  2831. sport->sdev = sdev;
  2832. sport->port = i;
  2833. sport->port_attrib.srp_max_rdma_size = DEFAULT_MAX_RDMA_SIZE;
  2834. sport->port_attrib.srp_max_rsp_size = DEFAULT_MAX_RSP_SIZE;
  2835. sport->port_attrib.srp_sq_size = DEF_SRPT_SQ_SIZE;
  2836. INIT_WORK(&sport->work, srpt_refresh_port_work);
  2837. INIT_LIST_HEAD(&sport->port_acl_list);
  2838. spin_lock_init(&sport->port_acl_lock);
  2839. if (srpt_refresh_port(sport)) {
  2840. printk(KERN_ERR "MAD registration failed for %s-%d.\n",
  2841. srpt_sdev_name(sdev), i);
  2842. goto err_ring;
  2843. }
  2844. snprintf(sport->port_guid, sizeof(sport->port_guid),
  2845. "0x%016llx%016llx",
  2846. be64_to_cpu(sport->gid.global.subnet_prefix),
  2847. be64_to_cpu(sport->gid.global.interface_id));
  2848. }
  2849. spin_lock(&srpt_dev_lock);
  2850. list_add_tail(&sdev->list, &srpt_dev_list);
  2851. spin_unlock(&srpt_dev_lock);
  2852. out:
  2853. ib_set_client_data(device, &srpt_client, sdev);
  2854. pr_debug("added %s.\n", device->name);
  2855. return;
  2856. err_ring:
  2857. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2858. sdev->srq_size, srp_max_req_size,
  2859. DMA_FROM_DEVICE);
  2860. err_event:
  2861. ib_unregister_event_handler(&sdev->event_handler);
  2862. err_cm:
  2863. ib_destroy_cm_id(sdev->cm_id);
  2864. err_srq:
  2865. ib_destroy_srq(sdev->srq);
  2866. err_mr:
  2867. ib_dereg_mr(sdev->mr);
  2868. err_pd:
  2869. ib_dealloc_pd(sdev->pd);
  2870. free_dev:
  2871. kfree(sdev);
  2872. err:
  2873. sdev = NULL;
  2874. printk(KERN_INFO "%s(%s) failed.\n", __func__, device->name);
  2875. goto out;
  2876. }
  2877. /**
  2878. * srpt_remove_one() - InfiniBand device removal callback function.
  2879. */
  2880. static void srpt_remove_one(struct ib_device *device)
  2881. {
  2882. struct srpt_device *sdev;
  2883. int i;
  2884. sdev = ib_get_client_data(device, &srpt_client);
  2885. if (!sdev) {
  2886. printk(KERN_INFO "%s(%s): nothing to do.\n", __func__,
  2887. device->name);
  2888. return;
  2889. }
  2890. srpt_unregister_mad_agent(sdev);
  2891. ib_unregister_event_handler(&sdev->event_handler);
  2892. /* Cancel any work queued by the just unregistered IB event handler. */
  2893. for (i = 0; i < sdev->device->phys_port_cnt; i++)
  2894. cancel_work_sync(&sdev->port[i].work);
  2895. ib_destroy_cm_id(sdev->cm_id);
  2896. /*
  2897. * Unregistering a target must happen after destroying sdev->cm_id
  2898. * such that no new SRP_LOGIN_REQ information units can arrive while
  2899. * destroying the target.
  2900. */
  2901. spin_lock(&srpt_dev_lock);
  2902. list_del(&sdev->list);
  2903. spin_unlock(&srpt_dev_lock);
  2904. srpt_release_sdev(sdev);
  2905. ib_destroy_srq(sdev->srq);
  2906. ib_dereg_mr(sdev->mr);
  2907. ib_dealloc_pd(sdev->pd);
  2908. srpt_free_ioctx_ring((struct srpt_ioctx **)sdev->ioctx_ring, sdev,
  2909. sdev->srq_size, srp_max_req_size, DMA_FROM_DEVICE);
  2910. sdev->ioctx_ring = NULL;
  2911. kfree(sdev);
  2912. }
  2913. static struct ib_client srpt_client = {
  2914. .name = DRV_NAME,
  2915. .add = srpt_add_one,
  2916. .remove = srpt_remove_one
  2917. };
  2918. static int srpt_check_true(struct se_portal_group *se_tpg)
  2919. {
  2920. return 1;
  2921. }
  2922. static int srpt_check_false(struct se_portal_group *se_tpg)
  2923. {
  2924. return 0;
  2925. }
  2926. static char *srpt_get_fabric_name(void)
  2927. {
  2928. return "srpt";
  2929. }
  2930. static u8 srpt_get_fabric_proto_ident(struct se_portal_group *se_tpg)
  2931. {
  2932. return SCSI_TRANSPORTID_PROTOCOLID_SRP;
  2933. }
  2934. static char *srpt_get_fabric_wwn(struct se_portal_group *tpg)
  2935. {
  2936. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  2937. return sport->port_guid;
  2938. }
  2939. static u16 srpt_get_tag(struct se_portal_group *tpg)
  2940. {
  2941. return 1;
  2942. }
  2943. static u32 srpt_get_default_depth(struct se_portal_group *se_tpg)
  2944. {
  2945. return 1;
  2946. }
  2947. static u32 srpt_get_pr_transport_id(struct se_portal_group *se_tpg,
  2948. struct se_node_acl *se_nacl,
  2949. struct t10_pr_registration *pr_reg,
  2950. int *format_code, unsigned char *buf)
  2951. {
  2952. struct srpt_node_acl *nacl;
  2953. struct spc_rdma_transport_id *tr_id;
  2954. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  2955. tr_id = (void *)buf;
  2956. tr_id->protocol_identifier = SCSI_TRANSPORTID_PROTOCOLID_SRP;
  2957. memcpy(tr_id->i_port_id, nacl->i_port_id, sizeof(tr_id->i_port_id));
  2958. return sizeof(*tr_id);
  2959. }
  2960. static u32 srpt_get_pr_transport_id_len(struct se_portal_group *se_tpg,
  2961. struct se_node_acl *se_nacl,
  2962. struct t10_pr_registration *pr_reg,
  2963. int *format_code)
  2964. {
  2965. *format_code = 0;
  2966. return sizeof(struct spc_rdma_transport_id);
  2967. }
  2968. static char *srpt_parse_pr_out_transport_id(struct se_portal_group *se_tpg,
  2969. const char *buf, u32 *out_tid_len,
  2970. char **port_nexus_ptr)
  2971. {
  2972. struct spc_rdma_transport_id *tr_id;
  2973. *port_nexus_ptr = NULL;
  2974. *out_tid_len = sizeof(struct spc_rdma_transport_id);
  2975. tr_id = (void *)buf;
  2976. return (char *)tr_id->i_port_id;
  2977. }
  2978. static struct se_node_acl *srpt_alloc_fabric_acl(struct se_portal_group *se_tpg)
  2979. {
  2980. struct srpt_node_acl *nacl;
  2981. nacl = kzalloc(sizeof(struct srpt_node_acl), GFP_KERNEL);
  2982. if (!nacl) {
  2983. printk(KERN_ERR "Unable to allocate struct srpt_node_acl\n");
  2984. return NULL;
  2985. }
  2986. return &nacl->nacl;
  2987. }
  2988. static void srpt_release_fabric_acl(struct se_portal_group *se_tpg,
  2989. struct se_node_acl *se_nacl)
  2990. {
  2991. struct srpt_node_acl *nacl;
  2992. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  2993. kfree(nacl);
  2994. }
  2995. static u32 srpt_tpg_get_inst_index(struct se_portal_group *se_tpg)
  2996. {
  2997. return 1;
  2998. }
  2999. static void srpt_release_cmd(struct se_cmd *se_cmd)
  3000. {
  3001. struct srpt_send_ioctx *ioctx = container_of(se_cmd,
  3002. struct srpt_send_ioctx, cmd);
  3003. struct srpt_rdma_ch *ch = ioctx->ch;
  3004. unsigned long flags;
  3005. WARN_ON(ioctx->state != SRPT_STATE_DONE);
  3006. WARN_ON(ioctx->mapped_sg_count != 0);
  3007. if (ioctx->n_rbuf > 1) {
  3008. kfree(ioctx->rbufs);
  3009. ioctx->rbufs = NULL;
  3010. ioctx->n_rbuf = 0;
  3011. }
  3012. spin_lock_irqsave(&ch->spinlock, flags);
  3013. list_add(&ioctx->free_list, &ch->free_list);
  3014. spin_unlock_irqrestore(&ch->spinlock, flags);
  3015. }
  3016. /**
  3017. * srpt_close_session() - Forcibly close a session.
  3018. *
  3019. * Callback function invoked by the TCM core to clean up sessions associated
  3020. * with a node ACL when the user invokes
  3021. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3022. */
  3023. static void srpt_close_session(struct se_session *se_sess)
  3024. {
  3025. DECLARE_COMPLETION_ONSTACK(release_done);
  3026. struct srpt_rdma_ch *ch;
  3027. struct srpt_device *sdev;
  3028. int res;
  3029. ch = se_sess->fabric_sess_ptr;
  3030. WARN_ON(ch->sess != se_sess);
  3031. pr_debug("ch %p state %d\n", ch, srpt_get_ch_state(ch));
  3032. sdev = ch->sport->sdev;
  3033. spin_lock_irq(&sdev->spinlock);
  3034. BUG_ON(ch->release_done);
  3035. ch->release_done = &release_done;
  3036. __srpt_close_ch(ch);
  3037. spin_unlock_irq(&sdev->spinlock);
  3038. res = wait_for_completion_timeout(&release_done, 60 * HZ);
  3039. WARN_ON(res <= 0);
  3040. }
  3041. /**
  3042. * srpt_sess_get_index() - Return the value of scsiAttIntrPortIndex (SCSI-MIB).
  3043. *
  3044. * A quote from RFC 4455 (SCSI-MIB) about this MIB object:
  3045. * This object represents an arbitrary integer used to uniquely identify a
  3046. * particular attached remote initiator port to a particular SCSI target port
  3047. * within a particular SCSI target device within a particular SCSI instance.
  3048. */
  3049. static u32 srpt_sess_get_index(struct se_session *se_sess)
  3050. {
  3051. return 0;
  3052. }
  3053. static void srpt_set_default_node_attrs(struct se_node_acl *nacl)
  3054. {
  3055. }
  3056. static u32 srpt_get_task_tag(struct se_cmd *se_cmd)
  3057. {
  3058. struct srpt_send_ioctx *ioctx;
  3059. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  3060. return ioctx->tag;
  3061. }
  3062. /* Note: only used from inside debug printk's by the TCM core. */
  3063. static int srpt_get_tcm_cmd_state(struct se_cmd *se_cmd)
  3064. {
  3065. struct srpt_send_ioctx *ioctx;
  3066. ioctx = container_of(se_cmd, struct srpt_send_ioctx, cmd);
  3067. return srpt_get_cmd_state(ioctx);
  3068. }
  3069. /**
  3070. * srpt_parse_i_port_id() - Parse an initiator port ID.
  3071. * @name: ASCII representation of a 128-bit initiator port ID.
  3072. * @i_port_id: Binary 128-bit port ID.
  3073. */
  3074. static int srpt_parse_i_port_id(u8 i_port_id[16], const char *name)
  3075. {
  3076. const char *p;
  3077. unsigned len, count, leading_zero_bytes;
  3078. int ret, rc;
  3079. p = name;
  3080. if (strncasecmp(p, "0x", 2) == 0)
  3081. p += 2;
  3082. ret = -EINVAL;
  3083. len = strlen(p);
  3084. if (len % 2)
  3085. goto out;
  3086. count = min(len / 2, 16U);
  3087. leading_zero_bytes = 16 - count;
  3088. memset(i_port_id, 0, leading_zero_bytes);
  3089. rc = hex2bin(i_port_id + leading_zero_bytes, p, count);
  3090. if (rc < 0)
  3091. pr_debug("hex2bin failed for srpt_parse_i_port_id: %d\n", rc);
  3092. ret = 0;
  3093. out:
  3094. return ret;
  3095. }
  3096. /*
  3097. * configfs callback function invoked for
  3098. * mkdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3099. */
  3100. static struct se_node_acl *srpt_make_nodeacl(struct se_portal_group *tpg,
  3101. struct config_group *group,
  3102. const char *name)
  3103. {
  3104. struct srpt_port *sport = container_of(tpg, struct srpt_port, port_tpg_1);
  3105. struct se_node_acl *se_nacl, *se_nacl_new;
  3106. struct srpt_node_acl *nacl;
  3107. int ret = 0;
  3108. u32 nexus_depth = 1;
  3109. u8 i_port_id[16];
  3110. if (srpt_parse_i_port_id(i_port_id, name) < 0) {
  3111. printk(KERN_ERR "invalid initiator port ID %s\n", name);
  3112. ret = -EINVAL;
  3113. goto err;
  3114. }
  3115. se_nacl_new = srpt_alloc_fabric_acl(tpg);
  3116. if (!se_nacl_new) {
  3117. ret = -ENOMEM;
  3118. goto err;
  3119. }
  3120. /*
  3121. * nacl_new may be released by core_tpg_add_initiator_node_acl()
  3122. * when converting a node ACL from demo mode to explict
  3123. */
  3124. se_nacl = core_tpg_add_initiator_node_acl(tpg, se_nacl_new, name,
  3125. nexus_depth);
  3126. if (IS_ERR(se_nacl)) {
  3127. ret = PTR_ERR(se_nacl);
  3128. goto err;
  3129. }
  3130. /* Locate our struct srpt_node_acl and set sdev and i_port_id. */
  3131. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3132. memcpy(&nacl->i_port_id[0], &i_port_id[0], 16);
  3133. nacl->sport = sport;
  3134. spin_lock_irq(&sport->port_acl_lock);
  3135. list_add_tail(&nacl->list, &sport->port_acl_list);
  3136. spin_unlock_irq(&sport->port_acl_lock);
  3137. return se_nacl;
  3138. err:
  3139. return ERR_PTR(ret);
  3140. }
  3141. /*
  3142. * configfs callback function invoked for
  3143. * rmdir /sys/kernel/config/target/$driver/$port/$tpg/acls/$i_port_id
  3144. */
  3145. static void srpt_drop_nodeacl(struct se_node_acl *se_nacl)
  3146. {
  3147. struct srpt_node_acl *nacl;
  3148. struct srpt_device *sdev;
  3149. struct srpt_port *sport;
  3150. nacl = container_of(se_nacl, struct srpt_node_acl, nacl);
  3151. sport = nacl->sport;
  3152. sdev = sport->sdev;
  3153. spin_lock_irq(&sport->port_acl_lock);
  3154. list_del(&nacl->list);
  3155. spin_unlock_irq(&sport->port_acl_lock);
  3156. core_tpg_del_initiator_node_acl(&sport->port_tpg_1, se_nacl, 1);
  3157. srpt_release_fabric_acl(NULL, se_nacl);
  3158. }
  3159. static ssize_t srpt_tpg_attrib_show_srp_max_rdma_size(
  3160. struct se_portal_group *se_tpg,
  3161. char *page)
  3162. {
  3163. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3164. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rdma_size);
  3165. }
  3166. static ssize_t srpt_tpg_attrib_store_srp_max_rdma_size(
  3167. struct se_portal_group *se_tpg,
  3168. const char *page,
  3169. size_t count)
  3170. {
  3171. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3172. unsigned long val;
  3173. int ret;
  3174. ret = kstrtoul(page, 0, &val);
  3175. if (ret < 0) {
  3176. pr_err("kstrtoul() failed with ret: %d\n", ret);
  3177. return -EINVAL;
  3178. }
  3179. if (val > MAX_SRPT_RDMA_SIZE) {
  3180. pr_err("val: %lu exceeds MAX_SRPT_RDMA_SIZE: %d\n", val,
  3181. MAX_SRPT_RDMA_SIZE);
  3182. return -EINVAL;
  3183. }
  3184. if (val < DEFAULT_MAX_RDMA_SIZE) {
  3185. pr_err("val: %lu smaller than DEFAULT_MAX_RDMA_SIZE: %d\n",
  3186. val, DEFAULT_MAX_RDMA_SIZE);
  3187. return -EINVAL;
  3188. }
  3189. sport->port_attrib.srp_max_rdma_size = val;
  3190. return count;
  3191. }
  3192. TF_TPG_ATTRIB_ATTR(srpt, srp_max_rdma_size, S_IRUGO | S_IWUSR);
  3193. static ssize_t srpt_tpg_attrib_show_srp_max_rsp_size(
  3194. struct se_portal_group *se_tpg,
  3195. char *page)
  3196. {
  3197. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3198. return sprintf(page, "%u\n", sport->port_attrib.srp_max_rsp_size);
  3199. }
  3200. static ssize_t srpt_tpg_attrib_store_srp_max_rsp_size(
  3201. struct se_portal_group *se_tpg,
  3202. const char *page,
  3203. size_t count)
  3204. {
  3205. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3206. unsigned long val;
  3207. int ret;
  3208. ret = kstrtoul(page, 0, &val);
  3209. if (ret < 0) {
  3210. pr_err("kstrtoul() failed with ret: %d\n", ret);
  3211. return -EINVAL;
  3212. }
  3213. if (val > MAX_SRPT_RSP_SIZE) {
  3214. pr_err("val: %lu exceeds MAX_SRPT_RSP_SIZE: %d\n", val,
  3215. MAX_SRPT_RSP_SIZE);
  3216. return -EINVAL;
  3217. }
  3218. if (val < MIN_MAX_RSP_SIZE) {
  3219. pr_err("val: %lu smaller than MIN_MAX_RSP_SIZE: %d\n", val,
  3220. MIN_MAX_RSP_SIZE);
  3221. return -EINVAL;
  3222. }
  3223. sport->port_attrib.srp_max_rsp_size = val;
  3224. return count;
  3225. }
  3226. TF_TPG_ATTRIB_ATTR(srpt, srp_max_rsp_size, S_IRUGO | S_IWUSR);
  3227. static ssize_t srpt_tpg_attrib_show_srp_sq_size(
  3228. struct se_portal_group *se_tpg,
  3229. char *page)
  3230. {
  3231. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3232. return sprintf(page, "%u\n", sport->port_attrib.srp_sq_size);
  3233. }
  3234. static ssize_t srpt_tpg_attrib_store_srp_sq_size(
  3235. struct se_portal_group *se_tpg,
  3236. const char *page,
  3237. size_t count)
  3238. {
  3239. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3240. unsigned long val;
  3241. int ret;
  3242. ret = kstrtoul(page, 0, &val);
  3243. if (ret < 0) {
  3244. pr_err("kstrtoul() failed with ret: %d\n", ret);
  3245. return -EINVAL;
  3246. }
  3247. if (val > MAX_SRPT_SRQ_SIZE) {
  3248. pr_err("val: %lu exceeds MAX_SRPT_SRQ_SIZE: %d\n", val,
  3249. MAX_SRPT_SRQ_SIZE);
  3250. return -EINVAL;
  3251. }
  3252. if (val < MIN_SRPT_SRQ_SIZE) {
  3253. pr_err("val: %lu smaller than MIN_SRPT_SRQ_SIZE: %d\n", val,
  3254. MIN_SRPT_SRQ_SIZE);
  3255. return -EINVAL;
  3256. }
  3257. sport->port_attrib.srp_sq_size = val;
  3258. return count;
  3259. }
  3260. TF_TPG_ATTRIB_ATTR(srpt, srp_sq_size, S_IRUGO | S_IWUSR);
  3261. static struct configfs_attribute *srpt_tpg_attrib_attrs[] = {
  3262. &srpt_tpg_attrib_srp_max_rdma_size.attr,
  3263. &srpt_tpg_attrib_srp_max_rsp_size.attr,
  3264. &srpt_tpg_attrib_srp_sq_size.attr,
  3265. NULL,
  3266. };
  3267. static ssize_t srpt_tpg_show_enable(
  3268. struct se_portal_group *se_tpg,
  3269. char *page)
  3270. {
  3271. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3272. return snprintf(page, PAGE_SIZE, "%d\n", (sport->enabled) ? 1: 0);
  3273. }
  3274. static ssize_t srpt_tpg_store_enable(
  3275. struct se_portal_group *se_tpg,
  3276. const char *page,
  3277. size_t count)
  3278. {
  3279. struct srpt_port *sport = container_of(se_tpg, struct srpt_port, port_tpg_1);
  3280. unsigned long tmp;
  3281. int ret;
  3282. ret = kstrtoul(page, 0, &tmp);
  3283. if (ret < 0) {
  3284. printk(KERN_ERR "Unable to extract srpt_tpg_store_enable\n");
  3285. return -EINVAL;
  3286. }
  3287. if ((tmp != 0) && (tmp != 1)) {
  3288. printk(KERN_ERR "Illegal value for srpt_tpg_store_enable: %lu\n", tmp);
  3289. return -EINVAL;
  3290. }
  3291. if (tmp == 1)
  3292. sport->enabled = true;
  3293. else
  3294. sport->enabled = false;
  3295. return count;
  3296. }
  3297. TF_TPG_BASE_ATTR(srpt, enable, S_IRUGO | S_IWUSR);
  3298. static struct configfs_attribute *srpt_tpg_attrs[] = {
  3299. &srpt_tpg_enable.attr,
  3300. NULL,
  3301. };
  3302. /**
  3303. * configfs callback invoked for
  3304. * mkdir /sys/kernel/config/target/$driver/$port/$tpg
  3305. */
  3306. static struct se_portal_group *srpt_make_tpg(struct se_wwn *wwn,
  3307. struct config_group *group,
  3308. const char *name)
  3309. {
  3310. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  3311. int res;
  3312. /* Initialize sport->port_wwn and sport->port_tpg_1 */
  3313. res = core_tpg_register(&srpt_target->tf_ops, &sport->port_wwn,
  3314. &sport->port_tpg_1, sport, TRANSPORT_TPG_TYPE_NORMAL);
  3315. if (res)
  3316. return ERR_PTR(res);
  3317. return &sport->port_tpg_1;
  3318. }
  3319. /**
  3320. * configfs callback invoked for
  3321. * rmdir /sys/kernel/config/target/$driver/$port/$tpg
  3322. */
  3323. static void srpt_drop_tpg(struct se_portal_group *tpg)
  3324. {
  3325. struct srpt_port *sport = container_of(tpg,
  3326. struct srpt_port, port_tpg_1);
  3327. sport->enabled = false;
  3328. core_tpg_deregister(&sport->port_tpg_1);
  3329. }
  3330. /**
  3331. * configfs callback invoked for
  3332. * mkdir /sys/kernel/config/target/$driver/$port
  3333. */
  3334. static struct se_wwn *srpt_make_tport(struct target_fabric_configfs *tf,
  3335. struct config_group *group,
  3336. const char *name)
  3337. {
  3338. struct srpt_port *sport;
  3339. int ret;
  3340. sport = srpt_lookup_port(name);
  3341. pr_debug("make_tport(%s)\n", name);
  3342. ret = -EINVAL;
  3343. if (!sport)
  3344. goto err;
  3345. return &sport->port_wwn;
  3346. err:
  3347. return ERR_PTR(ret);
  3348. }
  3349. /**
  3350. * configfs callback invoked for
  3351. * rmdir /sys/kernel/config/target/$driver/$port
  3352. */
  3353. static void srpt_drop_tport(struct se_wwn *wwn)
  3354. {
  3355. struct srpt_port *sport = container_of(wwn, struct srpt_port, port_wwn);
  3356. pr_debug("drop_tport(%s\n", config_item_name(&sport->port_wwn.wwn_group.cg_item));
  3357. }
  3358. static ssize_t srpt_wwn_show_attr_version(struct target_fabric_configfs *tf,
  3359. char *buf)
  3360. {
  3361. return scnprintf(buf, PAGE_SIZE, "%s\n", DRV_VERSION);
  3362. }
  3363. TF_WWN_ATTR_RO(srpt, version);
  3364. static struct configfs_attribute *srpt_wwn_attrs[] = {
  3365. &srpt_wwn_version.attr,
  3366. NULL,
  3367. };
  3368. static struct target_core_fabric_ops srpt_template = {
  3369. .get_fabric_name = srpt_get_fabric_name,
  3370. .get_fabric_proto_ident = srpt_get_fabric_proto_ident,
  3371. .tpg_get_wwn = srpt_get_fabric_wwn,
  3372. .tpg_get_tag = srpt_get_tag,
  3373. .tpg_get_default_depth = srpt_get_default_depth,
  3374. .tpg_get_pr_transport_id = srpt_get_pr_transport_id,
  3375. .tpg_get_pr_transport_id_len = srpt_get_pr_transport_id_len,
  3376. .tpg_parse_pr_out_transport_id = srpt_parse_pr_out_transport_id,
  3377. .tpg_check_demo_mode = srpt_check_false,
  3378. .tpg_check_demo_mode_cache = srpt_check_true,
  3379. .tpg_check_demo_mode_write_protect = srpt_check_true,
  3380. .tpg_check_prod_mode_write_protect = srpt_check_false,
  3381. .tpg_alloc_fabric_acl = srpt_alloc_fabric_acl,
  3382. .tpg_release_fabric_acl = srpt_release_fabric_acl,
  3383. .tpg_get_inst_index = srpt_tpg_get_inst_index,
  3384. .release_cmd = srpt_release_cmd,
  3385. .check_stop_free = srpt_check_stop_free,
  3386. .shutdown_session = srpt_shutdown_session,
  3387. .close_session = srpt_close_session,
  3388. .sess_get_index = srpt_sess_get_index,
  3389. .sess_get_initiator_sid = NULL,
  3390. .write_pending = srpt_write_pending,
  3391. .write_pending_status = srpt_write_pending_status,
  3392. .set_default_node_attributes = srpt_set_default_node_attrs,
  3393. .get_task_tag = srpt_get_task_tag,
  3394. .get_cmd_state = srpt_get_tcm_cmd_state,
  3395. .queue_data_in = srpt_queue_data_in,
  3396. .queue_status = srpt_queue_status,
  3397. .queue_tm_rsp = srpt_queue_tm_rsp,
  3398. .aborted_task = srpt_aborted_task,
  3399. /*
  3400. * Setup function pointers for generic logic in
  3401. * target_core_fabric_configfs.c
  3402. */
  3403. .fabric_make_wwn = srpt_make_tport,
  3404. .fabric_drop_wwn = srpt_drop_tport,
  3405. .fabric_make_tpg = srpt_make_tpg,
  3406. .fabric_drop_tpg = srpt_drop_tpg,
  3407. .fabric_post_link = NULL,
  3408. .fabric_pre_unlink = NULL,
  3409. .fabric_make_np = NULL,
  3410. .fabric_drop_np = NULL,
  3411. .fabric_make_nodeacl = srpt_make_nodeacl,
  3412. .fabric_drop_nodeacl = srpt_drop_nodeacl,
  3413. };
  3414. /**
  3415. * srpt_init_module() - Kernel module initialization.
  3416. *
  3417. * Note: Since ib_register_client() registers callback functions, and since at
  3418. * least one of these callback functions (srpt_add_one()) calls target core
  3419. * functions, this driver must be registered with the target core before
  3420. * ib_register_client() is called.
  3421. */
  3422. static int __init srpt_init_module(void)
  3423. {
  3424. int ret;
  3425. ret = -EINVAL;
  3426. if (srp_max_req_size < MIN_MAX_REQ_SIZE) {
  3427. printk(KERN_ERR "invalid value %d for kernel module parameter"
  3428. " srp_max_req_size -- must be at least %d.\n",
  3429. srp_max_req_size, MIN_MAX_REQ_SIZE);
  3430. goto out;
  3431. }
  3432. if (srpt_srq_size < MIN_SRPT_SRQ_SIZE
  3433. || srpt_srq_size > MAX_SRPT_SRQ_SIZE) {
  3434. printk(KERN_ERR "invalid value %d for kernel module parameter"
  3435. " srpt_srq_size -- must be in the range [%d..%d].\n",
  3436. srpt_srq_size, MIN_SRPT_SRQ_SIZE, MAX_SRPT_SRQ_SIZE);
  3437. goto out;
  3438. }
  3439. srpt_target = target_fabric_configfs_init(THIS_MODULE, "srpt");
  3440. if (IS_ERR(srpt_target)) {
  3441. printk(KERN_ERR "couldn't register\n");
  3442. ret = PTR_ERR(srpt_target);
  3443. goto out;
  3444. }
  3445. srpt_target->tf_ops = srpt_template;
  3446. /*
  3447. * Set up default attribute lists.
  3448. */
  3449. srpt_target->tf_cit_tmpl.tfc_wwn_cit.ct_attrs = srpt_wwn_attrs;
  3450. srpt_target->tf_cit_tmpl.tfc_tpg_base_cit.ct_attrs = srpt_tpg_attrs;
  3451. srpt_target->tf_cit_tmpl.tfc_tpg_attrib_cit.ct_attrs = srpt_tpg_attrib_attrs;
  3452. srpt_target->tf_cit_tmpl.tfc_tpg_param_cit.ct_attrs = NULL;
  3453. srpt_target->tf_cit_tmpl.tfc_tpg_np_base_cit.ct_attrs = NULL;
  3454. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_base_cit.ct_attrs = NULL;
  3455. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_attrib_cit.ct_attrs = NULL;
  3456. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_auth_cit.ct_attrs = NULL;
  3457. srpt_target->tf_cit_tmpl.tfc_tpg_nacl_param_cit.ct_attrs = NULL;
  3458. ret = target_fabric_configfs_register(srpt_target);
  3459. if (ret < 0) {
  3460. printk(KERN_ERR "couldn't register\n");
  3461. goto out_free_target;
  3462. }
  3463. ret = ib_register_client(&srpt_client);
  3464. if (ret) {
  3465. printk(KERN_ERR "couldn't register IB client\n");
  3466. goto out_unregister_target;
  3467. }
  3468. return 0;
  3469. out_unregister_target:
  3470. target_fabric_configfs_deregister(srpt_target);
  3471. srpt_target = NULL;
  3472. out_free_target:
  3473. if (srpt_target)
  3474. target_fabric_configfs_free(srpt_target);
  3475. out:
  3476. return ret;
  3477. }
  3478. static void __exit srpt_cleanup_module(void)
  3479. {
  3480. ib_unregister_client(&srpt_client);
  3481. target_fabric_configfs_deregister(srpt_target);
  3482. srpt_target = NULL;
  3483. }
  3484. module_init(srpt_init_module);
  3485. module_exit(srpt_cleanup_module);