iscsi_iser.c 31 KB

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