iscsi_iser.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  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. MODULE_VERSION(DRV_VER);
  75. static struct scsi_host_template iscsi_iser_sht;
  76. static struct iscsi_transport iscsi_iser_transport;
  77. static struct scsi_transport_template *iscsi_iser_scsi_transport;
  78. static struct workqueue_struct *release_wq;
  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. iser_conn_terminate(iser_conn);
  480. iscsi_conn_stop(cls_conn, flag);
  481. /* unbind */
  482. iser_conn->iscsi_conn = NULL;
  483. conn->dd_data = NULL;
  484. complete(&iser_conn->stop_completion);
  485. mutex_unlock(&iser_conn->state_mutex);
  486. } else {
  487. iscsi_conn_stop(cls_conn, flag);
  488. }
  489. }
  490. /**
  491. * iscsi_iser_session_destroy() - destroy iscsi-iser session
  492. * @cls_session: iscsi class session
  493. *
  494. * Removes and free iscsi host.
  495. */
  496. static void
  497. iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
  498. {
  499. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  500. iscsi_session_teardown(cls_session);
  501. iscsi_host_remove(shost);
  502. iscsi_host_free(shost);
  503. }
  504. static inline unsigned int
  505. iser_dif_prot_caps(int prot_caps)
  506. {
  507. return ((prot_caps & IB_PROT_T10DIF_TYPE_1) ?
  508. SHOST_DIF_TYPE1_PROTECTION | SHOST_DIX_TYPE0_PROTECTION |
  509. SHOST_DIX_TYPE1_PROTECTION : 0) |
  510. ((prot_caps & IB_PROT_T10DIF_TYPE_2) ?
  511. SHOST_DIF_TYPE2_PROTECTION | SHOST_DIX_TYPE2_PROTECTION : 0) |
  512. ((prot_caps & IB_PROT_T10DIF_TYPE_3) ?
  513. SHOST_DIF_TYPE3_PROTECTION | SHOST_DIX_TYPE3_PROTECTION : 0);
  514. }
  515. /**
  516. * iscsi_iser_session_create() - create an iscsi-iser session
  517. * @ep: iscsi end-point handle
  518. * @cmds_max: maximum commands in this session
  519. * @qdepth: session command queue depth
  520. * @initial_cmdsn: initiator command sequnce number
  521. *
  522. * Allocates and adds a scsi host, expose DIF supprot if
  523. * exists, and sets up an iscsi session.
  524. */
  525. static struct iscsi_cls_session *
  526. iscsi_iser_session_create(struct iscsi_endpoint *ep,
  527. uint16_t cmds_max, uint16_t qdepth,
  528. uint32_t initial_cmdsn)
  529. {
  530. struct iscsi_cls_session *cls_session;
  531. struct iscsi_session *session;
  532. struct Scsi_Host *shost;
  533. struct iser_conn *iser_conn = NULL;
  534. struct ib_conn *ib_conn;
  535. u32 max_fr_sectors;
  536. u16 max_cmds;
  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. max_cmds = iser_conn->max_cmds;
  553. shost->sg_tablesize = iser_conn->scsi_sg_tablesize;
  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. scsi_host_set_prot(shost, iser_dif_prot_caps(sig_caps));
  565. scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP |
  566. SHOST_DIX_GUARD_CRC);
  567. }
  568. /*
  569. * Limit the sg_tablesize and max_sectors based on the device
  570. * max fastreg page list length.
  571. */
  572. shost->sg_tablesize = min_t(unsigned short, shost->sg_tablesize,
  573. ib_conn->device->ib_device->attrs.max_fast_reg_page_list_len);
  574. if (iscsi_host_add(shost,
  575. ib_conn->device->ib_device->dma_device)) {
  576. mutex_unlock(&iser_conn->state_mutex);
  577. goto free_host;
  578. }
  579. mutex_unlock(&iser_conn->state_mutex);
  580. } else {
  581. max_cmds = ISER_DEF_XMIT_CMDS_MAX;
  582. if (iscsi_host_add(shost, NULL))
  583. goto free_host;
  584. }
  585. /*
  586. * FRs or FMRs can only map up to a (device) page per entry, but if the
  587. * first entry is misaligned we'll end up using using two entries
  588. * (head and tail) for a single page worth data, so we have to drop
  589. * one segment from the calculation.
  590. */
  591. max_fr_sectors = ((shost->sg_tablesize - 1) * PAGE_SIZE) >> 9;
  592. shost->max_sectors = min(iser_max_sectors, max_fr_sectors);
  593. if (cmds_max > max_cmds) {
  594. iser_info("cmds_max changed from %u to %u\n",
  595. cmds_max, max_cmds);
  596. cmds_max = max_cmds;
  597. }
  598. cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
  599. cmds_max, 0,
  600. sizeof(struct iscsi_iser_task),
  601. initial_cmdsn, 0);
  602. if (!cls_session)
  603. goto remove_host;
  604. session = cls_session->dd_data;
  605. shost->can_queue = session->scsi_cmds_max;
  606. return cls_session;
  607. remove_host:
  608. iscsi_host_remove(shost);
  609. free_host:
  610. iscsi_host_free(shost);
  611. return NULL;
  612. }
  613. static int
  614. iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
  615. enum iscsi_param param, char *buf, int buflen)
  616. {
  617. int value;
  618. switch (param) {
  619. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  620. /* TBD */
  621. break;
  622. case ISCSI_PARAM_HDRDGST_EN:
  623. sscanf(buf, "%d", &value);
  624. if (value) {
  625. iser_err("DataDigest wasn't negotiated to None\n");
  626. return -EPROTO;
  627. }
  628. break;
  629. case ISCSI_PARAM_DATADGST_EN:
  630. sscanf(buf, "%d", &value);
  631. if (value) {
  632. iser_err("DataDigest wasn't negotiated to None\n");
  633. return -EPROTO;
  634. }
  635. break;
  636. case ISCSI_PARAM_IFMARKER_EN:
  637. sscanf(buf, "%d", &value);
  638. if (value) {
  639. iser_err("IFMarker wasn't negotiated to No\n");
  640. return -EPROTO;
  641. }
  642. break;
  643. case ISCSI_PARAM_OFMARKER_EN:
  644. sscanf(buf, "%d", &value);
  645. if (value) {
  646. iser_err("OFMarker wasn't negotiated to No\n");
  647. return -EPROTO;
  648. }
  649. break;
  650. default:
  651. return iscsi_set_param(cls_conn, param, buf, buflen);
  652. }
  653. return 0;
  654. }
  655. /**
  656. * iscsi_iser_set_param() - set class connection parameter
  657. * @cls_conn: iscsi class connection
  658. * @stats: iscsi stats to output
  659. *
  660. * Output connection statistics.
  661. */
  662. static void
  663. iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  664. {
  665. struct iscsi_conn *conn = cls_conn->dd_data;
  666. stats->txdata_octets = conn->txdata_octets;
  667. stats->rxdata_octets = conn->rxdata_octets;
  668. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  669. stats->dataout_pdus = conn->dataout_pdus_cnt;
  670. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  671. stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
  672. stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
  673. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  674. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  675. stats->custom_length = 0;
  676. }
  677. static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
  678. enum iscsi_param param, char *buf)
  679. {
  680. struct iser_conn *iser_conn = ep->dd_data;
  681. int len;
  682. switch (param) {
  683. case ISCSI_PARAM_CONN_PORT:
  684. case ISCSI_PARAM_CONN_ADDRESS:
  685. if (!iser_conn || !iser_conn->ib_conn.cma_id)
  686. return -ENOTCONN;
  687. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  688. &iser_conn->ib_conn.cma_id->route.addr.dst_addr,
  689. param, buf);
  690. break;
  691. default:
  692. return -ENOSYS;
  693. }
  694. return len;
  695. }
  696. /**
  697. * iscsi_iser_ep_connect() - Initiate iSER connection establishment
  698. * @shost: scsi_host
  699. * @dst_addr: destination address
  700. * @non-blocking: indicate if routine can block
  701. *
  702. * Allocate an iscsi endpoint, an iser_conn structure and bind them.
  703. * After that start RDMA connection establishment via rdma_cm. We
  704. * don't allocate iser_conn embedded in iscsi_endpoint since in teardown
  705. * the endpoint will be destroyed at ep_disconnect while iser_conn will
  706. * cleanup its resources asynchronuously.
  707. *
  708. * Return: iscsi_endpoint created by iscsi layer or ERR_PTR(error)
  709. * if fails.
  710. */
  711. static struct iscsi_endpoint *
  712. iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  713. int non_blocking)
  714. {
  715. int err;
  716. struct iser_conn *iser_conn;
  717. struct iscsi_endpoint *ep;
  718. ep = iscsi_create_endpoint(0);
  719. if (!ep)
  720. return ERR_PTR(-ENOMEM);
  721. iser_conn = kzalloc(sizeof(*iser_conn), GFP_KERNEL);
  722. if (!iser_conn) {
  723. err = -ENOMEM;
  724. goto failure;
  725. }
  726. ep->dd_data = iser_conn;
  727. iser_conn->ep = ep;
  728. iser_conn_init(iser_conn);
  729. err = iser_connect(iser_conn, NULL, dst_addr, non_blocking);
  730. if (err)
  731. goto failure;
  732. return ep;
  733. failure:
  734. iscsi_destroy_endpoint(ep);
  735. return ERR_PTR(err);
  736. }
  737. /**
  738. * iscsi_iser_ep_poll() - poll for iser connection establishment to complete
  739. * @ep: iscsi endpoint (created at ep_connect)
  740. * @timeout_ms: polling timeout allowed in ms.
  741. *
  742. * This routine boils down to waiting for up_completion signaling
  743. * that cma_id got CONNECTED event.
  744. *
  745. * Return: 1 if succeeded in connection establishment, 0 if timeout expired
  746. * (libiscsi will retry will kick in) or -1 if interrupted by signal
  747. * or more likely iser connection state transitioned to TEMINATING or
  748. * DOWN during the wait period.
  749. */
  750. static int
  751. iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  752. {
  753. struct iser_conn *iser_conn = ep->dd_data;
  754. int rc;
  755. rc = wait_for_completion_interruptible_timeout(&iser_conn->up_completion,
  756. msecs_to_jiffies(timeout_ms));
  757. /* if conn establishment failed, return error code to iscsi */
  758. if (rc == 0) {
  759. mutex_lock(&iser_conn->state_mutex);
  760. if (iser_conn->state == ISER_CONN_TERMINATING ||
  761. iser_conn->state == ISER_CONN_DOWN)
  762. rc = -1;
  763. mutex_unlock(&iser_conn->state_mutex);
  764. }
  765. iser_info("iser conn %p rc = %d\n", iser_conn, rc);
  766. if (rc > 0)
  767. return 1; /* success, this is the equivalent of POLLOUT */
  768. else if (!rc)
  769. return 0; /* timeout */
  770. else
  771. return rc; /* signal */
  772. }
  773. /**
  774. * iscsi_iser_ep_disconnect() - Initiate connection teardown process
  775. * @ep: iscsi endpoint handle
  776. *
  777. * This routine is not blocked by iser and RDMA termination process
  778. * completion as we queue a deffered work for iser/RDMA destruction
  779. * and cleanup or actually call it immediately in case we didn't pass
  780. * iscsi conn bind/start stage, thus it is safe.
  781. */
  782. static void
  783. iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
  784. {
  785. struct iser_conn *iser_conn = ep->dd_data;
  786. iser_info("ep %p iser conn %p\n", ep, iser_conn);
  787. mutex_lock(&iser_conn->state_mutex);
  788. iser_conn_terminate(iser_conn);
  789. /*
  790. * if iser_conn and iscsi_conn are bound, we must wait for
  791. * iscsi_conn_stop and flush errors completion before freeing
  792. * the iser resources. Otherwise we are safe to free resources
  793. * immediately.
  794. */
  795. if (iser_conn->iscsi_conn) {
  796. INIT_WORK(&iser_conn->release_work, iser_release_work);
  797. queue_work(release_wq, &iser_conn->release_work);
  798. mutex_unlock(&iser_conn->state_mutex);
  799. } else {
  800. iser_conn->state = ISER_CONN_DOWN;
  801. mutex_unlock(&iser_conn->state_mutex);
  802. iser_conn_release(iser_conn);
  803. }
  804. iscsi_destroy_endpoint(ep);
  805. }
  806. static umode_t iser_attr_is_visible(int param_type, int param)
  807. {
  808. switch (param_type) {
  809. case ISCSI_HOST_PARAM:
  810. switch (param) {
  811. case ISCSI_HOST_PARAM_NETDEV_NAME:
  812. case ISCSI_HOST_PARAM_HWADDRESS:
  813. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  814. return S_IRUGO;
  815. default:
  816. return 0;
  817. }
  818. case ISCSI_PARAM:
  819. switch (param) {
  820. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  821. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  822. case ISCSI_PARAM_HDRDGST_EN:
  823. case ISCSI_PARAM_DATADGST_EN:
  824. case ISCSI_PARAM_CONN_ADDRESS:
  825. case ISCSI_PARAM_CONN_PORT:
  826. case ISCSI_PARAM_EXP_STATSN:
  827. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  828. case ISCSI_PARAM_PERSISTENT_PORT:
  829. case ISCSI_PARAM_PING_TMO:
  830. case ISCSI_PARAM_RECV_TMO:
  831. case ISCSI_PARAM_INITIAL_R2T_EN:
  832. case ISCSI_PARAM_MAX_R2T:
  833. case ISCSI_PARAM_IMM_DATA_EN:
  834. case ISCSI_PARAM_FIRST_BURST:
  835. case ISCSI_PARAM_MAX_BURST:
  836. case ISCSI_PARAM_PDU_INORDER_EN:
  837. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  838. case ISCSI_PARAM_TARGET_NAME:
  839. case ISCSI_PARAM_TPGT:
  840. case ISCSI_PARAM_USERNAME:
  841. case ISCSI_PARAM_PASSWORD:
  842. case ISCSI_PARAM_USERNAME_IN:
  843. case ISCSI_PARAM_PASSWORD_IN:
  844. case ISCSI_PARAM_FAST_ABORT:
  845. case ISCSI_PARAM_ABORT_TMO:
  846. case ISCSI_PARAM_LU_RESET_TMO:
  847. case ISCSI_PARAM_TGT_RESET_TMO:
  848. case ISCSI_PARAM_IFACE_NAME:
  849. case ISCSI_PARAM_INITIATOR_NAME:
  850. case ISCSI_PARAM_DISCOVERY_SESS:
  851. return S_IRUGO;
  852. default:
  853. return 0;
  854. }
  855. }
  856. return 0;
  857. }
  858. static int iscsi_iser_slave_alloc(struct scsi_device *sdev)
  859. {
  860. struct iscsi_session *session;
  861. struct iser_conn *iser_conn;
  862. struct ib_device *ib_dev;
  863. session = starget_to_session(scsi_target(sdev))->dd_data;
  864. iser_conn = session->leadconn->dd_data;
  865. ib_dev = iser_conn->ib_conn.device->ib_device;
  866. if (!(ib_dev->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG))
  867. blk_queue_virt_boundary(sdev->request_queue, ~MASK_4K);
  868. return 0;
  869. }
  870. static struct scsi_host_template iscsi_iser_sht = {
  871. .module = THIS_MODULE,
  872. .name = "iSCSI Initiator over iSER",
  873. .queuecommand = iscsi_queuecommand,
  874. .change_queue_depth = scsi_change_queue_depth,
  875. .sg_tablesize = ISCSI_ISER_DEF_SG_TABLESIZE,
  876. .cmd_per_lun = ISER_DEF_CMD_PER_LUN,
  877. .eh_abort_handler = iscsi_eh_abort,
  878. .eh_device_reset_handler= iscsi_eh_device_reset,
  879. .eh_target_reset_handler = iscsi_eh_recover_target,
  880. .target_alloc = iscsi_target_alloc,
  881. .use_clustering = ENABLE_CLUSTERING,
  882. .slave_alloc = iscsi_iser_slave_alloc,
  883. .proc_name = "iscsi_iser",
  884. .this_id = -1,
  885. .track_queue_depth = 1,
  886. };
  887. static struct iscsi_transport iscsi_iser_transport = {
  888. .owner = THIS_MODULE,
  889. .name = "iser",
  890. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_TEXT_NEGO,
  891. /* session management */
  892. .create_session = iscsi_iser_session_create,
  893. .destroy_session = iscsi_iser_session_destroy,
  894. /* connection management */
  895. .create_conn = iscsi_iser_conn_create,
  896. .bind_conn = iscsi_iser_conn_bind,
  897. .destroy_conn = iscsi_conn_teardown,
  898. .attr_is_visible = iser_attr_is_visible,
  899. .set_param = iscsi_iser_set_param,
  900. .get_conn_param = iscsi_conn_get_param,
  901. .get_ep_param = iscsi_iser_get_ep_param,
  902. .get_session_param = iscsi_session_get_param,
  903. .start_conn = iscsi_iser_conn_start,
  904. .stop_conn = iscsi_iser_conn_stop,
  905. /* iscsi host params */
  906. .get_host_param = iscsi_host_get_param,
  907. .set_host_param = iscsi_host_set_param,
  908. /* IO */
  909. .send_pdu = iscsi_conn_send_pdu,
  910. .get_stats = iscsi_iser_conn_get_stats,
  911. .init_task = iscsi_iser_task_init,
  912. .xmit_task = iscsi_iser_task_xmit,
  913. .cleanup_task = iscsi_iser_cleanup_task,
  914. .alloc_pdu = iscsi_iser_pdu_alloc,
  915. .check_protection = iscsi_iser_check_protection,
  916. /* recovery */
  917. .session_recovery_timedout = iscsi_session_recovery_timedout,
  918. .ep_connect = iscsi_iser_ep_connect,
  919. .ep_poll = iscsi_iser_ep_poll,
  920. .ep_disconnect = iscsi_iser_ep_disconnect
  921. };
  922. static int __init iser_init(void)
  923. {
  924. int err;
  925. iser_dbg("Starting iSER datamover...\n");
  926. if (iscsi_max_lun < 1) {
  927. iser_err("Invalid max_lun value of %u\n", iscsi_max_lun);
  928. return -EINVAL;
  929. }
  930. memset(&ig, 0, sizeof(struct iser_global));
  931. ig.desc_cache = kmem_cache_create("iser_descriptors",
  932. sizeof(struct iser_tx_desc),
  933. 0, SLAB_HWCACHE_ALIGN,
  934. NULL);
  935. if (ig.desc_cache == NULL)
  936. return -ENOMEM;
  937. /* device init is called only after the first addr resolution */
  938. mutex_init(&ig.device_list_mutex);
  939. INIT_LIST_HEAD(&ig.device_list);
  940. mutex_init(&ig.connlist_mutex);
  941. INIT_LIST_HEAD(&ig.connlist);
  942. release_wq = alloc_workqueue("release workqueue", 0, 0);
  943. if (!release_wq) {
  944. iser_err("failed to allocate release workqueue\n");
  945. err = -ENOMEM;
  946. goto err_alloc_wq;
  947. }
  948. iscsi_iser_scsi_transport = iscsi_register_transport(
  949. &iscsi_iser_transport);
  950. if (!iscsi_iser_scsi_transport) {
  951. iser_err("iscsi_register_transport failed\n");
  952. err = -EINVAL;
  953. goto err_reg;
  954. }
  955. return 0;
  956. err_reg:
  957. destroy_workqueue(release_wq);
  958. err_alloc_wq:
  959. kmem_cache_destroy(ig.desc_cache);
  960. return err;
  961. }
  962. static void __exit iser_exit(void)
  963. {
  964. struct iser_conn *iser_conn, *n;
  965. int connlist_empty;
  966. iser_dbg("Removing iSER datamover...\n");
  967. destroy_workqueue(release_wq);
  968. mutex_lock(&ig.connlist_mutex);
  969. connlist_empty = list_empty(&ig.connlist);
  970. mutex_unlock(&ig.connlist_mutex);
  971. if (!connlist_empty) {
  972. iser_err("Error cleanup stage completed but we still have iser "
  973. "connections, destroying them anyway\n");
  974. list_for_each_entry_safe(iser_conn, n, &ig.connlist,
  975. conn_list) {
  976. iser_conn_release(iser_conn);
  977. }
  978. }
  979. iscsi_unregister_transport(&iscsi_iser_transport);
  980. kmem_cache_destroy(ig.desc_cache);
  981. }
  982. module_init(iser_init);
  983. module_exit(iser_exit);