iscsi_iser.c 29 KB

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