ib_srp.c 74 KB

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