iscsi_iser.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083
  1. /*
  2. * iSCSI Initiator over iSER Data-Path
  3. *
  4. * Copyright (C) 2004 Dmitry Yusupov
  5. * Copyright (C) 2004 Alex Aizman
  6. * Copyright (C) 2005 Mike Christie
  7. * Copyright (c) 2005, 2006 Voltaire, Inc. All rights reserved.
  8. * Copyright (c) 2013-2014 Mellanox Technologies. All rights reserved.
  9. * maintained by openib-general@openib.org
  10. *
  11. * This software is available to you under a choice of one of two
  12. * licenses. You may choose to be licensed under the terms of the GNU
  13. * General Public License (GPL) Version 2, available from the file
  14. * COPYING in the main directory of this source tree, or the
  15. * OpenIB.org BSD license below:
  16. *
  17. * Redistribution and use in source and binary forms, with or
  18. * without modification, are permitted provided that the following
  19. * conditions are met:
  20. *
  21. * - Redistributions of source code must retain the above
  22. * copyright notice, this list of conditions and the following
  23. * disclaimer.
  24. *
  25. * - Redistributions in binary form must reproduce the above
  26. * copyright notice, this list of conditions and the following
  27. * disclaimer in the documentation and/or other materials
  28. * provided with the distribution.
  29. *
  30. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  31. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  32. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  33. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  34. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  35. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  36. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  37. * SOFTWARE.
  38. *
  39. * Credits:
  40. * Christoph Hellwig
  41. * FUJITA Tomonori
  42. * Arne Redlich
  43. * Zhenyu Wang
  44. * Modified by:
  45. * Erez Zilber
  46. */
  47. #include <linux/types.h>
  48. #include <linux/list.h>
  49. #include <linux/hardirq.h>
  50. #include <linux/kfifo.h>
  51. #include <linux/blkdev.h>
  52. #include <linux/init.h>
  53. #include <linux/ioctl.h>
  54. #include <linux/cdev.h>
  55. #include <linux/in.h>
  56. #include <linux/net.h>
  57. #include <linux/scatterlist.h>
  58. #include <linux/delay.h>
  59. #include <linux/slab.h>
  60. #include <linux/module.h>
  61. #include <net/sock.h>
  62. #include <linux/uaccess.h>
  63. #include <scsi/scsi_cmnd.h>
  64. #include <scsi/scsi_device.h>
  65. #include <scsi/scsi_eh.h>
  66. #include <scsi/scsi_tcq.h>
  67. #include <scsi/scsi_host.h>
  68. #include <scsi/scsi.h>
  69. #include <scsi/scsi_transport_iscsi.h>
  70. #include "iscsi_iser.h"
  71. MODULE_DESCRIPTION("iSER (iSCSI Extensions for RDMA) Datamover");
  72. MODULE_LICENSE("Dual BSD/GPL");
  73. MODULE_AUTHOR("Alex Nezhinsky, Dan Bar Dov, Or Gerlitz");
  74. static const struct scsi_host_template iscsi_iser_sht;
  75. static struct iscsi_transport iscsi_iser_transport;
  76. static struct scsi_transport_template *iscsi_iser_scsi_transport;
  77. static struct workqueue_struct *release_wq;
  78. static DEFINE_MUTEX(unbind_iser_conn_mutex);
  79. struct iser_global ig;
  80. int iser_debug_level = 0;
  81. module_param_named(debug_level, iser_debug_level, int, S_IRUGO | S_IWUSR);
  82. MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0 (default:disabled)");
  83. static int iscsi_iser_set(const char *val, const struct kernel_param *kp);
  84. static const struct kernel_param_ops iscsi_iser_size_ops = {
  85. .set = iscsi_iser_set,
  86. .get = param_get_uint,
  87. };
  88. static unsigned int iscsi_max_lun = 512;
  89. module_param_cb(max_lun, &iscsi_iser_size_ops, &iscsi_max_lun, S_IRUGO);
  90. MODULE_PARM_DESC(max_lun, "Max LUNs to allow per session, should > 0 (default:512)");
  91. unsigned int iser_max_sectors = ISER_DEF_MAX_SECTORS;
  92. module_param_cb(max_sectors, &iscsi_iser_size_ops, &iser_max_sectors,
  93. S_IRUGO | S_IWUSR);
  94. MODULE_PARM_DESC(max_sectors, "Max number of sectors in a single scsi command, should > 0 (default:1024)");
  95. bool iser_always_reg = true;
  96. module_param_named(always_register, iser_always_reg, bool, S_IRUGO);
  97. MODULE_PARM_DESC(always_register,
  98. "Always register memory, even for continuous memory regions (default:true)");
  99. bool iser_pi_enable = false;
  100. module_param_named(pi_enable, iser_pi_enable, bool, S_IRUGO);
  101. MODULE_PARM_DESC(pi_enable, "Enable T10-PI offload support (default:disabled)");
  102. static int iscsi_iser_set(const char *val, const struct kernel_param *kp)
  103. {
  104. int ret;
  105. unsigned int n = 0;
  106. ret = kstrtouint(val, 10, &n);
  107. if (ret != 0 || n == 0)
  108. return -EINVAL;
  109. return param_set_uint(val, kp);
  110. }
  111. /*
  112. * iscsi_iser_recv() - Process a successful recv completion
  113. * @conn: iscsi connection
  114. * @hdr: iscsi header
  115. * @rx_data: buffer containing receive data payload
  116. * @rx_data_len: length of rx_data
  117. *
  118. * Notes: In case of data length errors or iscsi PDU completion failures
  119. * this routine will signal iscsi layer of connection failure.
  120. */
  121. void iscsi_iser_recv(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  122. char *rx_data, int rx_data_len)
  123. {
  124. int rc = 0;
  125. int datalen;
  126. /* verify PDU length */
  127. datalen = ntoh24(hdr->dlength);
  128. if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
  129. iser_err("wrong datalen %d (hdr), %d (IB)\n",
  130. datalen, rx_data_len);
  131. rc = ISCSI_ERR_DATALEN;
  132. goto error;
  133. }
  134. if (datalen != rx_data_len)
  135. iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
  136. datalen, rx_data_len);
  137. rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
  138. if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
  139. goto error;
  140. return;
  141. error:
  142. iscsi_conn_failure(conn, rc);
  143. }
  144. /**
  145. * iscsi_iser_pdu_alloc() - allocate an iscsi-iser PDU
  146. * @task: iscsi task
  147. * @opcode: iscsi command opcode
  148. *
  149. * Netes: This routine can't fail, just assign iscsi task
  150. * hdr and max hdr size.
  151. */
  152. static int iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
  153. {
  154. struct iscsi_iser_task *iser_task = task->dd_data;
  155. task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
  156. task->hdr_max = sizeof(iser_task->desc.iscsi_header);
  157. return 0;
  158. }
  159. /**
  160. * iser_initialize_task_headers() - Initialize task headers
  161. * @task: iscsi task
  162. * @tx_desc: iser tx descriptor
  163. *
  164. * Notes:
  165. * This routine may race with iser teardown flow for scsi
  166. * error handling TMFs. So for TMF we should acquire the
  167. * state mutex to avoid dereferencing the IB device which
  168. * may have already been terminated.
  169. */
  170. int iser_initialize_task_headers(struct iscsi_task *task,
  171. struct iser_tx_desc *tx_desc)
  172. {
  173. struct iser_conn *iser_conn = task->conn->dd_data;
  174. struct iser_device *device = iser_conn->ib_conn.device;
  175. struct iscsi_iser_task *iser_task = task->dd_data;
  176. u64 dma_addr;
  177. if (unlikely(iser_conn->state != ISER_CONN_UP))
  178. return -ENODEV;
  179. dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
  180. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  181. if (ib_dma_mapping_error(device->ib_device, dma_addr))
  182. return -ENOMEM;
  183. tx_desc->inv_wr.next = NULL;
  184. tx_desc->reg_wr.wr.next = NULL;
  185. tx_desc->mapped = true;
  186. tx_desc->dma_addr = dma_addr;
  187. tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
  188. tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
  189. tx_desc->tx_sg[0].lkey = device->pd->local_dma_lkey;
  190. iser_task->iser_conn = iser_conn;
  191. return 0;
  192. }
  193. /**
  194. * iscsi_iser_task_init() - Initialize iscsi-iser task
  195. * @task: iscsi task
  196. *
  197. * Initialize the task for the scsi command or mgmt command.
  198. *
  199. * Return: Returns zero on success or -ENOMEM when failing
  200. * to init task headers (dma mapping error).
  201. */
  202. static int iscsi_iser_task_init(struct iscsi_task *task)
  203. {
  204. struct iscsi_iser_task *iser_task = task->dd_data;
  205. int ret;
  206. ret = iser_initialize_task_headers(task, &iser_task->desc);
  207. if (ret) {
  208. iser_err("Failed to init task %p, err = %d\n",
  209. iser_task, ret);
  210. return ret;
  211. }
  212. /* mgmt task */
  213. if (!task->sc)
  214. return 0;
  215. iser_task->command_sent = 0;
  216. iser_task_rdma_init(iser_task);
  217. iser_task->sc = task->sc;
  218. return 0;
  219. }
  220. /**
  221. * iscsi_iser_mtask_xmit() - xmit management (immediate) task
  222. * @conn: iscsi connection
  223. * @task: task management task
  224. *
  225. * Notes:
  226. * The function can return -EAGAIN in which case caller must
  227. * call it again later, or recover. '0' return code means successful
  228. * xmit.
  229. *
  230. **/
  231. static int iscsi_iser_mtask_xmit(struct iscsi_conn *conn,
  232. struct iscsi_task *task)
  233. {
  234. iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
  235. /* since iser xmits control with zero copy, tasks can not be recycled
  236. * right after sending them.
  237. * The recycling scheme is based on whether a response is expected
  238. * - if yes, the task is recycled at iscsi_complete_pdu
  239. * - if no, the task is recycled at iser_snd_completion
  240. */
  241. return iser_send_control(conn, task);
  242. }
  243. static int iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
  244. struct iscsi_task *task)
  245. {
  246. struct iscsi_r2t_info *r2t = &task->unsol_r2t;
  247. struct iscsi_data hdr;
  248. int error = 0;
  249. /* Send data-out PDUs while there's still unsolicited data to send */
  250. while (iscsi_task_has_unsol_data(task)) {
  251. iscsi_prep_data_out_pdu(task, r2t, &hdr);
  252. iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
  253. hdr.itt, r2t->data_count);
  254. /* the buffer description has been passed with the command */
  255. /* Send the command */
  256. error = iser_send_data_out(conn, task, &hdr);
  257. if (error) {
  258. r2t->datasn--;
  259. goto iscsi_iser_task_xmit_unsol_data_exit;
  260. }
  261. r2t->sent += r2t->data_count;
  262. iser_dbg("Need to send %d more as data-out PDUs\n",
  263. r2t->data_length - r2t->sent);
  264. }
  265. iscsi_iser_task_xmit_unsol_data_exit:
  266. return error;
  267. }
  268. /**
  269. * iscsi_iser_task_xmit() - xmit iscsi-iser task
  270. * @task: iscsi task
  271. *
  272. * Return: zero on success or escalates $error on failure.
  273. */
  274. static int iscsi_iser_task_xmit(struct iscsi_task *task)
  275. {
  276. struct iscsi_conn *conn = task->conn;
  277. struct iscsi_iser_task *iser_task = task->dd_data;
  278. int error = 0;
  279. if (!task->sc)
  280. return iscsi_iser_mtask_xmit(conn, task);
  281. if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
  282. BUG_ON(scsi_bufflen(task->sc) == 0);
  283. iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
  284. task->itt, scsi_bufflen(task->sc),
  285. task->imm_count, task->unsol_r2t.data_length);
  286. }
  287. iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
  288. conn->id, task->itt);
  289. /* Send the cmd PDU */
  290. if (!iser_task->command_sent) {
  291. error = iser_send_command(conn, task);
  292. if (error)
  293. goto iscsi_iser_task_xmit_exit;
  294. iser_task->command_sent = 1;
  295. }
  296. /* Send unsolicited data-out PDU(s) if necessary */
  297. if (iscsi_task_has_unsol_data(task))
  298. error = iscsi_iser_task_xmit_unsol_data(conn, task);
  299. iscsi_iser_task_xmit_exit:
  300. return error;
  301. }
  302. /**
  303. * iscsi_iser_cleanup_task() - cleanup an iscsi-iser task
  304. * @task: iscsi task
  305. *
  306. * Notes: In case the RDMA device is already NULL (might have
  307. * been removed in DEVICE_REMOVAL CM event it will bail-out
  308. * without doing dma unmapping.
  309. */
  310. static void iscsi_iser_cleanup_task(struct iscsi_task *task)
  311. {
  312. struct iscsi_iser_task *iser_task = task->dd_data;
  313. struct iser_tx_desc *tx_desc = &iser_task->desc;
  314. struct iser_conn *iser_conn = task->conn->dd_data;
  315. struct iser_device *device = iser_conn->ib_conn.device;
  316. /* DEVICE_REMOVAL event might have already released the device */
  317. if (!device)
  318. return;
  319. if (likely(tx_desc->mapped)) {
  320. ib_dma_unmap_single(device->ib_device, tx_desc->dma_addr,
  321. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  322. tx_desc->mapped = false;
  323. }
  324. /* mgmt tasks do not need special cleanup */
  325. if (!task->sc)
  326. return;
  327. if (iser_task->status == ISER_TASK_STATUS_STARTED) {
  328. iser_task->status = ISER_TASK_STATUS_COMPLETED;
  329. iser_task_rdma_finalize(iser_task);
  330. }
  331. }
  332. /**
  333. * iscsi_iser_check_protection() - check protection information status of task.
  334. * @task: iscsi task
  335. * @sector: error sector if exsists (output)
  336. *
  337. * Return: zero if no data-integrity errors have occurred
  338. * 0x1: data-integrity error occurred in the guard-block
  339. * 0x2: data-integrity error occurred in the reference tag
  340. * 0x3: data-integrity error occurred in the application tag
  341. *
  342. * In addition the error sector is marked.
  343. */
  344. static u8 iscsi_iser_check_protection(struct iscsi_task *task, sector_t *sector)
  345. {
  346. struct iscsi_iser_task *iser_task = task->dd_data;
  347. enum iser_data_dir dir = iser_task->dir[ISER_DIR_IN] ?
  348. ISER_DIR_IN : ISER_DIR_OUT;
  349. return iser_check_task_pi_status(iser_task, dir, sector);
  350. }
  351. /**
  352. * iscsi_iser_conn_create() - create a new iscsi-iser connection
  353. * @cls_session: iscsi class connection
  354. * @conn_idx: connection index within the session (for MCS)
  355. *
  356. * Return: iscsi_cls_conn when iscsi_conn_setup succeeds or NULL
  357. * otherwise.
  358. */
  359. static struct iscsi_cls_conn *
  360. iscsi_iser_conn_create(struct iscsi_cls_session *cls_session,
  361. uint32_t conn_idx)
  362. {
  363. struct iscsi_conn *conn;
  364. struct iscsi_cls_conn *cls_conn;
  365. cls_conn = iscsi_conn_setup(cls_session, 0, conn_idx);
  366. if (!cls_conn)
  367. return NULL;
  368. conn = cls_conn->dd_data;
  369. /*
  370. * due to issues with the login code re iser sematics
  371. * this not set in iscsi_conn_setup - FIXME
  372. */
  373. conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
  374. return cls_conn;
  375. }
  376. /**
  377. * iscsi_iser_conn_bind() - bind iscsi and iser connection structures
  378. * @cls_session: iscsi class session
  379. * @cls_conn: iscsi class connection
  380. * @transport_eph: transport end-point handle
  381. * @is_leading: indicate if this is the session leading connection (MCS)
  382. *
  383. * Return: zero on success, $error if iscsi_conn_bind fails and
  384. * -EINVAL in case end-point doesn't exists anymore or iser connection
  385. * state is not UP (teardown already started).
  386. */
  387. static int iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
  388. struct iscsi_cls_conn *cls_conn,
  389. uint64_t transport_eph, int is_leading)
  390. {
  391. struct iscsi_conn *conn = cls_conn->dd_data;
  392. struct iser_conn *iser_conn;
  393. struct iscsi_endpoint *ep;
  394. int error;
  395. error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  396. if (error)
  397. return error;
  398. /* the transport ep handle comes from user space so it must be
  399. * verified against the global ib connections list */
  400. ep = iscsi_lookup_endpoint(transport_eph);
  401. if (!ep) {
  402. iser_err("can't bind eph %llx\n",
  403. (unsigned long long)transport_eph);
  404. return -EINVAL;
  405. }
  406. iser_conn = ep->dd_data;
  407. mutex_lock(&iser_conn->state_mutex);
  408. if (iser_conn->state != ISER_CONN_UP) {
  409. error = -EINVAL;
  410. iser_err("iser_conn %p state is %d, teardown started\n",
  411. iser_conn, iser_conn->state);
  412. goto out;
  413. }
  414. error = iser_alloc_rx_descriptors(iser_conn, conn->session);
  415. if (error)
  416. goto out;
  417. /* binds the iSER connection retrieved from the previously
  418. * connected ep_handle to the iSCSI layer connection. exchanges
  419. * connection pointers */
  420. iser_info("binding iscsi conn %p to iser_conn %p\n", conn, iser_conn);
  421. conn->dd_data = iser_conn;
  422. iser_conn->iscsi_conn = conn;
  423. out:
  424. iscsi_put_endpoint(ep);
  425. mutex_unlock(&iser_conn->state_mutex);
  426. return error;
  427. }
  428. /**
  429. * iscsi_iser_conn_start() - start iscsi-iser connection
  430. * @cls_conn: iscsi class connection
  431. *
  432. * Notes: Here iser intialize (or re-initialize) stop_completion as
  433. * from this point iscsi must call conn_stop in session/connection
  434. * teardown so iser transport must wait for it.
  435. */
  436. static int iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
  437. {
  438. struct iscsi_conn *iscsi_conn;
  439. struct iser_conn *iser_conn;
  440. iscsi_conn = cls_conn->dd_data;
  441. iser_conn = iscsi_conn->dd_data;
  442. reinit_completion(&iser_conn->stop_completion);
  443. return iscsi_conn_start(cls_conn);
  444. }
  445. /**
  446. * iscsi_iser_conn_stop() - stop iscsi-iser connection
  447. * @cls_conn: iscsi class connection
  448. * @flag: indicate if recover or terminate (passed as is)
  449. *
  450. * Notes: Calling iscsi_conn_stop might theoretically race with
  451. * DEVICE_REMOVAL event and dereference a previously freed RDMA device
  452. * handle, so we call it under iser the state lock to protect against
  453. * this kind of race.
  454. */
  455. static void iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  456. {
  457. struct iscsi_conn *conn = cls_conn->dd_data;
  458. struct iser_conn *iser_conn = conn->dd_data;
  459. iser_info("stopping iscsi_conn: %p, iser_conn: %p\n", conn, iser_conn);
  460. /*
  461. * Userspace may have goofed up and not bound the connection or
  462. * might have only partially setup the connection.
  463. */
  464. if (iser_conn) {
  465. mutex_lock(&iser_conn->state_mutex);
  466. mutex_lock(&unbind_iser_conn_mutex);
  467. iser_conn_terminate(iser_conn);
  468. iscsi_conn_stop(cls_conn, flag);
  469. /* unbind */
  470. iser_conn->iscsi_conn = NULL;
  471. conn->dd_data = NULL;
  472. mutex_unlock(&unbind_iser_conn_mutex);
  473. complete(&iser_conn->stop_completion);
  474. mutex_unlock(&iser_conn->state_mutex);
  475. } else {
  476. iscsi_conn_stop(cls_conn, flag);
  477. }
  478. }
  479. /**
  480. * iscsi_iser_session_destroy() - destroy iscsi-iser session
  481. * @cls_session: iscsi class session
  482. *
  483. * Removes and free iscsi host.
  484. */
  485. static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
  486. {
  487. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  488. iscsi_session_teardown(cls_session);
  489. iscsi_host_remove(shost, false);
  490. iscsi_host_free(shost);
  491. }
  492. static inline unsigned int iser_dif_prot_caps(int prot_caps)
  493. {
  494. int ret = 0;
  495. if (prot_caps & IB_PROT_T10DIF_TYPE_1)
  496. ret |= SHOST_DIF_TYPE1_PROTECTION |
  497. SHOST_DIX_TYPE0_PROTECTION |
  498. SHOST_DIX_TYPE1_PROTECTION;
  499. if (prot_caps & IB_PROT_T10DIF_TYPE_2)
  500. ret |= SHOST_DIF_TYPE2_PROTECTION |
  501. SHOST_DIX_TYPE2_PROTECTION;
  502. if (prot_caps & IB_PROT_T10DIF_TYPE_3)
  503. ret |= SHOST_DIF_TYPE3_PROTECTION |
  504. SHOST_DIX_TYPE3_PROTECTION;
  505. return ret;
  506. }
  507. /**
  508. * iscsi_iser_session_create() - create an iscsi-iser session
  509. * @ep: iscsi end-point handle
  510. * @cmds_max: maximum commands in this session
  511. * @qdepth: session command queue depth
  512. * @initial_cmdsn: initiator command sequnce number
  513. *
  514. * Allocates and adds a scsi host, expose DIF supprot if
  515. * exists, and sets up an iscsi session.
  516. */
  517. static struct iscsi_cls_session *
  518. iscsi_iser_session_create(struct iscsi_endpoint *ep,
  519. uint16_t cmds_max, uint16_t qdepth,
  520. uint32_t initial_cmdsn)
  521. {
  522. struct iscsi_cls_session *cls_session;
  523. struct Scsi_Host *shost;
  524. struct iser_conn *iser_conn = NULL;
  525. struct ib_conn *ib_conn;
  526. struct ib_device *ib_dev;
  527. u32 max_fr_sectors;
  528. shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
  529. if (!shost)
  530. return NULL;
  531. shost->transportt = iscsi_iser_scsi_transport;
  532. shost->cmd_per_lun = qdepth;
  533. shost->max_lun = iscsi_max_lun;
  534. shost->max_id = 0;
  535. shost->max_channel = 0;
  536. shost->max_cmd_len = 16;
  537. /*
  538. * older userspace tools (before 2.0-870) did not pass us
  539. * the leading conn's ep so this will be NULL;
  540. */
  541. if (ep) {
  542. iser_conn = ep->dd_data;
  543. shost->sg_tablesize = iser_conn->scsi_sg_tablesize;
  544. shost->can_queue = min_t(u16, cmds_max, iser_conn->max_cmds);
  545. mutex_lock(&iser_conn->state_mutex);
  546. if (iser_conn->state != ISER_CONN_UP) {
  547. iser_err("iser conn %p already started teardown\n",
  548. iser_conn);
  549. mutex_unlock(&iser_conn->state_mutex);
  550. goto free_host;
  551. }
  552. ib_conn = &iser_conn->ib_conn;
  553. ib_dev = ib_conn->device->ib_device;
  554. if (ib_conn->pi_support) {
  555. u32 sig_caps = ib_dev->attrs.sig_prot_cap;
  556. shost->sg_prot_tablesize = shost->sg_tablesize;
  557. scsi_host_set_prot(shost, iser_dif_prot_caps(sig_caps));
  558. scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP |
  559. SHOST_DIX_GUARD_CRC);
  560. }
  561. if (!(ib_dev->attrs.kernel_cap_flags & IBK_SG_GAPS_REG))
  562. shost->virt_boundary_mask = SZ_4K - 1;
  563. if (iscsi_host_add(shost, ib_dev->dev.parent)) {
  564. mutex_unlock(&iser_conn->state_mutex);
  565. goto free_host;
  566. }
  567. mutex_unlock(&iser_conn->state_mutex);
  568. } else {
  569. shost->can_queue = min_t(u16, cmds_max, ISER_DEF_XMIT_CMDS_MAX);
  570. if (iscsi_host_add(shost, NULL))
  571. goto free_host;
  572. }
  573. max_fr_sectors = (shost->sg_tablesize * PAGE_SIZE) >> 9;
  574. shost->max_sectors = min(iser_max_sectors, max_fr_sectors);
  575. iser_dbg("iser_conn %p, sg_tablesize %u, max_sectors %u\n",
  576. iser_conn, shost->sg_tablesize,
  577. shost->max_sectors);
  578. if (shost->max_sectors < iser_max_sectors)
  579. iser_warn("max_sectors was reduced from %u to %u\n",
  580. iser_max_sectors, shost->max_sectors);
  581. cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
  582. shost->can_queue, 0,
  583. sizeof(struct iscsi_iser_task),
  584. initial_cmdsn, 0);
  585. if (!cls_session)
  586. goto remove_host;
  587. return cls_session;
  588. remove_host:
  589. iscsi_host_remove(shost, false);
  590. free_host:
  591. iscsi_host_free(shost);
  592. return NULL;
  593. }
  594. static int iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
  595. enum iscsi_param param, char *buf, int buflen)
  596. {
  597. int value;
  598. switch (param) {
  599. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  600. /* TBD */
  601. break;
  602. case ISCSI_PARAM_HDRDGST_EN:
  603. sscanf(buf, "%d", &value);
  604. if (value) {
  605. iser_err("DataDigest wasn't negotiated to None\n");
  606. return -EPROTO;
  607. }
  608. break;
  609. case ISCSI_PARAM_DATADGST_EN:
  610. sscanf(buf, "%d", &value);
  611. if (value) {
  612. iser_err("DataDigest wasn't negotiated to None\n");
  613. return -EPROTO;
  614. }
  615. break;
  616. case ISCSI_PARAM_IFMARKER_EN:
  617. sscanf(buf, "%d", &value);
  618. if (value) {
  619. iser_err("IFMarker wasn't negotiated to No\n");
  620. return -EPROTO;
  621. }
  622. break;
  623. case ISCSI_PARAM_OFMARKER_EN:
  624. sscanf(buf, "%d", &value);
  625. if (value) {
  626. iser_err("OFMarker wasn't negotiated to No\n");
  627. return -EPROTO;
  628. }
  629. break;
  630. default:
  631. return iscsi_set_param(cls_conn, param, buf, buflen);
  632. }
  633. return 0;
  634. }
  635. /**
  636. * iscsi_iser_conn_get_stats() - get iscsi connection statistics
  637. * @cls_conn: iscsi class connection
  638. * @stats: iscsi stats to output
  639. *
  640. * Output connection statistics.
  641. */
  642. static void iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn,
  643. struct iscsi_stats *stats)
  644. {
  645. struct iscsi_conn *conn = cls_conn->dd_data;
  646. stats->txdata_octets = conn->txdata_octets;
  647. stats->rxdata_octets = conn->rxdata_octets;
  648. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  649. stats->dataout_pdus = conn->dataout_pdus_cnt;
  650. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  651. stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
  652. stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
  653. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  654. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  655. stats->custom_length = 0;
  656. }
  657. static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
  658. enum iscsi_param param, char *buf)
  659. {
  660. struct iser_conn *iser_conn = ep->dd_data;
  661. switch (param) {
  662. case ISCSI_PARAM_CONN_PORT:
  663. case ISCSI_PARAM_CONN_ADDRESS:
  664. if (!iser_conn || !iser_conn->ib_conn.cma_id)
  665. return -ENOTCONN;
  666. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  667. &iser_conn->ib_conn.cma_id->route.addr.dst_addr,
  668. param, buf);
  669. default:
  670. break;
  671. }
  672. return -ENOSYS;
  673. }
  674. /**
  675. * iscsi_iser_ep_connect() - Initiate iSER connection establishment
  676. * @shost: scsi_host
  677. * @dst_addr: destination address
  678. * @non_blocking: indicate if routine can block
  679. *
  680. * Allocate an iscsi endpoint, an iser_conn structure and bind them.
  681. * After that start RDMA connection establishment via rdma_cm. We
  682. * don't allocate iser_conn embedded in iscsi_endpoint since in teardown
  683. * the endpoint will be destroyed at ep_disconnect while iser_conn will
  684. * cleanup its resources asynchronuously.
  685. *
  686. * Return: iscsi_endpoint created by iscsi layer or ERR_PTR(error)
  687. * if fails.
  688. */
  689. static struct iscsi_endpoint *iscsi_iser_ep_connect(struct Scsi_Host *shost,
  690. struct sockaddr *dst_addr,
  691. int non_blocking)
  692. {
  693. int err;
  694. struct iser_conn *iser_conn;
  695. struct iscsi_endpoint *ep;
  696. ep = iscsi_create_endpoint(0);
  697. if (!ep)
  698. return ERR_PTR(-ENOMEM);
  699. iser_conn = kzalloc_obj(*iser_conn);
  700. if (!iser_conn) {
  701. err = -ENOMEM;
  702. goto failure;
  703. }
  704. ep->dd_data = iser_conn;
  705. iser_conn->ep = ep;
  706. iser_conn_init(iser_conn);
  707. err = iser_connect(iser_conn, NULL, dst_addr, non_blocking);
  708. if (err)
  709. goto failure;
  710. return ep;
  711. failure:
  712. iscsi_destroy_endpoint(ep);
  713. return ERR_PTR(err);
  714. }
  715. /**
  716. * iscsi_iser_ep_poll() - poll for iser connection establishment to complete
  717. * @ep: iscsi endpoint (created at ep_connect)
  718. * @timeout_ms: polling timeout allowed in ms.
  719. *
  720. * This routine boils down to waiting for up_completion signaling
  721. * that cma_id got CONNECTED event.
  722. *
  723. * Return: 1 if succeeded in connection establishment, 0 if timeout expired
  724. * (libiscsi will retry will kick in) or -1 if interrupted by signal
  725. * or more likely iser connection state transitioned to TEMINATING or
  726. * DOWN during the wait period.
  727. */
  728. static int iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  729. {
  730. struct iser_conn *iser_conn = ep->dd_data;
  731. int rc;
  732. rc = wait_for_completion_interruptible_timeout(&iser_conn->up_completion,
  733. msecs_to_jiffies(timeout_ms));
  734. /* if conn establishment failed, return error code to iscsi */
  735. if (rc == 0) {
  736. mutex_lock(&iser_conn->state_mutex);
  737. if (iser_conn->state == ISER_CONN_TERMINATING ||
  738. iser_conn->state == ISER_CONN_DOWN)
  739. rc = -1;
  740. mutex_unlock(&iser_conn->state_mutex);
  741. }
  742. iser_info("iser conn %p rc = %d\n", iser_conn, rc);
  743. if (rc > 0)
  744. return 1; /* success, this is the equivalent of EPOLLOUT */
  745. else if (!rc)
  746. return 0; /* timeout */
  747. else
  748. return rc; /* signal */
  749. }
  750. /**
  751. * iscsi_iser_ep_disconnect() - Initiate connection teardown process
  752. * @ep: iscsi endpoint handle
  753. *
  754. * This routine is not blocked by iser and RDMA termination process
  755. * completion as we queue a deffered work for iser/RDMA destruction
  756. * and cleanup or actually call it immediately in case we didn't pass
  757. * iscsi conn bind/start stage, thus it is safe.
  758. */
  759. static void iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
  760. {
  761. struct iser_conn *iser_conn = ep->dd_data;
  762. iser_info("ep %p iser conn %p\n", ep, iser_conn);
  763. mutex_lock(&iser_conn->state_mutex);
  764. iser_conn_terminate(iser_conn);
  765. /*
  766. * if iser_conn and iscsi_conn are bound, we must wait for
  767. * iscsi_conn_stop and flush errors completion before freeing
  768. * the iser resources. Otherwise we are safe to free resources
  769. * immediately.
  770. */
  771. if (iser_conn->iscsi_conn) {
  772. INIT_WORK(&iser_conn->release_work, iser_release_work);
  773. queue_work(release_wq, &iser_conn->release_work);
  774. mutex_unlock(&iser_conn->state_mutex);
  775. } else {
  776. iser_conn->state = ISER_CONN_DOWN;
  777. mutex_unlock(&iser_conn->state_mutex);
  778. iser_conn_release(iser_conn);
  779. }
  780. iscsi_destroy_endpoint(ep);
  781. }
  782. static umode_t iser_attr_is_visible(int param_type, int param)
  783. {
  784. switch (param_type) {
  785. case ISCSI_HOST_PARAM:
  786. switch (param) {
  787. case ISCSI_HOST_PARAM_NETDEV_NAME:
  788. case ISCSI_HOST_PARAM_HWADDRESS:
  789. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  790. return S_IRUGO;
  791. default:
  792. return 0;
  793. }
  794. case ISCSI_PARAM:
  795. switch (param) {
  796. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  797. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  798. case ISCSI_PARAM_HDRDGST_EN:
  799. case ISCSI_PARAM_DATADGST_EN:
  800. case ISCSI_PARAM_CONN_ADDRESS:
  801. case ISCSI_PARAM_CONN_PORT:
  802. case ISCSI_PARAM_EXP_STATSN:
  803. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  804. case ISCSI_PARAM_PERSISTENT_PORT:
  805. case ISCSI_PARAM_PING_TMO:
  806. case ISCSI_PARAM_RECV_TMO:
  807. case ISCSI_PARAM_INITIAL_R2T_EN:
  808. case ISCSI_PARAM_MAX_R2T:
  809. case ISCSI_PARAM_IMM_DATA_EN:
  810. case ISCSI_PARAM_FIRST_BURST:
  811. case ISCSI_PARAM_MAX_BURST:
  812. case ISCSI_PARAM_PDU_INORDER_EN:
  813. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  814. case ISCSI_PARAM_TARGET_NAME:
  815. case ISCSI_PARAM_TPGT:
  816. case ISCSI_PARAM_USERNAME:
  817. case ISCSI_PARAM_PASSWORD:
  818. case ISCSI_PARAM_USERNAME_IN:
  819. case ISCSI_PARAM_PASSWORD_IN:
  820. case ISCSI_PARAM_FAST_ABORT:
  821. case ISCSI_PARAM_ABORT_TMO:
  822. case ISCSI_PARAM_LU_RESET_TMO:
  823. case ISCSI_PARAM_TGT_RESET_TMO:
  824. case ISCSI_PARAM_IFACE_NAME:
  825. case ISCSI_PARAM_INITIATOR_NAME:
  826. case ISCSI_PARAM_DISCOVERY_SESS:
  827. return S_IRUGO;
  828. default:
  829. return 0;
  830. }
  831. }
  832. return 0;
  833. }
  834. static const struct scsi_host_template iscsi_iser_sht = {
  835. .module = THIS_MODULE,
  836. .name = "iSCSI Initiator over iSER",
  837. .queuecommand = iscsi_queuecommand,
  838. .change_queue_depth = scsi_change_queue_depth,
  839. .sg_tablesize = ISCSI_ISER_DEF_SG_TABLESIZE,
  840. .cmd_per_lun = ISER_DEF_CMD_PER_LUN,
  841. .eh_timed_out = iscsi_eh_cmd_timed_out,
  842. .eh_abort_handler = iscsi_eh_abort,
  843. .eh_device_reset_handler= iscsi_eh_device_reset,
  844. .eh_target_reset_handler = iscsi_eh_recover_target,
  845. .target_alloc = iscsi_target_alloc,
  846. .proc_name = "iscsi_iser",
  847. .this_id = -1,
  848. .track_queue_depth = 1,
  849. .cmd_size = sizeof(struct iscsi_cmd),
  850. };
  851. static struct iscsi_transport iscsi_iser_transport = {
  852. .owner = THIS_MODULE,
  853. .name = "iser",
  854. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_TEXT_NEGO,
  855. /* session management */
  856. .create_session = iscsi_iser_session_create,
  857. .destroy_session = iscsi_iser_session_destroy,
  858. /* connection management */
  859. .create_conn = iscsi_iser_conn_create,
  860. .bind_conn = iscsi_iser_conn_bind,
  861. .unbind_conn = iscsi_conn_unbind,
  862. .destroy_conn = iscsi_conn_teardown,
  863. .attr_is_visible = iser_attr_is_visible,
  864. .set_param = iscsi_iser_set_param,
  865. .get_conn_param = iscsi_conn_get_param,
  866. .get_ep_param = iscsi_iser_get_ep_param,
  867. .get_session_param = iscsi_session_get_param,
  868. .start_conn = iscsi_iser_conn_start,
  869. .stop_conn = iscsi_iser_conn_stop,
  870. /* iscsi host params */
  871. .get_host_param = iscsi_host_get_param,
  872. .set_host_param = iscsi_host_set_param,
  873. /* IO */
  874. .send_pdu = iscsi_conn_send_pdu,
  875. .get_stats = iscsi_iser_conn_get_stats,
  876. .init_task = iscsi_iser_task_init,
  877. .xmit_task = iscsi_iser_task_xmit,
  878. .cleanup_task = iscsi_iser_cleanup_task,
  879. .alloc_pdu = iscsi_iser_pdu_alloc,
  880. .check_protection = iscsi_iser_check_protection,
  881. /* recovery */
  882. .session_recovery_timedout = iscsi_session_recovery_timedout,
  883. .ep_connect = iscsi_iser_ep_connect,
  884. .ep_poll = iscsi_iser_ep_poll,
  885. .ep_disconnect = iscsi_iser_ep_disconnect
  886. };
  887. static int __init iser_init(void)
  888. {
  889. int err;
  890. iser_dbg("Starting iSER datamover...\n");
  891. memset(&ig, 0, sizeof(struct iser_global));
  892. ig.desc_cache = kmem_cache_create("iser_descriptors",
  893. sizeof(struct iser_tx_desc),
  894. 0, SLAB_HWCACHE_ALIGN,
  895. NULL);
  896. if (ig.desc_cache == NULL)
  897. return -ENOMEM;
  898. /* device init is called only after the first addr resolution */
  899. mutex_init(&ig.device_list_mutex);
  900. INIT_LIST_HEAD(&ig.device_list);
  901. mutex_init(&ig.connlist_mutex);
  902. INIT_LIST_HEAD(&ig.connlist);
  903. release_wq = alloc_workqueue("release workqueue", WQ_PERCPU, 0);
  904. if (!release_wq) {
  905. iser_err("failed to allocate release workqueue\n");
  906. err = -ENOMEM;
  907. goto err_alloc_wq;
  908. }
  909. iscsi_iser_scsi_transport = iscsi_register_transport(
  910. &iscsi_iser_transport);
  911. if (!iscsi_iser_scsi_transport) {
  912. iser_err("iscsi_register_transport failed\n");
  913. err = -EINVAL;
  914. goto err_reg;
  915. }
  916. return 0;
  917. err_reg:
  918. destroy_workqueue(release_wq);
  919. err_alloc_wq:
  920. kmem_cache_destroy(ig.desc_cache);
  921. return err;
  922. }
  923. static void __exit iser_exit(void)
  924. {
  925. struct iser_conn *iser_conn, *n;
  926. int connlist_empty;
  927. iser_dbg("Removing iSER datamover...\n");
  928. destroy_workqueue(release_wq);
  929. mutex_lock(&ig.connlist_mutex);
  930. connlist_empty = list_empty(&ig.connlist);
  931. mutex_unlock(&ig.connlist_mutex);
  932. if (!connlist_empty) {
  933. iser_err("Error cleanup stage completed but we still have iser "
  934. "connections, destroying them anyway\n");
  935. list_for_each_entry_safe(iser_conn, n, &ig.connlist,
  936. conn_list) {
  937. iser_conn_release(iser_conn);
  938. }
  939. }
  940. iscsi_unregister_transport(&iscsi_iser_transport);
  941. kmem_cache_destroy(ig.desc_cache);
  942. }
  943. module_init(iser_init);
  944. module_exit(iser_exit);