ib_srp.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971
  1. /*
  2. * Copyright (c) 2005 Cisco Systems. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #define pr_fmt(fmt) PFX fmt
  33. #include <linux/module.h>
  34. #include <linux/init.h>
  35. #include <linux/slab.h>
  36. #include <linux/err.h>
  37. #include <linux/string.h>
  38. #include <linux/parser.h>
  39. #include <linux/random.h>
  40. #include <linux/jiffies.h>
  41. #include <linux/atomic.h>
  42. #include <scsi/scsi.h>
  43. #include <scsi/scsi_device.h>
  44. #include <scsi/scsi_dbg.h>
  45. #include <scsi/scsi_tcq.h>
  46. #include <scsi/srp.h>
  47. #include <scsi/scsi_transport_srp.h>
  48. #include "ib_srp.h"
  49. #define DRV_NAME "ib_srp"
  50. #define PFX DRV_NAME ": "
  51. #define DRV_VERSION "1.0"
  52. #define DRV_RELDATE "July 1, 2013"
  53. MODULE_AUTHOR("Roland Dreier");
  54. MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
  55. "v" DRV_VERSION " (" DRV_RELDATE ")");
  56. MODULE_LICENSE("Dual BSD/GPL");
  57. static unsigned int srp_sg_tablesize;
  58. static unsigned int cmd_sg_entries;
  59. static unsigned int indirect_sg_entries;
  60. static bool allow_ext_sg;
  61. static int topspin_workarounds = 1;
  62. module_param(srp_sg_tablesize, uint, 0444);
  63. MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries");
  64. module_param(cmd_sg_entries, uint, 0444);
  65. MODULE_PARM_DESC(cmd_sg_entries,
  66. "Default number of gather/scatter entries in the SRP command (default is 12, max 255)");
  67. module_param(indirect_sg_entries, uint, 0444);
  68. MODULE_PARM_DESC(indirect_sg_entries,
  69. "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")");
  70. module_param(allow_ext_sg, bool, 0444);
  71. MODULE_PARM_DESC(allow_ext_sg,
  72. "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)");
  73. module_param(topspin_workarounds, int, 0444);
  74. MODULE_PARM_DESC(topspin_workarounds,
  75. "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
  76. static struct kernel_param_ops srp_tmo_ops;
  77. static int srp_reconnect_delay = 10;
  78. module_param_cb(reconnect_delay, &srp_tmo_ops, &srp_reconnect_delay,
  79. S_IRUGO | S_IWUSR);
  80. MODULE_PARM_DESC(reconnect_delay, "Time between successive reconnect attempts");
  81. static int srp_fast_io_fail_tmo = 15;
  82. module_param_cb(fast_io_fail_tmo, &srp_tmo_ops, &srp_fast_io_fail_tmo,
  83. S_IRUGO | S_IWUSR);
  84. MODULE_PARM_DESC(fast_io_fail_tmo,
  85. "Number of seconds between the observation of a transport"
  86. " layer error and failing all I/O. \"off\" means that this"
  87. " functionality is disabled.");
  88. static int srp_dev_loss_tmo = 600;
  89. module_param_cb(dev_loss_tmo, &srp_tmo_ops, &srp_dev_loss_tmo,
  90. S_IRUGO | S_IWUSR);
  91. MODULE_PARM_DESC(dev_loss_tmo,
  92. "Maximum number of seconds that the SRP transport should"
  93. " insulate transport layer errors. After this time has been"
  94. " exceeded the SCSI host is removed. Should be"
  95. " between 1 and " __stringify(SCSI_DEVICE_BLOCK_MAX_TIMEOUT)
  96. " if fast_io_fail_tmo has not been set. \"off\" means that"
  97. " this functionality is disabled.");
  98. static void srp_add_one(struct ib_device *device);
  99. static void srp_remove_one(struct ib_device *device);
  100. static void srp_recv_completion(struct ib_cq *cq, void *target_ptr);
  101. static void srp_send_completion(struct ib_cq *cq, void *target_ptr);
  102. static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
  103. static struct scsi_transport_template *ib_srp_transport_template;
  104. static struct ib_client srp_client = {
  105. .name = "srp",
  106. .add = srp_add_one,
  107. .remove = srp_remove_one
  108. };
  109. static struct ib_sa_client srp_sa_client;
  110. static int srp_tmo_get(char *buffer, const struct kernel_param *kp)
  111. {
  112. int tmo = *(int *)kp->arg;
  113. if (tmo >= 0)
  114. return sprintf(buffer, "%d", tmo);
  115. else
  116. return sprintf(buffer, "off");
  117. }
  118. static int srp_tmo_set(const char *val, const struct kernel_param *kp)
  119. {
  120. int tmo, res;
  121. if (strncmp(val, "off", 3) != 0) {
  122. res = kstrtoint(val, 0, &tmo);
  123. if (res)
  124. goto out;
  125. } else {
  126. tmo = -1;
  127. }
  128. if (kp->arg == &srp_reconnect_delay)
  129. res = srp_tmo_valid(tmo, srp_fast_io_fail_tmo,
  130. srp_dev_loss_tmo);
  131. else if (kp->arg == &srp_fast_io_fail_tmo)
  132. res = srp_tmo_valid(srp_reconnect_delay, tmo, srp_dev_loss_tmo);
  133. else
  134. res = srp_tmo_valid(srp_reconnect_delay, srp_fast_io_fail_tmo,
  135. tmo);
  136. if (res)
  137. goto out;
  138. *(int *)kp->arg = tmo;
  139. out:
  140. return res;
  141. }
  142. static struct kernel_param_ops srp_tmo_ops = {
  143. .get = srp_tmo_get,
  144. .set = srp_tmo_set,
  145. };
  146. static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
  147. {
  148. return (struct srp_target_port *) host->hostdata;
  149. }
  150. static const char *srp_target_info(struct Scsi_Host *host)
  151. {
  152. return host_to_target(host)->target_name;
  153. }
  154. static int srp_target_is_topspin(struct srp_target_port *target)
  155. {
  156. static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
  157. static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d };
  158. return topspin_workarounds &&
  159. (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) ||
  160. !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui));
  161. }
  162. static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
  163. gfp_t gfp_mask,
  164. enum dma_data_direction direction)
  165. {
  166. struct srp_iu *iu;
  167. iu = kmalloc(sizeof *iu, gfp_mask);
  168. if (!iu)
  169. goto out;
  170. iu->buf = kzalloc(size, gfp_mask);
  171. if (!iu->buf)
  172. goto out_free_iu;
  173. iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size,
  174. direction);
  175. if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma))
  176. goto out_free_buf;
  177. iu->size = size;
  178. iu->direction = direction;
  179. return iu;
  180. out_free_buf:
  181. kfree(iu->buf);
  182. out_free_iu:
  183. kfree(iu);
  184. out:
  185. return NULL;
  186. }
  187. static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
  188. {
  189. if (!iu)
  190. return;
  191. ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size,
  192. iu->direction);
  193. kfree(iu->buf);
  194. kfree(iu);
  195. }
  196. static void srp_qp_event(struct ib_event *event, void *context)
  197. {
  198. pr_debug("QP event %d\n", event->event);
  199. }
  200. static int srp_init_qp(struct srp_target_port *target,
  201. struct ib_qp *qp)
  202. {
  203. struct ib_qp_attr *attr;
  204. int ret;
  205. attr = kmalloc(sizeof *attr, GFP_KERNEL);
  206. if (!attr)
  207. return -ENOMEM;
  208. ret = ib_find_pkey(target->srp_host->srp_dev->dev,
  209. target->srp_host->port,
  210. be16_to_cpu(target->path.pkey),
  211. &attr->pkey_index);
  212. if (ret)
  213. goto out;
  214. attr->qp_state = IB_QPS_INIT;
  215. attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
  216. IB_ACCESS_REMOTE_WRITE);
  217. attr->port_num = target->srp_host->port;
  218. ret = ib_modify_qp(qp, attr,
  219. IB_QP_STATE |
  220. IB_QP_PKEY_INDEX |
  221. IB_QP_ACCESS_FLAGS |
  222. IB_QP_PORT);
  223. out:
  224. kfree(attr);
  225. return ret;
  226. }
  227. static int srp_new_cm_id(struct srp_target_port *target)
  228. {
  229. struct ib_cm_id *new_cm_id;
  230. new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev,
  231. srp_cm_handler, target);
  232. if (IS_ERR(new_cm_id))
  233. return PTR_ERR(new_cm_id);
  234. if (target->cm_id)
  235. ib_destroy_cm_id(target->cm_id);
  236. target->cm_id = new_cm_id;
  237. return 0;
  238. }
  239. static int srp_create_target_ib(struct srp_target_port *target)
  240. {
  241. struct ib_qp_init_attr *init_attr;
  242. struct ib_cq *recv_cq, *send_cq;
  243. struct ib_qp *qp;
  244. int ret;
  245. init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
  246. if (!init_attr)
  247. return -ENOMEM;
  248. recv_cq = ib_create_cq(target->srp_host->srp_dev->dev,
  249. srp_recv_completion, NULL, target,
  250. target->queue_size, target->comp_vector);
  251. if (IS_ERR(recv_cq)) {
  252. ret = PTR_ERR(recv_cq);
  253. goto err;
  254. }
  255. send_cq = ib_create_cq(target->srp_host->srp_dev->dev,
  256. srp_send_completion, NULL, target,
  257. target->queue_size, target->comp_vector);
  258. if (IS_ERR(send_cq)) {
  259. ret = PTR_ERR(send_cq);
  260. goto err_recv_cq;
  261. }
  262. ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP);
  263. init_attr->event_handler = srp_qp_event;
  264. init_attr->cap.max_send_wr = target->queue_size;
  265. init_attr->cap.max_recv_wr = target->queue_size;
  266. init_attr->cap.max_recv_sge = 1;
  267. init_attr->cap.max_send_sge = 1;
  268. init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
  269. init_attr->qp_type = IB_QPT_RC;
  270. init_attr->send_cq = send_cq;
  271. init_attr->recv_cq = recv_cq;
  272. qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr);
  273. if (IS_ERR(qp)) {
  274. ret = PTR_ERR(qp);
  275. goto err_send_cq;
  276. }
  277. ret = srp_init_qp(target, qp);
  278. if (ret)
  279. goto err_qp;
  280. if (target->qp)
  281. ib_destroy_qp(target->qp);
  282. if (target->recv_cq)
  283. ib_destroy_cq(target->recv_cq);
  284. if (target->send_cq)
  285. ib_destroy_cq(target->send_cq);
  286. target->qp = qp;
  287. target->recv_cq = recv_cq;
  288. target->send_cq = send_cq;
  289. kfree(init_attr);
  290. return 0;
  291. err_qp:
  292. ib_destroy_qp(qp);
  293. err_send_cq:
  294. ib_destroy_cq(send_cq);
  295. err_recv_cq:
  296. ib_destroy_cq(recv_cq);
  297. err:
  298. kfree(init_attr);
  299. return ret;
  300. }
  301. /*
  302. * Note: this function may be called without srp_alloc_iu_bufs() having been
  303. * invoked. Hence the target->[rt]x_ring checks.
  304. */
  305. static void srp_free_target_ib(struct srp_target_port *target)
  306. {
  307. int i;
  308. ib_destroy_qp(target->qp);
  309. ib_destroy_cq(target->send_cq);
  310. ib_destroy_cq(target->recv_cq);
  311. target->qp = NULL;
  312. target->send_cq = target->recv_cq = NULL;
  313. if (target->rx_ring) {
  314. for (i = 0; i < target->queue_size; ++i)
  315. srp_free_iu(target->srp_host, target->rx_ring[i]);
  316. kfree(target->rx_ring);
  317. target->rx_ring = NULL;
  318. }
  319. if (target->tx_ring) {
  320. for (i = 0; i < target->queue_size; ++i)
  321. srp_free_iu(target->srp_host, target->tx_ring[i]);
  322. kfree(target->tx_ring);
  323. target->tx_ring = NULL;
  324. }
  325. }
  326. static void srp_path_rec_completion(int status,
  327. struct ib_sa_path_rec *pathrec,
  328. void *target_ptr)
  329. {
  330. struct srp_target_port *target = target_ptr;
  331. target->status = status;
  332. if (status)
  333. shost_printk(KERN_ERR, target->scsi_host,
  334. PFX "Got failed path rec status %d\n", status);
  335. else
  336. target->path = *pathrec;
  337. complete(&target->done);
  338. }
  339. static int srp_lookup_path(struct srp_target_port *target)
  340. {
  341. target->path.numb_path = 1;
  342. init_completion(&target->done);
  343. target->path_query_id = ib_sa_path_rec_get(&srp_sa_client,
  344. target->srp_host->srp_dev->dev,
  345. target->srp_host->port,
  346. &target->path,
  347. IB_SA_PATH_REC_SERVICE_ID |
  348. IB_SA_PATH_REC_DGID |
  349. IB_SA_PATH_REC_SGID |
  350. IB_SA_PATH_REC_NUMB_PATH |
  351. IB_SA_PATH_REC_PKEY,
  352. SRP_PATH_REC_TIMEOUT_MS,
  353. GFP_KERNEL,
  354. srp_path_rec_completion,
  355. target, &target->path_query);
  356. if (target->path_query_id < 0)
  357. return target->path_query_id;
  358. wait_for_completion(&target->done);
  359. if (target->status < 0)
  360. shost_printk(KERN_WARNING, target->scsi_host,
  361. PFX "Path record query failed\n");
  362. return target->status;
  363. }
  364. static int srp_send_req(struct srp_target_port *target)
  365. {
  366. struct {
  367. struct ib_cm_req_param param;
  368. struct srp_login_req priv;
  369. } *req = NULL;
  370. int status;
  371. req = kzalloc(sizeof *req, GFP_KERNEL);
  372. if (!req)
  373. return -ENOMEM;
  374. req->param.primary_path = &target->path;
  375. req->param.alternate_path = NULL;
  376. req->param.service_id = target->service_id;
  377. req->param.qp_num = target->qp->qp_num;
  378. req->param.qp_type = target->qp->qp_type;
  379. req->param.private_data = &req->priv;
  380. req->param.private_data_len = sizeof req->priv;
  381. req->param.flow_control = 1;
  382. get_random_bytes(&req->param.starting_psn, 4);
  383. req->param.starting_psn &= 0xffffff;
  384. /*
  385. * Pick some arbitrary defaults here; we could make these
  386. * module parameters if anyone cared about setting them.
  387. */
  388. req->param.responder_resources = 4;
  389. req->param.remote_cm_response_timeout = 20;
  390. req->param.local_cm_response_timeout = 20;
  391. req->param.retry_count = target->tl_retry_count;
  392. req->param.rnr_retry_count = 7;
  393. req->param.max_cm_retries = 15;
  394. req->priv.opcode = SRP_LOGIN_REQ;
  395. req->priv.tag = 0;
  396. req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len);
  397. req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
  398. SRP_BUF_FORMAT_INDIRECT);
  399. /*
  400. * In the published SRP specification (draft rev. 16a), the
  401. * port identifier format is 8 bytes of ID extension followed
  402. * by 8 bytes of GUID. Older drafts put the two halves in the
  403. * opposite order, so that the GUID comes first.
  404. *
  405. * Targets conforming to these obsolete drafts can be
  406. * recognized by the I/O Class they report.
  407. */
  408. if (target->io_class == SRP_REV10_IB_IO_CLASS) {
  409. memcpy(req->priv.initiator_port_id,
  410. &target->path.sgid.global.interface_id, 8);
  411. memcpy(req->priv.initiator_port_id + 8,
  412. &target->initiator_ext, 8);
  413. memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
  414. memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
  415. } else {
  416. memcpy(req->priv.initiator_port_id,
  417. &target->initiator_ext, 8);
  418. memcpy(req->priv.initiator_port_id + 8,
  419. &target->path.sgid.global.interface_id, 8);
  420. memcpy(req->priv.target_port_id, &target->id_ext, 8);
  421. memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
  422. }
  423. /*
  424. * Topspin/Cisco SRP targets will reject our login unless we
  425. * zero out the first 8 bytes of our initiator port ID and set
  426. * the second 8 bytes to the local node GUID.
  427. */
  428. if (srp_target_is_topspin(target)) {
  429. shost_printk(KERN_DEBUG, target->scsi_host,
  430. PFX "Topspin/Cisco initiator port ID workaround "
  431. "activated for target GUID %016llx\n",
  432. (unsigned long long) be64_to_cpu(target->ioc_guid));
  433. memset(req->priv.initiator_port_id, 0, 8);
  434. memcpy(req->priv.initiator_port_id + 8,
  435. &target->srp_host->srp_dev->dev->node_guid, 8);
  436. }
  437. status = ib_send_cm_req(target->cm_id, &req->param);
  438. kfree(req);
  439. return status;
  440. }
  441. static bool srp_queue_remove_work(struct srp_target_port *target)
  442. {
  443. bool changed = false;
  444. spin_lock_irq(&target->lock);
  445. if (target->state != SRP_TARGET_REMOVED) {
  446. target->state = SRP_TARGET_REMOVED;
  447. changed = true;
  448. }
  449. spin_unlock_irq(&target->lock);
  450. if (changed)
  451. queue_work(system_long_wq, &target->remove_work);
  452. return changed;
  453. }
  454. static bool srp_change_conn_state(struct srp_target_port *target,
  455. bool connected)
  456. {
  457. bool changed = false;
  458. spin_lock_irq(&target->lock);
  459. if (target->connected != connected) {
  460. target->connected = connected;
  461. changed = true;
  462. }
  463. spin_unlock_irq(&target->lock);
  464. return changed;
  465. }
  466. static void srp_disconnect_target(struct srp_target_port *target)
  467. {
  468. if (srp_change_conn_state(target, false)) {
  469. /* XXX should send SRP_I_LOGOUT request */
  470. if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
  471. shost_printk(KERN_DEBUG, target->scsi_host,
  472. PFX "Sending CM DREQ failed\n");
  473. }
  474. }
  475. }
  476. static void srp_free_req_data(struct srp_target_port *target)
  477. {
  478. struct ib_device *ibdev = target->srp_host->srp_dev->dev;
  479. struct srp_request *req;
  480. int i;
  481. if (!target->req_ring)
  482. return;
  483. for (i = 0; i < target->req_ring_size; ++i) {
  484. req = &target->req_ring[i];
  485. kfree(req->fmr_list);
  486. kfree(req->map_page);
  487. if (req->indirect_dma_addr) {
  488. ib_dma_unmap_single(ibdev, req->indirect_dma_addr,
  489. target->indirect_size,
  490. DMA_TO_DEVICE);
  491. }
  492. kfree(req->indirect_desc);
  493. }
  494. kfree(target->req_ring);
  495. target->req_ring = NULL;
  496. }
  497. static int srp_alloc_req_data(struct srp_target_port *target)
  498. {
  499. struct srp_device *srp_dev = target->srp_host->srp_dev;
  500. struct ib_device *ibdev = srp_dev->dev;
  501. struct srp_request *req;
  502. dma_addr_t dma_addr;
  503. int i, ret = -ENOMEM;
  504. INIT_LIST_HEAD(&target->free_reqs);
  505. target->req_ring = kzalloc(target->req_ring_size *
  506. sizeof(*target->req_ring), GFP_KERNEL);
  507. if (!target->req_ring)
  508. goto out;
  509. for (i = 0; i < target->req_ring_size; ++i) {
  510. req = &target->req_ring[i];
  511. req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof(void *),
  512. GFP_KERNEL);
  513. req->map_page = kmalloc(SRP_FMR_SIZE * sizeof(void *),
  514. GFP_KERNEL);
  515. req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL);
  516. if (!req->fmr_list || !req->map_page || !req->indirect_desc)
  517. goto out;
  518. dma_addr = ib_dma_map_single(ibdev, req->indirect_desc,
  519. target->indirect_size,
  520. DMA_TO_DEVICE);
  521. if (ib_dma_mapping_error(ibdev, dma_addr))
  522. goto out;
  523. req->indirect_dma_addr = dma_addr;
  524. req->index = i;
  525. list_add_tail(&req->list, &target->free_reqs);
  526. }
  527. ret = 0;
  528. out:
  529. return ret;
  530. }
  531. /**
  532. * srp_del_scsi_host_attr() - Remove attributes defined in the host template.
  533. * @shost: SCSI host whose attributes to remove from sysfs.
  534. *
  535. * Note: Any attributes defined in the host template and that did not exist
  536. * before invocation of this function will be ignored.
  537. */
  538. static void srp_del_scsi_host_attr(struct Scsi_Host *shost)
  539. {
  540. struct device_attribute **attr;
  541. for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr)
  542. device_remove_file(&shost->shost_dev, *attr);
  543. }
  544. static void srp_remove_target(struct srp_target_port *target)
  545. {
  546. WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
  547. srp_del_scsi_host_attr(target->scsi_host);
  548. srp_rport_get(target->rport);
  549. srp_remove_host(target->scsi_host);
  550. scsi_remove_host(target->scsi_host);
  551. srp_disconnect_target(target);
  552. ib_destroy_cm_id(target->cm_id);
  553. srp_free_target_ib(target);
  554. cancel_work_sync(&target->tl_err_work);
  555. srp_rport_put(target->rport);
  556. srp_free_req_data(target);
  557. spin_lock(&target->srp_host->target_lock);
  558. list_del(&target->list);
  559. spin_unlock(&target->srp_host->target_lock);
  560. scsi_host_put(target->scsi_host);
  561. }
  562. static void srp_remove_work(struct work_struct *work)
  563. {
  564. struct srp_target_port *target =
  565. container_of(work, struct srp_target_port, remove_work);
  566. WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED);
  567. srp_remove_target(target);
  568. }
  569. static void srp_rport_delete(struct srp_rport *rport)
  570. {
  571. struct srp_target_port *target = rport->lld_data;
  572. srp_queue_remove_work(target);
  573. }
  574. static int srp_connect_target(struct srp_target_port *target)
  575. {
  576. int retries = 3;
  577. int ret;
  578. WARN_ON_ONCE(target->connected);
  579. target->qp_in_error = false;
  580. ret = srp_lookup_path(target);
  581. if (ret)
  582. return ret;
  583. while (1) {
  584. init_completion(&target->done);
  585. ret = srp_send_req(target);
  586. if (ret)
  587. return ret;
  588. wait_for_completion(&target->done);
  589. /*
  590. * The CM event handling code will set status to
  591. * SRP_PORT_REDIRECT if we get a port redirect REJ
  592. * back, or SRP_DLID_REDIRECT if we get a lid/qp
  593. * redirect REJ back.
  594. */
  595. switch (target->status) {
  596. case 0:
  597. srp_change_conn_state(target, true);
  598. return 0;
  599. case SRP_PORT_REDIRECT:
  600. ret = srp_lookup_path(target);
  601. if (ret)
  602. return ret;
  603. break;
  604. case SRP_DLID_REDIRECT:
  605. break;
  606. case SRP_STALE_CONN:
  607. /* Our current CM id was stale, and is now in timewait.
  608. * Try to reconnect with a new one.
  609. */
  610. if (!retries-- || srp_new_cm_id(target)) {
  611. shost_printk(KERN_ERR, target->scsi_host, PFX
  612. "giving up on stale connection\n");
  613. target->status = -ECONNRESET;
  614. return target->status;
  615. }
  616. shost_printk(KERN_ERR, target->scsi_host, PFX
  617. "retrying stale connection\n");
  618. break;
  619. default:
  620. return target->status;
  621. }
  622. }
  623. }
  624. static void srp_unmap_data(struct scsi_cmnd *scmnd,
  625. struct srp_target_port *target,
  626. struct srp_request *req)
  627. {
  628. struct ib_device *ibdev = target->srp_host->srp_dev->dev;
  629. struct ib_pool_fmr **pfmr;
  630. if (!scsi_sglist(scmnd) ||
  631. (scmnd->sc_data_direction != DMA_TO_DEVICE &&
  632. scmnd->sc_data_direction != DMA_FROM_DEVICE))
  633. return;
  634. pfmr = req->fmr_list;
  635. while (req->nfmr--)
  636. ib_fmr_pool_unmap(*pfmr++);
  637. ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd),
  638. scmnd->sc_data_direction);
  639. }
  640. /**
  641. * srp_claim_req - Take ownership of the scmnd associated with a request.
  642. * @target: SRP target port.
  643. * @req: SRP request.
  644. * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take
  645. * ownership of @req->scmnd if it equals @scmnd.
  646. *
  647. * Return value:
  648. * Either NULL or a pointer to the SCSI command the caller became owner of.
  649. */
  650. static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target,
  651. struct srp_request *req,
  652. struct scsi_cmnd *scmnd)
  653. {
  654. unsigned long flags;
  655. spin_lock_irqsave(&target->lock, flags);
  656. if (!scmnd) {
  657. scmnd = req->scmnd;
  658. req->scmnd = NULL;
  659. } else if (req->scmnd == scmnd) {
  660. req->scmnd = NULL;
  661. } else {
  662. scmnd = NULL;
  663. }
  664. spin_unlock_irqrestore(&target->lock, flags);
  665. return scmnd;
  666. }
  667. /**
  668. * srp_free_req() - Unmap data and add request to the free request list.
  669. */
  670. static void srp_free_req(struct srp_target_port *target,
  671. struct srp_request *req, struct scsi_cmnd *scmnd,
  672. s32 req_lim_delta)
  673. {
  674. unsigned long flags;
  675. srp_unmap_data(scmnd, target, req);
  676. spin_lock_irqsave(&target->lock, flags);
  677. target->req_lim += req_lim_delta;
  678. list_add_tail(&req->list, &target->free_reqs);
  679. spin_unlock_irqrestore(&target->lock, flags);
  680. }
  681. static void srp_finish_req(struct srp_target_port *target,
  682. struct srp_request *req, int result)
  683. {
  684. struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL);
  685. if (scmnd) {
  686. srp_free_req(target, req, scmnd, 0);
  687. scmnd->result = result;
  688. scmnd->scsi_done(scmnd);
  689. }
  690. }
  691. static void srp_terminate_io(struct srp_rport *rport)
  692. {
  693. struct srp_target_port *target = rport->lld_data;
  694. int i;
  695. for (i = 0; i < target->req_ring_size; ++i) {
  696. struct srp_request *req = &target->req_ring[i];
  697. srp_finish_req(target, req, DID_TRANSPORT_FAILFAST << 16);
  698. }
  699. }
  700. /*
  701. * It is up to the caller to ensure that srp_rport_reconnect() calls are
  702. * serialized and that no concurrent srp_queuecommand(), srp_abort(),
  703. * srp_reset_device() or srp_reset_host() calls will occur while this function
  704. * is in progress. One way to realize that is not to call this function
  705. * directly but to call srp_reconnect_rport() instead since that last function
  706. * serializes calls of this function via rport->mutex and also blocks
  707. * srp_queuecommand() calls before invoking this function.
  708. */
  709. static int srp_rport_reconnect(struct srp_rport *rport)
  710. {
  711. struct srp_target_port *target = rport->lld_data;
  712. int i, ret;
  713. srp_disconnect_target(target);
  714. /*
  715. * Now get a new local CM ID so that we avoid confusing the target in
  716. * case things are really fouled up. Doing so also ensures that all CM
  717. * callbacks will have finished before a new QP is allocated.
  718. */
  719. ret = srp_new_cm_id(target);
  720. /*
  721. * Whether or not creating a new CM ID succeeded, create a new
  722. * QP. This guarantees that all completion callback function
  723. * invocations have finished before request resetting starts.
  724. */
  725. if (ret == 0)
  726. ret = srp_create_target_ib(target);
  727. else
  728. srp_create_target_ib(target);
  729. for (i = 0; i < target->req_ring_size; ++i) {
  730. struct srp_request *req = &target->req_ring[i];
  731. srp_finish_req(target, req, DID_RESET << 16);
  732. }
  733. INIT_LIST_HEAD(&target->free_tx);
  734. for (i = 0; i < target->queue_size; ++i)
  735. list_add(&target->tx_ring[i]->list, &target->free_tx);
  736. if (ret == 0)
  737. ret = srp_connect_target(target);
  738. if (ret == 0)
  739. shost_printk(KERN_INFO, target->scsi_host,
  740. PFX "reconnect succeeded\n");
  741. return ret;
  742. }
  743. static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr,
  744. unsigned int dma_len, u32 rkey)
  745. {
  746. struct srp_direct_buf *desc = state->desc;
  747. desc->va = cpu_to_be64(dma_addr);
  748. desc->key = cpu_to_be32(rkey);
  749. desc->len = cpu_to_be32(dma_len);
  750. state->total_len += dma_len;
  751. state->desc++;
  752. state->ndesc++;
  753. }
  754. static int srp_map_finish_fmr(struct srp_map_state *state,
  755. struct srp_target_port *target)
  756. {
  757. struct srp_device *dev = target->srp_host->srp_dev;
  758. struct ib_pool_fmr *fmr;
  759. u64 io_addr = 0;
  760. if (!state->npages)
  761. return 0;
  762. if (state->npages == 1) {
  763. srp_map_desc(state, state->base_dma_addr, state->fmr_len,
  764. target->rkey);
  765. state->npages = state->fmr_len = 0;
  766. return 0;
  767. }
  768. fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages,
  769. state->npages, io_addr);
  770. if (IS_ERR(fmr))
  771. return PTR_ERR(fmr);
  772. *state->next_fmr++ = fmr;
  773. state->nfmr++;
  774. srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey);
  775. state->npages = state->fmr_len = 0;
  776. return 0;
  777. }
  778. static void srp_map_update_start(struct srp_map_state *state,
  779. struct scatterlist *sg, int sg_index,
  780. dma_addr_t dma_addr)
  781. {
  782. state->unmapped_sg = sg;
  783. state->unmapped_index = sg_index;
  784. state->unmapped_addr = dma_addr;
  785. }
  786. static int srp_map_sg_entry(struct srp_map_state *state,
  787. struct srp_target_port *target,
  788. struct scatterlist *sg, int sg_index,
  789. int use_fmr)
  790. {
  791. struct srp_device *dev = target->srp_host->srp_dev;
  792. struct ib_device *ibdev = dev->dev;
  793. dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg);
  794. unsigned int dma_len = ib_sg_dma_len(ibdev, sg);
  795. unsigned int len;
  796. int ret;
  797. if (!dma_len)
  798. return 0;
  799. if (use_fmr == SRP_MAP_NO_FMR) {
  800. /* Once we're in direct map mode for a request, we don't
  801. * go back to FMR mode, so no need to update anything
  802. * other than the descriptor.
  803. */
  804. srp_map_desc(state, dma_addr, dma_len, target->rkey);
  805. return 0;
  806. }
  807. /* If we start at an offset into the FMR page, don't merge into
  808. * the current FMR. Finish it out, and use the kernel's MR for this
  809. * sg entry. This is to avoid potential bugs on some SRP targets
  810. * that were never quite defined, but went away when the initiator
  811. * avoided using FMR on such page fragments.
  812. */
  813. if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) {
  814. ret = srp_map_finish_fmr(state, target);
  815. if (ret)
  816. return ret;
  817. srp_map_desc(state, dma_addr, dma_len, target->rkey);
  818. srp_map_update_start(state, NULL, 0, 0);
  819. return 0;
  820. }
  821. /* If this is the first sg to go into the FMR, save our position.
  822. * We need to know the first unmapped entry, its index, and the
  823. * first unmapped address within that entry to be able to restart
  824. * mapping after an error.
  825. */
  826. if (!state->unmapped_sg)
  827. srp_map_update_start(state, sg, sg_index, dma_addr);
  828. while (dma_len) {
  829. if (state->npages == SRP_FMR_SIZE) {
  830. ret = srp_map_finish_fmr(state, target);
  831. if (ret)
  832. return ret;
  833. srp_map_update_start(state, sg, sg_index, dma_addr);
  834. }
  835. len = min_t(unsigned int, dma_len, dev->fmr_page_size);
  836. if (!state->npages)
  837. state->base_dma_addr = dma_addr;
  838. state->pages[state->npages++] = dma_addr;
  839. state->fmr_len += len;
  840. dma_addr += len;
  841. dma_len -= len;
  842. }
  843. /* If the last entry of the FMR wasn't a full page, then we need to
  844. * close it out and start a new one -- we can only merge at page
  845. * boundries.
  846. */
  847. ret = 0;
  848. if (len != dev->fmr_page_size) {
  849. ret = srp_map_finish_fmr(state, target);
  850. if (!ret)
  851. srp_map_update_start(state, NULL, 0, 0);
  852. }
  853. return ret;
  854. }
  855. static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
  856. struct srp_request *req)
  857. {
  858. struct scatterlist *scat, *sg;
  859. struct srp_cmd *cmd = req->cmd->buf;
  860. int i, len, nents, count, use_fmr;
  861. struct srp_device *dev;
  862. struct ib_device *ibdev;
  863. struct srp_map_state state;
  864. struct srp_indirect_buf *indirect_hdr;
  865. u32 table_len;
  866. u8 fmt;
  867. if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE)
  868. return sizeof (struct srp_cmd);
  869. if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
  870. scmnd->sc_data_direction != DMA_TO_DEVICE) {
  871. shost_printk(KERN_WARNING, target->scsi_host,
  872. PFX "Unhandled data direction %d\n",
  873. scmnd->sc_data_direction);
  874. return -EINVAL;
  875. }
  876. nents = scsi_sg_count(scmnd);
  877. scat = scsi_sglist(scmnd);
  878. dev = target->srp_host->srp_dev;
  879. ibdev = dev->dev;
  880. count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction);
  881. if (unlikely(count == 0))
  882. return -EIO;
  883. fmt = SRP_DATA_DESC_DIRECT;
  884. len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
  885. if (count == 1) {
  886. /*
  887. * The midlayer only generated a single gather/scatter
  888. * entry, or DMA mapping coalesced everything to a
  889. * single entry. So a direct descriptor along with
  890. * the DMA MR suffices.
  891. */
  892. struct srp_direct_buf *buf = (void *) cmd->add_data;
  893. buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat));
  894. buf->key = cpu_to_be32(target->rkey);
  895. buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat));
  896. req->nfmr = 0;
  897. goto map_complete;
  898. }
  899. /* We have more than one scatter/gather entry, so build our indirect
  900. * descriptor table, trying to merge as many entries with FMR as we
  901. * can.
  902. */
  903. indirect_hdr = (void *) cmd->add_data;
  904. ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr,
  905. target->indirect_size, DMA_TO_DEVICE);
  906. memset(&state, 0, sizeof(state));
  907. state.desc = req->indirect_desc;
  908. state.pages = req->map_page;
  909. state.next_fmr = req->fmr_list;
  910. use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR;
  911. for_each_sg(scat, sg, count, i) {
  912. if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) {
  913. /* FMR mapping failed, so backtrack to the first
  914. * unmapped entry and continue on without using FMR.
  915. */
  916. dma_addr_t dma_addr;
  917. unsigned int dma_len;
  918. backtrack:
  919. sg = state.unmapped_sg;
  920. i = state.unmapped_index;
  921. dma_addr = ib_sg_dma_address(ibdev, sg);
  922. dma_len = ib_sg_dma_len(ibdev, sg);
  923. dma_len -= (state.unmapped_addr - dma_addr);
  924. dma_addr = state.unmapped_addr;
  925. use_fmr = SRP_MAP_NO_FMR;
  926. srp_map_desc(&state, dma_addr, dma_len, target->rkey);
  927. }
  928. }
  929. if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target))
  930. goto backtrack;
  931. /* We've mapped the request, now pull as much of the indirect
  932. * descriptor table as we can into the command buffer. If this
  933. * target is not using an external indirect table, we are
  934. * guaranteed to fit into the command, as the SCSI layer won't
  935. * give us more S/G entries than we allow.
  936. */
  937. req->nfmr = state.nfmr;
  938. if (state.ndesc == 1) {
  939. /* FMR mapping was able to collapse this to one entry,
  940. * so use a direct descriptor.
  941. */
  942. struct srp_direct_buf *buf = (void *) cmd->add_data;
  943. *buf = req->indirect_desc[0];
  944. goto map_complete;
  945. }
  946. if (unlikely(target->cmd_sg_cnt < state.ndesc &&
  947. !target->allow_ext_sg)) {
  948. shost_printk(KERN_ERR, target->scsi_host,
  949. "Could not fit S/G list into SRP_CMD\n");
  950. return -EIO;
  951. }
  952. count = min(state.ndesc, target->cmd_sg_cnt);
  953. table_len = state.ndesc * sizeof (struct srp_direct_buf);
  954. fmt = SRP_DATA_DESC_INDIRECT;
  955. len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf);
  956. len += count * sizeof (struct srp_direct_buf);
  957. memcpy(indirect_hdr->desc_list, req->indirect_desc,
  958. count * sizeof (struct srp_direct_buf));
  959. indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr);
  960. indirect_hdr->table_desc.key = cpu_to_be32(target->rkey);
  961. indirect_hdr->table_desc.len = cpu_to_be32(table_len);
  962. indirect_hdr->len = cpu_to_be32(state.total_len);
  963. if (scmnd->sc_data_direction == DMA_TO_DEVICE)
  964. cmd->data_out_desc_cnt = count;
  965. else
  966. cmd->data_in_desc_cnt = count;
  967. ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len,
  968. DMA_TO_DEVICE);
  969. map_complete:
  970. if (scmnd->sc_data_direction == DMA_TO_DEVICE)
  971. cmd->buf_fmt = fmt << 4;
  972. else
  973. cmd->buf_fmt = fmt;
  974. return len;
  975. }
  976. /*
  977. * Return an IU and possible credit to the free pool
  978. */
  979. static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu,
  980. enum srp_iu_type iu_type)
  981. {
  982. unsigned long flags;
  983. spin_lock_irqsave(&target->lock, flags);
  984. list_add(&iu->list, &target->free_tx);
  985. if (iu_type != SRP_IU_RSP)
  986. ++target->req_lim;
  987. spin_unlock_irqrestore(&target->lock, flags);
  988. }
  989. /*
  990. * Must be called with target->lock held to protect req_lim and free_tx.
  991. * If IU is not sent, it must be returned using srp_put_tx_iu().
  992. *
  993. * Note:
  994. * An upper limit for the number of allocated information units for each
  995. * request type is:
  996. * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues
  997. * more than Scsi_Host.can_queue requests.
  998. * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE.
  999. * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than
  1000. * one unanswered SRP request to an initiator.
  1001. */
  1002. static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target,
  1003. enum srp_iu_type iu_type)
  1004. {
  1005. s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE;
  1006. struct srp_iu *iu;
  1007. srp_send_completion(target->send_cq, target);
  1008. if (list_empty(&target->free_tx))
  1009. return NULL;
  1010. /* Initiator responses to target requests do not consume credits */
  1011. if (iu_type != SRP_IU_RSP) {
  1012. if (target->req_lim <= rsv) {
  1013. ++target->zero_req_lim;
  1014. return NULL;
  1015. }
  1016. --target->req_lim;
  1017. }
  1018. iu = list_first_entry(&target->free_tx, struct srp_iu, list);
  1019. list_del(&iu->list);
  1020. return iu;
  1021. }
  1022. static int srp_post_send(struct srp_target_port *target,
  1023. struct srp_iu *iu, int len)
  1024. {
  1025. struct ib_sge list;
  1026. struct ib_send_wr wr, *bad_wr;
  1027. list.addr = iu->dma;
  1028. list.length = len;
  1029. list.lkey = target->lkey;
  1030. wr.next = NULL;
  1031. wr.wr_id = (uintptr_t) iu;
  1032. wr.sg_list = &list;
  1033. wr.num_sge = 1;
  1034. wr.opcode = IB_WR_SEND;
  1035. wr.send_flags = IB_SEND_SIGNALED;
  1036. return ib_post_send(target->qp, &wr, &bad_wr);
  1037. }
  1038. static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu)
  1039. {
  1040. struct ib_recv_wr wr, *bad_wr;
  1041. struct ib_sge list;
  1042. list.addr = iu->dma;
  1043. list.length = iu->size;
  1044. list.lkey = target->lkey;
  1045. wr.next = NULL;
  1046. wr.wr_id = (uintptr_t) iu;
  1047. wr.sg_list = &list;
  1048. wr.num_sge = 1;
  1049. return ib_post_recv(target->qp, &wr, &bad_wr);
  1050. }
  1051. static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
  1052. {
  1053. struct srp_request *req;
  1054. struct scsi_cmnd *scmnd;
  1055. unsigned long flags;
  1056. if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
  1057. spin_lock_irqsave(&target->lock, flags);
  1058. target->req_lim += be32_to_cpu(rsp->req_lim_delta);
  1059. spin_unlock_irqrestore(&target->lock, flags);
  1060. target->tsk_mgmt_status = -1;
  1061. if (be32_to_cpu(rsp->resp_data_len) >= 4)
  1062. target->tsk_mgmt_status = rsp->data[3];
  1063. complete(&target->tsk_mgmt_done);
  1064. } else {
  1065. req = &target->req_ring[rsp->tag];
  1066. scmnd = srp_claim_req(target, req, NULL);
  1067. if (!scmnd) {
  1068. shost_printk(KERN_ERR, target->scsi_host,
  1069. "Null scmnd for RSP w/tag %016llx\n",
  1070. (unsigned long long) rsp->tag);
  1071. spin_lock_irqsave(&target->lock, flags);
  1072. target->req_lim += be32_to_cpu(rsp->req_lim_delta);
  1073. spin_unlock_irqrestore(&target->lock, flags);
  1074. return;
  1075. }
  1076. scmnd->result = rsp->status;
  1077. if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
  1078. memcpy(scmnd->sense_buffer, rsp->data +
  1079. be32_to_cpu(rsp->resp_data_len),
  1080. min_t(int, be32_to_cpu(rsp->sense_data_len),
  1081. SCSI_SENSE_BUFFERSIZE));
  1082. }
  1083. if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
  1084. scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt));
  1085. else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
  1086. scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt));
  1087. srp_free_req(target, req, scmnd,
  1088. be32_to_cpu(rsp->req_lim_delta));
  1089. scmnd->host_scribble = NULL;
  1090. scmnd->scsi_done(scmnd);
  1091. }
  1092. }
  1093. static int srp_response_common(struct srp_target_port *target, s32 req_delta,
  1094. void *rsp, int len)
  1095. {
  1096. struct ib_device *dev = target->srp_host->srp_dev->dev;
  1097. unsigned long flags;
  1098. struct srp_iu *iu;
  1099. int err;
  1100. spin_lock_irqsave(&target->lock, flags);
  1101. target->req_lim += req_delta;
  1102. iu = __srp_get_tx_iu(target, SRP_IU_RSP);
  1103. spin_unlock_irqrestore(&target->lock, flags);
  1104. if (!iu) {
  1105. shost_printk(KERN_ERR, target->scsi_host, PFX
  1106. "no IU available to send response\n");
  1107. return 1;
  1108. }
  1109. ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE);
  1110. memcpy(iu->buf, rsp, len);
  1111. ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE);
  1112. err = srp_post_send(target, iu, len);
  1113. if (err) {
  1114. shost_printk(KERN_ERR, target->scsi_host, PFX
  1115. "unable to post response: %d\n", err);
  1116. srp_put_tx_iu(target, iu, SRP_IU_RSP);
  1117. }
  1118. return err;
  1119. }
  1120. static void srp_process_cred_req(struct srp_target_port *target,
  1121. struct srp_cred_req *req)
  1122. {
  1123. struct srp_cred_rsp rsp = {
  1124. .opcode = SRP_CRED_RSP,
  1125. .tag = req->tag,
  1126. };
  1127. s32 delta = be32_to_cpu(req->req_lim_delta);
  1128. if (srp_response_common(target, delta, &rsp, sizeof rsp))
  1129. shost_printk(KERN_ERR, target->scsi_host, PFX
  1130. "problems processing SRP_CRED_REQ\n");
  1131. }
  1132. static void srp_process_aer_req(struct srp_target_port *target,
  1133. struct srp_aer_req *req)
  1134. {
  1135. struct srp_aer_rsp rsp = {
  1136. .opcode = SRP_AER_RSP,
  1137. .tag = req->tag,
  1138. };
  1139. s32 delta = be32_to_cpu(req->req_lim_delta);
  1140. shost_printk(KERN_ERR, target->scsi_host, PFX
  1141. "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun));
  1142. if (srp_response_common(target, delta, &rsp, sizeof rsp))
  1143. shost_printk(KERN_ERR, target->scsi_host, PFX
  1144. "problems processing SRP_AER_REQ\n");
  1145. }
  1146. static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
  1147. {
  1148. struct ib_device *dev = target->srp_host->srp_dev->dev;
  1149. struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id;
  1150. int res;
  1151. u8 opcode;
  1152. ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len,
  1153. DMA_FROM_DEVICE);
  1154. opcode = *(u8 *) iu->buf;
  1155. if (0) {
  1156. shost_printk(KERN_ERR, target->scsi_host,
  1157. PFX "recv completion, opcode 0x%02x\n", opcode);
  1158. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1,
  1159. iu->buf, wc->byte_len, true);
  1160. }
  1161. switch (opcode) {
  1162. case SRP_RSP:
  1163. srp_process_rsp(target, iu->buf);
  1164. break;
  1165. case SRP_CRED_REQ:
  1166. srp_process_cred_req(target, iu->buf);
  1167. break;
  1168. case SRP_AER_REQ:
  1169. srp_process_aer_req(target, iu->buf);
  1170. break;
  1171. case SRP_T_LOGOUT:
  1172. /* XXX Handle target logout */
  1173. shost_printk(KERN_WARNING, target->scsi_host,
  1174. PFX "Got target logout request\n");
  1175. break;
  1176. default:
  1177. shost_printk(KERN_WARNING, target->scsi_host,
  1178. PFX "Unhandled SRP opcode 0x%02x\n", opcode);
  1179. break;
  1180. }
  1181. ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len,
  1182. DMA_FROM_DEVICE);
  1183. res = srp_post_recv(target, iu);
  1184. if (res != 0)
  1185. shost_printk(KERN_ERR, target->scsi_host,
  1186. PFX "Recv failed with error code %d\n", res);
  1187. }
  1188. /**
  1189. * srp_tl_err_work() - handle a transport layer error
  1190. *
  1191. * Note: This function may get invoked before the rport has been created,
  1192. * hence the target->rport test.
  1193. */
  1194. static void srp_tl_err_work(struct work_struct *work)
  1195. {
  1196. struct srp_target_port *target;
  1197. target = container_of(work, struct srp_target_port, tl_err_work);
  1198. if (target->rport)
  1199. srp_start_tl_fail_timers(target->rport);
  1200. }
  1201. static void srp_handle_qp_err(enum ib_wc_status wc_status, bool send_err,
  1202. struct srp_target_port *target)
  1203. {
  1204. if (target->connected && !target->qp_in_error) {
  1205. shost_printk(KERN_ERR, target->scsi_host,
  1206. PFX "failed %s status %d\n",
  1207. send_err ? "send" : "receive",
  1208. wc_status);
  1209. queue_work(system_long_wq, &target->tl_err_work);
  1210. }
  1211. target->qp_in_error = true;
  1212. }
  1213. static void srp_recv_completion(struct ib_cq *cq, void *target_ptr)
  1214. {
  1215. struct srp_target_port *target = target_ptr;
  1216. struct ib_wc wc;
  1217. ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
  1218. while (ib_poll_cq(cq, 1, &wc) > 0) {
  1219. if (likely(wc.status == IB_WC_SUCCESS)) {
  1220. srp_handle_recv(target, &wc);
  1221. } else {
  1222. srp_handle_qp_err(wc.status, false, target);
  1223. }
  1224. }
  1225. }
  1226. static void srp_send_completion(struct ib_cq *cq, void *target_ptr)
  1227. {
  1228. struct srp_target_port *target = target_ptr;
  1229. struct ib_wc wc;
  1230. struct srp_iu *iu;
  1231. while (ib_poll_cq(cq, 1, &wc) > 0) {
  1232. if (likely(wc.status == IB_WC_SUCCESS)) {
  1233. iu = (struct srp_iu *) (uintptr_t) wc.wr_id;
  1234. list_add(&iu->list, &target->free_tx);
  1235. } else {
  1236. srp_handle_qp_err(wc.status, true, target);
  1237. }
  1238. }
  1239. }
  1240. static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd)
  1241. {
  1242. struct srp_target_port *target = host_to_target(shost);
  1243. struct srp_rport *rport = target->rport;
  1244. struct srp_request *req;
  1245. struct srp_iu *iu;
  1246. struct srp_cmd *cmd;
  1247. struct ib_device *dev;
  1248. unsigned long flags;
  1249. int len, result;
  1250. const bool in_scsi_eh = !in_interrupt() && current == shost->ehandler;
  1251. /*
  1252. * The SCSI EH thread is the only context from which srp_queuecommand()
  1253. * can get invoked for blocked devices (SDEV_BLOCK /
  1254. * SDEV_CREATED_BLOCK). Avoid racing with srp_reconnect_rport() by
  1255. * locking the rport mutex if invoked from inside the SCSI EH.
  1256. */
  1257. if (in_scsi_eh)
  1258. mutex_lock(&rport->mutex);
  1259. result = srp_chkready(target->rport);
  1260. if (unlikely(result)) {
  1261. scmnd->result = result;
  1262. scmnd->scsi_done(scmnd);
  1263. goto unlock_rport;
  1264. }
  1265. spin_lock_irqsave(&target->lock, flags);
  1266. iu = __srp_get_tx_iu(target, SRP_IU_CMD);
  1267. if (!iu)
  1268. goto err_unlock;
  1269. req = list_first_entry(&target->free_reqs, struct srp_request, list);
  1270. list_del(&req->list);
  1271. spin_unlock_irqrestore(&target->lock, flags);
  1272. dev = target->srp_host->srp_dev->dev;
  1273. ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len,
  1274. DMA_TO_DEVICE);
  1275. scmnd->result = 0;
  1276. scmnd->host_scribble = (void *) req;
  1277. cmd = iu->buf;
  1278. memset(cmd, 0, sizeof *cmd);
  1279. cmd->opcode = SRP_CMD;
  1280. cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
  1281. cmd->tag = req->index;
  1282. memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
  1283. req->scmnd = scmnd;
  1284. req->cmd = iu;
  1285. len = srp_map_data(scmnd, target, req);
  1286. if (len < 0) {
  1287. shost_printk(KERN_ERR, target->scsi_host,
  1288. PFX "Failed to map data\n");
  1289. goto err_iu;
  1290. }
  1291. ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len,
  1292. DMA_TO_DEVICE);
  1293. if (srp_post_send(target, iu, len)) {
  1294. shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n");
  1295. goto err_unmap;
  1296. }
  1297. unlock_rport:
  1298. if (in_scsi_eh)
  1299. mutex_unlock(&rport->mutex);
  1300. return 0;
  1301. err_unmap:
  1302. srp_unmap_data(scmnd, target, req);
  1303. err_iu:
  1304. srp_put_tx_iu(target, iu, SRP_IU_CMD);
  1305. spin_lock_irqsave(&target->lock, flags);
  1306. list_add(&req->list, &target->free_reqs);
  1307. err_unlock:
  1308. spin_unlock_irqrestore(&target->lock, flags);
  1309. if (in_scsi_eh)
  1310. mutex_unlock(&rport->mutex);
  1311. return SCSI_MLQUEUE_HOST_BUSY;
  1312. }
  1313. /*
  1314. * Note: the resources allocated in this function are freed in
  1315. * srp_free_target_ib().
  1316. */
  1317. static int srp_alloc_iu_bufs(struct srp_target_port *target)
  1318. {
  1319. int i;
  1320. target->rx_ring = kzalloc(target->queue_size * sizeof(*target->rx_ring),
  1321. GFP_KERNEL);
  1322. if (!target->rx_ring)
  1323. goto err_no_ring;
  1324. target->tx_ring = kzalloc(target->queue_size * sizeof(*target->tx_ring),
  1325. GFP_KERNEL);
  1326. if (!target->tx_ring)
  1327. goto err_no_ring;
  1328. for (i = 0; i < target->queue_size; ++i) {
  1329. target->rx_ring[i] = srp_alloc_iu(target->srp_host,
  1330. target->max_ti_iu_len,
  1331. GFP_KERNEL, DMA_FROM_DEVICE);
  1332. if (!target->rx_ring[i])
  1333. goto err;
  1334. }
  1335. for (i = 0; i < target->queue_size; ++i) {
  1336. target->tx_ring[i] = srp_alloc_iu(target->srp_host,
  1337. target->max_iu_len,
  1338. GFP_KERNEL, DMA_TO_DEVICE);
  1339. if (!target->tx_ring[i])
  1340. goto err;
  1341. list_add(&target->tx_ring[i]->list, &target->free_tx);
  1342. }
  1343. return 0;
  1344. err:
  1345. for (i = 0; i < target->queue_size; ++i) {
  1346. srp_free_iu(target->srp_host, target->rx_ring[i]);
  1347. srp_free_iu(target->srp_host, target->tx_ring[i]);
  1348. }
  1349. err_no_ring:
  1350. kfree(target->tx_ring);
  1351. target->tx_ring = NULL;
  1352. kfree(target->rx_ring);
  1353. target->rx_ring = NULL;
  1354. return -ENOMEM;
  1355. }
  1356. static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask)
  1357. {
  1358. uint64_t T_tr_ns, max_compl_time_ms;
  1359. uint32_t rq_tmo_jiffies;
  1360. /*
  1361. * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair,
  1362. * table 91), both the QP timeout and the retry count have to be set
  1363. * for RC QP's during the RTR to RTS transition.
  1364. */
  1365. WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) !=
  1366. (IB_QP_TIMEOUT | IB_QP_RETRY_CNT));
  1367. /*
  1368. * Set target->rq_tmo_jiffies to one second more than the largest time
  1369. * it can take before an error completion is generated. See also
  1370. * C9-140..142 in the IBTA spec for more information about how to
  1371. * convert the QP Local ACK Timeout value to nanoseconds.
  1372. */
  1373. T_tr_ns = 4096 * (1ULL << qp_attr->timeout);
  1374. max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns;
  1375. do_div(max_compl_time_ms, NSEC_PER_MSEC);
  1376. rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000);
  1377. return rq_tmo_jiffies;
  1378. }
  1379. static void srp_cm_rep_handler(struct ib_cm_id *cm_id,
  1380. struct srp_login_rsp *lrsp,
  1381. struct srp_target_port *target)
  1382. {
  1383. struct ib_qp_attr *qp_attr = NULL;
  1384. int attr_mask = 0;
  1385. int ret;
  1386. int i;
  1387. if (lrsp->opcode == SRP_LOGIN_RSP) {
  1388. target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len);
  1389. target->req_lim = be32_to_cpu(lrsp->req_lim_delta);
  1390. /*
  1391. * Reserve credits for task management so we don't
  1392. * bounce requests back to the SCSI mid-layer.
  1393. */
  1394. target->scsi_host->can_queue
  1395. = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE,
  1396. target->scsi_host->can_queue);
  1397. target->scsi_host->cmd_per_lun
  1398. = min_t(int, target->scsi_host->can_queue,
  1399. target->scsi_host->cmd_per_lun);
  1400. } else {
  1401. shost_printk(KERN_WARNING, target->scsi_host,
  1402. PFX "Unhandled RSP opcode %#x\n", lrsp->opcode);
  1403. ret = -ECONNRESET;
  1404. goto error;
  1405. }
  1406. if (!target->rx_ring) {
  1407. ret = srp_alloc_iu_bufs(target);
  1408. if (ret)
  1409. goto error;
  1410. }
  1411. ret = -ENOMEM;
  1412. qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
  1413. if (!qp_attr)
  1414. goto error;
  1415. qp_attr->qp_state = IB_QPS_RTR;
  1416. ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
  1417. if (ret)
  1418. goto error_free;
  1419. ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
  1420. if (ret)
  1421. goto error_free;
  1422. for (i = 0; i < target->queue_size; i++) {
  1423. struct srp_iu *iu = target->rx_ring[i];
  1424. ret = srp_post_recv(target, iu);
  1425. if (ret)
  1426. goto error_free;
  1427. }
  1428. qp_attr->qp_state = IB_QPS_RTS;
  1429. ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
  1430. if (ret)
  1431. goto error_free;
  1432. target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask);
  1433. ret = ib_modify_qp(target->qp, qp_attr, attr_mask);
  1434. if (ret)
  1435. goto error_free;
  1436. ret = ib_send_cm_rtu(cm_id, NULL, 0);
  1437. error_free:
  1438. kfree(qp_attr);
  1439. error:
  1440. target->status = ret;
  1441. }
  1442. static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
  1443. struct ib_cm_event *event,
  1444. struct srp_target_port *target)
  1445. {
  1446. struct Scsi_Host *shost = target->scsi_host;
  1447. struct ib_class_port_info *cpi;
  1448. int opcode;
  1449. switch (event->param.rej_rcvd.reason) {
  1450. case IB_CM_REJ_PORT_CM_REDIRECT:
  1451. cpi = event->param.rej_rcvd.ari;
  1452. target->path.dlid = cpi->redirect_lid;
  1453. target->path.pkey = cpi->redirect_pkey;
  1454. cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
  1455. memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
  1456. target->status = target->path.dlid ?
  1457. SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
  1458. break;
  1459. case IB_CM_REJ_PORT_REDIRECT:
  1460. if (srp_target_is_topspin(target)) {
  1461. /*
  1462. * Topspin/Cisco SRP gateways incorrectly send
  1463. * reject reason code 25 when they mean 24
  1464. * (port redirect).
  1465. */
  1466. memcpy(target->path.dgid.raw,
  1467. event->param.rej_rcvd.ari, 16);
  1468. shost_printk(KERN_DEBUG, shost,
  1469. PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
  1470. (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
  1471. (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
  1472. target->status = SRP_PORT_REDIRECT;
  1473. } else {
  1474. shost_printk(KERN_WARNING, shost,
  1475. " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
  1476. target->status = -ECONNRESET;
  1477. }
  1478. break;
  1479. case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
  1480. shost_printk(KERN_WARNING, shost,
  1481. " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
  1482. target->status = -ECONNRESET;
  1483. break;
  1484. case IB_CM_REJ_CONSUMER_DEFINED:
  1485. opcode = *(u8 *) event->private_data;
  1486. if (opcode == SRP_LOGIN_REJ) {
  1487. struct srp_login_rej *rej = event->private_data;
  1488. u32 reason = be32_to_cpu(rej->reason);
  1489. if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
  1490. shost_printk(KERN_WARNING, shost,
  1491. PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
  1492. else
  1493. shost_printk(KERN_WARNING, shost,
  1494. PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
  1495. } else
  1496. shost_printk(KERN_WARNING, shost,
  1497. " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
  1498. " opcode 0x%02x\n", opcode);
  1499. target->status = -ECONNRESET;
  1500. break;
  1501. case IB_CM_REJ_STALE_CONN:
  1502. shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n");
  1503. target->status = SRP_STALE_CONN;
  1504. break;
  1505. default:
  1506. shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n",
  1507. event->param.rej_rcvd.reason);
  1508. target->status = -ECONNRESET;
  1509. }
  1510. }
  1511. static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
  1512. {
  1513. struct srp_target_port *target = cm_id->context;
  1514. int comp = 0;
  1515. switch (event->event) {
  1516. case IB_CM_REQ_ERROR:
  1517. shost_printk(KERN_DEBUG, target->scsi_host,
  1518. PFX "Sending CM REQ failed\n");
  1519. comp = 1;
  1520. target->status = -ECONNRESET;
  1521. break;
  1522. case IB_CM_REP_RECEIVED:
  1523. comp = 1;
  1524. srp_cm_rep_handler(cm_id, event->private_data, target);
  1525. break;
  1526. case IB_CM_REJ_RECEIVED:
  1527. shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n");
  1528. comp = 1;
  1529. srp_cm_rej_handler(cm_id, event, target);
  1530. break;
  1531. case IB_CM_DREQ_RECEIVED:
  1532. shost_printk(KERN_WARNING, target->scsi_host,
  1533. PFX "DREQ received - connection closed\n");
  1534. srp_change_conn_state(target, false);
  1535. if (ib_send_cm_drep(cm_id, NULL, 0))
  1536. shost_printk(KERN_ERR, target->scsi_host,
  1537. PFX "Sending CM DREP failed\n");
  1538. queue_work(system_long_wq, &target->tl_err_work);
  1539. break;
  1540. case IB_CM_TIMEWAIT_EXIT:
  1541. shost_printk(KERN_ERR, target->scsi_host,
  1542. PFX "connection closed\n");
  1543. target->status = 0;
  1544. break;
  1545. case IB_CM_MRA_RECEIVED:
  1546. case IB_CM_DREQ_ERROR:
  1547. case IB_CM_DREP_RECEIVED:
  1548. break;
  1549. default:
  1550. shost_printk(KERN_WARNING, target->scsi_host,
  1551. PFX "Unhandled CM event %d\n", event->event);
  1552. break;
  1553. }
  1554. if (comp)
  1555. complete(&target->done);
  1556. return 0;
  1557. }
  1558. /**
  1559. * srp_change_queue_type - changing device queue tag type
  1560. * @sdev: scsi device struct
  1561. * @tag_type: requested tag type
  1562. *
  1563. * Returns queue tag type.
  1564. */
  1565. static int
  1566. srp_change_queue_type(struct scsi_device *sdev, int tag_type)
  1567. {
  1568. if (sdev->tagged_supported) {
  1569. scsi_set_tag_type(sdev, tag_type);
  1570. if (tag_type)
  1571. scsi_activate_tcq(sdev, sdev->queue_depth);
  1572. else
  1573. scsi_deactivate_tcq(sdev, sdev->queue_depth);
  1574. } else
  1575. tag_type = 0;
  1576. return tag_type;
  1577. }
  1578. /**
  1579. * srp_change_queue_depth - setting device queue depth
  1580. * @sdev: scsi device struct
  1581. * @qdepth: requested queue depth
  1582. * @reason: SCSI_QDEPTH_DEFAULT/SCSI_QDEPTH_QFULL/SCSI_QDEPTH_RAMP_UP
  1583. * (see include/scsi/scsi_host.h for definition)
  1584. *
  1585. * Returns queue depth.
  1586. */
  1587. static int
  1588. srp_change_queue_depth(struct scsi_device *sdev, int qdepth, int reason)
  1589. {
  1590. struct Scsi_Host *shost = sdev->host;
  1591. int max_depth;
  1592. if (reason == SCSI_QDEPTH_DEFAULT || reason == SCSI_QDEPTH_RAMP_UP) {
  1593. max_depth = shost->can_queue;
  1594. if (!sdev->tagged_supported)
  1595. max_depth = 1;
  1596. if (qdepth > max_depth)
  1597. qdepth = max_depth;
  1598. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), qdepth);
  1599. } else if (reason == SCSI_QDEPTH_QFULL)
  1600. scsi_track_queue_full(sdev, qdepth);
  1601. else
  1602. return -EOPNOTSUPP;
  1603. return sdev->queue_depth;
  1604. }
  1605. static int srp_send_tsk_mgmt(struct srp_target_port *target,
  1606. u64 req_tag, unsigned int lun, u8 func)
  1607. {
  1608. struct srp_rport *rport = target->rport;
  1609. struct ib_device *dev = target->srp_host->srp_dev->dev;
  1610. struct srp_iu *iu;
  1611. struct srp_tsk_mgmt *tsk_mgmt;
  1612. if (!target->connected || target->qp_in_error)
  1613. return -1;
  1614. init_completion(&target->tsk_mgmt_done);
  1615. /*
  1616. * Lock the rport mutex to avoid that srp_create_target_ib() is
  1617. * invoked while a task management function is being sent.
  1618. */
  1619. mutex_lock(&rport->mutex);
  1620. spin_lock_irq(&target->lock);
  1621. iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT);
  1622. spin_unlock_irq(&target->lock);
  1623. if (!iu) {
  1624. mutex_unlock(&rport->mutex);
  1625. return -1;
  1626. }
  1627. ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt,
  1628. DMA_TO_DEVICE);
  1629. tsk_mgmt = iu->buf;
  1630. memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
  1631. tsk_mgmt->opcode = SRP_TSK_MGMT;
  1632. tsk_mgmt->lun = cpu_to_be64((u64) lun << 48);
  1633. tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT;
  1634. tsk_mgmt->tsk_mgmt_func = func;
  1635. tsk_mgmt->task_tag = req_tag;
  1636. ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt,
  1637. DMA_TO_DEVICE);
  1638. if (srp_post_send(target, iu, sizeof *tsk_mgmt)) {
  1639. srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT);
  1640. mutex_unlock(&rport->mutex);
  1641. return -1;
  1642. }
  1643. mutex_unlock(&rport->mutex);
  1644. if (!wait_for_completion_timeout(&target->tsk_mgmt_done,
  1645. msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
  1646. return -1;
  1647. return 0;
  1648. }
  1649. static int srp_abort(struct scsi_cmnd *scmnd)
  1650. {
  1651. struct srp_target_port *target = host_to_target(scmnd->device->host);
  1652. struct srp_request *req = (struct srp_request *) scmnd->host_scribble;
  1653. int ret;
  1654. shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n");
  1655. if (!req || !srp_claim_req(target, req, scmnd))
  1656. return SUCCESS;
  1657. if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun,
  1658. SRP_TSK_ABORT_TASK) == 0)
  1659. ret = SUCCESS;
  1660. else if (target->rport->state == SRP_RPORT_LOST)
  1661. ret = FAST_IO_FAIL;
  1662. else
  1663. ret = FAILED;
  1664. srp_free_req(target, req, scmnd, 0);
  1665. scmnd->result = DID_ABORT << 16;
  1666. scmnd->scsi_done(scmnd);
  1667. return ret;
  1668. }
  1669. static int srp_reset_device(struct scsi_cmnd *scmnd)
  1670. {
  1671. struct srp_target_port *target = host_to_target(scmnd->device->host);
  1672. int i;
  1673. shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n");
  1674. if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun,
  1675. SRP_TSK_LUN_RESET))
  1676. return FAILED;
  1677. if (target->tsk_mgmt_status)
  1678. return FAILED;
  1679. for (i = 0; i < target->req_ring_size; ++i) {
  1680. struct srp_request *req = &target->req_ring[i];
  1681. if (req->scmnd && req->scmnd->device == scmnd->device)
  1682. srp_finish_req(target, req, DID_RESET << 16);
  1683. }
  1684. return SUCCESS;
  1685. }
  1686. static int srp_reset_host(struct scsi_cmnd *scmnd)
  1687. {
  1688. struct srp_target_port *target = host_to_target(scmnd->device->host);
  1689. shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n");
  1690. return srp_reconnect_rport(target->rport) == 0 ? SUCCESS : FAILED;
  1691. }
  1692. static int srp_slave_configure(struct scsi_device *sdev)
  1693. {
  1694. struct Scsi_Host *shost = sdev->host;
  1695. struct srp_target_port *target = host_to_target(shost);
  1696. struct request_queue *q = sdev->request_queue;
  1697. unsigned long timeout;
  1698. if (sdev->type == TYPE_DISK) {
  1699. timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies);
  1700. blk_queue_rq_timeout(q, timeout);
  1701. }
  1702. return 0;
  1703. }
  1704. static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr,
  1705. char *buf)
  1706. {
  1707. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1708. return sprintf(buf, "0x%016llx\n",
  1709. (unsigned long long) be64_to_cpu(target->id_ext));
  1710. }
  1711. static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr,
  1712. char *buf)
  1713. {
  1714. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1715. return sprintf(buf, "0x%016llx\n",
  1716. (unsigned long long) be64_to_cpu(target->ioc_guid));
  1717. }
  1718. static ssize_t show_service_id(struct device *dev,
  1719. struct device_attribute *attr, char *buf)
  1720. {
  1721. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1722. return sprintf(buf, "0x%016llx\n",
  1723. (unsigned long long) be64_to_cpu(target->service_id));
  1724. }
  1725. static ssize_t show_pkey(struct device *dev, struct device_attribute *attr,
  1726. char *buf)
  1727. {
  1728. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1729. return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
  1730. }
  1731. static ssize_t show_sgid(struct device *dev, struct device_attribute *attr,
  1732. char *buf)
  1733. {
  1734. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1735. return sprintf(buf, "%pI6\n", target->path.sgid.raw);
  1736. }
  1737. static ssize_t show_dgid(struct device *dev, struct device_attribute *attr,
  1738. char *buf)
  1739. {
  1740. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1741. return sprintf(buf, "%pI6\n", target->path.dgid.raw);
  1742. }
  1743. static ssize_t show_orig_dgid(struct device *dev,
  1744. struct device_attribute *attr, char *buf)
  1745. {
  1746. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1747. return sprintf(buf, "%pI6\n", target->orig_dgid);
  1748. }
  1749. static ssize_t show_req_lim(struct device *dev,
  1750. struct device_attribute *attr, char *buf)
  1751. {
  1752. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1753. return sprintf(buf, "%d\n", target->req_lim);
  1754. }
  1755. static ssize_t show_zero_req_lim(struct device *dev,
  1756. struct device_attribute *attr, char *buf)
  1757. {
  1758. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1759. return sprintf(buf, "%d\n", target->zero_req_lim);
  1760. }
  1761. static ssize_t show_local_ib_port(struct device *dev,
  1762. struct device_attribute *attr, char *buf)
  1763. {
  1764. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1765. return sprintf(buf, "%d\n", target->srp_host->port);
  1766. }
  1767. static ssize_t show_local_ib_device(struct device *dev,
  1768. struct device_attribute *attr, char *buf)
  1769. {
  1770. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1771. return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name);
  1772. }
  1773. static ssize_t show_comp_vector(struct device *dev,
  1774. struct device_attribute *attr, char *buf)
  1775. {
  1776. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1777. return sprintf(buf, "%d\n", target->comp_vector);
  1778. }
  1779. static ssize_t show_tl_retry_count(struct device *dev,
  1780. struct device_attribute *attr, char *buf)
  1781. {
  1782. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1783. return sprintf(buf, "%d\n", target->tl_retry_count);
  1784. }
  1785. static ssize_t show_cmd_sg_entries(struct device *dev,
  1786. struct device_attribute *attr, char *buf)
  1787. {
  1788. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1789. return sprintf(buf, "%u\n", target->cmd_sg_cnt);
  1790. }
  1791. static ssize_t show_allow_ext_sg(struct device *dev,
  1792. struct device_attribute *attr, char *buf)
  1793. {
  1794. struct srp_target_port *target = host_to_target(class_to_shost(dev));
  1795. return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false");
  1796. }
  1797. static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
  1798. static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
  1799. static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
  1800. static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
  1801. static DEVICE_ATTR(sgid, S_IRUGO, show_sgid, NULL);
  1802. static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
  1803. static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL);
  1804. static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL);
  1805. static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
  1806. static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
  1807. static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
  1808. static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL);
  1809. static DEVICE_ATTR(tl_retry_count, S_IRUGO, show_tl_retry_count, NULL);
  1810. static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL);
  1811. static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL);
  1812. static struct device_attribute *srp_host_attrs[] = {
  1813. &dev_attr_id_ext,
  1814. &dev_attr_ioc_guid,
  1815. &dev_attr_service_id,
  1816. &dev_attr_pkey,
  1817. &dev_attr_sgid,
  1818. &dev_attr_dgid,
  1819. &dev_attr_orig_dgid,
  1820. &dev_attr_req_lim,
  1821. &dev_attr_zero_req_lim,
  1822. &dev_attr_local_ib_port,
  1823. &dev_attr_local_ib_device,
  1824. &dev_attr_comp_vector,
  1825. &dev_attr_tl_retry_count,
  1826. &dev_attr_cmd_sg_entries,
  1827. &dev_attr_allow_ext_sg,
  1828. NULL
  1829. };
  1830. static struct scsi_host_template srp_template = {
  1831. .module = THIS_MODULE,
  1832. .name = "InfiniBand SRP initiator",
  1833. .proc_name = DRV_NAME,
  1834. .slave_configure = srp_slave_configure,
  1835. .info = srp_target_info,
  1836. .queuecommand = srp_queuecommand,
  1837. .change_queue_depth = srp_change_queue_depth,
  1838. .change_queue_type = srp_change_queue_type,
  1839. .eh_abort_handler = srp_abort,
  1840. .eh_device_reset_handler = srp_reset_device,
  1841. .eh_host_reset_handler = srp_reset_host,
  1842. .skip_settle_delay = true,
  1843. .sg_tablesize = SRP_DEF_SG_TABLESIZE,
  1844. .can_queue = SRP_DEFAULT_CMD_SQ_SIZE,
  1845. .this_id = -1,
  1846. .cmd_per_lun = SRP_DEFAULT_CMD_SQ_SIZE,
  1847. .use_clustering = ENABLE_CLUSTERING,
  1848. .shost_attrs = srp_host_attrs
  1849. };
  1850. static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
  1851. {
  1852. struct srp_rport_identifiers ids;
  1853. struct srp_rport *rport;
  1854. sprintf(target->target_name, "SRP.T10:%016llX",
  1855. (unsigned long long) be64_to_cpu(target->id_ext));
  1856. if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device))
  1857. return -ENODEV;
  1858. memcpy(ids.port_id, &target->id_ext, 8);
  1859. memcpy(ids.port_id + 8, &target->ioc_guid, 8);
  1860. ids.roles = SRP_RPORT_ROLE_TARGET;
  1861. rport = srp_rport_add(target->scsi_host, &ids);
  1862. if (IS_ERR(rport)) {
  1863. scsi_remove_host(target->scsi_host);
  1864. return PTR_ERR(rport);
  1865. }
  1866. rport->lld_data = target;
  1867. target->rport = rport;
  1868. spin_lock(&host->target_lock);
  1869. list_add_tail(&target->list, &host->target_list);
  1870. spin_unlock(&host->target_lock);
  1871. target->state = SRP_TARGET_LIVE;
  1872. scsi_scan_target(&target->scsi_host->shost_gendev,
  1873. 0, target->scsi_id, SCAN_WILD_CARD, 0);
  1874. return 0;
  1875. }
  1876. static void srp_release_dev(struct device *dev)
  1877. {
  1878. struct srp_host *host =
  1879. container_of(dev, struct srp_host, dev);
  1880. complete(&host->released);
  1881. }
  1882. static struct class srp_class = {
  1883. .name = "infiniband_srp",
  1884. .dev_release = srp_release_dev
  1885. };
  1886. /**
  1887. * srp_conn_unique() - check whether the connection to a target is unique
  1888. */
  1889. static bool srp_conn_unique(struct srp_host *host,
  1890. struct srp_target_port *target)
  1891. {
  1892. struct srp_target_port *t;
  1893. bool ret = false;
  1894. if (target->state == SRP_TARGET_REMOVED)
  1895. goto out;
  1896. ret = true;
  1897. spin_lock(&host->target_lock);
  1898. list_for_each_entry(t, &host->target_list, list) {
  1899. if (t != target &&
  1900. target->id_ext == t->id_ext &&
  1901. target->ioc_guid == t->ioc_guid &&
  1902. target->initiator_ext == t->initiator_ext) {
  1903. ret = false;
  1904. break;
  1905. }
  1906. }
  1907. spin_unlock(&host->target_lock);
  1908. out:
  1909. return ret;
  1910. }
  1911. /*
  1912. * Target ports are added by writing
  1913. *
  1914. * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
  1915. * pkey=<P_Key>,service_id=<service ID>
  1916. *
  1917. * to the add_target sysfs attribute.
  1918. */
  1919. enum {
  1920. SRP_OPT_ERR = 0,
  1921. SRP_OPT_ID_EXT = 1 << 0,
  1922. SRP_OPT_IOC_GUID = 1 << 1,
  1923. SRP_OPT_DGID = 1 << 2,
  1924. SRP_OPT_PKEY = 1 << 3,
  1925. SRP_OPT_SERVICE_ID = 1 << 4,
  1926. SRP_OPT_MAX_SECT = 1 << 5,
  1927. SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
  1928. SRP_OPT_IO_CLASS = 1 << 7,
  1929. SRP_OPT_INITIATOR_EXT = 1 << 8,
  1930. SRP_OPT_CMD_SG_ENTRIES = 1 << 9,
  1931. SRP_OPT_ALLOW_EXT_SG = 1 << 10,
  1932. SRP_OPT_SG_TABLESIZE = 1 << 11,
  1933. SRP_OPT_COMP_VECTOR = 1 << 12,
  1934. SRP_OPT_TL_RETRY_COUNT = 1 << 13,
  1935. SRP_OPT_QUEUE_SIZE = 1 << 14,
  1936. SRP_OPT_ALL = (SRP_OPT_ID_EXT |
  1937. SRP_OPT_IOC_GUID |
  1938. SRP_OPT_DGID |
  1939. SRP_OPT_PKEY |
  1940. SRP_OPT_SERVICE_ID),
  1941. };
  1942. static const match_table_t srp_opt_tokens = {
  1943. { SRP_OPT_ID_EXT, "id_ext=%s" },
  1944. { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
  1945. { SRP_OPT_DGID, "dgid=%s" },
  1946. { SRP_OPT_PKEY, "pkey=%x" },
  1947. { SRP_OPT_SERVICE_ID, "service_id=%s" },
  1948. { SRP_OPT_MAX_SECT, "max_sect=%d" },
  1949. { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
  1950. { SRP_OPT_IO_CLASS, "io_class=%x" },
  1951. { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" },
  1952. { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" },
  1953. { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" },
  1954. { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" },
  1955. { SRP_OPT_COMP_VECTOR, "comp_vector=%u" },
  1956. { SRP_OPT_TL_RETRY_COUNT, "tl_retry_count=%u" },
  1957. { SRP_OPT_QUEUE_SIZE, "queue_size=%d" },
  1958. { SRP_OPT_ERR, NULL }
  1959. };
  1960. static int srp_parse_options(const char *buf, struct srp_target_port *target)
  1961. {
  1962. char *options, *sep_opt;
  1963. char *p;
  1964. char dgid[3];
  1965. substring_t args[MAX_OPT_ARGS];
  1966. int opt_mask = 0;
  1967. int token;
  1968. int ret = -EINVAL;
  1969. int i;
  1970. options = kstrdup(buf, GFP_KERNEL);
  1971. if (!options)
  1972. return -ENOMEM;
  1973. sep_opt = options;
  1974. while ((p = strsep(&sep_opt, ",")) != NULL) {
  1975. if (!*p)
  1976. continue;
  1977. token = match_token(p, srp_opt_tokens, args);
  1978. opt_mask |= token;
  1979. switch (token) {
  1980. case SRP_OPT_ID_EXT:
  1981. p = match_strdup(args);
  1982. if (!p) {
  1983. ret = -ENOMEM;
  1984. goto out;
  1985. }
  1986. target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
  1987. kfree(p);
  1988. break;
  1989. case SRP_OPT_IOC_GUID:
  1990. p = match_strdup(args);
  1991. if (!p) {
  1992. ret = -ENOMEM;
  1993. goto out;
  1994. }
  1995. target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
  1996. kfree(p);
  1997. break;
  1998. case SRP_OPT_DGID:
  1999. p = match_strdup(args);
  2000. if (!p) {
  2001. ret = -ENOMEM;
  2002. goto out;
  2003. }
  2004. if (strlen(p) != 32) {
  2005. pr_warn("bad dest GID parameter '%s'\n", p);
  2006. kfree(p);
  2007. goto out;
  2008. }
  2009. for (i = 0; i < 16; ++i) {
  2010. strlcpy(dgid, p + i * 2, 3);
  2011. target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
  2012. }
  2013. kfree(p);
  2014. memcpy(target->orig_dgid, target->path.dgid.raw, 16);
  2015. break;
  2016. case SRP_OPT_PKEY:
  2017. if (match_hex(args, &token)) {
  2018. pr_warn("bad P_Key parameter '%s'\n", p);
  2019. goto out;
  2020. }
  2021. target->path.pkey = cpu_to_be16(token);
  2022. break;
  2023. case SRP_OPT_SERVICE_ID:
  2024. p = match_strdup(args);
  2025. if (!p) {
  2026. ret = -ENOMEM;
  2027. goto out;
  2028. }
  2029. target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
  2030. target->path.service_id = target->service_id;
  2031. kfree(p);
  2032. break;
  2033. case SRP_OPT_MAX_SECT:
  2034. if (match_int(args, &token)) {
  2035. pr_warn("bad max sect parameter '%s'\n", p);
  2036. goto out;
  2037. }
  2038. target->scsi_host->max_sectors = token;
  2039. break;
  2040. case SRP_OPT_QUEUE_SIZE:
  2041. if (match_int(args, &token) || token < 1) {
  2042. pr_warn("bad queue_size parameter '%s'\n", p);
  2043. goto out;
  2044. }
  2045. target->scsi_host->can_queue = token;
  2046. target->queue_size = token + SRP_RSP_SQ_SIZE +
  2047. SRP_TSK_MGMT_SQ_SIZE;
  2048. if (!(opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
  2049. target->scsi_host->cmd_per_lun = token;
  2050. break;
  2051. case SRP_OPT_MAX_CMD_PER_LUN:
  2052. if (match_int(args, &token) || token < 1) {
  2053. pr_warn("bad max cmd_per_lun parameter '%s'\n",
  2054. p);
  2055. goto out;
  2056. }
  2057. target->scsi_host->cmd_per_lun = token;
  2058. break;
  2059. case SRP_OPT_IO_CLASS:
  2060. if (match_hex(args, &token)) {
  2061. pr_warn("bad IO class parameter '%s'\n", p);
  2062. goto out;
  2063. }
  2064. if (token != SRP_REV10_IB_IO_CLASS &&
  2065. token != SRP_REV16A_IB_IO_CLASS) {
  2066. pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n",
  2067. token, SRP_REV10_IB_IO_CLASS,
  2068. SRP_REV16A_IB_IO_CLASS);
  2069. goto out;
  2070. }
  2071. target->io_class = token;
  2072. break;
  2073. case SRP_OPT_INITIATOR_EXT:
  2074. p = match_strdup(args);
  2075. if (!p) {
  2076. ret = -ENOMEM;
  2077. goto out;
  2078. }
  2079. target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
  2080. kfree(p);
  2081. break;
  2082. case SRP_OPT_CMD_SG_ENTRIES:
  2083. if (match_int(args, &token) || token < 1 || token > 255) {
  2084. pr_warn("bad max cmd_sg_entries parameter '%s'\n",
  2085. p);
  2086. goto out;
  2087. }
  2088. target->cmd_sg_cnt = token;
  2089. break;
  2090. case SRP_OPT_ALLOW_EXT_SG:
  2091. if (match_int(args, &token)) {
  2092. pr_warn("bad allow_ext_sg parameter '%s'\n", p);
  2093. goto out;
  2094. }
  2095. target->allow_ext_sg = !!token;
  2096. break;
  2097. case SRP_OPT_SG_TABLESIZE:
  2098. if (match_int(args, &token) || token < 1 ||
  2099. token > SCSI_MAX_SG_CHAIN_SEGMENTS) {
  2100. pr_warn("bad max sg_tablesize parameter '%s'\n",
  2101. p);
  2102. goto out;
  2103. }
  2104. target->sg_tablesize = token;
  2105. break;
  2106. case SRP_OPT_COMP_VECTOR:
  2107. if (match_int(args, &token) || token < 0) {
  2108. pr_warn("bad comp_vector parameter '%s'\n", p);
  2109. goto out;
  2110. }
  2111. target->comp_vector = token;
  2112. break;
  2113. case SRP_OPT_TL_RETRY_COUNT:
  2114. if (match_int(args, &token) || token < 2 || token > 7) {
  2115. pr_warn("bad tl_retry_count parameter '%s' (must be a number between 2 and 7)\n",
  2116. p);
  2117. goto out;
  2118. }
  2119. target->tl_retry_count = token;
  2120. break;
  2121. default:
  2122. pr_warn("unknown parameter or missing value '%s' in target creation request\n",
  2123. p);
  2124. goto out;
  2125. }
  2126. }
  2127. if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
  2128. ret = 0;
  2129. else
  2130. for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
  2131. if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
  2132. !(srp_opt_tokens[i].token & opt_mask))
  2133. pr_warn("target creation request is missing parameter '%s'\n",
  2134. srp_opt_tokens[i].pattern);
  2135. if (target->scsi_host->cmd_per_lun > target->scsi_host->can_queue
  2136. && (opt_mask & SRP_OPT_MAX_CMD_PER_LUN))
  2137. pr_warn("cmd_per_lun = %d > queue_size = %d\n",
  2138. target->scsi_host->cmd_per_lun,
  2139. target->scsi_host->can_queue);
  2140. out:
  2141. kfree(options);
  2142. return ret;
  2143. }
  2144. static ssize_t srp_create_target(struct device *dev,
  2145. struct device_attribute *attr,
  2146. const char *buf, size_t count)
  2147. {
  2148. struct srp_host *host =
  2149. container_of(dev, struct srp_host, dev);
  2150. struct Scsi_Host *target_host;
  2151. struct srp_target_port *target;
  2152. struct ib_device *ibdev = host->srp_dev->dev;
  2153. int ret;
  2154. target_host = scsi_host_alloc(&srp_template,
  2155. sizeof (struct srp_target_port));
  2156. if (!target_host)
  2157. return -ENOMEM;
  2158. target_host->transportt = ib_srp_transport_template;
  2159. target_host->max_channel = 0;
  2160. target_host->max_id = 1;
  2161. target_host->max_lun = SRP_MAX_LUN;
  2162. target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb;
  2163. target = host_to_target(target_host);
  2164. target->io_class = SRP_REV16A_IB_IO_CLASS;
  2165. target->scsi_host = target_host;
  2166. target->srp_host = host;
  2167. target->lkey = host->srp_dev->mr->lkey;
  2168. target->rkey = host->srp_dev->mr->rkey;
  2169. target->cmd_sg_cnt = cmd_sg_entries;
  2170. target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries;
  2171. target->allow_ext_sg = allow_ext_sg;
  2172. target->tl_retry_count = 7;
  2173. target->queue_size = SRP_DEFAULT_QUEUE_SIZE;
  2174. ret = srp_parse_options(buf, target);
  2175. if (ret)
  2176. goto err;
  2177. target->req_ring_size = target->queue_size - SRP_TSK_MGMT_SQ_SIZE;
  2178. if (!srp_conn_unique(target->srp_host, target)) {
  2179. shost_printk(KERN_INFO, target->scsi_host,
  2180. PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n",
  2181. be64_to_cpu(target->id_ext),
  2182. be64_to_cpu(target->ioc_guid),
  2183. be64_to_cpu(target->initiator_ext));
  2184. ret = -EEXIST;
  2185. goto err;
  2186. }
  2187. if (!host->srp_dev->fmr_pool && !target->allow_ext_sg &&
  2188. target->cmd_sg_cnt < target->sg_tablesize) {
  2189. pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n");
  2190. target->sg_tablesize = target->cmd_sg_cnt;
  2191. }
  2192. target_host->sg_tablesize = target->sg_tablesize;
  2193. target->indirect_size = target->sg_tablesize *
  2194. sizeof (struct srp_direct_buf);
  2195. target->max_iu_len = sizeof (struct srp_cmd) +
  2196. sizeof (struct srp_indirect_buf) +
  2197. target->cmd_sg_cnt * sizeof (struct srp_direct_buf);
  2198. INIT_WORK(&target->tl_err_work, srp_tl_err_work);
  2199. INIT_WORK(&target->remove_work, srp_remove_work);
  2200. spin_lock_init(&target->lock);
  2201. INIT_LIST_HEAD(&target->free_tx);
  2202. ret = srp_alloc_req_data(target);
  2203. if (ret)
  2204. goto err_free_mem;
  2205. ib_query_gid(ibdev, host->port, 0, &target->path.sgid);
  2206. shost_printk(KERN_DEBUG, target->scsi_host, PFX
  2207. "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
  2208. "service_id %016llx dgid %pI6\n",
  2209. (unsigned long long) be64_to_cpu(target->id_ext),
  2210. (unsigned long long) be64_to_cpu(target->ioc_guid),
  2211. be16_to_cpu(target->path.pkey),
  2212. (unsigned long long) be64_to_cpu(target->service_id),
  2213. target->path.dgid.raw);
  2214. ret = srp_create_target_ib(target);
  2215. if (ret)
  2216. goto err_free_mem;
  2217. ret = srp_new_cm_id(target);
  2218. if (ret)
  2219. goto err_free_ib;
  2220. ret = srp_connect_target(target);
  2221. if (ret) {
  2222. shost_printk(KERN_ERR, target->scsi_host,
  2223. PFX "Connection failed\n");
  2224. goto err_cm_id;
  2225. }
  2226. ret = srp_add_target(host, target);
  2227. if (ret)
  2228. goto err_disconnect;
  2229. return count;
  2230. err_disconnect:
  2231. srp_disconnect_target(target);
  2232. err_cm_id:
  2233. ib_destroy_cm_id(target->cm_id);
  2234. err_free_ib:
  2235. srp_free_target_ib(target);
  2236. err_free_mem:
  2237. srp_free_req_data(target);
  2238. err:
  2239. scsi_host_put(target_host);
  2240. return ret;
  2241. }
  2242. static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
  2243. static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr,
  2244. char *buf)
  2245. {
  2246. struct srp_host *host = container_of(dev, struct srp_host, dev);
  2247. return sprintf(buf, "%s\n", host->srp_dev->dev->name);
  2248. }
  2249. static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
  2250. static ssize_t show_port(struct device *dev, struct device_attribute *attr,
  2251. char *buf)
  2252. {
  2253. struct srp_host *host = container_of(dev, struct srp_host, dev);
  2254. return sprintf(buf, "%d\n", host->port);
  2255. }
  2256. static DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
  2257. static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
  2258. {
  2259. struct srp_host *host;
  2260. host = kzalloc(sizeof *host, GFP_KERNEL);
  2261. if (!host)
  2262. return NULL;
  2263. INIT_LIST_HEAD(&host->target_list);
  2264. spin_lock_init(&host->target_lock);
  2265. init_completion(&host->released);
  2266. host->srp_dev = device;
  2267. host->port = port;
  2268. host->dev.class = &srp_class;
  2269. host->dev.parent = device->dev->dma_device;
  2270. dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port);
  2271. if (device_register(&host->dev))
  2272. goto free_host;
  2273. if (device_create_file(&host->dev, &dev_attr_add_target))
  2274. goto err_class;
  2275. if (device_create_file(&host->dev, &dev_attr_ibdev))
  2276. goto err_class;
  2277. if (device_create_file(&host->dev, &dev_attr_port))
  2278. goto err_class;
  2279. return host;
  2280. err_class:
  2281. device_unregister(&host->dev);
  2282. free_host:
  2283. kfree(host);
  2284. return NULL;
  2285. }
  2286. static void srp_add_one(struct ib_device *device)
  2287. {
  2288. struct srp_device *srp_dev;
  2289. struct ib_device_attr *dev_attr;
  2290. struct ib_fmr_pool_param fmr_param;
  2291. struct srp_host *host;
  2292. int max_pages_per_fmr, fmr_page_shift, s, e, p;
  2293. dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
  2294. if (!dev_attr)
  2295. return;
  2296. if (ib_query_device(device, dev_attr)) {
  2297. pr_warn("Query device failed for %s\n", device->name);
  2298. goto free_attr;
  2299. }
  2300. srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
  2301. if (!srp_dev)
  2302. goto free_attr;
  2303. /*
  2304. * Use the smallest page size supported by the HCA, down to a
  2305. * minimum of 4096 bytes. We're unlikely to build large sglists
  2306. * out of smaller entries.
  2307. */
  2308. fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1);
  2309. srp_dev->fmr_page_size = 1 << fmr_page_shift;
  2310. srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1);
  2311. srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE;
  2312. INIT_LIST_HEAD(&srp_dev->dev_list);
  2313. srp_dev->dev = device;
  2314. srp_dev->pd = ib_alloc_pd(device);
  2315. if (IS_ERR(srp_dev->pd))
  2316. goto free_dev;
  2317. srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
  2318. IB_ACCESS_LOCAL_WRITE |
  2319. IB_ACCESS_REMOTE_READ |
  2320. IB_ACCESS_REMOTE_WRITE);
  2321. if (IS_ERR(srp_dev->mr))
  2322. goto err_pd;
  2323. for (max_pages_per_fmr = SRP_FMR_SIZE;
  2324. max_pages_per_fmr >= SRP_FMR_MIN_SIZE;
  2325. max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) {
  2326. memset(&fmr_param, 0, sizeof fmr_param);
  2327. fmr_param.pool_size = SRP_FMR_POOL_SIZE;
  2328. fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
  2329. fmr_param.cache = 1;
  2330. fmr_param.max_pages_per_fmr = max_pages_per_fmr;
  2331. fmr_param.page_shift = fmr_page_shift;
  2332. fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
  2333. IB_ACCESS_REMOTE_WRITE |
  2334. IB_ACCESS_REMOTE_READ);
  2335. srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
  2336. if (!IS_ERR(srp_dev->fmr_pool))
  2337. break;
  2338. }
  2339. if (IS_ERR(srp_dev->fmr_pool))
  2340. srp_dev->fmr_pool = NULL;
  2341. if (device->node_type == RDMA_NODE_IB_SWITCH) {
  2342. s = 0;
  2343. e = 0;
  2344. } else {
  2345. s = 1;
  2346. e = device->phys_port_cnt;
  2347. }
  2348. for (p = s; p <= e; ++p) {
  2349. host = srp_add_port(srp_dev, p);
  2350. if (host)
  2351. list_add_tail(&host->list, &srp_dev->dev_list);
  2352. }
  2353. ib_set_client_data(device, &srp_client, srp_dev);
  2354. goto free_attr;
  2355. err_pd:
  2356. ib_dealloc_pd(srp_dev->pd);
  2357. free_dev:
  2358. kfree(srp_dev);
  2359. free_attr:
  2360. kfree(dev_attr);
  2361. }
  2362. static void srp_remove_one(struct ib_device *device)
  2363. {
  2364. struct srp_device *srp_dev;
  2365. struct srp_host *host, *tmp_host;
  2366. struct srp_target_port *target;
  2367. srp_dev = ib_get_client_data(device, &srp_client);
  2368. if (!srp_dev)
  2369. return;
  2370. list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
  2371. device_unregister(&host->dev);
  2372. /*
  2373. * Wait for the sysfs entry to go away, so that no new
  2374. * target ports can be created.
  2375. */
  2376. wait_for_completion(&host->released);
  2377. /*
  2378. * Remove all target ports.
  2379. */
  2380. spin_lock(&host->target_lock);
  2381. list_for_each_entry(target, &host->target_list, list)
  2382. srp_queue_remove_work(target);
  2383. spin_unlock(&host->target_lock);
  2384. /*
  2385. * Wait for target port removal tasks.
  2386. */
  2387. flush_workqueue(system_long_wq);
  2388. kfree(host);
  2389. }
  2390. if (srp_dev->fmr_pool)
  2391. ib_destroy_fmr_pool(srp_dev->fmr_pool);
  2392. ib_dereg_mr(srp_dev->mr);
  2393. ib_dealloc_pd(srp_dev->pd);
  2394. kfree(srp_dev);
  2395. }
  2396. static struct srp_function_template ib_srp_transport_functions = {
  2397. .has_rport_state = true,
  2398. .reset_timer_if_blocked = true,
  2399. .reconnect_delay = &srp_reconnect_delay,
  2400. .fast_io_fail_tmo = &srp_fast_io_fail_tmo,
  2401. .dev_loss_tmo = &srp_dev_loss_tmo,
  2402. .reconnect = srp_rport_reconnect,
  2403. .rport_delete = srp_rport_delete,
  2404. .terminate_rport_io = srp_terminate_io,
  2405. };
  2406. static int __init srp_init_module(void)
  2407. {
  2408. int ret;
  2409. BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *));
  2410. if (srp_sg_tablesize) {
  2411. pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n");
  2412. if (!cmd_sg_entries)
  2413. cmd_sg_entries = srp_sg_tablesize;
  2414. }
  2415. if (!cmd_sg_entries)
  2416. cmd_sg_entries = SRP_DEF_SG_TABLESIZE;
  2417. if (cmd_sg_entries > 255) {
  2418. pr_warn("Clamping cmd_sg_entries to 255\n");
  2419. cmd_sg_entries = 255;
  2420. }
  2421. if (!indirect_sg_entries)
  2422. indirect_sg_entries = cmd_sg_entries;
  2423. else if (indirect_sg_entries < cmd_sg_entries) {
  2424. pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n",
  2425. cmd_sg_entries);
  2426. indirect_sg_entries = cmd_sg_entries;
  2427. }
  2428. ib_srp_transport_template =
  2429. srp_attach_transport(&ib_srp_transport_functions);
  2430. if (!ib_srp_transport_template)
  2431. return -ENOMEM;
  2432. ret = class_register(&srp_class);
  2433. if (ret) {
  2434. pr_err("couldn't register class infiniband_srp\n");
  2435. srp_release_transport(ib_srp_transport_template);
  2436. return ret;
  2437. }
  2438. ib_sa_register_client(&srp_sa_client);
  2439. ret = ib_register_client(&srp_client);
  2440. if (ret) {
  2441. pr_err("couldn't register IB client\n");
  2442. srp_release_transport(ib_srp_transport_template);
  2443. ib_sa_unregister_client(&srp_sa_client);
  2444. class_unregister(&srp_class);
  2445. return ret;
  2446. }
  2447. return 0;
  2448. }
  2449. static void __exit srp_cleanup_module(void)
  2450. {
  2451. ib_unregister_client(&srp_client);
  2452. ib_sa_unregister_client(&srp_sa_client);
  2453. class_unregister(&srp_class);
  2454. srp_release_transport(ib_srp_transport_template);
  2455. }
  2456. module_init(srp_init_module);
  2457. module_exit(srp_cleanup_module);