iscsi_iser.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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 = 0;
  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:CRC)");
  89. static struct workqueue_struct *release_wq;
  90. struct iser_global ig;
  91. void
  92. iscsi_iser_recv(struct iscsi_conn *conn,
  93. struct iscsi_hdr *hdr, char *rx_data, int rx_data_len)
  94. {
  95. int rc = 0;
  96. int datalen;
  97. int ahslen;
  98. /* verify PDU length */
  99. datalen = ntoh24(hdr->dlength);
  100. if (datalen > rx_data_len || (datalen + 4) < rx_data_len) {
  101. iser_err("wrong datalen %d (hdr), %d (IB)\n",
  102. datalen, rx_data_len);
  103. rc = ISCSI_ERR_DATALEN;
  104. goto error;
  105. }
  106. if (datalen != rx_data_len)
  107. iser_dbg("aligned datalen (%d) hdr, %d (IB)\n",
  108. datalen, rx_data_len);
  109. /* read AHS */
  110. ahslen = hdr->hlength * 4;
  111. rc = iscsi_complete_pdu(conn, hdr, rx_data, rx_data_len);
  112. if (rc && rc != ISCSI_ERR_NO_SCSI_CMD)
  113. goto error;
  114. return;
  115. error:
  116. iscsi_conn_failure(conn, rc);
  117. }
  118. static int iscsi_iser_pdu_alloc(struct iscsi_task *task, uint8_t opcode)
  119. {
  120. struct iscsi_iser_task *iser_task = task->dd_data;
  121. task->hdr = (struct iscsi_hdr *)&iser_task->desc.iscsi_header;
  122. task->hdr_max = sizeof(iser_task->desc.iscsi_header);
  123. return 0;
  124. }
  125. int iser_initialize_task_headers(struct iscsi_task *task,
  126. struct iser_tx_desc *tx_desc)
  127. {
  128. struct iser_conn *ib_conn = task->conn->dd_data;
  129. struct iser_device *device = ib_conn->device;
  130. struct iscsi_iser_task *iser_task = task->dd_data;
  131. u64 dma_addr;
  132. dma_addr = ib_dma_map_single(device->ib_device, (void *)tx_desc,
  133. ISER_HEADERS_LEN, DMA_TO_DEVICE);
  134. if (ib_dma_mapping_error(device->ib_device, dma_addr))
  135. return -ENOMEM;
  136. tx_desc->dma_addr = dma_addr;
  137. tx_desc->tx_sg[0].addr = tx_desc->dma_addr;
  138. tx_desc->tx_sg[0].length = ISER_HEADERS_LEN;
  139. tx_desc->tx_sg[0].lkey = device->mr->lkey;
  140. iser_task->ib_conn = ib_conn;
  141. return 0;
  142. }
  143. /**
  144. * iscsi_iser_task_init - Initialize task
  145. * @task: iscsi task
  146. *
  147. * Initialize the task for the scsi command or mgmt command.
  148. */
  149. static int
  150. iscsi_iser_task_init(struct iscsi_task *task)
  151. {
  152. struct iscsi_iser_task *iser_task = task->dd_data;
  153. if (iser_initialize_task_headers(task, &iser_task->desc))
  154. return -ENOMEM;
  155. /* mgmt task */
  156. if (!task->sc)
  157. return 0;
  158. iser_task->command_sent = 0;
  159. iser_task_rdma_init(iser_task);
  160. iser_task->sc = task->sc;
  161. return 0;
  162. }
  163. /**
  164. * iscsi_iser_mtask_xmit - xmit management(immediate) task
  165. * @conn: iscsi connection
  166. * @task: task management task
  167. *
  168. * Notes:
  169. * The function can return -EAGAIN in which case caller must
  170. * call it again later, or recover. '0' return code means successful
  171. * xmit.
  172. *
  173. **/
  174. static int
  175. iscsi_iser_mtask_xmit(struct iscsi_conn *conn, struct iscsi_task *task)
  176. {
  177. int error = 0;
  178. iser_dbg("mtask xmit [cid %d itt 0x%x]\n", conn->id, task->itt);
  179. error = iser_send_control(conn, task);
  180. /* since iser xmits control with zero copy, tasks can not be recycled
  181. * right after sending them.
  182. * The recycling scheme is based on whether a response is expected
  183. * - if yes, the task is recycled at iscsi_complete_pdu
  184. * - if no, the task is recycled at iser_snd_completion
  185. */
  186. return error;
  187. }
  188. static int
  189. iscsi_iser_task_xmit_unsol_data(struct iscsi_conn *conn,
  190. struct iscsi_task *task)
  191. {
  192. struct iscsi_r2t_info *r2t = &task->unsol_r2t;
  193. struct iscsi_data hdr;
  194. int error = 0;
  195. /* Send data-out PDUs while there's still unsolicited data to send */
  196. while (iscsi_task_has_unsol_data(task)) {
  197. iscsi_prep_data_out_pdu(task, r2t, &hdr);
  198. iser_dbg("Sending data-out: itt 0x%x, data count %d\n",
  199. hdr.itt, r2t->data_count);
  200. /* the buffer description has been passed with the command */
  201. /* Send the command */
  202. error = iser_send_data_out(conn, task, &hdr);
  203. if (error) {
  204. r2t->datasn--;
  205. goto iscsi_iser_task_xmit_unsol_data_exit;
  206. }
  207. r2t->sent += r2t->data_count;
  208. iser_dbg("Need to send %d more as data-out PDUs\n",
  209. r2t->data_length - r2t->sent);
  210. }
  211. iscsi_iser_task_xmit_unsol_data_exit:
  212. return error;
  213. }
  214. static int
  215. iscsi_iser_task_xmit(struct iscsi_task *task)
  216. {
  217. struct iscsi_conn *conn = task->conn;
  218. struct iscsi_iser_task *iser_task = task->dd_data;
  219. int error = 0;
  220. if (!task->sc)
  221. return iscsi_iser_mtask_xmit(conn, task);
  222. if (task->sc->sc_data_direction == DMA_TO_DEVICE) {
  223. BUG_ON(scsi_bufflen(task->sc) == 0);
  224. iser_dbg("cmd [itt %x total %d imm %d unsol_data %d\n",
  225. task->itt, scsi_bufflen(task->sc),
  226. task->imm_count, task->unsol_r2t.data_length);
  227. }
  228. iser_dbg("ctask xmit [cid %d itt 0x%x]\n",
  229. conn->id, task->itt);
  230. /* Send the cmd PDU */
  231. if (!iser_task->command_sent) {
  232. error = iser_send_command(conn, task);
  233. if (error)
  234. goto iscsi_iser_task_xmit_exit;
  235. iser_task->command_sent = 1;
  236. }
  237. /* Send unsolicited data-out PDU(s) if necessary */
  238. if (iscsi_task_has_unsol_data(task))
  239. error = iscsi_iser_task_xmit_unsol_data(conn, task);
  240. iscsi_iser_task_xmit_exit:
  241. return error;
  242. }
  243. static void iscsi_iser_cleanup_task(struct iscsi_task *task)
  244. {
  245. struct iscsi_iser_task *iser_task = task->dd_data;
  246. struct iser_tx_desc *tx_desc = &iser_task->desc;
  247. struct iser_conn *ib_conn = task->conn->dd_data;
  248. struct iser_device *device = ib_conn->device;
  249. ib_dma_unmap_single(device->ib_device,
  250. tx_desc->dma_addr, ISER_HEADERS_LEN, DMA_TO_DEVICE);
  251. /* mgmt tasks do not need special cleanup */
  252. if (!task->sc)
  253. return;
  254. if (iser_task->status == ISER_TASK_STATUS_STARTED) {
  255. iser_task->status = ISER_TASK_STATUS_COMPLETED;
  256. iser_task_rdma_finalize(iser_task);
  257. }
  258. }
  259. static u8 iscsi_iser_check_protection(struct iscsi_task *task, sector_t *sector)
  260. {
  261. struct iscsi_iser_task *iser_task = task->dd_data;
  262. if (iser_task->dir[ISER_DIR_IN])
  263. return iser_check_task_pi_status(iser_task, ISER_DIR_IN,
  264. sector);
  265. else
  266. return iser_check_task_pi_status(iser_task, ISER_DIR_OUT,
  267. sector);
  268. }
  269. static struct iscsi_cls_conn *
  270. iscsi_iser_conn_create(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  271. {
  272. struct iscsi_conn *conn;
  273. struct iscsi_cls_conn *cls_conn;
  274. cls_conn = iscsi_conn_setup(cls_session, 0, conn_idx);
  275. if (!cls_conn)
  276. return NULL;
  277. conn = cls_conn->dd_data;
  278. /*
  279. * due to issues with the login code re iser sematics
  280. * this not set in iscsi_conn_setup - FIXME
  281. */
  282. conn->max_recv_dlength = ISER_RECV_DATA_SEG_LEN;
  283. return cls_conn;
  284. }
  285. static int
  286. iscsi_iser_conn_bind(struct iscsi_cls_session *cls_session,
  287. struct iscsi_cls_conn *cls_conn, uint64_t transport_eph,
  288. int is_leading)
  289. {
  290. struct iscsi_conn *conn = cls_conn->dd_data;
  291. struct iscsi_session *session;
  292. struct iser_conn *ib_conn;
  293. struct iscsi_endpoint *ep;
  294. int error;
  295. error = iscsi_conn_bind(cls_session, cls_conn, is_leading);
  296. if (error)
  297. return error;
  298. /* the transport ep handle comes from user space so it must be
  299. * verified against the global ib connections list */
  300. ep = iscsi_lookup_endpoint(transport_eph);
  301. if (!ep) {
  302. iser_err("can't bind eph %llx\n",
  303. (unsigned long long)transport_eph);
  304. return -EINVAL;
  305. }
  306. ib_conn = ep->dd_data;
  307. session = conn->session;
  308. if (iser_alloc_rx_descriptors(ib_conn, session))
  309. return -ENOMEM;
  310. /* binds the iSER connection retrieved from the previously
  311. * connected ep_handle to the iSCSI layer connection. exchanges
  312. * connection pointers */
  313. iser_info("binding iscsi conn %p to ib_conn %p\n", conn, ib_conn);
  314. conn->dd_data = ib_conn;
  315. ib_conn->iscsi_conn = conn;
  316. return 0;
  317. }
  318. static int
  319. iscsi_iser_conn_start(struct iscsi_cls_conn *cls_conn)
  320. {
  321. struct iscsi_conn *iscsi_conn;
  322. struct iser_conn *ib_conn;
  323. iscsi_conn = cls_conn->dd_data;
  324. ib_conn = iscsi_conn->dd_data;
  325. reinit_completion(&ib_conn->stop_completion);
  326. return iscsi_conn_start(cls_conn);
  327. }
  328. static void
  329. iscsi_iser_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  330. {
  331. struct iscsi_conn *conn = cls_conn->dd_data;
  332. struct iser_conn *ib_conn = conn->dd_data;
  333. iser_dbg("stopping iscsi_conn: %p, ib_conn: %p\n", conn, ib_conn);
  334. iscsi_conn_stop(cls_conn, flag);
  335. /*
  336. * Userspace may have goofed up and not bound the connection or
  337. * might have only partially setup the connection.
  338. */
  339. if (ib_conn) {
  340. conn->dd_data = NULL;
  341. complete(&ib_conn->stop_completion);
  342. }
  343. }
  344. static void iscsi_iser_session_destroy(struct iscsi_cls_session *cls_session)
  345. {
  346. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  347. iscsi_session_teardown(cls_session);
  348. iscsi_host_remove(shost);
  349. iscsi_host_free(shost);
  350. }
  351. static inline unsigned int
  352. iser_dif_prot_caps(int prot_caps)
  353. {
  354. return ((prot_caps & IB_PROT_T10DIF_TYPE_1) ? SHOST_DIF_TYPE1_PROTECTION |
  355. SHOST_DIX_TYPE1_PROTECTION : 0) |
  356. ((prot_caps & IB_PROT_T10DIF_TYPE_2) ? SHOST_DIF_TYPE2_PROTECTION |
  357. SHOST_DIX_TYPE2_PROTECTION : 0) |
  358. ((prot_caps & IB_PROT_T10DIF_TYPE_3) ? SHOST_DIF_TYPE3_PROTECTION |
  359. SHOST_DIX_TYPE3_PROTECTION : 0);
  360. }
  361. static struct iscsi_cls_session *
  362. iscsi_iser_session_create(struct iscsi_endpoint *ep,
  363. uint16_t cmds_max, uint16_t qdepth,
  364. uint32_t initial_cmdsn)
  365. {
  366. struct iscsi_cls_session *cls_session;
  367. struct iscsi_session *session;
  368. struct Scsi_Host *shost;
  369. struct iser_conn *ib_conn = NULL;
  370. shost = iscsi_host_alloc(&iscsi_iser_sht, 0, 0);
  371. if (!shost)
  372. return NULL;
  373. shost->transportt = iscsi_iser_scsi_transport;
  374. shost->cmd_per_lun = qdepth;
  375. shost->max_lun = iscsi_max_lun;
  376. shost->max_id = 0;
  377. shost->max_channel = 0;
  378. shost->max_cmd_len = 16;
  379. /*
  380. * older userspace tools (before 2.0-870) did not pass us
  381. * the leading conn's ep so this will be NULL;
  382. */
  383. if (ep) {
  384. ib_conn = ep->dd_data;
  385. if (ib_conn->pi_support) {
  386. u32 sig_caps = ib_conn->device->dev_attr.sig_prot_cap;
  387. scsi_host_set_prot(shost, iser_dif_prot_caps(sig_caps));
  388. if (iser_pi_guard)
  389. scsi_host_set_guard(shost, SHOST_DIX_GUARD_IP);
  390. else
  391. scsi_host_set_guard(shost, SHOST_DIX_GUARD_CRC);
  392. }
  393. }
  394. if (iscsi_host_add(shost,
  395. ep ? ib_conn->device->ib_device->dma_device : NULL))
  396. goto free_host;
  397. if (cmds_max > ISER_DEF_XMIT_CMDS_MAX) {
  398. iser_info("cmds_max changed from %u to %u\n",
  399. cmds_max, ISER_DEF_XMIT_CMDS_MAX);
  400. cmds_max = ISER_DEF_XMIT_CMDS_MAX;
  401. }
  402. cls_session = iscsi_session_setup(&iscsi_iser_transport, shost,
  403. cmds_max, 0,
  404. sizeof(struct iscsi_iser_task),
  405. initial_cmdsn, 0);
  406. if (!cls_session)
  407. goto remove_host;
  408. session = cls_session->dd_data;
  409. shost->can_queue = session->scsi_cmds_max;
  410. return cls_session;
  411. remove_host:
  412. iscsi_host_remove(shost);
  413. free_host:
  414. iscsi_host_free(shost);
  415. return NULL;
  416. }
  417. static int
  418. iscsi_iser_set_param(struct iscsi_cls_conn *cls_conn,
  419. enum iscsi_param param, char *buf, int buflen)
  420. {
  421. int value;
  422. switch (param) {
  423. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  424. /* TBD */
  425. break;
  426. case ISCSI_PARAM_HDRDGST_EN:
  427. sscanf(buf, "%d", &value);
  428. if (value) {
  429. iser_err("DataDigest wasn't negotiated to None\n");
  430. return -EPROTO;
  431. }
  432. break;
  433. case ISCSI_PARAM_DATADGST_EN:
  434. sscanf(buf, "%d", &value);
  435. if (value) {
  436. iser_err("DataDigest wasn't negotiated to None\n");
  437. return -EPROTO;
  438. }
  439. break;
  440. case ISCSI_PARAM_IFMARKER_EN:
  441. sscanf(buf, "%d", &value);
  442. if (value) {
  443. iser_err("IFMarker wasn't negotiated to No\n");
  444. return -EPROTO;
  445. }
  446. break;
  447. case ISCSI_PARAM_OFMARKER_EN:
  448. sscanf(buf, "%d", &value);
  449. if (value) {
  450. iser_err("OFMarker wasn't negotiated to No\n");
  451. return -EPROTO;
  452. }
  453. break;
  454. default:
  455. return iscsi_set_param(cls_conn, param, buf, buflen);
  456. }
  457. return 0;
  458. }
  459. static void
  460. iscsi_iser_conn_get_stats(struct iscsi_cls_conn *cls_conn, struct iscsi_stats *stats)
  461. {
  462. struct iscsi_conn *conn = cls_conn->dd_data;
  463. stats->txdata_octets = conn->txdata_octets;
  464. stats->rxdata_octets = conn->rxdata_octets;
  465. stats->scsicmd_pdus = conn->scsicmd_pdus_cnt;
  466. stats->dataout_pdus = conn->dataout_pdus_cnt;
  467. stats->scsirsp_pdus = conn->scsirsp_pdus_cnt;
  468. stats->datain_pdus = conn->datain_pdus_cnt; /* always 0 */
  469. stats->r2t_pdus = conn->r2t_pdus_cnt; /* always 0 */
  470. stats->tmfcmd_pdus = conn->tmfcmd_pdus_cnt;
  471. stats->tmfrsp_pdus = conn->tmfrsp_pdus_cnt;
  472. stats->custom_length = 4;
  473. strcpy(stats->custom[0].desc, "qp_tx_queue_full");
  474. stats->custom[0].value = 0; /* TB iser_conn->qp_tx_queue_full; */
  475. strcpy(stats->custom[1].desc, "fmr_map_not_avail");
  476. stats->custom[1].value = 0; /* TB iser_conn->fmr_map_not_avail */;
  477. strcpy(stats->custom[2].desc, "eh_abort_cnt");
  478. stats->custom[2].value = conn->eh_abort_cnt;
  479. strcpy(stats->custom[3].desc, "fmr_unalign_cnt");
  480. stats->custom[3].value = conn->fmr_unalign_cnt;
  481. }
  482. static int iscsi_iser_get_ep_param(struct iscsi_endpoint *ep,
  483. enum iscsi_param param, char *buf)
  484. {
  485. struct iser_conn *ib_conn = ep->dd_data;
  486. int len;
  487. switch (param) {
  488. case ISCSI_PARAM_CONN_PORT:
  489. case ISCSI_PARAM_CONN_ADDRESS:
  490. if (!ib_conn || !ib_conn->cma_id)
  491. return -ENOTCONN;
  492. return iscsi_conn_get_addr_param((struct sockaddr_storage *)
  493. &ib_conn->cma_id->route.addr.dst_addr,
  494. param, buf);
  495. break;
  496. default:
  497. return -ENOSYS;
  498. }
  499. return len;
  500. }
  501. static struct iscsi_endpoint *
  502. iscsi_iser_ep_connect(struct Scsi_Host *shost, struct sockaddr *dst_addr,
  503. int non_blocking)
  504. {
  505. int err;
  506. struct iser_conn *ib_conn;
  507. struct iscsi_endpoint *ep;
  508. ep = iscsi_create_endpoint(sizeof(*ib_conn));
  509. if (!ep)
  510. return ERR_PTR(-ENOMEM);
  511. ib_conn = ep->dd_data;
  512. ib_conn->ep = ep;
  513. iser_conn_init(ib_conn);
  514. err = iser_connect(ib_conn, NULL, (struct sockaddr_in *)dst_addr,
  515. non_blocking);
  516. if (err)
  517. return ERR_PTR(err);
  518. return ep;
  519. }
  520. static int
  521. iscsi_iser_ep_poll(struct iscsi_endpoint *ep, int timeout_ms)
  522. {
  523. struct iser_conn *ib_conn;
  524. int rc;
  525. ib_conn = ep->dd_data;
  526. rc = wait_event_interruptible_timeout(ib_conn->wait,
  527. ib_conn->state == ISER_CONN_UP,
  528. msecs_to_jiffies(timeout_ms));
  529. /* if conn establishment failed, return error code to iscsi */
  530. if (!rc &&
  531. (ib_conn->state == ISER_CONN_TERMINATING ||
  532. ib_conn->state == ISER_CONN_DOWN))
  533. rc = -1;
  534. iser_info("ib conn %p rc = %d\n", ib_conn, rc);
  535. if (rc > 0)
  536. return 1; /* success, this is the equivalent of POLLOUT */
  537. else if (!rc)
  538. return 0; /* timeout */
  539. else
  540. return rc; /* signal */
  541. }
  542. static void
  543. iscsi_iser_ep_disconnect(struct iscsi_endpoint *ep)
  544. {
  545. struct iser_conn *ib_conn;
  546. ib_conn = ep->dd_data;
  547. iser_info("ep %p ib conn %p state %d\n", ep, ib_conn, ib_conn->state);
  548. iser_conn_terminate(ib_conn);
  549. /*
  550. * if iser_conn and iscsi_conn are bound, we must wait iscsi_conn_stop
  551. * call and ISER_CONN_DOWN state before freeing the iser resources.
  552. * otherwise we are safe to free resources immediately.
  553. */
  554. if (ib_conn->iscsi_conn) {
  555. INIT_WORK(&ib_conn->release_work, iser_release_work);
  556. queue_work(release_wq, &ib_conn->release_work);
  557. } else {
  558. iser_conn_release(ib_conn);
  559. }
  560. }
  561. static umode_t iser_attr_is_visible(int param_type, int param)
  562. {
  563. switch (param_type) {
  564. case ISCSI_HOST_PARAM:
  565. switch (param) {
  566. case ISCSI_HOST_PARAM_NETDEV_NAME:
  567. case ISCSI_HOST_PARAM_HWADDRESS:
  568. case ISCSI_HOST_PARAM_INITIATOR_NAME:
  569. return S_IRUGO;
  570. default:
  571. return 0;
  572. }
  573. case ISCSI_PARAM:
  574. switch (param) {
  575. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  576. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  577. case ISCSI_PARAM_HDRDGST_EN:
  578. case ISCSI_PARAM_DATADGST_EN:
  579. case ISCSI_PARAM_CONN_ADDRESS:
  580. case ISCSI_PARAM_CONN_PORT:
  581. case ISCSI_PARAM_EXP_STATSN:
  582. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  583. case ISCSI_PARAM_PERSISTENT_PORT:
  584. case ISCSI_PARAM_PING_TMO:
  585. case ISCSI_PARAM_RECV_TMO:
  586. case ISCSI_PARAM_INITIAL_R2T_EN:
  587. case ISCSI_PARAM_MAX_R2T:
  588. case ISCSI_PARAM_IMM_DATA_EN:
  589. case ISCSI_PARAM_FIRST_BURST:
  590. case ISCSI_PARAM_MAX_BURST:
  591. case ISCSI_PARAM_PDU_INORDER_EN:
  592. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  593. case ISCSI_PARAM_TARGET_NAME:
  594. case ISCSI_PARAM_TPGT:
  595. case ISCSI_PARAM_USERNAME:
  596. case ISCSI_PARAM_PASSWORD:
  597. case ISCSI_PARAM_USERNAME_IN:
  598. case ISCSI_PARAM_PASSWORD_IN:
  599. case ISCSI_PARAM_FAST_ABORT:
  600. case ISCSI_PARAM_ABORT_TMO:
  601. case ISCSI_PARAM_LU_RESET_TMO:
  602. case ISCSI_PARAM_TGT_RESET_TMO:
  603. case ISCSI_PARAM_IFACE_NAME:
  604. case ISCSI_PARAM_INITIATOR_NAME:
  605. case ISCSI_PARAM_DISCOVERY_SESS:
  606. return S_IRUGO;
  607. default:
  608. return 0;
  609. }
  610. }
  611. return 0;
  612. }
  613. static struct scsi_host_template iscsi_iser_sht = {
  614. .module = THIS_MODULE,
  615. .name = "iSCSI Initiator over iSER",
  616. .queuecommand = iscsi_queuecommand,
  617. .change_queue_depth = iscsi_change_queue_depth,
  618. .sg_tablesize = ISCSI_ISER_SG_TABLESIZE,
  619. .max_sectors = 1024,
  620. .cmd_per_lun = ISER_DEF_CMD_PER_LUN,
  621. .eh_abort_handler = iscsi_eh_abort,
  622. .eh_device_reset_handler= iscsi_eh_device_reset,
  623. .eh_target_reset_handler = iscsi_eh_recover_target,
  624. .target_alloc = iscsi_target_alloc,
  625. .use_clustering = DISABLE_CLUSTERING,
  626. .proc_name = "iscsi_iser",
  627. .this_id = -1,
  628. };
  629. static struct iscsi_transport iscsi_iser_transport = {
  630. .owner = THIS_MODULE,
  631. .name = "iser",
  632. .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_TEXT_NEGO,
  633. /* session management */
  634. .create_session = iscsi_iser_session_create,
  635. .destroy_session = iscsi_iser_session_destroy,
  636. /* connection management */
  637. .create_conn = iscsi_iser_conn_create,
  638. .bind_conn = iscsi_iser_conn_bind,
  639. .destroy_conn = iscsi_conn_teardown,
  640. .attr_is_visible = iser_attr_is_visible,
  641. .set_param = iscsi_iser_set_param,
  642. .get_conn_param = iscsi_conn_get_param,
  643. .get_ep_param = iscsi_iser_get_ep_param,
  644. .get_session_param = iscsi_session_get_param,
  645. .start_conn = iscsi_iser_conn_start,
  646. .stop_conn = iscsi_iser_conn_stop,
  647. /* iscsi host params */
  648. .get_host_param = iscsi_host_get_param,
  649. .set_host_param = iscsi_host_set_param,
  650. /* IO */
  651. .send_pdu = iscsi_conn_send_pdu,
  652. .get_stats = iscsi_iser_conn_get_stats,
  653. .init_task = iscsi_iser_task_init,
  654. .xmit_task = iscsi_iser_task_xmit,
  655. .cleanup_task = iscsi_iser_cleanup_task,
  656. .alloc_pdu = iscsi_iser_pdu_alloc,
  657. .check_protection = iscsi_iser_check_protection,
  658. /* recovery */
  659. .session_recovery_timedout = iscsi_session_recovery_timedout,
  660. .ep_connect = iscsi_iser_ep_connect,
  661. .ep_poll = iscsi_iser_ep_poll,
  662. .ep_disconnect = iscsi_iser_ep_disconnect
  663. };
  664. static int __init iser_init(void)
  665. {
  666. int err;
  667. iser_dbg("Starting iSER datamover...\n");
  668. if (iscsi_max_lun < 1) {
  669. iser_err("Invalid max_lun value of %u\n", iscsi_max_lun);
  670. return -EINVAL;
  671. }
  672. memset(&ig, 0, sizeof(struct iser_global));
  673. ig.desc_cache = kmem_cache_create("iser_descriptors",
  674. sizeof(struct iser_tx_desc),
  675. 0, SLAB_HWCACHE_ALIGN,
  676. NULL);
  677. if (ig.desc_cache == NULL)
  678. return -ENOMEM;
  679. /* device init is called only after the first addr resolution */
  680. mutex_init(&ig.device_list_mutex);
  681. INIT_LIST_HEAD(&ig.device_list);
  682. mutex_init(&ig.connlist_mutex);
  683. INIT_LIST_HEAD(&ig.connlist);
  684. release_wq = alloc_workqueue("release workqueue", 0, 0);
  685. if (!release_wq) {
  686. iser_err("failed to allocate release workqueue\n");
  687. return -ENOMEM;
  688. }
  689. iscsi_iser_scsi_transport = iscsi_register_transport(
  690. &iscsi_iser_transport);
  691. if (!iscsi_iser_scsi_transport) {
  692. iser_err("iscsi_register_transport failed\n");
  693. err = -EINVAL;
  694. goto register_transport_failure;
  695. }
  696. return 0;
  697. register_transport_failure:
  698. kmem_cache_destroy(ig.desc_cache);
  699. return err;
  700. }
  701. static void __exit iser_exit(void)
  702. {
  703. struct iser_conn *ib_conn, *n;
  704. int connlist_empty;
  705. iser_dbg("Removing iSER datamover...\n");
  706. destroy_workqueue(release_wq);
  707. mutex_lock(&ig.connlist_mutex);
  708. connlist_empty = list_empty(&ig.connlist);
  709. mutex_unlock(&ig.connlist_mutex);
  710. if (!connlist_empty) {
  711. iser_err("Error cleanup stage completed but we still have iser "
  712. "connections, destroying them anyway.\n");
  713. list_for_each_entry_safe(ib_conn, n, &ig.connlist, conn_list) {
  714. iser_conn_release(ib_conn);
  715. }
  716. }
  717. iscsi_unregister_transport(&iscsi_iser_transport);
  718. kmem_cache_destroy(ig.desc_cache);
  719. }
  720. module_init(iser_init);
  721. module_exit(iser_exit);