tcp.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * NVMe over Fabrics TCP host.
  4. * Copyright (c) 2018 Lightbits Labs. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/module.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/err.h>
  11. #include <linux/key.h>
  12. #include <linux/nvme-tcp.h>
  13. #include <linux/nvme-keyring.h>
  14. #include <net/sock.h>
  15. #include <net/tcp.h>
  16. #include <net/tls.h>
  17. #include <net/tls_prot.h>
  18. #include <net/handshake.h>
  19. #include <linux/blk-mq.h>
  20. #include <crypto/hash.h>
  21. #include <net/busy_poll.h>
  22. #include <trace/events/sock.h>
  23. #include "nvme.h"
  24. #include "fabrics.h"
  25. struct nvme_tcp_queue;
  26. /* Define the socket priority to use for connections were it is desirable
  27. * that the NIC consider performing optimized packet processing or filtering.
  28. * A non-zero value being sufficient to indicate general consideration of any
  29. * possible optimization. Making it a module param allows for alternative
  30. * values that may be unique for some NIC implementations.
  31. */
  32. static int so_priority;
  33. module_param(so_priority, int, 0644);
  34. MODULE_PARM_DESC(so_priority, "nvme tcp socket optimize priority");
  35. /*
  36. * Use the unbound workqueue for nvme_tcp_wq, then we can set the cpu affinity
  37. * from sysfs.
  38. */
  39. static bool wq_unbound;
  40. module_param(wq_unbound, bool, 0644);
  41. MODULE_PARM_DESC(wq_unbound, "Use unbound workqueue for nvme-tcp IO context (default false)");
  42. /*
  43. * TLS handshake timeout
  44. */
  45. static int tls_handshake_timeout = 10;
  46. #ifdef CONFIG_NVME_TCP_TLS
  47. module_param(tls_handshake_timeout, int, 0644);
  48. MODULE_PARM_DESC(tls_handshake_timeout,
  49. "nvme TLS handshake timeout in seconds (default 10)");
  50. #endif
  51. static atomic_t nvme_tcp_cpu_queues[NR_CPUS];
  52. #ifdef CONFIG_DEBUG_LOCK_ALLOC
  53. /* lockdep can detect a circular dependency of the form
  54. * sk_lock -> mmap_lock (page fault) -> fs locks -> sk_lock
  55. * because dependencies are tracked for both nvme-tcp and user contexts. Using
  56. * a separate class prevents lockdep from conflating nvme-tcp socket use with
  57. * user-space socket API use.
  58. */
  59. static struct lock_class_key nvme_tcp_sk_key[2];
  60. static struct lock_class_key nvme_tcp_slock_key[2];
  61. static void nvme_tcp_reclassify_socket(struct socket *sock)
  62. {
  63. struct sock *sk = sock->sk;
  64. if (WARN_ON_ONCE(!sock_allow_reclassification(sk)))
  65. return;
  66. switch (sk->sk_family) {
  67. case AF_INET:
  68. sock_lock_init_class_and_name(sk, "slock-AF_INET-NVME",
  69. &nvme_tcp_slock_key[0],
  70. "sk_lock-AF_INET-NVME",
  71. &nvme_tcp_sk_key[0]);
  72. break;
  73. case AF_INET6:
  74. sock_lock_init_class_and_name(sk, "slock-AF_INET6-NVME",
  75. &nvme_tcp_slock_key[1],
  76. "sk_lock-AF_INET6-NVME",
  77. &nvme_tcp_sk_key[1]);
  78. break;
  79. default:
  80. WARN_ON_ONCE(1);
  81. }
  82. }
  83. #else
  84. static void nvme_tcp_reclassify_socket(struct socket *sock) { }
  85. #endif
  86. enum nvme_tcp_send_state {
  87. NVME_TCP_SEND_CMD_PDU = 0,
  88. NVME_TCP_SEND_H2C_PDU,
  89. NVME_TCP_SEND_DATA,
  90. NVME_TCP_SEND_DDGST,
  91. };
  92. struct nvme_tcp_request {
  93. struct nvme_request req;
  94. void *pdu;
  95. struct nvme_tcp_queue *queue;
  96. u32 data_len;
  97. u32 pdu_len;
  98. u32 pdu_sent;
  99. u32 h2cdata_left;
  100. u32 h2cdata_offset;
  101. u16 ttag;
  102. __le16 status;
  103. struct list_head entry;
  104. struct llist_node lentry;
  105. __le32 ddgst;
  106. struct bio *curr_bio;
  107. struct iov_iter iter;
  108. /* send state */
  109. size_t offset;
  110. size_t data_sent;
  111. enum nvme_tcp_send_state state;
  112. };
  113. enum nvme_tcp_queue_flags {
  114. NVME_TCP_Q_ALLOCATED = 0,
  115. NVME_TCP_Q_LIVE = 1,
  116. NVME_TCP_Q_POLLING = 2,
  117. NVME_TCP_Q_IO_CPU_SET = 3,
  118. };
  119. enum nvme_tcp_recv_state {
  120. NVME_TCP_RECV_PDU = 0,
  121. NVME_TCP_RECV_DATA,
  122. NVME_TCP_RECV_DDGST,
  123. };
  124. struct nvme_tcp_ctrl;
  125. struct nvme_tcp_queue {
  126. struct socket *sock;
  127. struct work_struct io_work;
  128. int io_cpu;
  129. struct mutex queue_lock;
  130. struct mutex send_mutex;
  131. struct llist_head req_list;
  132. struct list_head send_list;
  133. /* recv state */
  134. void *pdu;
  135. int pdu_remaining;
  136. int pdu_offset;
  137. size_t data_remaining;
  138. size_t ddgst_remaining;
  139. unsigned int nr_cqe;
  140. /* send state */
  141. struct nvme_tcp_request *request;
  142. u32 maxh2cdata;
  143. size_t cmnd_capsule_len;
  144. struct nvme_tcp_ctrl *ctrl;
  145. unsigned long flags;
  146. bool rd_enabled;
  147. bool hdr_digest;
  148. bool data_digest;
  149. bool tls_enabled;
  150. struct ahash_request *rcv_hash;
  151. struct ahash_request *snd_hash;
  152. __le32 exp_ddgst;
  153. __le32 recv_ddgst;
  154. struct completion tls_complete;
  155. int tls_err;
  156. struct page_frag_cache pf_cache;
  157. void (*state_change)(struct sock *);
  158. void (*data_ready)(struct sock *);
  159. void (*write_space)(struct sock *);
  160. };
  161. struct nvme_tcp_ctrl {
  162. /* read only in the hot path */
  163. struct nvme_tcp_queue *queues;
  164. struct blk_mq_tag_set tag_set;
  165. /* other member variables */
  166. struct list_head list;
  167. struct blk_mq_tag_set admin_tag_set;
  168. struct sockaddr_storage addr;
  169. struct sockaddr_storage src_addr;
  170. struct nvme_ctrl ctrl;
  171. struct work_struct err_work;
  172. struct delayed_work connect_work;
  173. struct nvme_tcp_request async_req;
  174. u32 io_queues[HCTX_MAX_TYPES];
  175. };
  176. static LIST_HEAD(nvme_tcp_ctrl_list);
  177. static DEFINE_MUTEX(nvme_tcp_ctrl_mutex);
  178. static struct workqueue_struct *nvme_tcp_wq;
  179. static const struct blk_mq_ops nvme_tcp_mq_ops;
  180. static const struct blk_mq_ops nvme_tcp_admin_mq_ops;
  181. static int nvme_tcp_try_send(struct nvme_tcp_queue *queue);
  182. static inline struct nvme_tcp_ctrl *to_tcp_ctrl(struct nvme_ctrl *ctrl)
  183. {
  184. return container_of(ctrl, struct nvme_tcp_ctrl, ctrl);
  185. }
  186. static inline int nvme_tcp_queue_id(struct nvme_tcp_queue *queue)
  187. {
  188. return queue - queue->ctrl->queues;
  189. }
  190. static inline bool nvme_tcp_recv_pdu_supported(enum nvme_tcp_pdu_type type)
  191. {
  192. switch (type) {
  193. case nvme_tcp_c2h_term:
  194. case nvme_tcp_c2h_data:
  195. case nvme_tcp_r2t:
  196. case nvme_tcp_rsp:
  197. return true;
  198. default:
  199. return false;
  200. }
  201. }
  202. /*
  203. * Check if the queue is TLS encrypted
  204. */
  205. static inline bool nvme_tcp_queue_tls(struct nvme_tcp_queue *queue)
  206. {
  207. if (!IS_ENABLED(CONFIG_NVME_TCP_TLS))
  208. return 0;
  209. return queue->tls_enabled;
  210. }
  211. /*
  212. * Check if TLS is configured for the controller.
  213. */
  214. static inline bool nvme_tcp_tls_configured(struct nvme_ctrl *ctrl)
  215. {
  216. if (!IS_ENABLED(CONFIG_NVME_TCP_TLS))
  217. return 0;
  218. return ctrl->opts->tls;
  219. }
  220. static inline struct blk_mq_tags *nvme_tcp_tagset(struct nvme_tcp_queue *queue)
  221. {
  222. u32 queue_idx = nvme_tcp_queue_id(queue);
  223. if (queue_idx == 0)
  224. return queue->ctrl->admin_tag_set.tags[queue_idx];
  225. return queue->ctrl->tag_set.tags[queue_idx - 1];
  226. }
  227. static inline u8 nvme_tcp_hdgst_len(struct nvme_tcp_queue *queue)
  228. {
  229. return queue->hdr_digest ? NVME_TCP_DIGEST_LENGTH : 0;
  230. }
  231. static inline u8 nvme_tcp_ddgst_len(struct nvme_tcp_queue *queue)
  232. {
  233. return queue->data_digest ? NVME_TCP_DIGEST_LENGTH : 0;
  234. }
  235. static inline void *nvme_tcp_req_cmd_pdu(struct nvme_tcp_request *req)
  236. {
  237. return req->pdu;
  238. }
  239. static inline void *nvme_tcp_req_data_pdu(struct nvme_tcp_request *req)
  240. {
  241. /* use the pdu space in the back for the data pdu */
  242. return req->pdu + sizeof(struct nvme_tcp_cmd_pdu) -
  243. sizeof(struct nvme_tcp_data_pdu);
  244. }
  245. static inline size_t nvme_tcp_inline_data_size(struct nvme_tcp_request *req)
  246. {
  247. if (nvme_is_fabrics(req->req.cmd))
  248. return NVME_TCP_ADMIN_CCSZ;
  249. return req->queue->cmnd_capsule_len - sizeof(struct nvme_command);
  250. }
  251. static inline bool nvme_tcp_async_req(struct nvme_tcp_request *req)
  252. {
  253. return req == &req->queue->ctrl->async_req;
  254. }
  255. static inline bool nvme_tcp_has_inline_data(struct nvme_tcp_request *req)
  256. {
  257. struct request *rq;
  258. if (unlikely(nvme_tcp_async_req(req)))
  259. return false; /* async events don't have a request */
  260. rq = blk_mq_rq_from_pdu(req);
  261. return rq_data_dir(rq) == WRITE && req->data_len &&
  262. req->data_len <= nvme_tcp_inline_data_size(req);
  263. }
  264. static inline struct page *nvme_tcp_req_cur_page(struct nvme_tcp_request *req)
  265. {
  266. return req->iter.bvec->bv_page;
  267. }
  268. static inline size_t nvme_tcp_req_cur_offset(struct nvme_tcp_request *req)
  269. {
  270. return req->iter.bvec->bv_offset + req->iter.iov_offset;
  271. }
  272. static inline size_t nvme_tcp_req_cur_length(struct nvme_tcp_request *req)
  273. {
  274. return min_t(size_t, iov_iter_single_seg_count(&req->iter),
  275. req->pdu_len - req->pdu_sent);
  276. }
  277. static inline size_t nvme_tcp_pdu_data_left(struct nvme_tcp_request *req)
  278. {
  279. return rq_data_dir(blk_mq_rq_from_pdu(req)) == WRITE ?
  280. req->pdu_len - req->pdu_sent : 0;
  281. }
  282. static inline size_t nvme_tcp_pdu_last_send(struct nvme_tcp_request *req,
  283. int len)
  284. {
  285. return nvme_tcp_pdu_data_left(req) <= len;
  286. }
  287. static void nvme_tcp_init_iter(struct nvme_tcp_request *req,
  288. unsigned int dir)
  289. {
  290. struct request *rq = blk_mq_rq_from_pdu(req);
  291. struct bio_vec *vec;
  292. unsigned int size;
  293. int nr_bvec;
  294. size_t offset;
  295. if (rq->rq_flags & RQF_SPECIAL_PAYLOAD) {
  296. vec = &rq->special_vec;
  297. nr_bvec = 1;
  298. size = blk_rq_payload_bytes(rq);
  299. offset = 0;
  300. } else {
  301. struct bio *bio = req->curr_bio;
  302. struct bvec_iter bi;
  303. struct bio_vec bv;
  304. vec = __bvec_iter_bvec(bio->bi_io_vec, bio->bi_iter);
  305. nr_bvec = 0;
  306. bio_for_each_bvec(bv, bio, bi) {
  307. nr_bvec++;
  308. }
  309. size = bio->bi_iter.bi_size;
  310. offset = bio->bi_iter.bi_bvec_done;
  311. }
  312. iov_iter_bvec(&req->iter, dir, vec, nr_bvec, size);
  313. req->iter.iov_offset = offset;
  314. }
  315. static inline void nvme_tcp_advance_req(struct nvme_tcp_request *req,
  316. int len)
  317. {
  318. req->data_sent += len;
  319. req->pdu_sent += len;
  320. iov_iter_advance(&req->iter, len);
  321. if (!iov_iter_count(&req->iter) &&
  322. req->data_sent < req->data_len) {
  323. req->curr_bio = req->curr_bio->bi_next;
  324. nvme_tcp_init_iter(req, ITER_SOURCE);
  325. }
  326. }
  327. static inline void nvme_tcp_send_all(struct nvme_tcp_queue *queue)
  328. {
  329. int ret;
  330. /* drain the send queue as much as we can... */
  331. do {
  332. ret = nvme_tcp_try_send(queue);
  333. } while (ret > 0);
  334. }
  335. static inline bool nvme_tcp_queue_has_pending(struct nvme_tcp_queue *queue)
  336. {
  337. return !list_empty(&queue->send_list) ||
  338. !llist_empty(&queue->req_list);
  339. }
  340. static inline bool nvme_tcp_queue_more(struct nvme_tcp_queue *queue)
  341. {
  342. return !nvme_tcp_queue_tls(queue) &&
  343. nvme_tcp_queue_has_pending(queue);
  344. }
  345. static inline void nvme_tcp_queue_request(struct nvme_tcp_request *req,
  346. bool sync, bool last)
  347. {
  348. struct nvme_tcp_queue *queue = req->queue;
  349. bool empty;
  350. empty = llist_add(&req->lentry, &queue->req_list) &&
  351. list_empty(&queue->send_list) && !queue->request;
  352. /*
  353. * if we're the first on the send_list and we can try to send
  354. * directly, otherwise queue io_work. Also, only do that if we
  355. * are on the same cpu, so we don't introduce contention.
  356. */
  357. if (queue->io_cpu == raw_smp_processor_id() &&
  358. sync && empty && mutex_trylock(&queue->send_mutex)) {
  359. nvme_tcp_send_all(queue);
  360. mutex_unlock(&queue->send_mutex);
  361. }
  362. if (last && nvme_tcp_queue_has_pending(queue))
  363. queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
  364. }
  365. static void nvme_tcp_process_req_list(struct nvme_tcp_queue *queue)
  366. {
  367. struct nvme_tcp_request *req;
  368. struct llist_node *node;
  369. for (node = llist_del_all(&queue->req_list); node; node = node->next) {
  370. req = llist_entry(node, struct nvme_tcp_request, lentry);
  371. list_add(&req->entry, &queue->send_list);
  372. }
  373. }
  374. static inline struct nvme_tcp_request *
  375. nvme_tcp_fetch_request(struct nvme_tcp_queue *queue)
  376. {
  377. struct nvme_tcp_request *req;
  378. req = list_first_entry_or_null(&queue->send_list,
  379. struct nvme_tcp_request, entry);
  380. if (!req) {
  381. nvme_tcp_process_req_list(queue);
  382. req = list_first_entry_or_null(&queue->send_list,
  383. struct nvme_tcp_request, entry);
  384. if (unlikely(!req))
  385. return NULL;
  386. }
  387. list_del_init(&req->entry);
  388. init_llist_node(&req->lentry);
  389. return req;
  390. }
  391. static inline void nvme_tcp_ddgst_final(struct ahash_request *hash,
  392. __le32 *dgst)
  393. {
  394. ahash_request_set_crypt(hash, NULL, (u8 *)dgst, 0);
  395. crypto_ahash_final(hash);
  396. }
  397. static inline void nvme_tcp_ddgst_update(struct ahash_request *hash,
  398. struct page *page, off_t off, size_t len)
  399. {
  400. struct scatterlist sg;
  401. sg_init_table(&sg, 1);
  402. sg_set_page(&sg, page, len, off);
  403. ahash_request_set_crypt(hash, &sg, NULL, len);
  404. crypto_ahash_update(hash);
  405. }
  406. static inline void nvme_tcp_hdgst(struct ahash_request *hash,
  407. void *pdu, size_t len)
  408. {
  409. struct scatterlist sg;
  410. sg_init_one(&sg, pdu, len);
  411. ahash_request_set_crypt(hash, &sg, pdu + len, len);
  412. crypto_ahash_digest(hash);
  413. }
  414. static int nvme_tcp_verify_hdgst(struct nvme_tcp_queue *queue,
  415. void *pdu, size_t pdu_len)
  416. {
  417. struct nvme_tcp_hdr *hdr = pdu;
  418. __le32 recv_digest;
  419. __le32 exp_digest;
  420. if (unlikely(!(hdr->flags & NVME_TCP_F_HDGST))) {
  421. dev_err(queue->ctrl->ctrl.device,
  422. "queue %d: header digest flag is cleared\n",
  423. nvme_tcp_queue_id(queue));
  424. return -EPROTO;
  425. }
  426. recv_digest = *(__le32 *)(pdu + hdr->hlen);
  427. nvme_tcp_hdgst(queue->rcv_hash, pdu, pdu_len);
  428. exp_digest = *(__le32 *)(pdu + hdr->hlen);
  429. if (recv_digest != exp_digest) {
  430. dev_err(queue->ctrl->ctrl.device,
  431. "header digest error: recv %#x expected %#x\n",
  432. le32_to_cpu(recv_digest), le32_to_cpu(exp_digest));
  433. return -EIO;
  434. }
  435. return 0;
  436. }
  437. static int nvme_tcp_check_ddgst(struct nvme_tcp_queue *queue, void *pdu)
  438. {
  439. struct nvme_tcp_hdr *hdr = pdu;
  440. u8 digest_len = nvme_tcp_hdgst_len(queue);
  441. u32 len;
  442. len = le32_to_cpu(hdr->plen) - hdr->hlen -
  443. ((hdr->flags & NVME_TCP_F_HDGST) ? digest_len : 0);
  444. if (unlikely(len && !(hdr->flags & NVME_TCP_F_DDGST))) {
  445. dev_err(queue->ctrl->ctrl.device,
  446. "queue %d: data digest flag is cleared\n",
  447. nvme_tcp_queue_id(queue));
  448. return -EPROTO;
  449. }
  450. crypto_ahash_init(queue->rcv_hash);
  451. return 0;
  452. }
  453. static void nvme_tcp_exit_request(struct blk_mq_tag_set *set,
  454. struct request *rq, unsigned int hctx_idx)
  455. {
  456. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  457. page_frag_free(req->pdu);
  458. }
  459. static int nvme_tcp_init_request(struct blk_mq_tag_set *set,
  460. struct request *rq, unsigned int hctx_idx,
  461. unsigned int numa_node)
  462. {
  463. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data);
  464. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  465. struct nvme_tcp_cmd_pdu *pdu;
  466. int queue_idx = (set == &ctrl->tag_set) ? hctx_idx + 1 : 0;
  467. struct nvme_tcp_queue *queue = &ctrl->queues[queue_idx];
  468. u8 hdgst = nvme_tcp_hdgst_len(queue);
  469. req->pdu = page_frag_alloc(&queue->pf_cache,
  470. sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
  471. GFP_KERNEL | __GFP_ZERO);
  472. if (!req->pdu)
  473. return -ENOMEM;
  474. pdu = req->pdu;
  475. req->queue = queue;
  476. nvme_req(rq)->ctrl = &ctrl->ctrl;
  477. nvme_req(rq)->cmd = &pdu->cmd;
  478. init_llist_node(&req->lentry);
  479. INIT_LIST_HEAD(&req->entry);
  480. return 0;
  481. }
  482. static int nvme_tcp_init_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  483. unsigned int hctx_idx)
  484. {
  485. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data);
  486. struct nvme_tcp_queue *queue = &ctrl->queues[hctx_idx + 1];
  487. hctx->driver_data = queue;
  488. return 0;
  489. }
  490. static int nvme_tcp_init_admin_hctx(struct blk_mq_hw_ctx *hctx, void *data,
  491. unsigned int hctx_idx)
  492. {
  493. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(data);
  494. struct nvme_tcp_queue *queue = &ctrl->queues[0];
  495. hctx->driver_data = queue;
  496. return 0;
  497. }
  498. static enum nvme_tcp_recv_state
  499. nvme_tcp_recv_state(struct nvme_tcp_queue *queue)
  500. {
  501. return (queue->pdu_remaining) ? NVME_TCP_RECV_PDU :
  502. (queue->ddgst_remaining) ? NVME_TCP_RECV_DDGST :
  503. NVME_TCP_RECV_DATA;
  504. }
  505. static void nvme_tcp_init_recv_ctx(struct nvme_tcp_queue *queue)
  506. {
  507. queue->pdu_remaining = sizeof(struct nvme_tcp_rsp_pdu) +
  508. nvme_tcp_hdgst_len(queue);
  509. queue->pdu_offset = 0;
  510. queue->data_remaining = -1;
  511. queue->ddgst_remaining = 0;
  512. }
  513. static void nvme_tcp_error_recovery(struct nvme_ctrl *ctrl)
  514. {
  515. if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_RESETTING))
  516. return;
  517. dev_warn(ctrl->device, "starting error recovery\n");
  518. queue_work(nvme_reset_wq, &to_tcp_ctrl(ctrl)->err_work);
  519. }
  520. static int nvme_tcp_process_nvme_cqe(struct nvme_tcp_queue *queue,
  521. struct nvme_completion *cqe)
  522. {
  523. struct nvme_tcp_request *req;
  524. struct request *rq;
  525. rq = nvme_find_rq(nvme_tcp_tagset(queue), cqe->command_id);
  526. if (!rq) {
  527. dev_err(queue->ctrl->ctrl.device,
  528. "got bad cqe.command_id %#x on queue %d\n",
  529. cqe->command_id, nvme_tcp_queue_id(queue));
  530. nvme_tcp_error_recovery(&queue->ctrl->ctrl);
  531. return -EINVAL;
  532. }
  533. req = blk_mq_rq_to_pdu(rq);
  534. if (req->status == cpu_to_le16(NVME_SC_SUCCESS))
  535. req->status = cqe->status;
  536. if (!nvme_try_complete_req(rq, req->status, cqe->result))
  537. nvme_complete_rq(rq);
  538. queue->nr_cqe++;
  539. return 0;
  540. }
  541. static int nvme_tcp_handle_c2h_data(struct nvme_tcp_queue *queue,
  542. struct nvme_tcp_data_pdu *pdu)
  543. {
  544. struct request *rq;
  545. rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
  546. if (!rq) {
  547. dev_err(queue->ctrl->ctrl.device,
  548. "got bad c2hdata.command_id %#x on queue %d\n",
  549. pdu->command_id, nvme_tcp_queue_id(queue));
  550. return -ENOENT;
  551. }
  552. if (!blk_rq_payload_bytes(rq)) {
  553. dev_err(queue->ctrl->ctrl.device,
  554. "queue %d tag %#x unexpected data\n",
  555. nvme_tcp_queue_id(queue), rq->tag);
  556. return -EIO;
  557. }
  558. queue->data_remaining = le32_to_cpu(pdu->data_length);
  559. if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS &&
  560. unlikely(!(pdu->hdr.flags & NVME_TCP_F_DATA_LAST))) {
  561. dev_err(queue->ctrl->ctrl.device,
  562. "queue %d tag %#x SUCCESS set but not last PDU\n",
  563. nvme_tcp_queue_id(queue), rq->tag);
  564. nvme_tcp_error_recovery(&queue->ctrl->ctrl);
  565. return -EPROTO;
  566. }
  567. return 0;
  568. }
  569. static int nvme_tcp_handle_comp(struct nvme_tcp_queue *queue,
  570. struct nvme_tcp_rsp_pdu *pdu)
  571. {
  572. struct nvme_completion *cqe = &pdu->cqe;
  573. int ret = 0;
  574. /*
  575. * AEN requests are special as they don't time out and can
  576. * survive any kind of queue freeze and often don't respond to
  577. * aborts. We don't even bother to allocate a struct request
  578. * for them but rather special case them here.
  579. */
  580. if (unlikely(nvme_is_aen_req(nvme_tcp_queue_id(queue),
  581. cqe->command_id)))
  582. nvme_complete_async_event(&queue->ctrl->ctrl, cqe->status,
  583. &cqe->result);
  584. else
  585. ret = nvme_tcp_process_nvme_cqe(queue, cqe);
  586. return ret;
  587. }
  588. static void nvme_tcp_setup_h2c_data_pdu(struct nvme_tcp_request *req)
  589. {
  590. struct nvme_tcp_data_pdu *data = nvme_tcp_req_data_pdu(req);
  591. struct nvme_tcp_queue *queue = req->queue;
  592. struct request *rq = blk_mq_rq_from_pdu(req);
  593. u32 h2cdata_sent = req->pdu_len;
  594. u8 hdgst = nvme_tcp_hdgst_len(queue);
  595. u8 ddgst = nvme_tcp_ddgst_len(queue);
  596. req->state = NVME_TCP_SEND_H2C_PDU;
  597. req->offset = 0;
  598. req->pdu_len = min(req->h2cdata_left, queue->maxh2cdata);
  599. req->pdu_sent = 0;
  600. req->h2cdata_left -= req->pdu_len;
  601. req->h2cdata_offset += h2cdata_sent;
  602. memset(data, 0, sizeof(*data));
  603. data->hdr.type = nvme_tcp_h2c_data;
  604. if (!req->h2cdata_left)
  605. data->hdr.flags = NVME_TCP_F_DATA_LAST;
  606. if (queue->hdr_digest)
  607. data->hdr.flags |= NVME_TCP_F_HDGST;
  608. if (queue->data_digest)
  609. data->hdr.flags |= NVME_TCP_F_DDGST;
  610. data->hdr.hlen = sizeof(*data);
  611. data->hdr.pdo = data->hdr.hlen + hdgst;
  612. data->hdr.plen =
  613. cpu_to_le32(data->hdr.hlen + hdgst + req->pdu_len + ddgst);
  614. data->ttag = req->ttag;
  615. data->command_id = nvme_cid(rq);
  616. data->data_offset = cpu_to_le32(req->h2cdata_offset);
  617. data->data_length = cpu_to_le32(req->pdu_len);
  618. }
  619. static int nvme_tcp_handle_r2t(struct nvme_tcp_queue *queue,
  620. struct nvme_tcp_r2t_pdu *pdu)
  621. {
  622. struct nvme_tcp_request *req;
  623. struct request *rq;
  624. u32 r2t_length = le32_to_cpu(pdu->r2t_length);
  625. u32 r2t_offset = le32_to_cpu(pdu->r2t_offset);
  626. rq = nvme_find_rq(nvme_tcp_tagset(queue), pdu->command_id);
  627. if (!rq) {
  628. dev_err(queue->ctrl->ctrl.device,
  629. "got bad r2t.command_id %#x on queue %d\n",
  630. pdu->command_id, nvme_tcp_queue_id(queue));
  631. return -ENOENT;
  632. }
  633. req = blk_mq_rq_to_pdu(rq);
  634. if (unlikely(!r2t_length)) {
  635. dev_err(queue->ctrl->ctrl.device,
  636. "req %d r2t len is %u, probably a bug...\n",
  637. rq->tag, r2t_length);
  638. return -EPROTO;
  639. }
  640. if (unlikely(req->data_sent + r2t_length > req->data_len)) {
  641. dev_err(queue->ctrl->ctrl.device,
  642. "req %d r2t len %u exceeded data len %u (%zu sent)\n",
  643. rq->tag, r2t_length, req->data_len, req->data_sent);
  644. return -EPROTO;
  645. }
  646. if (unlikely(r2t_offset < req->data_sent)) {
  647. dev_err(queue->ctrl->ctrl.device,
  648. "req %d unexpected r2t offset %u (expected %zu)\n",
  649. rq->tag, r2t_offset, req->data_sent);
  650. return -EPROTO;
  651. }
  652. if (llist_on_list(&req->lentry) ||
  653. !list_empty(&req->entry)) {
  654. dev_err(queue->ctrl->ctrl.device,
  655. "req %d unexpected r2t while processing request\n",
  656. rq->tag);
  657. return -EPROTO;
  658. }
  659. req->pdu_len = 0;
  660. req->h2cdata_left = r2t_length;
  661. req->h2cdata_offset = r2t_offset;
  662. req->ttag = pdu->ttag;
  663. nvme_tcp_setup_h2c_data_pdu(req);
  664. nvme_tcp_queue_request(req, false, true);
  665. return 0;
  666. }
  667. static void nvme_tcp_handle_c2h_term(struct nvme_tcp_queue *queue,
  668. struct nvme_tcp_term_pdu *pdu)
  669. {
  670. u16 fes;
  671. const char *msg;
  672. u32 plen = le32_to_cpu(pdu->hdr.plen);
  673. static const char * const msg_table[] = {
  674. [NVME_TCP_FES_INVALID_PDU_HDR] = "Invalid PDU Header Field",
  675. [NVME_TCP_FES_PDU_SEQ_ERR] = "PDU Sequence Error",
  676. [NVME_TCP_FES_HDR_DIGEST_ERR] = "Header Digest Error",
  677. [NVME_TCP_FES_DATA_OUT_OF_RANGE] = "Data Transfer Out Of Range",
  678. [NVME_TCP_FES_DATA_LIMIT_EXCEEDED] = "Data Transfer Limit Exceeded",
  679. [NVME_TCP_FES_UNSUPPORTED_PARAM] = "Unsupported Parameter",
  680. };
  681. if (plen < NVME_TCP_MIN_C2HTERM_PLEN ||
  682. plen > NVME_TCP_MAX_C2HTERM_PLEN) {
  683. dev_err(queue->ctrl->ctrl.device,
  684. "Received a malformed C2HTermReq PDU (plen = %u)\n",
  685. plen);
  686. return;
  687. }
  688. fes = le16_to_cpu(pdu->fes);
  689. if (fes && fes < ARRAY_SIZE(msg_table))
  690. msg = msg_table[fes];
  691. else
  692. msg = "Unknown";
  693. dev_err(queue->ctrl->ctrl.device,
  694. "Received C2HTermReq (FES = %s)\n", msg);
  695. }
  696. static int nvme_tcp_recv_pdu(struct nvme_tcp_queue *queue, struct sk_buff *skb,
  697. unsigned int *offset, size_t *len)
  698. {
  699. struct nvme_tcp_hdr *hdr;
  700. char *pdu = queue->pdu;
  701. size_t rcv_len = min_t(size_t, *len, queue->pdu_remaining);
  702. int ret;
  703. ret = skb_copy_bits(skb, *offset,
  704. &pdu[queue->pdu_offset], rcv_len);
  705. if (unlikely(ret))
  706. return ret;
  707. queue->pdu_remaining -= rcv_len;
  708. queue->pdu_offset += rcv_len;
  709. *offset += rcv_len;
  710. *len -= rcv_len;
  711. if (queue->pdu_remaining)
  712. return 0;
  713. hdr = queue->pdu;
  714. if (unlikely(hdr->hlen != sizeof(struct nvme_tcp_rsp_pdu))) {
  715. if (!nvme_tcp_recv_pdu_supported(hdr->type))
  716. goto unsupported_pdu;
  717. dev_err(queue->ctrl->ctrl.device,
  718. "pdu type %d has unexpected header length (%d)\n",
  719. hdr->type, hdr->hlen);
  720. return -EPROTO;
  721. }
  722. if (unlikely(hdr->type == nvme_tcp_c2h_term)) {
  723. /*
  724. * C2HTermReq never includes Header or Data digests.
  725. * Skip the checks.
  726. */
  727. nvme_tcp_handle_c2h_term(queue, (void *)queue->pdu);
  728. return -EINVAL;
  729. }
  730. if (queue->hdr_digest) {
  731. ret = nvme_tcp_verify_hdgst(queue, queue->pdu, hdr->hlen);
  732. if (unlikely(ret))
  733. return ret;
  734. }
  735. if (queue->data_digest) {
  736. ret = nvme_tcp_check_ddgst(queue, queue->pdu);
  737. if (unlikely(ret))
  738. return ret;
  739. }
  740. switch (hdr->type) {
  741. case nvme_tcp_c2h_data:
  742. return nvme_tcp_handle_c2h_data(queue, (void *)queue->pdu);
  743. case nvme_tcp_rsp:
  744. nvme_tcp_init_recv_ctx(queue);
  745. return nvme_tcp_handle_comp(queue, (void *)queue->pdu);
  746. case nvme_tcp_r2t:
  747. nvme_tcp_init_recv_ctx(queue);
  748. return nvme_tcp_handle_r2t(queue, (void *)queue->pdu);
  749. default:
  750. goto unsupported_pdu;
  751. }
  752. unsupported_pdu:
  753. dev_err(queue->ctrl->ctrl.device,
  754. "unsupported pdu type (%d)\n", hdr->type);
  755. return -EINVAL;
  756. }
  757. static inline void nvme_tcp_end_request(struct request *rq, u16 status)
  758. {
  759. union nvme_result res = {};
  760. if (!nvme_try_complete_req(rq, cpu_to_le16(status << 1), res))
  761. nvme_complete_rq(rq);
  762. }
  763. static int nvme_tcp_recv_data(struct nvme_tcp_queue *queue, struct sk_buff *skb,
  764. unsigned int *offset, size_t *len)
  765. {
  766. struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
  767. struct request *rq =
  768. nvme_cid_to_rq(nvme_tcp_tagset(queue), pdu->command_id);
  769. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  770. while (true) {
  771. int recv_len, ret;
  772. recv_len = min_t(size_t, *len, queue->data_remaining);
  773. if (!recv_len)
  774. break;
  775. if (!iov_iter_count(&req->iter)) {
  776. req->curr_bio = req->curr_bio->bi_next;
  777. /*
  778. * If we don`t have any bios it means that controller
  779. * sent more data than we requested, hence error
  780. */
  781. if (!req->curr_bio) {
  782. dev_err(queue->ctrl->ctrl.device,
  783. "queue %d no space in request %#x",
  784. nvme_tcp_queue_id(queue), rq->tag);
  785. nvme_tcp_init_recv_ctx(queue);
  786. return -EIO;
  787. }
  788. nvme_tcp_init_iter(req, ITER_DEST);
  789. }
  790. /* we can read only from what is left in this bio */
  791. recv_len = min_t(size_t, recv_len,
  792. iov_iter_count(&req->iter));
  793. if (queue->data_digest)
  794. ret = skb_copy_and_hash_datagram_iter(skb, *offset,
  795. &req->iter, recv_len, queue->rcv_hash);
  796. else
  797. ret = skb_copy_datagram_iter(skb, *offset,
  798. &req->iter, recv_len);
  799. if (ret) {
  800. dev_err(queue->ctrl->ctrl.device,
  801. "queue %d failed to copy request %#x data",
  802. nvme_tcp_queue_id(queue), rq->tag);
  803. return ret;
  804. }
  805. *len -= recv_len;
  806. *offset += recv_len;
  807. queue->data_remaining -= recv_len;
  808. }
  809. if (!queue->data_remaining) {
  810. if (queue->data_digest) {
  811. nvme_tcp_ddgst_final(queue->rcv_hash, &queue->exp_ddgst);
  812. queue->ddgst_remaining = NVME_TCP_DIGEST_LENGTH;
  813. } else {
  814. if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
  815. nvme_tcp_end_request(rq,
  816. le16_to_cpu(req->status));
  817. queue->nr_cqe++;
  818. }
  819. nvme_tcp_init_recv_ctx(queue);
  820. }
  821. }
  822. return 0;
  823. }
  824. static int nvme_tcp_recv_ddgst(struct nvme_tcp_queue *queue,
  825. struct sk_buff *skb, unsigned int *offset, size_t *len)
  826. {
  827. struct nvme_tcp_data_pdu *pdu = (void *)queue->pdu;
  828. char *ddgst = (char *)&queue->recv_ddgst;
  829. size_t recv_len = min_t(size_t, *len, queue->ddgst_remaining);
  830. off_t off = NVME_TCP_DIGEST_LENGTH - queue->ddgst_remaining;
  831. int ret;
  832. ret = skb_copy_bits(skb, *offset, &ddgst[off], recv_len);
  833. if (unlikely(ret))
  834. return ret;
  835. queue->ddgst_remaining -= recv_len;
  836. *offset += recv_len;
  837. *len -= recv_len;
  838. if (queue->ddgst_remaining)
  839. return 0;
  840. if (queue->recv_ddgst != queue->exp_ddgst) {
  841. struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
  842. pdu->command_id);
  843. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  844. req->status = cpu_to_le16(NVME_SC_DATA_XFER_ERROR);
  845. dev_err(queue->ctrl->ctrl.device,
  846. "data digest error: recv %#x expected %#x\n",
  847. le32_to_cpu(queue->recv_ddgst),
  848. le32_to_cpu(queue->exp_ddgst));
  849. }
  850. if (pdu->hdr.flags & NVME_TCP_F_DATA_SUCCESS) {
  851. struct request *rq = nvme_cid_to_rq(nvme_tcp_tagset(queue),
  852. pdu->command_id);
  853. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  854. nvme_tcp_end_request(rq, le16_to_cpu(req->status));
  855. queue->nr_cqe++;
  856. }
  857. nvme_tcp_init_recv_ctx(queue);
  858. return 0;
  859. }
  860. static int nvme_tcp_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,
  861. unsigned int offset, size_t len)
  862. {
  863. struct nvme_tcp_queue *queue = desc->arg.data;
  864. size_t consumed = len;
  865. int result;
  866. if (unlikely(!queue->rd_enabled))
  867. return -EFAULT;
  868. while (len) {
  869. switch (nvme_tcp_recv_state(queue)) {
  870. case NVME_TCP_RECV_PDU:
  871. result = nvme_tcp_recv_pdu(queue, skb, &offset, &len);
  872. break;
  873. case NVME_TCP_RECV_DATA:
  874. result = nvme_tcp_recv_data(queue, skb, &offset, &len);
  875. break;
  876. case NVME_TCP_RECV_DDGST:
  877. result = nvme_tcp_recv_ddgst(queue, skb, &offset, &len);
  878. break;
  879. default:
  880. result = -EFAULT;
  881. }
  882. if (result) {
  883. dev_err(queue->ctrl->ctrl.device,
  884. "receive failed: %d\n", result);
  885. queue->rd_enabled = false;
  886. nvme_tcp_error_recovery(&queue->ctrl->ctrl);
  887. return result;
  888. }
  889. }
  890. return consumed;
  891. }
  892. static void nvme_tcp_data_ready(struct sock *sk)
  893. {
  894. struct nvme_tcp_queue *queue;
  895. trace_sk_data_ready(sk);
  896. read_lock_bh(&sk->sk_callback_lock);
  897. queue = sk->sk_user_data;
  898. if (likely(queue && queue->rd_enabled) &&
  899. !test_bit(NVME_TCP_Q_POLLING, &queue->flags))
  900. queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
  901. read_unlock_bh(&sk->sk_callback_lock);
  902. }
  903. static void nvme_tcp_write_space(struct sock *sk)
  904. {
  905. struct nvme_tcp_queue *queue;
  906. read_lock_bh(&sk->sk_callback_lock);
  907. queue = sk->sk_user_data;
  908. if (likely(queue && sk_stream_is_writeable(sk))) {
  909. clear_bit(SOCK_NOSPACE, &sk->sk_socket->flags);
  910. /* Ensure pending TLS partial records are retried */
  911. if (nvme_tcp_queue_tls(queue))
  912. queue->write_space(sk);
  913. queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
  914. }
  915. read_unlock_bh(&sk->sk_callback_lock);
  916. }
  917. static void nvme_tcp_state_change(struct sock *sk)
  918. {
  919. struct nvme_tcp_queue *queue;
  920. read_lock_bh(&sk->sk_callback_lock);
  921. queue = sk->sk_user_data;
  922. if (!queue)
  923. goto done;
  924. switch (sk->sk_state) {
  925. case TCP_CLOSE:
  926. case TCP_CLOSE_WAIT:
  927. case TCP_LAST_ACK:
  928. case TCP_FIN_WAIT1:
  929. case TCP_FIN_WAIT2:
  930. nvme_tcp_error_recovery(&queue->ctrl->ctrl);
  931. break;
  932. default:
  933. dev_info(queue->ctrl->ctrl.device,
  934. "queue %d socket state %d\n",
  935. nvme_tcp_queue_id(queue), sk->sk_state);
  936. }
  937. queue->state_change(sk);
  938. done:
  939. read_unlock_bh(&sk->sk_callback_lock);
  940. }
  941. static inline void nvme_tcp_done_send_req(struct nvme_tcp_queue *queue)
  942. {
  943. queue->request = NULL;
  944. }
  945. static void nvme_tcp_fail_request(struct nvme_tcp_request *req)
  946. {
  947. if (nvme_tcp_async_req(req)) {
  948. union nvme_result res = {};
  949. nvme_complete_async_event(&req->queue->ctrl->ctrl,
  950. cpu_to_le16(NVME_SC_HOST_PATH_ERROR), &res);
  951. } else {
  952. nvme_tcp_end_request(blk_mq_rq_from_pdu(req),
  953. NVME_SC_HOST_PATH_ERROR);
  954. }
  955. }
  956. static int nvme_tcp_try_send_data(struct nvme_tcp_request *req)
  957. {
  958. struct nvme_tcp_queue *queue = req->queue;
  959. int req_data_len = req->data_len;
  960. u32 h2cdata_left = req->h2cdata_left;
  961. while (true) {
  962. struct bio_vec bvec;
  963. struct msghdr msg = {
  964. .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES,
  965. };
  966. struct page *page = nvme_tcp_req_cur_page(req);
  967. size_t offset = nvme_tcp_req_cur_offset(req);
  968. size_t len = nvme_tcp_req_cur_length(req);
  969. bool last = nvme_tcp_pdu_last_send(req, len);
  970. int req_data_sent = req->data_sent;
  971. int ret;
  972. if (last && !queue->data_digest && !nvme_tcp_queue_more(queue))
  973. msg.msg_flags |= MSG_EOR;
  974. else
  975. msg.msg_flags |= MSG_MORE;
  976. if (!sendpages_ok(page, len, offset))
  977. msg.msg_flags &= ~MSG_SPLICE_PAGES;
  978. bvec_set_page(&bvec, page, len, offset);
  979. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
  980. ret = sock_sendmsg(queue->sock, &msg);
  981. if (ret <= 0)
  982. return ret;
  983. if (queue->data_digest)
  984. nvme_tcp_ddgst_update(queue->snd_hash, page,
  985. offset, ret);
  986. /*
  987. * update the request iterator except for the last payload send
  988. * in the request where we don't want to modify it as we may
  989. * compete with the RX path completing the request.
  990. */
  991. if (req_data_sent + ret < req_data_len)
  992. nvme_tcp_advance_req(req, ret);
  993. /* fully successful last send in current PDU */
  994. if (last && ret == len) {
  995. if (queue->data_digest) {
  996. nvme_tcp_ddgst_final(queue->snd_hash,
  997. &req->ddgst);
  998. req->state = NVME_TCP_SEND_DDGST;
  999. req->offset = 0;
  1000. } else {
  1001. if (h2cdata_left)
  1002. nvme_tcp_setup_h2c_data_pdu(req);
  1003. else
  1004. nvme_tcp_done_send_req(queue);
  1005. }
  1006. return 1;
  1007. }
  1008. }
  1009. return -EAGAIN;
  1010. }
  1011. static int nvme_tcp_try_send_cmd_pdu(struct nvme_tcp_request *req)
  1012. {
  1013. struct nvme_tcp_queue *queue = req->queue;
  1014. struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
  1015. struct bio_vec bvec;
  1016. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_SPLICE_PAGES, };
  1017. bool inline_data = nvme_tcp_has_inline_data(req);
  1018. u8 hdgst = nvme_tcp_hdgst_len(queue);
  1019. int len = sizeof(*pdu) + hdgst - req->offset;
  1020. int ret;
  1021. if (inline_data || nvme_tcp_queue_more(queue))
  1022. msg.msg_flags |= MSG_MORE;
  1023. else
  1024. msg.msg_flags |= MSG_EOR;
  1025. if (queue->hdr_digest && !req->offset)
  1026. nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
  1027. bvec_set_virt(&bvec, (void *)pdu + req->offset, len);
  1028. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
  1029. ret = sock_sendmsg(queue->sock, &msg);
  1030. if (unlikely(ret <= 0))
  1031. return ret;
  1032. len -= ret;
  1033. if (!len) {
  1034. if (inline_data) {
  1035. req->state = NVME_TCP_SEND_DATA;
  1036. if (queue->data_digest)
  1037. crypto_ahash_init(queue->snd_hash);
  1038. } else {
  1039. nvme_tcp_done_send_req(queue);
  1040. }
  1041. return 1;
  1042. }
  1043. req->offset += ret;
  1044. return -EAGAIN;
  1045. }
  1046. static int nvme_tcp_try_send_data_pdu(struct nvme_tcp_request *req)
  1047. {
  1048. struct nvme_tcp_queue *queue = req->queue;
  1049. struct nvme_tcp_data_pdu *pdu = nvme_tcp_req_data_pdu(req);
  1050. struct bio_vec bvec;
  1051. struct msghdr msg = { .msg_flags = MSG_DONTWAIT | MSG_MORE, };
  1052. u8 hdgst = nvme_tcp_hdgst_len(queue);
  1053. int len = sizeof(*pdu) - req->offset + hdgst;
  1054. int ret;
  1055. if (queue->hdr_digest && !req->offset)
  1056. nvme_tcp_hdgst(queue->snd_hash, pdu, sizeof(*pdu));
  1057. if (!req->h2cdata_left)
  1058. msg.msg_flags |= MSG_SPLICE_PAGES;
  1059. bvec_set_virt(&bvec, (void *)pdu + req->offset, len);
  1060. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
  1061. ret = sock_sendmsg(queue->sock, &msg);
  1062. if (unlikely(ret <= 0))
  1063. return ret;
  1064. len -= ret;
  1065. if (!len) {
  1066. req->state = NVME_TCP_SEND_DATA;
  1067. if (queue->data_digest)
  1068. crypto_ahash_init(queue->snd_hash);
  1069. return 1;
  1070. }
  1071. req->offset += ret;
  1072. return -EAGAIN;
  1073. }
  1074. static int nvme_tcp_try_send_ddgst(struct nvme_tcp_request *req)
  1075. {
  1076. struct nvme_tcp_queue *queue = req->queue;
  1077. size_t offset = req->offset;
  1078. u32 h2cdata_left = req->h2cdata_left;
  1079. int ret;
  1080. struct msghdr msg = { .msg_flags = MSG_DONTWAIT };
  1081. struct kvec iov = {
  1082. .iov_base = (u8 *)&req->ddgst + req->offset,
  1083. .iov_len = NVME_TCP_DIGEST_LENGTH - req->offset
  1084. };
  1085. if (nvme_tcp_queue_more(queue))
  1086. msg.msg_flags |= MSG_MORE;
  1087. else
  1088. msg.msg_flags |= MSG_EOR;
  1089. ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
  1090. if (unlikely(ret <= 0))
  1091. return ret;
  1092. if (offset + ret == NVME_TCP_DIGEST_LENGTH) {
  1093. if (h2cdata_left)
  1094. nvme_tcp_setup_h2c_data_pdu(req);
  1095. else
  1096. nvme_tcp_done_send_req(queue);
  1097. return 1;
  1098. }
  1099. req->offset += ret;
  1100. return -EAGAIN;
  1101. }
  1102. static int nvme_tcp_try_send(struct nvme_tcp_queue *queue)
  1103. {
  1104. struct nvme_tcp_request *req;
  1105. unsigned int noreclaim_flag;
  1106. int ret = 1;
  1107. if (!queue->request) {
  1108. queue->request = nvme_tcp_fetch_request(queue);
  1109. if (!queue->request)
  1110. return 0;
  1111. }
  1112. req = queue->request;
  1113. noreclaim_flag = memalloc_noreclaim_save();
  1114. if (req->state == NVME_TCP_SEND_CMD_PDU) {
  1115. ret = nvme_tcp_try_send_cmd_pdu(req);
  1116. if (ret <= 0)
  1117. goto done;
  1118. if (!nvme_tcp_has_inline_data(req))
  1119. goto out;
  1120. }
  1121. if (req->state == NVME_TCP_SEND_H2C_PDU) {
  1122. ret = nvme_tcp_try_send_data_pdu(req);
  1123. if (ret <= 0)
  1124. goto done;
  1125. }
  1126. if (req->state == NVME_TCP_SEND_DATA) {
  1127. ret = nvme_tcp_try_send_data(req);
  1128. if (ret <= 0)
  1129. goto done;
  1130. }
  1131. if (req->state == NVME_TCP_SEND_DDGST)
  1132. ret = nvme_tcp_try_send_ddgst(req);
  1133. done:
  1134. if (ret == -EAGAIN) {
  1135. ret = 0;
  1136. } else if (ret < 0) {
  1137. dev_err(queue->ctrl->ctrl.device,
  1138. "failed to send request %d\n", ret);
  1139. nvme_tcp_fail_request(queue->request);
  1140. nvme_tcp_done_send_req(queue);
  1141. }
  1142. out:
  1143. memalloc_noreclaim_restore(noreclaim_flag);
  1144. return ret;
  1145. }
  1146. static int nvme_tcp_try_recv(struct nvme_tcp_queue *queue)
  1147. {
  1148. struct socket *sock = queue->sock;
  1149. struct sock *sk = sock->sk;
  1150. read_descriptor_t rd_desc;
  1151. int consumed;
  1152. rd_desc.arg.data = queue;
  1153. rd_desc.count = 1;
  1154. lock_sock(sk);
  1155. queue->nr_cqe = 0;
  1156. consumed = sock->ops->read_sock(sk, &rd_desc, nvme_tcp_recv_skb);
  1157. release_sock(sk);
  1158. return consumed == -EAGAIN ? 0 : consumed;
  1159. }
  1160. static void nvme_tcp_io_work(struct work_struct *w)
  1161. {
  1162. struct nvme_tcp_queue *queue =
  1163. container_of(w, struct nvme_tcp_queue, io_work);
  1164. unsigned long deadline = jiffies + msecs_to_jiffies(1);
  1165. do {
  1166. bool pending = false;
  1167. int result;
  1168. if (mutex_trylock(&queue->send_mutex)) {
  1169. result = nvme_tcp_try_send(queue);
  1170. mutex_unlock(&queue->send_mutex);
  1171. if (result > 0)
  1172. pending = true;
  1173. else if (unlikely(result < 0))
  1174. break;
  1175. }
  1176. result = nvme_tcp_try_recv(queue);
  1177. if (result > 0)
  1178. pending = true;
  1179. else if (unlikely(result < 0))
  1180. return;
  1181. /* did we get some space after spending time in recv? */
  1182. if (nvme_tcp_queue_has_pending(queue) &&
  1183. sk_stream_is_writeable(queue->sock->sk))
  1184. pending = true;
  1185. if (!pending || !queue->rd_enabled)
  1186. return;
  1187. } while (!time_after(jiffies, deadline)); /* quota is exhausted */
  1188. queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
  1189. }
  1190. static void nvme_tcp_free_crypto(struct nvme_tcp_queue *queue)
  1191. {
  1192. struct crypto_ahash *tfm = crypto_ahash_reqtfm(queue->rcv_hash);
  1193. ahash_request_free(queue->rcv_hash);
  1194. ahash_request_free(queue->snd_hash);
  1195. crypto_free_ahash(tfm);
  1196. }
  1197. static int nvme_tcp_alloc_crypto(struct nvme_tcp_queue *queue)
  1198. {
  1199. struct crypto_ahash *tfm;
  1200. tfm = crypto_alloc_ahash("crc32c", 0, CRYPTO_ALG_ASYNC);
  1201. if (IS_ERR(tfm))
  1202. return PTR_ERR(tfm);
  1203. queue->snd_hash = ahash_request_alloc(tfm, GFP_KERNEL);
  1204. if (!queue->snd_hash)
  1205. goto free_tfm;
  1206. ahash_request_set_callback(queue->snd_hash, 0, NULL, NULL);
  1207. queue->rcv_hash = ahash_request_alloc(tfm, GFP_KERNEL);
  1208. if (!queue->rcv_hash)
  1209. goto free_snd_hash;
  1210. ahash_request_set_callback(queue->rcv_hash, 0, NULL, NULL);
  1211. return 0;
  1212. free_snd_hash:
  1213. ahash_request_free(queue->snd_hash);
  1214. free_tfm:
  1215. crypto_free_ahash(tfm);
  1216. return -ENOMEM;
  1217. }
  1218. static void nvme_tcp_free_async_req(struct nvme_tcp_ctrl *ctrl)
  1219. {
  1220. struct nvme_tcp_request *async = &ctrl->async_req;
  1221. page_frag_free(async->pdu);
  1222. }
  1223. static int nvme_tcp_alloc_async_req(struct nvme_tcp_ctrl *ctrl)
  1224. {
  1225. struct nvme_tcp_queue *queue = &ctrl->queues[0];
  1226. struct nvme_tcp_request *async = &ctrl->async_req;
  1227. u8 hdgst = nvme_tcp_hdgst_len(queue);
  1228. async->pdu = page_frag_alloc(&queue->pf_cache,
  1229. sizeof(struct nvme_tcp_cmd_pdu) + hdgst,
  1230. GFP_KERNEL | __GFP_ZERO);
  1231. if (!async->pdu)
  1232. return -ENOMEM;
  1233. async->queue = &ctrl->queues[0];
  1234. return 0;
  1235. }
  1236. static void nvme_tcp_free_queue(struct nvme_ctrl *nctrl, int qid)
  1237. {
  1238. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  1239. struct nvme_tcp_queue *queue = &ctrl->queues[qid];
  1240. unsigned int noreclaim_flag;
  1241. if (!test_and_clear_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
  1242. return;
  1243. if (queue->hdr_digest || queue->data_digest)
  1244. nvme_tcp_free_crypto(queue);
  1245. page_frag_cache_drain(&queue->pf_cache);
  1246. noreclaim_flag = memalloc_noreclaim_save();
  1247. /* ->sock will be released by fput() */
  1248. fput(queue->sock->file);
  1249. queue->sock = NULL;
  1250. memalloc_noreclaim_restore(noreclaim_flag);
  1251. kfree(queue->pdu);
  1252. mutex_destroy(&queue->send_mutex);
  1253. mutex_destroy(&queue->queue_lock);
  1254. }
  1255. static int nvme_tcp_init_connection(struct nvme_tcp_queue *queue)
  1256. {
  1257. struct nvme_tcp_icreq_pdu *icreq;
  1258. struct nvme_tcp_icresp_pdu *icresp;
  1259. char cbuf[CMSG_LEN(sizeof(char))] = {};
  1260. u8 ctype;
  1261. struct msghdr msg = {};
  1262. struct kvec iov;
  1263. bool ctrl_hdgst, ctrl_ddgst;
  1264. u32 maxh2cdata;
  1265. int ret;
  1266. icreq = kzalloc(sizeof(*icreq), GFP_KERNEL);
  1267. if (!icreq)
  1268. return -ENOMEM;
  1269. icresp = kzalloc(sizeof(*icresp), GFP_KERNEL);
  1270. if (!icresp) {
  1271. ret = -ENOMEM;
  1272. goto free_icreq;
  1273. }
  1274. icreq->hdr.type = nvme_tcp_icreq;
  1275. icreq->hdr.hlen = sizeof(*icreq);
  1276. icreq->hdr.pdo = 0;
  1277. icreq->hdr.plen = cpu_to_le32(icreq->hdr.hlen);
  1278. icreq->pfv = cpu_to_le16(NVME_TCP_PFV_1_0);
  1279. icreq->maxr2t = 0; /* single inflight r2t supported */
  1280. icreq->hpda = 0; /* no alignment constraint */
  1281. if (queue->hdr_digest)
  1282. icreq->digest |= NVME_TCP_HDR_DIGEST_ENABLE;
  1283. if (queue->data_digest)
  1284. icreq->digest |= NVME_TCP_DATA_DIGEST_ENABLE;
  1285. iov.iov_base = icreq;
  1286. iov.iov_len = sizeof(*icreq);
  1287. ret = kernel_sendmsg(queue->sock, &msg, &iov, 1, iov.iov_len);
  1288. if (ret < 0) {
  1289. pr_warn("queue %d: failed to send icreq, error %d\n",
  1290. nvme_tcp_queue_id(queue), ret);
  1291. goto free_icresp;
  1292. }
  1293. memset(&msg, 0, sizeof(msg));
  1294. iov.iov_base = icresp;
  1295. iov.iov_len = sizeof(*icresp);
  1296. if (nvme_tcp_queue_tls(queue)) {
  1297. msg.msg_control = cbuf;
  1298. msg.msg_controllen = sizeof(cbuf);
  1299. }
  1300. msg.msg_flags = MSG_WAITALL;
  1301. ret = kernel_recvmsg(queue->sock, &msg, &iov, 1,
  1302. iov.iov_len, msg.msg_flags);
  1303. if (ret >= 0 && ret < sizeof(*icresp))
  1304. ret = -ECONNRESET;
  1305. if (ret < 0) {
  1306. pr_warn("queue %d: failed to receive icresp, error %d\n",
  1307. nvme_tcp_queue_id(queue), ret);
  1308. goto free_icresp;
  1309. }
  1310. ret = -ENOTCONN;
  1311. if (nvme_tcp_queue_tls(queue)) {
  1312. ctype = tls_get_record_type(queue->sock->sk,
  1313. (struct cmsghdr *)cbuf);
  1314. if (ctype != TLS_RECORD_TYPE_DATA) {
  1315. pr_err("queue %d: unhandled TLS record %d\n",
  1316. nvme_tcp_queue_id(queue), ctype);
  1317. goto free_icresp;
  1318. }
  1319. }
  1320. ret = -EINVAL;
  1321. if (icresp->hdr.type != nvme_tcp_icresp) {
  1322. pr_err("queue %d: bad type returned %d\n",
  1323. nvme_tcp_queue_id(queue), icresp->hdr.type);
  1324. goto free_icresp;
  1325. }
  1326. if (le32_to_cpu(icresp->hdr.plen) != sizeof(*icresp)) {
  1327. pr_err("queue %d: bad pdu length returned %d\n",
  1328. nvme_tcp_queue_id(queue), icresp->hdr.plen);
  1329. goto free_icresp;
  1330. }
  1331. if (icresp->pfv != NVME_TCP_PFV_1_0) {
  1332. pr_err("queue %d: bad pfv returned %d\n",
  1333. nvme_tcp_queue_id(queue), icresp->pfv);
  1334. goto free_icresp;
  1335. }
  1336. ctrl_ddgst = !!(icresp->digest & NVME_TCP_DATA_DIGEST_ENABLE);
  1337. if ((queue->data_digest && !ctrl_ddgst) ||
  1338. (!queue->data_digest && ctrl_ddgst)) {
  1339. pr_err("queue %d: data digest mismatch host: %s ctrl: %s\n",
  1340. nvme_tcp_queue_id(queue),
  1341. queue->data_digest ? "enabled" : "disabled",
  1342. ctrl_ddgst ? "enabled" : "disabled");
  1343. goto free_icresp;
  1344. }
  1345. ctrl_hdgst = !!(icresp->digest & NVME_TCP_HDR_DIGEST_ENABLE);
  1346. if ((queue->hdr_digest && !ctrl_hdgst) ||
  1347. (!queue->hdr_digest && ctrl_hdgst)) {
  1348. pr_err("queue %d: header digest mismatch host: %s ctrl: %s\n",
  1349. nvme_tcp_queue_id(queue),
  1350. queue->hdr_digest ? "enabled" : "disabled",
  1351. ctrl_hdgst ? "enabled" : "disabled");
  1352. goto free_icresp;
  1353. }
  1354. if (icresp->cpda != 0) {
  1355. pr_err("queue %d: unsupported cpda returned %d\n",
  1356. nvme_tcp_queue_id(queue), icresp->cpda);
  1357. goto free_icresp;
  1358. }
  1359. maxh2cdata = le32_to_cpu(icresp->maxdata);
  1360. if ((maxh2cdata % 4) || (maxh2cdata < NVME_TCP_MIN_MAXH2CDATA)) {
  1361. pr_err("queue %d: invalid maxh2cdata returned %u\n",
  1362. nvme_tcp_queue_id(queue), maxh2cdata);
  1363. goto free_icresp;
  1364. }
  1365. queue->maxh2cdata = maxh2cdata;
  1366. ret = 0;
  1367. free_icresp:
  1368. kfree(icresp);
  1369. free_icreq:
  1370. kfree(icreq);
  1371. return ret;
  1372. }
  1373. static bool nvme_tcp_admin_queue(struct nvme_tcp_queue *queue)
  1374. {
  1375. return nvme_tcp_queue_id(queue) == 0;
  1376. }
  1377. static bool nvme_tcp_default_queue(struct nvme_tcp_queue *queue)
  1378. {
  1379. struct nvme_tcp_ctrl *ctrl = queue->ctrl;
  1380. int qid = nvme_tcp_queue_id(queue);
  1381. return !nvme_tcp_admin_queue(queue) &&
  1382. qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT];
  1383. }
  1384. static bool nvme_tcp_read_queue(struct nvme_tcp_queue *queue)
  1385. {
  1386. struct nvme_tcp_ctrl *ctrl = queue->ctrl;
  1387. int qid = nvme_tcp_queue_id(queue);
  1388. return !nvme_tcp_admin_queue(queue) &&
  1389. !nvme_tcp_default_queue(queue) &&
  1390. qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
  1391. ctrl->io_queues[HCTX_TYPE_READ];
  1392. }
  1393. static bool nvme_tcp_poll_queue(struct nvme_tcp_queue *queue)
  1394. {
  1395. struct nvme_tcp_ctrl *ctrl = queue->ctrl;
  1396. int qid = nvme_tcp_queue_id(queue);
  1397. return !nvme_tcp_admin_queue(queue) &&
  1398. !nvme_tcp_default_queue(queue) &&
  1399. !nvme_tcp_read_queue(queue) &&
  1400. qid < 1 + ctrl->io_queues[HCTX_TYPE_DEFAULT] +
  1401. ctrl->io_queues[HCTX_TYPE_READ] +
  1402. ctrl->io_queues[HCTX_TYPE_POLL];
  1403. }
  1404. /*
  1405. * Track the number of queues assigned to each cpu using a global per-cpu
  1406. * counter and select the least used cpu from the mq_map. Our goal is to spread
  1407. * different controllers I/O threads across different cpu cores.
  1408. *
  1409. * Note that the accounting is not 100% perfect, but we don't need to be, we're
  1410. * simply putting our best effort to select the best candidate cpu core that we
  1411. * find at any given point.
  1412. */
  1413. static void nvme_tcp_set_queue_io_cpu(struct nvme_tcp_queue *queue)
  1414. {
  1415. struct nvme_tcp_ctrl *ctrl = queue->ctrl;
  1416. struct blk_mq_tag_set *set = &ctrl->tag_set;
  1417. int qid = nvme_tcp_queue_id(queue) - 1;
  1418. unsigned int *mq_map = NULL;
  1419. int cpu, min_queues = INT_MAX, io_cpu;
  1420. if (wq_unbound)
  1421. goto out;
  1422. if (nvme_tcp_default_queue(queue))
  1423. mq_map = set->map[HCTX_TYPE_DEFAULT].mq_map;
  1424. else if (nvme_tcp_read_queue(queue))
  1425. mq_map = set->map[HCTX_TYPE_READ].mq_map;
  1426. else if (nvme_tcp_poll_queue(queue))
  1427. mq_map = set->map[HCTX_TYPE_POLL].mq_map;
  1428. if (WARN_ON(!mq_map))
  1429. goto out;
  1430. /* Search for the least used cpu from the mq_map */
  1431. io_cpu = WORK_CPU_UNBOUND;
  1432. for_each_online_cpu(cpu) {
  1433. int num_queues = atomic_read(&nvme_tcp_cpu_queues[cpu]);
  1434. if (mq_map[cpu] != qid)
  1435. continue;
  1436. if (num_queues < min_queues) {
  1437. io_cpu = cpu;
  1438. min_queues = num_queues;
  1439. }
  1440. }
  1441. if (io_cpu != WORK_CPU_UNBOUND) {
  1442. queue->io_cpu = io_cpu;
  1443. atomic_inc(&nvme_tcp_cpu_queues[io_cpu]);
  1444. set_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags);
  1445. }
  1446. out:
  1447. dev_dbg(ctrl->ctrl.device, "queue %d: using cpu %d\n",
  1448. qid, queue->io_cpu);
  1449. }
  1450. static void nvme_tcp_tls_done(void *data, int status, key_serial_t pskid)
  1451. {
  1452. struct nvme_tcp_queue *queue = data;
  1453. struct nvme_tcp_ctrl *ctrl = queue->ctrl;
  1454. int qid = nvme_tcp_queue_id(queue);
  1455. struct key *tls_key;
  1456. dev_dbg(ctrl->ctrl.device, "queue %d: TLS handshake done, key %x, status %d\n",
  1457. qid, pskid, status);
  1458. if (status) {
  1459. queue->tls_err = -status;
  1460. goto out_complete;
  1461. }
  1462. tls_key = nvme_tls_key_lookup(pskid);
  1463. if (IS_ERR(tls_key)) {
  1464. dev_warn(ctrl->ctrl.device, "queue %d: Invalid key %x\n",
  1465. qid, pskid);
  1466. queue->tls_err = -ENOKEY;
  1467. } else {
  1468. queue->tls_enabled = true;
  1469. if (qid == 0)
  1470. ctrl->ctrl.tls_pskid = key_serial(tls_key);
  1471. key_put(tls_key);
  1472. queue->tls_err = 0;
  1473. }
  1474. out_complete:
  1475. complete(&queue->tls_complete);
  1476. }
  1477. static int nvme_tcp_start_tls(struct nvme_ctrl *nctrl,
  1478. struct nvme_tcp_queue *queue,
  1479. key_serial_t pskid)
  1480. {
  1481. int qid = nvme_tcp_queue_id(queue);
  1482. int ret;
  1483. struct tls_handshake_args args;
  1484. unsigned long tmo = tls_handshake_timeout * HZ;
  1485. key_serial_t keyring = nvme_keyring_id();
  1486. dev_dbg(nctrl->device, "queue %d: start TLS with key %x\n",
  1487. qid, pskid);
  1488. memset(&args, 0, sizeof(args));
  1489. args.ta_sock = queue->sock;
  1490. args.ta_done = nvme_tcp_tls_done;
  1491. args.ta_data = queue;
  1492. args.ta_my_peerids[0] = pskid;
  1493. args.ta_num_peerids = 1;
  1494. if (nctrl->opts->keyring)
  1495. keyring = key_serial(nctrl->opts->keyring);
  1496. args.ta_keyring = keyring;
  1497. args.ta_timeout_ms = tls_handshake_timeout * 1000;
  1498. queue->tls_err = -EOPNOTSUPP;
  1499. init_completion(&queue->tls_complete);
  1500. ret = tls_client_hello_psk(&args, GFP_KERNEL);
  1501. if (ret) {
  1502. dev_err(nctrl->device, "queue %d: failed to start TLS: %d\n",
  1503. qid, ret);
  1504. return ret;
  1505. }
  1506. ret = wait_for_completion_interruptible_timeout(&queue->tls_complete, tmo);
  1507. if (ret <= 0) {
  1508. if (ret == 0)
  1509. ret = -ETIMEDOUT;
  1510. dev_err(nctrl->device,
  1511. "queue %d: TLS handshake failed, error %d\n",
  1512. qid, ret);
  1513. tls_handshake_cancel(queue->sock->sk);
  1514. } else {
  1515. if (queue->tls_err) {
  1516. dev_err(nctrl->device,
  1517. "queue %d: TLS handshake complete, error %d\n",
  1518. qid, queue->tls_err);
  1519. } else {
  1520. dev_dbg(nctrl->device,
  1521. "queue %d: TLS handshake complete\n", qid);
  1522. }
  1523. ret = queue->tls_err;
  1524. }
  1525. return ret;
  1526. }
  1527. static int nvme_tcp_alloc_queue(struct nvme_ctrl *nctrl, int qid,
  1528. key_serial_t pskid)
  1529. {
  1530. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  1531. struct nvme_tcp_queue *queue = &ctrl->queues[qid];
  1532. int ret, rcv_pdu_size;
  1533. struct file *sock_file;
  1534. mutex_init(&queue->queue_lock);
  1535. queue->ctrl = ctrl;
  1536. init_llist_head(&queue->req_list);
  1537. INIT_LIST_HEAD(&queue->send_list);
  1538. mutex_init(&queue->send_mutex);
  1539. INIT_WORK(&queue->io_work, nvme_tcp_io_work);
  1540. if (qid > 0)
  1541. queue->cmnd_capsule_len = nctrl->ioccsz * 16;
  1542. else
  1543. queue->cmnd_capsule_len = sizeof(struct nvme_command) +
  1544. NVME_TCP_ADMIN_CCSZ;
  1545. ret = sock_create(ctrl->addr.ss_family, SOCK_STREAM,
  1546. IPPROTO_TCP, &queue->sock);
  1547. if (ret) {
  1548. dev_err(nctrl->device,
  1549. "failed to create socket: %d\n", ret);
  1550. goto err_destroy_mutex;
  1551. }
  1552. sock_file = sock_alloc_file(queue->sock, O_CLOEXEC, NULL);
  1553. if (IS_ERR(sock_file)) {
  1554. ret = PTR_ERR(sock_file);
  1555. goto err_destroy_mutex;
  1556. }
  1557. nvme_tcp_reclassify_socket(queue->sock);
  1558. /* Single syn retry */
  1559. tcp_sock_set_syncnt(queue->sock->sk, 1);
  1560. /* Set TCP no delay */
  1561. tcp_sock_set_nodelay(queue->sock->sk);
  1562. /*
  1563. * Cleanup whatever is sitting in the TCP transmit queue on socket
  1564. * close. This is done to prevent stale data from being sent should
  1565. * the network connection be restored before TCP times out.
  1566. */
  1567. sock_no_linger(queue->sock->sk);
  1568. if (so_priority > 0)
  1569. sock_set_priority(queue->sock->sk, so_priority);
  1570. /* Set socket type of service */
  1571. if (nctrl->opts->tos >= 0)
  1572. ip_sock_set_tos(queue->sock->sk, nctrl->opts->tos);
  1573. /* Set 10 seconds timeout for icresp recvmsg */
  1574. queue->sock->sk->sk_rcvtimeo = 10 * HZ;
  1575. queue->sock->sk->sk_allocation = GFP_ATOMIC;
  1576. queue->sock->sk->sk_use_task_frag = false;
  1577. queue->io_cpu = WORK_CPU_UNBOUND;
  1578. queue->request = NULL;
  1579. queue->data_remaining = 0;
  1580. queue->ddgst_remaining = 0;
  1581. queue->pdu_remaining = 0;
  1582. queue->pdu_offset = 0;
  1583. sk_set_memalloc(queue->sock->sk);
  1584. if (nctrl->opts->mask & NVMF_OPT_HOST_TRADDR) {
  1585. ret = kernel_bind(queue->sock, (struct sockaddr *)&ctrl->src_addr,
  1586. sizeof(ctrl->src_addr));
  1587. if (ret) {
  1588. dev_err(nctrl->device,
  1589. "failed to bind queue %d socket %d\n",
  1590. qid, ret);
  1591. goto err_sock;
  1592. }
  1593. }
  1594. if (nctrl->opts->mask & NVMF_OPT_HOST_IFACE) {
  1595. char *iface = nctrl->opts->host_iface;
  1596. sockptr_t optval = KERNEL_SOCKPTR(iface);
  1597. ret = sock_setsockopt(queue->sock, SOL_SOCKET, SO_BINDTODEVICE,
  1598. optval, strlen(iface));
  1599. if (ret) {
  1600. dev_err(nctrl->device,
  1601. "failed to bind to interface %s queue %d err %d\n",
  1602. iface, qid, ret);
  1603. goto err_sock;
  1604. }
  1605. }
  1606. queue->hdr_digest = nctrl->opts->hdr_digest;
  1607. queue->data_digest = nctrl->opts->data_digest;
  1608. if (queue->hdr_digest || queue->data_digest) {
  1609. ret = nvme_tcp_alloc_crypto(queue);
  1610. if (ret) {
  1611. dev_err(nctrl->device,
  1612. "failed to allocate queue %d crypto\n", qid);
  1613. goto err_sock;
  1614. }
  1615. }
  1616. rcv_pdu_size = sizeof(struct nvme_tcp_rsp_pdu) +
  1617. nvme_tcp_hdgst_len(queue);
  1618. queue->pdu = kmalloc(rcv_pdu_size, GFP_KERNEL);
  1619. if (!queue->pdu) {
  1620. ret = -ENOMEM;
  1621. goto err_crypto;
  1622. }
  1623. dev_dbg(nctrl->device, "connecting queue %d\n",
  1624. nvme_tcp_queue_id(queue));
  1625. ret = kernel_connect(queue->sock, (struct sockaddr *)&ctrl->addr,
  1626. sizeof(ctrl->addr), 0);
  1627. if (ret) {
  1628. dev_err(nctrl->device,
  1629. "failed to connect socket: %d\n", ret);
  1630. goto err_rcv_pdu;
  1631. }
  1632. /* If PSKs are configured try to start TLS */
  1633. if (nvme_tcp_tls_configured(nctrl) && pskid) {
  1634. ret = nvme_tcp_start_tls(nctrl, queue, pskid);
  1635. if (ret)
  1636. goto err_init_connect;
  1637. }
  1638. ret = nvme_tcp_init_connection(queue);
  1639. if (ret)
  1640. goto err_init_connect;
  1641. set_bit(NVME_TCP_Q_ALLOCATED, &queue->flags);
  1642. return 0;
  1643. err_init_connect:
  1644. kernel_sock_shutdown(queue->sock, SHUT_RDWR);
  1645. err_rcv_pdu:
  1646. kfree(queue->pdu);
  1647. err_crypto:
  1648. if (queue->hdr_digest || queue->data_digest)
  1649. nvme_tcp_free_crypto(queue);
  1650. err_sock:
  1651. /* ->sock will be released by fput() */
  1652. fput(queue->sock->file);
  1653. queue->sock = NULL;
  1654. err_destroy_mutex:
  1655. mutex_destroy(&queue->send_mutex);
  1656. mutex_destroy(&queue->queue_lock);
  1657. return ret;
  1658. }
  1659. static void nvme_tcp_restore_sock_ops(struct nvme_tcp_queue *queue)
  1660. {
  1661. struct socket *sock = queue->sock;
  1662. write_lock_bh(&sock->sk->sk_callback_lock);
  1663. sock->sk->sk_user_data = NULL;
  1664. sock->sk->sk_data_ready = queue->data_ready;
  1665. sock->sk->sk_state_change = queue->state_change;
  1666. sock->sk->sk_write_space = queue->write_space;
  1667. write_unlock_bh(&sock->sk->sk_callback_lock);
  1668. }
  1669. static void __nvme_tcp_stop_queue(struct nvme_tcp_queue *queue)
  1670. {
  1671. kernel_sock_shutdown(queue->sock, SHUT_RDWR);
  1672. nvme_tcp_restore_sock_ops(queue);
  1673. cancel_work_sync(&queue->io_work);
  1674. }
  1675. static void nvme_tcp_stop_queue_nowait(struct nvme_ctrl *nctrl, int qid)
  1676. {
  1677. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  1678. struct nvme_tcp_queue *queue = &ctrl->queues[qid];
  1679. if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
  1680. return;
  1681. if (test_and_clear_bit(NVME_TCP_Q_IO_CPU_SET, &queue->flags))
  1682. atomic_dec(&nvme_tcp_cpu_queues[queue->io_cpu]);
  1683. mutex_lock(&queue->queue_lock);
  1684. if (test_and_clear_bit(NVME_TCP_Q_LIVE, &queue->flags))
  1685. __nvme_tcp_stop_queue(queue);
  1686. /* Stopping the queue will disable TLS */
  1687. queue->tls_enabled = false;
  1688. mutex_unlock(&queue->queue_lock);
  1689. }
  1690. static void nvme_tcp_wait_queue(struct nvme_ctrl *nctrl, int qid)
  1691. {
  1692. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  1693. struct nvme_tcp_queue *queue = &ctrl->queues[qid];
  1694. int timeout = 100;
  1695. while (timeout > 0) {
  1696. if (!test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags) ||
  1697. !sk_wmem_alloc_get(queue->sock->sk))
  1698. return;
  1699. msleep(2);
  1700. timeout -= 2;
  1701. }
  1702. dev_warn(nctrl->device,
  1703. "qid %d: timeout draining sock wmem allocation expired\n",
  1704. qid);
  1705. }
  1706. static void nvme_tcp_stop_queue(struct nvme_ctrl *nctrl, int qid)
  1707. {
  1708. nvme_tcp_stop_queue_nowait(nctrl, qid);
  1709. nvme_tcp_wait_queue(nctrl, qid);
  1710. }
  1711. static void nvme_tcp_setup_sock_ops(struct nvme_tcp_queue *queue)
  1712. {
  1713. write_lock_bh(&queue->sock->sk->sk_callback_lock);
  1714. queue->sock->sk->sk_user_data = queue;
  1715. queue->state_change = queue->sock->sk->sk_state_change;
  1716. queue->data_ready = queue->sock->sk->sk_data_ready;
  1717. queue->write_space = queue->sock->sk->sk_write_space;
  1718. queue->sock->sk->sk_data_ready = nvme_tcp_data_ready;
  1719. queue->sock->sk->sk_state_change = nvme_tcp_state_change;
  1720. queue->sock->sk->sk_write_space = nvme_tcp_write_space;
  1721. #ifdef CONFIG_NET_RX_BUSY_POLL
  1722. queue->sock->sk->sk_ll_usec = 1;
  1723. #endif
  1724. write_unlock_bh(&queue->sock->sk->sk_callback_lock);
  1725. }
  1726. static int nvme_tcp_start_queue(struct nvme_ctrl *nctrl, int idx)
  1727. {
  1728. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  1729. struct nvme_tcp_queue *queue = &ctrl->queues[idx];
  1730. int ret;
  1731. queue->rd_enabled = true;
  1732. nvme_tcp_init_recv_ctx(queue);
  1733. nvme_tcp_setup_sock_ops(queue);
  1734. if (idx) {
  1735. nvme_tcp_set_queue_io_cpu(queue);
  1736. ret = nvmf_connect_io_queue(nctrl, idx);
  1737. } else
  1738. ret = nvmf_connect_admin_queue(nctrl);
  1739. if (!ret) {
  1740. set_bit(NVME_TCP_Q_LIVE, &queue->flags);
  1741. } else {
  1742. if (test_bit(NVME_TCP_Q_ALLOCATED, &queue->flags))
  1743. __nvme_tcp_stop_queue(queue);
  1744. dev_err(nctrl->device,
  1745. "failed to connect queue: %d ret=%d\n", idx, ret);
  1746. }
  1747. return ret;
  1748. }
  1749. static void nvme_tcp_free_admin_queue(struct nvme_ctrl *ctrl)
  1750. {
  1751. if (to_tcp_ctrl(ctrl)->async_req.pdu) {
  1752. cancel_work_sync(&ctrl->async_event_work);
  1753. nvme_tcp_free_async_req(to_tcp_ctrl(ctrl));
  1754. to_tcp_ctrl(ctrl)->async_req.pdu = NULL;
  1755. }
  1756. nvme_tcp_free_queue(ctrl, 0);
  1757. }
  1758. static void nvme_tcp_free_io_queues(struct nvme_ctrl *ctrl)
  1759. {
  1760. int i;
  1761. for (i = 1; i < ctrl->queue_count; i++)
  1762. nvme_tcp_free_queue(ctrl, i);
  1763. }
  1764. static void nvme_tcp_stop_io_queues(struct nvme_ctrl *ctrl)
  1765. {
  1766. int i;
  1767. for (i = 1; i < ctrl->queue_count; i++)
  1768. nvme_tcp_stop_queue_nowait(ctrl, i);
  1769. for (i = 1; i < ctrl->queue_count; i++)
  1770. nvme_tcp_wait_queue(ctrl, i);
  1771. }
  1772. static int nvme_tcp_start_io_queues(struct nvme_ctrl *ctrl,
  1773. int first, int last)
  1774. {
  1775. int i, ret;
  1776. for (i = first; i < last; i++) {
  1777. ret = nvme_tcp_start_queue(ctrl, i);
  1778. if (ret)
  1779. goto out_stop_queues;
  1780. }
  1781. return 0;
  1782. out_stop_queues:
  1783. for (i--; i >= first; i--)
  1784. nvme_tcp_stop_queue(ctrl, i);
  1785. return ret;
  1786. }
  1787. static int nvme_tcp_alloc_admin_queue(struct nvme_ctrl *ctrl)
  1788. {
  1789. int ret;
  1790. key_serial_t pskid = 0;
  1791. if (nvme_tcp_tls_configured(ctrl)) {
  1792. if (ctrl->opts->tls_key)
  1793. pskid = key_serial(ctrl->opts->tls_key);
  1794. else {
  1795. pskid = nvme_tls_psk_default(ctrl->opts->keyring,
  1796. ctrl->opts->host->nqn,
  1797. ctrl->opts->subsysnqn);
  1798. if (!pskid) {
  1799. dev_err(ctrl->device, "no valid PSK found\n");
  1800. return -ENOKEY;
  1801. }
  1802. }
  1803. }
  1804. ret = nvme_tcp_alloc_queue(ctrl, 0, pskid);
  1805. if (ret)
  1806. return ret;
  1807. ret = nvme_tcp_alloc_async_req(to_tcp_ctrl(ctrl));
  1808. if (ret)
  1809. goto out_free_queue;
  1810. return 0;
  1811. out_free_queue:
  1812. nvme_tcp_free_queue(ctrl, 0);
  1813. return ret;
  1814. }
  1815. static int __nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
  1816. {
  1817. int i, ret;
  1818. if (nvme_tcp_tls_configured(ctrl) && !ctrl->tls_pskid) {
  1819. dev_err(ctrl->device, "no PSK negotiated\n");
  1820. return -ENOKEY;
  1821. }
  1822. for (i = 1; i < ctrl->queue_count; i++) {
  1823. ret = nvme_tcp_alloc_queue(ctrl, i,
  1824. ctrl->tls_pskid);
  1825. if (ret)
  1826. goto out_free_queues;
  1827. }
  1828. return 0;
  1829. out_free_queues:
  1830. for (i--; i >= 1; i--)
  1831. nvme_tcp_free_queue(ctrl, i);
  1832. return ret;
  1833. }
  1834. static int nvme_tcp_alloc_io_queues(struct nvme_ctrl *ctrl)
  1835. {
  1836. unsigned int nr_io_queues;
  1837. int ret;
  1838. nr_io_queues = nvmf_nr_io_queues(ctrl->opts);
  1839. ret = nvme_set_queue_count(ctrl, &nr_io_queues);
  1840. if (ret)
  1841. return ret;
  1842. if (nr_io_queues == 0) {
  1843. dev_err(ctrl->device,
  1844. "unable to set any I/O queues\n");
  1845. return -ENOMEM;
  1846. }
  1847. ctrl->queue_count = nr_io_queues + 1;
  1848. dev_info(ctrl->device,
  1849. "creating %d I/O queues.\n", nr_io_queues);
  1850. nvmf_set_io_queues(ctrl->opts, nr_io_queues,
  1851. to_tcp_ctrl(ctrl)->io_queues);
  1852. return __nvme_tcp_alloc_io_queues(ctrl);
  1853. }
  1854. static void nvme_tcp_destroy_io_queues(struct nvme_ctrl *ctrl, bool remove)
  1855. {
  1856. nvme_tcp_stop_io_queues(ctrl);
  1857. if (remove)
  1858. nvme_remove_io_tag_set(ctrl);
  1859. nvme_tcp_free_io_queues(ctrl);
  1860. }
  1861. static int nvme_tcp_configure_io_queues(struct nvme_ctrl *ctrl, bool new)
  1862. {
  1863. int ret, nr_queues;
  1864. ret = nvme_tcp_alloc_io_queues(ctrl);
  1865. if (ret)
  1866. return ret;
  1867. if (new) {
  1868. ret = nvme_alloc_io_tag_set(ctrl, &to_tcp_ctrl(ctrl)->tag_set,
  1869. &nvme_tcp_mq_ops,
  1870. ctrl->opts->nr_poll_queues ? HCTX_MAX_TYPES : 2,
  1871. sizeof(struct nvme_tcp_request));
  1872. if (ret)
  1873. goto out_free_io_queues;
  1874. }
  1875. /*
  1876. * Only start IO queues for which we have allocated the tagset
  1877. * and limitted it to the available queues. On reconnects, the
  1878. * queue number might have changed.
  1879. */
  1880. nr_queues = min(ctrl->tagset->nr_hw_queues + 1, ctrl->queue_count);
  1881. ret = nvme_tcp_start_io_queues(ctrl, 1, nr_queues);
  1882. if (ret)
  1883. goto out_cleanup_connect_q;
  1884. if (!new) {
  1885. nvme_start_freeze(ctrl);
  1886. nvme_unquiesce_io_queues(ctrl);
  1887. if (!nvme_wait_freeze_timeout(ctrl, NVME_IO_TIMEOUT)) {
  1888. /*
  1889. * If we timed out waiting for freeze we are likely to
  1890. * be stuck. Fail the controller initialization just
  1891. * to be safe.
  1892. */
  1893. ret = -ENODEV;
  1894. nvme_unfreeze(ctrl);
  1895. goto out_wait_freeze_timed_out;
  1896. }
  1897. blk_mq_update_nr_hw_queues(ctrl->tagset,
  1898. ctrl->queue_count - 1);
  1899. nvme_unfreeze(ctrl);
  1900. }
  1901. /*
  1902. * If the number of queues has increased (reconnect case)
  1903. * start all new queues now.
  1904. */
  1905. ret = nvme_tcp_start_io_queues(ctrl, nr_queues,
  1906. ctrl->tagset->nr_hw_queues + 1);
  1907. if (ret)
  1908. goto out_wait_freeze_timed_out;
  1909. return 0;
  1910. out_wait_freeze_timed_out:
  1911. nvme_quiesce_io_queues(ctrl);
  1912. nvme_sync_io_queues(ctrl);
  1913. nvme_tcp_stop_io_queues(ctrl);
  1914. out_cleanup_connect_q:
  1915. nvme_cancel_tagset(ctrl);
  1916. if (new)
  1917. nvme_remove_io_tag_set(ctrl);
  1918. out_free_io_queues:
  1919. nvme_tcp_free_io_queues(ctrl);
  1920. return ret;
  1921. }
  1922. static void nvme_tcp_destroy_admin_queue(struct nvme_ctrl *ctrl, bool remove)
  1923. {
  1924. nvme_tcp_stop_queue(ctrl, 0);
  1925. if (remove)
  1926. nvme_remove_admin_tag_set(ctrl);
  1927. nvme_tcp_free_admin_queue(ctrl);
  1928. }
  1929. static int nvme_tcp_configure_admin_queue(struct nvme_ctrl *ctrl, bool new)
  1930. {
  1931. int error;
  1932. error = nvme_tcp_alloc_admin_queue(ctrl);
  1933. if (error)
  1934. return error;
  1935. if (new) {
  1936. error = nvme_alloc_admin_tag_set(ctrl,
  1937. &to_tcp_ctrl(ctrl)->admin_tag_set,
  1938. &nvme_tcp_admin_mq_ops,
  1939. sizeof(struct nvme_tcp_request));
  1940. if (error)
  1941. goto out_free_queue;
  1942. }
  1943. error = nvme_tcp_start_queue(ctrl, 0);
  1944. if (error)
  1945. goto out_cleanup_tagset;
  1946. error = nvme_enable_ctrl(ctrl);
  1947. if (error)
  1948. goto out_stop_queue;
  1949. nvme_unquiesce_admin_queue(ctrl);
  1950. error = nvme_init_ctrl_finish(ctrl, false);
  1951. if (error)
  1952. goto out_quiesce_queue;
  1953. return 0;
  1954. out_quiesce_queue:
  1955. nvme_quiesce_admin_queue(ctrl);
  1956. blk_sync_queue(ctrl->admin_q);
  1957. out_stop_queue:
  1958. nvme_tcp_stop_queue(ctrl, 0);
  1959. nvme_cancel_admin_tagset(ctrl);
  1960. out_cleanup_tagset:
  1961. if (new)
  1962. nvme_remove_admin_tag_set(ctrl);
  1963. out_free_queue:
  1964. nvme_tcp_free_admin_queue(ctrl);
  1965. return error;
  1966. }
  1967. static void nvme_tcp_teardown_admin_queue(struct nvme_ctrl *ctrl,
  1968. bool remove)
  1969. {
  1970. nvme_quiesce_admin_queue(ctrl);
  1971. blk_sync_queue(ctrl->admin_q);
  1972. nvme_tcp_stop_queue(ctrl, 0);
  1973. nvme_cancel_admin_tagset(ctrl);
  1974. if (remove)
  1975. nvme_unquiesce_admin_queue(ctrl);
  1976. nvme_tcp_destroy_admin_queue(ctrl, remove);
  1977. if (ctrl->tls_pskid) {
  1978. dev_dbg(ctrl->device, "Wipe negotiated TLS_PSK %08x\n",
  1979. ctrl->tls_pskid);
  1980. ctrl->tls_pskid = 0;
  1981. }
  1982. }
  1983. static void nvme_tcp_teardown_io_queues(struct nvme_ctrl *ctrl,
  1984. bool remove)
  1985. {
  1986. if (ctrl->queue_count <= 1)
  1987. return;
  1988. nvme_quiesce_admin_queue(ctrl);
  1989. nvme_quiesce_io_queues(ctrl);
  1990. nvme_sync_io_queues(ctrl);
  1991. nvme_tcp_stop_io_queues(ctrl);
  1992. nvme_cancel_tagset(ctrl);
  1993. if (remove)
  1994. nvme_unquiesce_io_queues(ctrl);
  1995. nvme_tcp_destroy_io_queues(ctrl, remove);
  1996. }
  1997. static void nvme_tcp_reconnect_or_remove(struct nvme_ctrl *ctrl,
  1998. int status)
  1999. {
  2000. enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
  2001. /* If we are resetting/deleting then do nothing */
  2002. if (state != NVME_CTRL_CONNECTING) {
  2003. WARN_ON_ONCE(state == NVME_CTRL_NEW || state == NVME_CTRL_LIVE);
  2004. return;
  2005. }
  2006. if (nvmf_should_reconnect(ctrl, status)) {
  2007. dev_info(ctrl->device, "Reconnecting in %d seconds...\n",
  2008. ctrl->opts->reconnect_delay);
  2009. queue_delayed_work(nvme_wq, &to_tcp_ctrl(ctrl)->connect_work,
  2010. ctrl->opts->reconnect_delay * HZ);
  2011. } else {
  2012. dev_info(ctrl->device, "Removing controller (%d)...\n",
  2013. status);
  2014. nvme_delete_ctrl(ctrl);
  2015. }
  2016. }
  2017. static int nvme_tcp_setup_ctrl(struct nvme_ctrl *ctrl, bool new)
  2018. {
  2019. struct nvmf_ctrl_options *opts = ctrl->opts;
  2020. int ret;
  2021. ret = nvme_tcp_configure_admin_queue(ctrl, new);
  2022. if (ret)
  2023. return ret;
  2024. if (ctrl->icdoff) {
  2025. ret = -EOPNOTSUPP;
  2026. dev_err(ctrl->device, "icdoff is not supported!\n");
  2027. goto destroy_admin;
  2028. }
  2029. if (!nvme_ctrl_sgl_supported(ctrl)) {
  2030. ret = -EOPNOTSUPP;
  2031. dev_err(ctrl->device, "Mandatory sgls are not supported!\n");
  2032. goto destroy_admin;
  2033. }
  2034. if (opts->queue_size > ctrl->sqsize + 1)
  2035. dev_warn(ctrl->device,
  2036. "queue_size %zu > ctrl sqsize %u, clamping down\n",
  2037. opts->queue_size, ctrl->sqsize + 1);
  2038. if (ctrl->sqsize + 1 > ctrl->maxcmd) {
  2039. dev_warn(ctrl->device,
  2040. "sqsize %u > ctrl maxcmd %u, clamping down\n",
  2041. ctrl->sqsize + 1, ctrl->maxcmd);
  2042. ctrl->sqsize = ctrl->maxcmd - 1;
  2043. }
  2044. if (ctrl->queue_count > 1) {
  2045. ret = nvme_tcp_configure_io_queues(ctrl, new);
  2046. if (ret)
  2047. goto destroy_admin;
  2048. }
  2049. if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_LIVE)) {
  2050. /*
  2051. * state change failure is ok if we started ctrl delete,
  2052. * unless we're during creation of a new controller to
  2053. * avoid races with teardown flow.
  2054. */
  2055. enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
  2056. WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
  2057. state != NVME_CTRL_DELETING_NOIO);
  2058. WARN_ON_ONCE(new);
  2059. ret = -EINVAL;
  2060. goto destroy_io;
  2061. }
  2062. nvme_start_ctrl(ctrl);
  2063. return 0;
  2064. destroy_io:
  2065. if (ctrl->queue_count > 1) {
  2066. nvme_quiesce_io_queues(ctrl);
  2067. nvme_sync_io_queues(ctrl);
  2068. nvme_tcp_stop_io_queues(ctrl);
  2069. nvme_cancel_tagset(ctrl);
  2070. nvme_tcp_destroy_io_queues(ctrl, new);
  2071. }
  2072. destroy_admin:
  2073. nvme_stop_keep_alive(ctrl);
  2074. nvme_tcp_teardown_admin_queue(ctrl, new);
  2075. return ret;
  2076. }
  2077. static void nvme_tcp_reconnect_ctrl_work(struct work_struct *work)
  2078. {
  2079. struct nvme_tcp_ctrl *tcp_ctrl = container_of(to_delayed_work(work),
  2080. struct nvme_tcp_ctrl, connect_work);
  2081. struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
  2082. int ret;
  2083. ++ctrl->nr_reconnects;
  2084. ret = nvme_tcp_setup_ctrl(ctrl, false);
  2085. if (ret)
  2086. goto requeue;
  2087. dev_info(ctrl->device, "Successfully reconnected (attempt %d/%d)\n",
  2088. ctrl->nr_reconnects, ctrl->opts->max_reconnects);
  2089. ctrl->nr_reconnects = 0;
  2090. return;
  2091. requeue:
  2092. dev_info(ctrl->device, "Failed reconnect attempt %d/%d\n",
  2093. ctrl->nr_reconnects, ctrl->opts->max_reconnects);
  2094. nvme_tcp_reconnect_or_remove(ctrl, ret);
  2095. }
  2096. static void nvme_tcp_error_recovery_work(struct work_struct *work)
  2097. {
  2098. struct nvme_tcp_ctrl *tcp_ctrl = container_of(work,
  2099. struct nvme_tcp_ctrl, err_work);
  2100. struct nvme_ctrl *ctrl = &tcp_ctrl->ctrl;
  2101. nvme_stop_keep_alive(ctrl);
  2102. flush_work(&ctrl->async_event_work);
  2103. nvme_tcp_teardown_io_queues(ctrl, false);
  2104. /* unquiesce to fail fast pending requests */
  2105. nvme_unquiesce_io_queues(ctrl);
  2106. nvme_tcp_teardown_admin_queue(ctrl, false);
  2107. nvme_unquiesce_admin_queue(ctrl);
  2108. nvme_auth_stop(ctrl);
  2109. if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
  2110. /* state change failure is ok if we started ctrl delete */
  2111. enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
  2112. WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
  2113. state != NVME_CTRL_DELETING_NOIO);
  2114. return;
  2115. }
  2116. nvme_tcp_reconnect_or_remove(ctrl, 0);
  2117. }
  2118. static void nvme_tcp_teardown_ctrl(struct nvme_ctrl *ctrl, bool shutdown)
  2119. {
  2120. nvme_tcp_teardown_io_queues(ctrl, shutdown);
  2121. nvme_quiesce_admin_queue(ctrl);
  2122. nvme_disable_ctrl(ctrl, shutdown);
  2123. nvme_tcp_teardown_admin_queue(ctrl, shutdown);
  2124. }
  2125. static void nvme_tcp_delete_ctrl(struct nvme_ctrl *ctrl)
  2126. {
  2127. nvme_tcp_teardown_ctrl(ctrl, true);
  2128. }
  2129. static void nvme_reset_ctrl_work(struct work_struct *work)
  2130. {
  2131. struct nvme_ctrl *ctrl =
  2132. container_of(work, struct nvme_ctrl, reset_work);
  2133. int ret;
  2134. nvme_stop_ctrl(ctrl);
  2135. nvme_tcp_teardown_ctrl(ctrl, false);
  2136. if (!nvme_change_ctrl_state(ctrl, NVME_CTRL_CONNECTING)) {
  2137. /* state change failure is ok if we started ctrl delete */
  2138. enum nvme_ctrl_state state = nvme_ctrl_state(ctrl);
  2139. WARN_ON_ONCE(state != NVME_CTRL_DELETING &&
  2140. state != NVME_CTRL_DELETING_NOIO);
  2141. return;
  2142. }
  2143. ret = nvme_tcp_setup_ctrl(ctrl, false);
  2144. if (ret)
  2145. goto out_fail;
  2146. return;
  2147. out_fail:
  2148. ++ctrl->nr_reconnects;
  2149. nvme_tcp_reconnect_or_remove(ctrl, ret);
  2150. }
  2151. static void nvme_tcp_stop_ctrl(struct nvme_ctrl *ctrl)
  2152. {
  2153. flush_work(&to_tcp_ctrl(ctrl)->err_work);
  2154. cancel_delayed_work_sync(&to_tcp_ctrl(ctrl)->connect_work);
  2155. }
  2156. static void nvme_tcp_free_ctrl(struct nvme_ctrl *nctrl)
  2157. {
  2158. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(nctrl);
  2159. if (list_empty(&ctrl->list))
  2160. goto free_ctrl;
  2161. mutex_lock(&nvme_tcp_ctrl_mutex);
  2162. list_del(&ctrl->list);
  2163. mutex_unlock(&nvme_tcp_ctrl_mutex);
  2164. nvmf_free_options(nctrl->opts);
  2165. free_ctrl:
  2166. kfree(ctrl->queues);
  2167. kfree(ctrl);
  2168. }
  2169. static void nvme_tcp_set_sg_null(struct nvme_command *c)
  2170. {
  2171. struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
  2172. sg->addr = 0;
  2173. sg->length = 0;
  2174. sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
  2175. NVME_SGL_FMT_TRANSPORT_A;
  2176. }
  2177. static void nvme_tcp_set_sg_inline(struct nvme_tcp_queue *queue,
  2178. struct nvme_command *c, u32 data_len)
  2179. {
  2180. struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
  2181. sg->addr = cpu_to_le64(queue->ctrl->ctrl.icdoff);
  2182. sg->length = cpu_to_le32(data_len);
  2183. sg->type = (NVME_SGL_FMT_DATA_DESC << 4) | NVME_SGL_FMT_OFFSET;
  2184. }
  2185. static void nvme_tcp_set_sg_host_data(struct nvme_command *c,
  2186. u32 data_len)
  2187. {
  2188. struct nvme_sgl_desc *sg = &c->common.dptr.sgl;
  2189. sg->addr = 0;
  2190. sg->length = cpu_to_le32(data_len);
  2191. sg->type = (NVME_TRANSPORT_SGL_DATA_DESC << 4) |
  2192. NVME_SGL_FMT_TRANSPORT_A;
  2193. }
  2194. static void nvme_tcp_submit_async_event(struct nvme_ctrl *arg)
  2195. {
  2196. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(arg);
  2197. struct nvme_tcp_queue *queue = &ctrl->queues[0];
  2198. struct nvme_tcp_cmd_pdu *pdu = ctrl->async_req.pdu;
  2199. struct nvme_command *cmd = &pdu->cmd;
  2200. u8 hdgst = nvme_tcp_hdgst_len(queue);
  2201. memset(pdu, 0, sizeof(*pdu));
  2202. pdu->hdr.type = nvme_tcp_cmd;
  2203. if (queue->hdr_digest)
  2204. pdu->hdr.flags |= NVME_TCP_F_HDGST;
  2205. pdu->hdr.hlen = sizeof(*pdu);
  2206. pdu->hdr.plen = cpu_to_le32(pdu->hdr.hlen + hdgst);
  2207. cmd->common.opcode = nvme_admin_async_event;
  2208. cmd->common.command_id = NVME_AQ_BLK_MQ_DEPTH;
  2209. cmd->common.flags |= NVME_CMD_SGL_METABUF;
  2210. nvme_tcp_set_sg_null(cmd);
  2211. ctrl->async_req.state = NVME_TCP_SEND_CMD_PDU;
  2212. ctrl->async_req.offset = 0;
  2213. ctrl->async_req.curr_bio = NULL;
  2214. ctrl->async_req.data_len = 0;
  2215. init_llist_node(&ctrl->async_req.lentry);
  2216. INIT_LIST_HEAD(&ctrl->async_req.entry);
  2217. nvme_tcp_queue_request(&ctrl->async_req, true, true);
  2218. }
  2219. static void nvme_tcp_complete_timed_out(struct request *rq)
  2220. {
  2221. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  2222. struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
  2223. nvme_tcp_stop_queue(ctrl, nvme_tcp_queue_id(req->queue));
  2224. nvmf_complete_timed_out_request(rq);
  2225. }
  2226. static enum blk_eh_timer_return nvme_tcp_timeout(struct request *rq)
  2227. {
  2228. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  2229. struct nvme_ctrl *ctrl = &req->queue->ctrl->ctrl;
  2230. struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
  2231. struct nvme_command *cmd = &pdu->cmd;
  2232. int qid = nvme_tcp_queue_id(req->queue);
  2233. dev_warn(ctrl->device,
  2234. "I/O tag %d (%04x) type %d opcode %#x (%s) QID %d timeout\n",
  2235. rq->tag, nvme_cid(rq), pdu->hdr.type, cmd->common.opcode,
  2236. nvme_fabrics_opcode_str(qid, cmd), qid);
  2237. if (nvme_ctrl_state(ctrl) != NVME_CTRL_LIVE) {
  2238. /*
  2239. * If we are resetting, connecting or deleting we should
  2240. * complete immediately because we may block controller
  2241. * teardown or setup sequence
  2242. * - ctrl disable/shutdown fabrics requests
  2243. * - connect requests
  2244. * - initialization admin requests
  2245. * - I/O requests that entered after unquiescing and
  2246. * the controller stopped responding
  2247. *
  2248. * All other requests should be cancelled by the error
  2249. * recovery work, so it's fine that we fail it here.
  2250. */
  2251. nvme_tcp_complete_timed_out(rq);
  2252. return BLK_EH_DONE;
  2253. }
  2254. /*
  2255. * LIVE state should trigger the normal error recovery which will
  2256. * handle completing this request.
  2257. */
  2258. nvme_tcp_error_recovery(ctrl);
  2259. return BLK_EH_RESET_TIMER;
  2260. }
  2261. static blk_status_t nvme_tcp_map_data(struct nvme_tcp_queue *queue,
  2262. struct request *rq)
  2263. {
  2264. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  2265. struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
  2266. struct nvme_command *c = &pdu->cmd;
  2267. c->common.flags |= NVME_CMD_SGL_METABUF;
  2268. if (!blk_rq_nr_phys_segments(rq))
  2269. nvme_tcp_set_sg_null(c);
  2270. else if (rq_data_dir(rq) == WRITE &&
  2271. req->data_len <= nvme_tcp_inline_data_size(req))
  2272. nvme_tcp_set_sg_inline(queue, c, req->data_len);
  2273. else
  2274. nvme_tcp_set_sg_host_data(c, req->data_len);
  2275. return 0;
  2276. }
  2277. static blk_status_t nvme_tcp_setup_cmd_pdu(struct nvme_ns *ns,
  2278. struct request *rq)
  2279. {
  2280. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  2281. struct nvme_tcp_cmd_pdu *pdu = nvme_tcp_req_cmd_pdu(req);
  2282. struct nvme_tcp_queue *queue = req->queue;
  2283. u8 hdgst = nvme_tcp_hdgst_len(queue), ddgst = 0;
  2284. blk_status_t ret;
  2285. ret = nvme_setup_cmd(ns, rq);
  2286. if (ret)
  2287. return ret;
  2288. req->state = NVME_TCP_SEND_CMD_PDU;
  2289. req->status = cpu_to_le16(NVME_SC_SUCCESS);
  2290. req->offset = 0;
  2291. req->data_sent = 0;
  2292. req->pdu_len = 0;
  2293. req->pdu_sent = 0;
  2294. req->h2cdata_left = 0;
  2295. req->data_len = blk_rq_nr_phys_segments(rq) ?
  2296. blk_rq_payload_bytes(rq) : 0;
  2297. req->curr_bio = rq->bio;
  2298. if (req->curr_bio && req->data_len)
  2299. nvme_tcp_init_iter(req, rq_data_dir(rq));
  2300. if (rq_data_dir(rq) == WRITE &&
  2301. req->data_len <= nvme_tcp_inline_data_size(req))
  2302. req->pdu_len = req->data_len;
  2303. pdu->hdr.type = nvme_tcp_cmd;
  2304. pdu->hdr.flags = 0;
  2305. if (queue->hdr_digest)
  2306. pdu->hdr.flags |= NVME_TCP_F_HDGST;
  2307. if (queue->data_digest && req->pdu_len) {
  2308. pdu->hdr.flags |= NVME_TCP_F_DDGST;
  2309. ddgst = nvme_tcp_ddgst_len(queue);
  2310. }
  2311. pdu->hdr.hlen = sizeof(*pdu);
  2312. pdu->hdr.pdo = req->pdu_len ? pdu->hdr.hlen + hdgst : 0;
  2313. pdu->hdr.plen =
  2314. cpu_to_le32(pdu->hdr.hlen + hdgst + req->pdu_len + ddgst);
  2315. ret = nvme_tcp_map_data(queue, rq);
  2316. if (unlikely(ret)) {
  2317. nvme_cleanup_cmd(rq);
  2318. dev_err(queue->ctrl->ctrl.device,
  2319. "Failed to map data (%d)\n", ret);
  2320. return ret;
  2321. }
  2322. return 0;
  2323. }
  2324. static void nvme_tcp_commit_rqs(struct blk_mq_hw_ctx *hctx)
  2325. {
  2326. struct nvme_tcp_queue *queue = hctx->driver_data;
  2327. if (!llist_empty(&queue->req_list))
  2328. queue_work_on(queue->io_cpu, nvme_tcp_wq, &queue->io_work);
  2329. }
  2330. static blk_status_t nvme_tcp_queue_rq(struct blk_mq_hw_ctx *hctx,
  2331. const struct blk_mq_queue_data *bd)
  2332. {
  2333. struct nvme_ns *ns = hctx->queue->queuedata;
  2334. struct nvme_tcp_queue *queue = hctx->driver_data;
  2335. struct request *rq = bd->rq;
  2336. struct nvme_tcp_request *req = blk_mq_rq_to_pdu(rq);
  2337. bool queue_ready = test_bit(NVME_TCP_Q_LIVE, &queue->flags);
  2338. blk_status_t ret;
  2339. if (!nvme_check_ready(&queue->ctrl->ctrl, rq, queue_ready))
  2340. return nvme_fail_nonready_command(&queue->ctrl->ctrl, rq);
  2341. ret = nvme_tcp_setup_cmd_pdu(ns, rq);
  2342. if (unlikely(ret))
  2343. return ret;
  2344. nvme_start_request(rq);
  2345. nvme_tcp_queue_request(req, true, bd->last);
  2346. return BLK_STS_OK;
  2347. }
  2348. static void nvme_tcp_map_queues(struct blk_mq_tag_set *set)
  2349. {
  2350. struct nvme_tcp_ctrl *ctrl = to_tcp_ctrl(set->driver_data);
  2351. nvmf_map_queues(set, &ctrl->ctrl, ctrl->io_queues);
  2352. }
  2353. static int nvme_tcp_poll(struct blk_mq_hw_ctx *hctx, struct io_comp_batch *iob)
  2354. {
  2355. struct nvme_tcp_queue *queue = hctx->driver_data;
  2356. struct sock *sk = queue->sock->sk;
  2357. int ret;
  2358. if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
  2359. return 0;
  2360. set_bit(NVME_TCP_Q_POLLING, &queue->flags);
  2361. if (sk_can_busy_loop(sk) && skb_queue_empty_lockless(&sk->sk_receive_queue))
  2362. sk_busy_loop(sk, true);
  2363. ret = nvme_tcp_try_recv(queue);
  2364. clear_bit(NVME_TCP_Q_POLLING, &queue->flags);
  2365. return ret < 0 ? ret : queue->nr_cqe;
  2366. }
  2367. static int nvme_tcp_get_address(struct nvme_ctrl *ctrl, char *buf, int size)
  2368. {
  2369. struct nvme_tcp_queue *queue = &to_tcp_ctrl(ctrl)->queues[0];
  2370. struct sockaddr_storage src_addr;
  2371. int ret, len;
  2372. len = nvmf_get_address(ctrl, buf, size);
  2373. if (!test_bit(NVME_TCP_Q_LIVE, &queue->flags))
  2374. return len;
  2375. mutex_lock(&queue->queue_lock);
  2376. ret = kernel_getsockname(queue->sock, (struct sockaddr *)&src_addr);
  2377. if (ret > 0) {
  2378. if (len > 0)
  2379. len--; /* strip trailing newline */
  2380. len += scnprintf(buf + len, size - len, "%ssrc_addr=%pISc\n",
  2381. (len) ? "," : "", &src_addr);
  2382. }
  2383. mutex_unlock(&queue->queue_lock);
  2384. return len;
  2385. }
  2386. static const struct blk_mq_ops nvme_tcp_mq_ops = {
  2387. .queue_rq = nvme_tcp_queue_rq,
  2388. .commit_rqs = nvme_tcp_commit_rqs,
  2389. .complete = nvme_complete_rq,
  2390. .init_request = nvme_tcp_init_request,
  2391. .exit_request = nvme_tcp_exit_request,
  2392. .init_hctx = nvme_tcp_init_hctx,
  2393. .timeout = nvme_tcp_timeout,
  2394. .map_queues = nvme_tcp_map_queues,
  2395. .poll = nvme_tcp_poll,
  2396. };
  2397. static const struct blk_mq_ops nvme_tcp_admin_mq_ops = {
  2398. .queue_rq = nvme_tcp_queue_rq,
  2399. .complete = nvme_complete_rq,
  2400. .init_request = nvme_tcp_init_request,
  2401. .exit_request = nvme_tcp_exit_request,
  2402. .init_hctx = nvme_tcp_init_admin_hctx,
  2403. .timeout = nvme_tcp_timeout,
  2404. };
  2405. static const struct nvme_ctrl_ops nvme_tcp_ctrl_ops = {
  2406. .name = "tcp",
  2407. .module = THIS_MODULE,
  2408. .flags = NVME_F_FABRICS | NVME_F_BLOCKING,
  2409. .reg_read32 = nvmf_reg_read32,
  2410. .reg_read64 = nvmf_reg_read64,
  2411. .reg_write32 = nvmf_reg_write32,
  2412. .subsystem_reset = nvmf_subsystem_reset,
  2413. .free_ctrl = nvme_tcp_free_ctrl,
  2414. .submit_async_event = nvme_tcp_submit_async_event,
  2415. .delete_ctrl = nvme_tcp_delete_ctrl,
  2416. .get_address = nvme_tcp_get_address,
  2417. .stop_ctrl = nvme_tcp_stop_ctrl,
  2418. };
  2419. static bool
  2420. nvme_tcp_existing_controller(struct nvmf_ctrl_options *opts)
  2421. {
  2422. struct nvme_tcp_ctrl *ctrl;
  2423. bool found = false;
  2424. mutex_lock(&nvme_tcp_ctrl_mutex);
  2425. list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list) {
  2426. found = nvmf_ip_options_match(&ctrl->ctrl, opts);
  2427. if (found)
  2428. break;
  2429. }
  2430. mutex_unlock(&nvme_tcp_ctrl_mutex);
  2431. return found;
  2432. }
  2433. static struct nvme_tcp_ctrl *nvme_tcp_alloc_ctrl(struct device *dev,
  2434. struct nvmf_ctrl_options *opts)
  2435. {
  2436. struct nvme_tcp_ctrl *ctrl;
  2437. int ret;
  2438. ctrl = kzalloc(sizeof(*ctrl), GFP_KERNEL);
  2439. if (!ctrl)
  2440. return ERR_PTR(-ENOMEM);
  2441. INIT_LIST_HEAD(&ctrl->list);
  2442. ctrl->ctrl.opts = opts;
  2443. ctrl->ctrl.queue_count = opts->nr_io_queues + opts->nr_write_queues +
  2444. opts->nr_poll_queues + 1;
  2445. ctrl->ctrl.sqsize = opts->queue_size - 1;
  2446. ctrl->ctrl.kato = opts->kato;
  2447. INIT_DELAYED_WORK(&ctrl->connect_work,
  2448. nvme_tcp_reconnect_ctrl_work);
  2449. INIT_WORK(&ctrl->err_work, nvme_tcp_error_recovery_work);
  2450. INIT_WORK(&ctrl->ctrl.reset_work, nvme_reset_ctrl_work);
  2451. if (!(opts->mask & NVMF_OPT_TRSVCID)) {
  2452. opts->trsvcid =
  2453. kstrdup(__stringify(NVME_TCP_DISC_PORT), GFP_KERNEL);
  2454. if (!opts->trsvcid) {
  2455. ret = -ENOMEM;
  2456. goto out_free_ctrl;
  2457. }
  2458. opts->mask |= NVMF_OPT_TRSVCID;
  2459. }
  2460. ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
  2461. opts->traddr, opts->trsvcid, &ctrl->addr);
  2462. if (ret) {
  2463. pr_err("malformed address passed: %s:%s\n",
  2464. opts->traddr, opts->trsvcid);
  2465. goto out_free_ctrl;
  2466. }
  2467. if (opts->mask & NVMF_OPT_HOST_TRADDR) {
  2468. ret = inet_pton_with_scope(&init_net, AF_UNSPEC,
  2469. opts->host_traddr, NULL, &ctrl->src_addr);
  2470. if (ret) {
  2471. pr_err("malformed src address passed: %s\n",
  2472. opts->host_traddr);
  2473. goto out_free_ctrl;
  2474. }
  2475. }
  2476. if (opts->mask & NVMF_OPT_HOST_IFACE) {
  2477. if (!__dev_get_by_name(&init_net, opts->host_iface)) {
  2478. pr_err("invalid interface passed: %s\n",
  2479. opts->host_iface);
  2480. ret = -ENODEV;
  2481. goto out_free_ctrl;
  2482. }
  2483. }
  2484. if (!opts->duplicate_connect && nvme_tcp_existing_controller(opts)) {
  2485. ret = -EALREADY;
  2486. goto out_free_ctrl;
  2487. }
  2488. ctrl->queues = kcalloc(ctrl->ctrl.queue_count, sizeof(*ctrl->queues),
  2489. GFP_KERNEL);
  2490. if (!ctrl->queues) {
  2491. ret = -ENOMEM;
  2492. goto out_free_ctrl;
  2493. }
  2494. ret = nvme_init_ctrl(&ctrl->ctrl, dev, &nvme_tcp_ctrl_ops, 0);
  2495. if (ret)
  2496. goto out_kfree_queues;
  2497. return ctrl;
  2498. out_kfree_queues:
  2499. kfree(ctrl->queues);
  2500. out_free_ctrl:
  2501. kfree(ctrl);
  2502. return ERR_PTR(ret);
  2503. }
  2504. static struct nvme_ctrl *nvme_tcp_create_ctrl(struct device *dev,
  2505. struct nvmf_ctrl_options *opts)
  2506. {
  2507. struct nvme_tcp_ctrl *ctrl;
  2508. int ret;
  2509. ctrl = nvme_tcp_alloc_ctrl(dev, opts);
  2510. if (IS_ERR(ctrl))
  2511. return ERR_CAST(ctrl);
  2512. ret = nvme_add_ctrl(&ctrl->ctrl);
  2513. if (ret)
  2514. goto out_put_ctrl;
  2515. if (!nvme_change_ctrl_state(&ctrl->ctrl, NVME_CTRL_CONNECTING)) {
  2516. WARN_ON_ONCE(1);
  2517. ret = -EINTR;
  2518. goto out_uninit_ctrl;
  2519. }
  2520. ret = nvme_tcp_setup_ctrl(&ctrl->ctrl, true);
  2521. if (ret)
  2522. goto out_uninit_ctrl;
  2523. dev_info(ctrl->ctrl.device, "new ctrl: NQN \"%s\", addr %pISp, hostnqn: %s\n",
  2524. nvmf_ctrl_subsysnqn(&ctrl->ctrl), &ctrl->addr, opts->host->nqn);
  2525. mutex_lock(&nvme_tcp_ctrl_mutex);
  2526. list_add_tail(&ctrl->list, &nvme_tcp_ctrl_list);
  2527. mutex_unlock(&nvme_tcp_ctrl_mutex);
  2528. return &ctrl->ctrl;
  2529. out_uninit_ctrl:
  2530. nvme_uninit_ctrl(&ctrl->ctrl);
  2531. out_put_ctrl:
  2532. nvme_put_ctrl(&ctrl->ctrl);
  2533. if (ret > 0)
  2534. ret = -EIO;
  2535. return ERR_PTR(ret);
  2536. }
  2537. static struct nvmf_transport_ops nvme_tcp_transport = {
  2538. .name = "tcp",
  2539. .module = THIS_MODULE,
  2540. .required_opts = NVMF_OPT_TRADDR,
  2541. .allowed_opts = NVMF_OPT_TRSVCID | NVMF_OPT_RECONNECT_DELAY |
  2542. NVMF_OPT_HOST_TRADDR | NVMF_OPT_CTRL_LOSS_TMO |
  2543. NVMF_OPT_HDR_DIGEST | NVMF_OPT_DATA_DIGEST |
  2544. NVMF_OPT_NR_WRITE_QUEUES | NVMF_OPT_NR_POLL_QUEUES |
  2545. NVMF_OPT_TOS | NVMF_OPT_HOST_IFACE | NVMF_OPT_TLS |
  2546. NVMF_OPT_KEYRING | NVMF_OPT_TLS_KEY,
  2547. .create_ctrl = nvme_tcp_create_ctrl,
  2548. };
  2549. static int __init nvme_tcp_init_module(void)
  2550. {
  2551. unsigned int wq_flags = WQ_MEM_RECLAIM | WQ_HIGHPRI | WQ_SYSFS;
  2552. int cpu;
  2553. BUILD_BUG_ON(sizeof(struct nvme_tcp_hdr) != 8);
  2554. BUILD_BUG_ON(sizeof(struct nvme_tcp_cmd_pdu) != 72);
  2555. BUILD_BUG_ON(sizeof(struct nvme_tcp_data_pdu) != 24);
  2556. BUILD_BUG_ON(sizeof(struct nvme_tcp_rsp_pdu) != 24);
  2557. BUILD_BUG_ON(sizeof(struct nvme_tcp_r2t_pdu) != 24);
  2558. BUILD_BUG_ON(sizeof(struct nvme_tcp_icreq_pdu) != 128);
  2559. BUILD_BUG_ON(sizeof(struct nvme_tcp_icresp_pdu) != 128);
  2560. BUILD_BUG_ON(sizeof(struct nvme_tcp_term_pdu) != 24);
  2561. if (wq_unbound)
  2562. wq_flags |= WQ_UNBOUND;
  2563. nvme_tcp_wq = alloc_workqueue("nvme_tcp_wq", wq_flags, 0);
  2564. if (!nvme_tcp_wq)
  2565. return -ENOMEM;
  2566. for_each_possible_cpu(cpu)
  2567. atomic_set(&nvme_tcp_cpu_queues[cpu], 0);
  2568. nvmf_register_transport(&nvme_tcp_transport);
  2569. return 0;
  2570. }
  2571. static void __exit nvme_tcp_cleanup_module(void)
  2572. {
  2573. struct nvme_tcp_ctrl *ctrl;
  2574. nvmf_unregister_transport(&nvme_tcp_transport);
  2575. mutex_lock(&nvme_tcp_ctrl_mutex);
  2576. list_for_each_entry(ctrl, &nvme_tcp_ctrl_list, list)
  2577. nvme_delete_ctrl(&ctrl->ctrl);
  2578. mutex_unlock(&nvme_tcp_ctrl_mutex);
  2579. flush_workqueue(nvme_delete_wq);
  2580. destroy_workqueue(nvme_tcp_wq);
  2581. }
  2582. module_init(nvme_tcp_init_module);
  2583. module_exit(nvme_tcp_cleanup_module);
  2584. MODULE_DESCRIPTION("NVMe host TCP transport driver");
  2585. MODULE_LICENSE("GPL v2");