iscsi_iser.c 30 KB

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