iscsi_iser.c 31 KB

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