iscsi_iser.c 31 KB

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