ib_srp.c 79 KB

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