Commit | Line | Data |
---|---|---|
c942fddf | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
7ba24713 AA |
2 | /* |
3 | * iSCSI Initiator over TCP/IP Data-Path | |
4 | * | |
5 | * Copyright (C) 2004 Dmitry Yusupov | |
6 | * Copyright (C) 2004 Alex Aizman | |
5bb0b55a MC |
7 | * Copyright (C) 2005 - 2006 Mike Christie |
8 | * Copyright (C) 2006 Red Hat, Inc. All rights reserved. | |
7ba24713 AA |
9 | * maintained by open-iscsi@googlegroups.com |
10 | * | |
7ba24713 AA |
11 | * See the file COPYING included with this distribution for more details. |
12 | * | |
13 | * Credits: | |
14 | * Christoph Hellwig | |
15 | * FUJITA Tomonori | |
16 | * Arne Redlich | |
17 | * Zhenyu Wang | |
18 | */ | |
19 | ||
20 | #include <linux/types.h> | |
7ba24713 | 21 | #include <linux/inet.h> |
5a0e3ad6 | 22 | #include <linux/slab.h> |
f1083048 | 23 | #include <linux/sched/mm.h> |
4e7aba73 | 24 | #include <linux/file.h> |
7ba24713 | 25 | #include <linux/blkdev.h> |
7ba24713 AA |
26 | #include <linux/delay.h> |
27 | #include <linux/kfifo.h> | |
28 | #include <linux/scatterlist.h> | |
acf3368f | 29 | #include <linux/module.h> |
89d0c804 | 30 | #include <linux/backing-dev.h> |
7ba24713 AA |
31 | #include <net/tcp.h> |
32 | #include <scsi/scsi_cmnd.h> | |
d1d81c01 | 33 | #include <scsi/scsi_device.h> |
7ba24713 AA |
34 | #include <scsi/scsi_host.h> |
35 | #include <scsi/scsi.h> | |
36 | #include <scsi/scsi_transport_iscsi.h> | |
c2332b00 | 37 | #include <trace/events/iscsi.h> |
40e0b090 | 38 | #include <trace/events/sock.h> |
7ba24713 AA |
39 | |
40 | #include "iscsi_tcp.h" | |
41 | ||
38e1a8f5 MC |
42 | MODULE_AUTHOR("Mike Christie <michaelc@cs.wisc.edu>, " |
43 | "Dmitry Yusupov <dmitry_yus@yahoo.com>, " | |
7ba24713 AA |
44 | "Alex Aizman <itn780@yahoo.com>"); |
45 | MODULE_DESCRIPTION("iSCSI/TCP data-path"); | |
46 | MODULE_LICENSE("GPL"); | |
7ba24713 | 47 | |
38e1a8f5 | 48 | static struct scsi_transport_template *iscsi_sw_tcp_scsi_transport; |
80602aca | 49 | static const struct scsi_host_template iscsi_sw_tcp_sht; |
38e1a8f5 | 50 | static struct iscsi_transport iscsi_sw_tcp_transport; |
75613521 | 51 | |
c0e4eaae | 52 | static unsigned int iscsi_max_lun = ~0; |
7ba24713 AA |
53 | module_param_named(max_lun, iscsi_max_lun, uint, S_IRUGO); |
54 | ||
f1d26976 MC |
55 | static bool iscsi_recv_from_iscsi_q; |
56 | module_param_named(recv_from_iscsi_q, iscsi_recv_from_iscsi_q, bool, 0644); | |
57 | MODULE_PARM_DESC(recv_from_iscsi_q, "Set to true to read iSCSI data/headers from the iscsi_q workqueue. The default is false which will perform reads from the network softirq context."); | |
58 | ||
c93f87c7 MC |
59 | static int iscsi_sw_tcp_dbg; |
60 | module_param_named(debug_iscsi_tcp, iscsi_sw_tcp_dbg, int, | |
61 | S_IRUGO | S_IWUSR); | |
62 | MODULE_PARM_DESC(debug_iscsi_tcp, "Turn on debugging for iscsi_tcp module " | |
63 | "Set to 1 to turn on, and zero to turn off. Default is off."); | |
64 | ||
65 | #define ISCSI_SW_TCP_DBG(_conn, dbg_fmt, arg...) \ | |
66 | do { \ | |
67 | if (iscsi_sw_tcp_dbg) \ | |
68 | iscsi_conn_printk(KERN_INFO, _conn, \ | |
69 | "%s " dbg_fmt, \ | |
70 | __func__, ##arg); \ | |
c2332b00 FH |
71 | iscsi_dbg_trace(trace_iscsi_dbg_sw_tcp, \ |
72 | &(_conn)->cls_conn->dev, \ | |
73 | "%s " dbg_fmt, __func__, ##arg);\ | |
c93f87c7 MC |
74 | } while (0); |
75 | ||
76 | ||
da32dd68 | 77 | /** |
38e1a8f5 | 78 | * iscsi_sw_tcp_recv - TCP receive in sendfile fashion |
63c62f1c MC |
79 | * @rd_desc: read descriptor |
80 | * @skb: socket buffer | |
81 | * @offset: offset in skb | |
82 | * @len: skb->len - offset | |
38e1a8f5 MC |
83 | */ |
84 | static int iscsi_sw_tcp_recv(read_descriptor_t *rd_desc, struct sk_buff *skb, | |
85 | unsigned int offset, size_t len) | |
63c62f1c MC |
86 | { |
87 | struct iscsi_conn *conn = rd_desc->arg.data; | |
88 | unsigned int consumed, total_consumed = 0; | |
89 | int status; | |
90 | ||
c93f87c7 | 91 | ISCSI_SW_TCP_DBG(conn, "in %d bytes\n", skb->len - offset); |
63c62f1c MC |
92 | |
93 | do { | |
94 | status = 0; | |
95 | consumed = iscsi_tcp_recv_skb(conn, skb, offset, 0, &status); | |
96 | offset += consumed; | |
97 | total_consumed += consumed; | |
98 | } while (consumed != 0 && status != ISCSI_TCP_SKB_DONE); | |
99 | ||
c93f87c7 MC |
100 | ISCSI_SW_TCP_DBG(conn, "read %d bytes status %d\n", |
101 | skb->len - offset, status); | |
63c62f1c | 102 | return total_consumed; |
7ba24713 AA |
103 | } |
104 | ||
523eeac6 HR |
105 | /** |
106 | * iscsi_sw_sk_state_check - check socket state | |
107 | * @sk: socket | |
108 | * | |
109 | * If the socket is in CLOSE or CLOSE_WAIT we should | |
110 | * not close the connection if there is still some | |
111 | * data pending. | |
03adb5f9 MC |
112 | * |
113 | * Must be called with sk_callback_lock. | |
523eeac6 HR |
114 | */ |
115 | static inline int iscsi_sw_sk_state_check(struct sock *sk) | |
116 | { | |
03adb5f9 | 117 | struct iscsi_conn *conn = sk->sk_user_data; |
523eeac6 | 118 | |
d1af8a32 | 119 | if ((sk->sk_state == TCP_CLOSE_WAIT || sk->sk_state == TCP_CLOSE) && |
c712495e | 120 | (conn->session->state != ISCSI_STATE_LOGGING_OUT) && |
d1af8a32 MC |
121 | !atomic_read(&sk->sk_rmem_alloc)) { |
122 | ISCSI_SW_TCP_DBG(conn, "TCP_CLOSE|TCP_CLOSE_WAIT\n"); | |
123 | iscsi_conn_failure(conn, ISCSI_ERR_TCP_CONN_CLOSE); | |
124 | return -ECONNRESET; | |
125 | } | |
523eeac6 HR |
126 | return 0; |
127 | } | |
128 | ||
f1d26976 | 129 | static void iscsi_sw_tcp_recv_data(struct iscsi_conn *conn) |
7ba24713 | 130 | { |
f1d26976 MC |
131 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
132 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; | |
133 | struct sock *sk = tcp_sw_conn->sock->sk; | |
7ba24713 AA |
134 | read_descriptor_t rd_desc; |
135 | ||
665b44ae | 136 | /* |
da32dd68 | 137 | * Use rd_desc to pass 'conn' to iscsi_tcp_recv. |
665b44ae | 138 | * We set count to 1 because we want the network layer to |
da32dd68 | 139 | * hand us all the skbs that are available. iscsi_tcp_recv |
665b44ae MC |
140 | * handled pdus that cross buffers or pdus that still need data. |
141 | */ | |
7ba24713 | 142 | rd_desc.arg.data = conn; |
665b44ae | 143 | rd_desc.count = 1; |
7ba24713 | 144 | |
f1d26976 | 145 | tcp_read_sock(sk, &rd_desc, iscsi_sw_tcp_recv); |
523eeac6 | 146 | |
da32dd68 OK |
147 | /* If we had to (atomically) map a highmem page, |
148 | * unmap it now. */ | |
a8ac6311 | 149 | iscsi_tcp_segment_unmap(&tcp_conn->in.segment); |
f1d26976 MC |
150 | |
151 | iscsi_sw_sk_state_check(sk); | |
152 | } | |
153 | ||
154 | static void iscsi_sw_tcp_recv_data_work(struct work_struct *work) | |
155 | { | |
156 | struct iscsi_conn *conn = container_of(work, struct iscsi_conn, | |
157 | recvwork); | |
158 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
159 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; | |
160 | struct sock *sk = tcp_sw_conn->sock->sk; | |
161 | ||
162 | lock_sock(sk); | |
163 | iscsi_sw_tcp_recv_data(conn); | |
164 | release_sock(sk); | |
165 | } | |
166 | ||
167 | static void iscsi_sw_tcp_data_ready(struct sock *sk) | |
168 | { | |
169 | struct iscsi_sw_tcp_conn *tcp_sw_conn; | |
170 | struct iscsi_tcp_conn *tcp_conn; | |
171 | struct iscsi_conn *conn; | |
172 | ||
40e0b090 PY |
173 | trace_sk_data_ready(sk); |
174 | ||
f1d26976 MC |
175 | read_lock_bh(&sk->sk_callback_lock); |
176 | conn = sk->sk_user_data; | |
177 | if (!conn) { | |
178 | read_unlock_bh(&sk->sk_callback_lock); | |
179 | return; | |
180 | } | |
181 | tcp_conn = conn->dd_data; | |
182 | tcp_sw_conn = tcp_conn->dd_data; | |
183 | ||
184 | if (tcp_sw_conn->queue_recv) | |
185 | iscsi_conn_queue_recv(conn); | |
186 | else | |
187 | iscsi_sw_tcp_recv_data(conn); | |
7cb001d4 | 188 | read_unlock_bh(&sk->sk_callback_lock); |
7ba24713 AA |
189 | } |
190 | ||
38e1a8f5 | 191 | static void iscsi_sw_tcp_state_change(struct sock *sk) |
7ba24713 | 192 | { |
5bb0b55a | 193 | struct iscsi_tcp_conn *tcp_conn; |
38e1a8f5 | 194 | struct iscsi_sw_tcp_conn *tcp_sw_conn; |
7ba24713 | 195 | struct iscsi_conn *conn; |
7ba24713 AA |
196 | void (*old_state_change)(struct sock *); |
197 | ||
7cb001d4 | 198 | read_lock_bh(&sk->sk_callback_lock); |
03adb5f9 MC |
199 | conn = sk->sk_user_data; |
200 | if (!conn) { | |
7cb001d4 | 201 | read_unlock_bh(&sk->sk_callback_lock); |
03adb5f9 MC |
202 | return; |
203 | } | |
7ba24713 | 204 | |
d1af8a32 | 205 | iscsi_sw_sk_state_check(sk); |
7ba24713 | 206 | |
5bb0b55a | 207 | tcp_conn = conn->dd_data; |
38e1a8f5 MC |
208 | tcp_sw_conn = tcp_conn->dd_data; |
209 | old_state_change = tcp_sw_conn->old_state_change; | |
7ba24713 | 210 | |
7cb001d4 | 211 | read_unlock_bh(&sk->sk_callback_lock); |
7ba24713 AA |
212 | |
213 | old_state_change(sk); | |
214 | } | |
215 | ||
216 | /** | |
ae6b4e69 | 217 | * iscsi_sw_tcp_write_space - Called when more output buffer space is available |
7ba24713 AA |
218 | * @sk: socket space is available for |
219 | **/ | |
38e1a8f5 | 220 | static void iscsi_sw_tcp_write_space(struct sock *sk) |
7ba24713 | 221 | { |
03adb5f9 MC |
222 | struct iscsi_conn *conn; |
223 | struct iscsi_tcp_conn *tcp_conn; | |
224 | struct iscsi_sw_tcp_conn *tcp_sw_conn; | |
225 | void (*old_write_space)(struct sock *); | |
226 | ||
227 | read_lock_bh(&sk->sk_callback_lock); | |
228 | conn = sk->sk_user_data; | |
229 | if (!conn) { | |
230 | read_unlock_bh(&sk->sk_callback_lock); | |
231 | return; | |
232 | } | |
233 | ||
234 | tcp_conn = conn->dd_data; | |
235 | tcp_sw_conn = tcp_conn->dd_data; | |
236 | old_write_space = tcp_sw_conn->old_write_space; | |
237 | read_unlock_bh(&sk->sk_callback_lock); | |
238 | ||
239 | old_write_space(sk); | |
5bb0b55a | 240 | |
c93f87c7 | 241 | ISCSI_SW_TCP_DBG(conn, "iscsi_write_space\n"); |
4b9f8ce4 | 242 | iscsi_conn_queue_xmit(conn); |
7ba24713 AA |
243 | } |
244 | ||
38e1a8f5 | 245 | static void iscsi_sw_tcp_conn_set_callbacks(struct iscsi_conn *conn) |
7ba24713 | 246 | { |
5bb0b55a | 247 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
38e1a8f5 MC |
248 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
249 | struct sock *sk = tcp_sw_conn->sock->sk; | |
7ba24713 AA |
250 | |
251 | /* assign new callbacks */ | |
252 | write_lock_bh(&sk->sk_callback_lock); | |
253 | sk->sk_user_data = conn; | |
38e1a8f5 MC |
254 | tcp_sw_conn->old_data_ready = sk->sk_data_ready; |
255 | tcp_sw_conn->old_state_change = sk->sk_state_change; | |
256 | tcp_sw_conn->old_write_space = sk->sk_write_space; | |
257 | sk->sk_data_ready = iscsi_sw_tcp_data_ready; | |
258 | sk->sk_state_change = iscsi_sw_tcp_state_change; | |
259 | sk->sk_write_space = iscsi_sw_tcp_write_space; | |
7ba24713 AA |
260 | write_unlock_bh(&sk->sk_callback_lock); |
261 | } | |
262 | ||
38e1a8f5 | 263 | static void |
c484a50a | 264 | iscsi_sw_tcp_conn_restore_callbacks(struct iscsi_conn *conn) |
7ba24713 | 265 | { |
c484a50a AK |
266 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
267 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; | |
38e1a8f5 | 268 | struct sock *sk = tcp_sw_conn->sock->sk; |
7ba24713 AA |
269 | |
270 | /* restore socket callbacks, see also: iscsi_conn_set_callbacks() */ | |
271 | write_lock_bh(&sk->sk_callback_lock); | |
272 | sk->sk_user_data = NULL; | |
38e1a8f5 MC |
273 | sk->sk_data_ready = tcp_sw_conn->old_data_ready; |
274 | sk->sk_state_change = tcp_sw_conn->old_state_change; | |
275 | sk->sk_write_space = tcp_sw_conn->old_write_space; | |
28448b80 | 276 | sk->sk_no_check_tx = 0; |
7ba24713 AA |
277 | write_unlock_bh(&sk->sk_callback_lock); |
278 | } | |
279 | ||
280 | /** | |
38e1a8f5 | 281 | * iscsi_sw_tcp_xmit_segment - transmit segment |
6df19a79 | 282 | * @tcp_conn: the iSCSI TCP connection |
38e1a8f5 MC |
283 | * @segment: the buffer to transmnit |
284 | * | |
285 | * This function transmits as much of the buffer as | |
286 | * the network layer will accept, and returns the number of | |
287 | * bytes transmitted. | |
288 | * | |
289 | * If CRC hashing is enabled, the function will compute the | |
290 | * hash as it goes. When the entire segment has been transmitted, | |
291 | * it will retrieve the hash value and send it as well. | |
292 | */ | |
6df19a79 | 293 | static int iscsi_sw_tcp_xmit_segment(struct iscsi_tcp_conn *tcp_conn, |
38e1a8f5 MC |
294 | struct iscsi_segment *segment) |
295 | { | |
6df19a79 | 296 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
38e1a8f5 MC |
297 | struct socket *sk = tcp_sw_conn->sock; |
298 | unsigned int copied = 0; | |
299 | int r = 0; | |
300 | ||
6df19a79 | 301 | while (!iscsi_tcp_segment_done(tcp_conn, segment, 0, r)) { |
38e1a8f5 | 302 | struct scatterlist *sg; |
fa8df343 DH |
303 | struct msghdr msg = {}; |
304 | struct bio_vec bv; | |
38e1a8f5 | 305 | unsigned int offset, copy; |
38e1a8f5 MC |
306 | |
307 | r = 0; | |
308 | offset = segment->copied; | |
309 | copy = segment->size - offset; | |
310 | ||
311 | if (segment->total_copied + segment->size < segment->total_size) | |
fa8df343 | 312 | msg.msg_flags |= MSG_MORE; |
38e1a8f5 | 313 | |
f1d26976 | 314 | if (tcp_sw_conn->queue_recv) |
fa8df343 | 315 | msg.msg_flags |= MSG_DONTWAIT; |
f1d26976 | 316 | |
38e1a8f5 | 317 | if (!segment->data) { |
fa8df343 DH |
318 | if (!tcp_conn->iscsi_conn->datadgst_en) |
319 | msg.msg_flags |= MSG_SPLICE_PAGES; | |
38e1a8f5 MC |
320 | sg = segment->sg; |
321 | offset += segment->sg_offset + sg->offset; | |
fa8df343 | 322 | bvec_set_page(&bv, sg_page(sg), copy, offset); |
38e1a8f5 | 323 | } else { |
fa8df343 | 324 | bvec_set_virt(&bv, segment->data + offset, copy); |
38e1a8f5 | 325 | } |
fa8df343 | 326 | iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bv, 1, copy); |
38e1a8f5 | 327 | |
fa8df343 | 328 | r = sock_sendmsg(sk, &msg); |
38e1a8f5 MC |
329 | if (r < 0) { |
330 | iscsi_tcp_segment_unmap(segment); | |
38e1a8f5 MC |
331 | return r; |
332 | } | |
333 | copied += r; | |
334 | } | |
335 | return copied; | |
336 | } | |
337 | ||
338 | /** | |
339 | * iscsi_sw_tcp_xmit - TCP transmit | |
ccd4a430 | 340 | * @conn: iscsi connection |
a8ac6311 | 341 | **/ |
38e1a8f5 | 342 | static int iscsi_sw_tcp_xmit(struct iscsi_conn *conn) |
7ba24713 | 343 | { |
5bb0b55a | 344 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
38e1a8f5 MC |
345 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
346 | struct iscsi_segment *segment = &tcp_sw_conn->out.segment; | |
a8ac6311 OK |
347 | unsigned int consumed = 0; |
348 | int rc = 0; | |
7ba24713 | 349 | |
a8ac6311 | 350 | while (1) { |
6df19a79 | 351 | rc = iscsi_sw_tcp_xmit_segment(tcp_conn, segment); |
32382492 MC |
352 | /* |
353 | * We may not have been able to send data because the conn | |
25985edc | 354 | * is getting stopped. libiscsi will know so propagate err |
32382492 MC |
355 | * for it to do the right thing. |
356 | */ | |
357 | if (rc == -EAGAIN) | |
358 | return rc; | |
359 | else if (rc < 0) { | |
6f481e3c | 360 | rc = ISCSI_ERR_XMIT_FAILED; |
a8ac6311 | 361 | goto error; |
32382492 | 362 | } else if (rc == 0) |
a8ac6311 OK |
363 | break; |
364 | ||
365 | consumed += rc; | |
366 | ||
367 | if (segment->total_copied >= segment->total_size) { | |
368 | if (segment->done != NULL) { | |
369 | rc = segment->done(tcp_conn, segment); | |
6f481e3c | 370 | if (rc != 0) |
a8ac6311 OK |
371 | goto error; |
372 | } | |
373 | } | |
3219e529 MC |
374 | } |
375 | ||
c93f87c7 | 376 | ISCSI_SW_TCP_DBG(conn, "xmit %d bytes\n", consumed); |
a8ac6311 OK |
377 | |
378 | conn->txdata_octets += consumed; | |
379 | return consumed; | |
380 | ||
381 | error: | |
382 | /* Transmit error. We could initiate error recovery | |
383 | * here. */ | |
c93f87c7 | 384 | ISCSI_SW_TCP_DBG(conn, "Error sending PDU, errno=%d\n", rc); |
6f481e3c MC |
385 | iscsi_conn_failure(conn, rc); |
386 | return -EIO; | |
7ba24713 AA |
387 | } |
388 | ||
389 | /** | |
ae6b4e69 | 390 | * iscsi_sw_tcp_xmit_qlen - return the number of bytes queued for xmit |
ccd4a430 | 391 | * @conn: iscsi connection |
a8ac6311 | 392 | */ |
38e1a8f5 | 393 | static inline int iscsi_sw_tcp_xmit_qlen(struct iscsi_conn *conn) |
7ba24713 | 394 | { |
a8ac6311 | 395 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
38e1a8f5 MC |
396 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
397 | struct iscsi_segment *segment = &tcp_sw_conn->out.segment; | |
7ba24713 | 398 | |
a8ac6311 | 399 | return segment->total_copied - segment->total_size; |
7ba24713 AA |
400 | } |
401 | ||
38e1a8f5 | 402 | static int iscsi_sw_tcp_pdu_xmit(struct iscsi_task *task) |
7ba24713 | 403 | { |
e5a7efef | 404 | struct iscsi_conn *conn = task->conn; |
f1083048 | 405 | unsigned int noreclaim_flag; |
238191d6 AP |
406 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
407 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; | |
9e45dd73 MC |
408 | int rc = 0; |
409 | ||
238191d6 AP |
410 | if (!tcp_sw_conn->sock) { |
411 | iscsi_conn_printk(KERN_ERR, conn, | |
412 | "Transport not bound to socket!\n"); | |
413 | return -EINVAL; | |
414 | } | |
415 | ||
f1083048 | 416 | noreclaim_flag = memalloc_noreclaim_save(); |
a8ac6311 | 417 | |
38e1a8f5 MC |
418 | while (iscsi_sw_tcp_xmit_qlen(conn)) { |
419 | rc = iscsi_sw_tcp_xmit(conn); | |
9e45dd73 MC |
420 | if (rc == 0) { |
421 | rc = -EAGAIN; | |
422 | break; | |
423 | } | |
a8ac6311 | 424 | if (rc < 0) |
9e45dd73 MC |
425 | break; |
426 | rc = 0; | |
3219e529 | 427 | } |
7ba24713 | 428 | |
f1083048 | 429 | memalloc_noreclaim_restore(noreclaim_flag); |
9e45dd73 | 430 | return rc; |
7ba24713 AA |
431 | } |
432 | ||
a8ac6311 OK |
433 | /* |
434 | * This is called when we're done sending the header. | |
435 | * Simply copy the data_segment to the send segment, and return. | |
436 | */ | |
38e1a8f5 MC |
437 | static int iscsi_sw_tcp_send_hdr_done(struct iscsi_tcp_conn *tcp_conn, |
438 | struct iscsi_segment *segment) | |
7ba24713 | 439 | { |
38e1a8f5 MC |
440 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
441 | ||
442 | tcp_sw_conn->out.segment = tcp_sw_conn->out.data_segment; | |
c93f87c7 MC |
443 | ISCSI_SW_TCP_DBG(tcp_conn->iscsi_conn, |
444 | "Header done. Next segment size %u total_size %u\n", | |
445 | tcp_sw_conn->out.segment.size, | |
446 | tcp_sw_conn->out.segment.total_size); | |
a8ac6311 OK |
447 | return 0; |
448 | } | |
449 | ||
38e1a8f5 MC |
450 | static void iscsi_sw_tcp_send_hdr_prep(struct iscsi_conn *conn, void *hdr, |
451 | size_t hdrlen) | |
a8ac6311 OK |
452 | { |
453 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
38e1a8f5 | 454 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
a8ac6311 | 455 | |
c93f87c7 MC |
456 | ISCSI_SW_TCP_DBG(conn, "%s\n", conn->hdrdgst_en ? |
457 | "digest enabled" : "digest disabled"); | |
a8ac6311 OK |
458 | |
459 | /* Clear the data segment - needs to be filled in by the | |
460 | * caller using iscsi_tcp_send_data_prep() */ | |
38e1a8f5 MC |
461 | memset(&tcp_sw_conn->out.data_segment, 0, |
462 | sizeof(struct iscsi_segment)); | |
a8ac6311 OK |
463 | |
464 | /* If header digest is enabled, compute the CRC and | |
465 | * place the digest into the same buffer. We make | |
135a8ad4 | 466 | * sure that both iscsi_tcp_task and mtask have |
a8ac6311 OK |
467 | * sufficient room. |
468 | */ | |
469 | if (conn->hdrdgst_en) { | |
92186c14 | 470 | iscsi_tcp_dgst_header(hdr, hdrlen, hdr + hdrlen); |
a8ac6311 OK |
471 | hdrlen += ISCSI_DIGEST_SIZE; |
472 | } | |
473 | ||
474 | /* Remember header pointer for later, when we need | |
475 | * to decide whether there's a payload to go along | |
476 | * with the header. */ | |
38e1a8f5 | 477 | tcp_sw_conn->out.hdr = hdr; |
a8ac6311 | 478 | |
38e1a8f5 MC |
479 | iscsi_segment_init_linear(&tcp_sw_conn->out.segment, hdr, hdrlen, |
480 | iscsi_sw_tcp_send_hdr_done, NULL); | |
a8ac6311 OK |
481 | } |
482 | ||
483 | /* | |
484 | * Prepare the send buffer for the payload data. | |
485 | * Padding and checksumming will all be taken care | |
486 | * of by the iscsi_segment routines. | |
487 | */ | |
488 | static int | |
38e1a8f5 MC |
489 | iscsi_sw_tcp_send_data_prep(struct iscsi_conn *conn, struct scatterlist *sg, |
490 | unsigned int count, unsigned int offset, | |
491 | unsigned int len) | |
a8ac6311 OK |
492 | { |
493 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
38e1a8f5 | 494 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
92186c14 | 495 | u32 *tx_crcp = NULL; |
a8ac6311 OK |
496 | unsigned int hdr_spec_len; |
497 | ||
c93f87c7 MC |
498 | ISCSI_SW_TCP_DBG(conn, "offset=%d, datalen=%d %s\n", offset, len, |
499 | conn->datadgst_en ? | |
500 | "digest enabled" : "digest disabled"); | |
a8ac6311 OK |
501 | |
502 | /* Make sure the datalen matches what the caller | |
503 | said he would send. */ | |
38e1a8f5 | 504 | hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength); |
a8ac6311 OK |
505 | WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len)); |
506 | ||
507 | if (conn->datadgst_en) | |
92186c14 | 508 | tx_crcp = &tcp_sw_conn->tx_crc; |
a8ac6311 | 509 | |
38e1a8f5 | 510 | return iscsi_segment_seek_sg(&tcp_sw_conn->out.data_segment, |
92186c14 | 511 | sg, count, offset, len, NULL, tx_crcp); |
a8ac6311 OK |
512 | } |
513 | ||
514 | static void | |
38e1a8f5 | 515 | iscsi_sw_tcp_send_linear_data_prep(struct iscsi_conn *conn, void *data, |
a8ac6311 OK |
516 | size_t len) |
517 | { | |
518 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
38e1a8f5 | 519 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
92186c14 | 520 | u32 *tx_crcp = NULL; |
a8ac6311 OK |
521 | unsigned int hdr_spec_len; |
522 | ||
c93f87c7 MC |
523 | ISCSI_SW_TCP_DBG(conn, "datalen=%zd %s\n", len, conn->datadgst_en ? |
524 | "digest enabled" : "digest disabled"); | |
a8ac6311 OK |
525 | |
526 | /* Make sure the datalen matches what the caller | |
527 | said he would send. */ | |
38e1a8f5 | 528 | hdr_spec_len = ntoh24(tcp_sw_conn->out.hdr->dlength); |
a8ac6311 OK |
529 | WARN_ON(iscsi_padded(len) != iscsi_padded(hdr_spec_len)); |
530 | ||
531 | if (conn->datadgst_en) | |
92186c14 | 532 | tx_crcp = &tcp_sw_conn->tx_crc; |
a8ac6311 | 533 | |
38e1a8f5 | 534 | iscsi_segment_init_linear(&tcp_sw_conn->out.data_segment, |
92186c14 | 535 | data, len, NULL, tx_crcp); |
7ba24713 AA |
536 | } |
537 | ||
38e1a8f5 MC |
538 | static int iscsi_sw_tcp_pdu_init(struct iscsi_task *task, |
539 | unsigned int offset, unsigned int count) | |
e5a7efef MC |
540 | { |
541 | struct iscsi_conn *conn = task->conn; | |
542 | int err = 0; | |
543 | ||
38e1a8f5 | 544 | iscsi_sw_tcp_send_hdr_prep(conn, task->hdr, task->hdr_len); |
e5a7efef MC |
545 | |
546 | if (!count) | |
547 | return 0; | |
548 | ||
549 | if (!task->sc) | |
38e1a8f5 | 550 | iscsi_sw_tcp_send_linear_data_prep(conn, task->data, count); |
e5a7efef | 551 | else { |
ae3d56d8 | 552 | struct scsi_data_buffer *sdb = &task->sc->sdb; |
e5a7efef | 553 | |
38e1a8f5 MC |
554 | err = iscsi_sw_tcp_send_data_prep(conn, sdb->table.sgl, |
555 | sdb->table.nents, offset, | |
556 | count); | |
e5a7efef MC |
557 | } |
558 | ||
559 | if (err) { | |
9a6510eb | 560 | /* got invalid offset/len */ |
e5a7efef MC |
561 | return -EIO; |
562 | } | |
563 | return 0; | |
564 | } | |
565 | ||
2ff79d52 | 566 | static int iscsi_sw_tcp_pdu_alloc(struct iscsi_task *task, uint8_t opcode) |
7ba24713 | 567 | { |
135a8ad4 | 568 | struct iscsi_tcp_task *tcp_task = task->dd_data; |
7ba24713 | 569 | |
38e1a8f5 MC |
570 | task->hdr = task->dd_data + sizeof(*tcp_task); |
571 | task->hdr_max = sizeof(struct iscsi_sw_tcp_hdrbuf) - ISCSI_DIGEST_SIZE; | |
a8ac6311 | 572 | return 0; |
7ba24713 AA |
573 | } |
574 | ||
5bb0b55a | 575 | static struct iscsi_cls_conn * |
38e1a8f5 MC |
576 | iscsi_sw_tcp_conn_create(struct iscsi_cls_session *cls_session, |
577 | uint32_t conn_idx) | |
7ba24713 | 578 | { |
5bb0b55a MC |
579 | struct iscsi_conn *conn; |
580 | struct iscsi_cls_conn *cls_conn; | |
581 | struct iscsi_tcp_conn *tcp_conn; | |
38e1a8f5 | 582 | struct iscsi_sw_tcp_conn *tcp_sw_conn; |
7ba24713 | 583 | |
38e1a8f5 MC |
584 | cls_conn = iscsi_tcp_conn_setup(cls_session, sizeof(*tcp_sw_conn), |
585 | conn_idx); | |
5bb0b55a MC |
586 | if (!cls_conn) |
587 | return NULL; | |
588 | conn = cls_conn->dd_data; | |
5d91e209 | 589 | tcp_conn = conn->dd_data; |
38e1a8f5 | 590 | tcp_sw_conn = tcp_conn->dd_data; |
f1d26976 MC |
591 | INIT_WORK(&conn->recvwork, iscsi_sw_tcp_recv_data_work); |
592 | tcp_sw_conn->queue_recv = iscsi_recv_from_iscsi_q; | |
7ba24713 | 593 | |
57569c37 | 594 | mutex_init(&tcp_sw_conn->sock_lock); |
92186c14 | 595 | tcp_conn->rx_crcp = &tcp_sw_conn->rx_crc; |
dd8c0d95 | 596 | |
5bb0b55a | 597 | return cls_conn; |
7ba24713 AA |
598 | } |
599 | ||
38e1a8f5 | 600 | static void iscsi_sw_tcp_release_conn(struct iscsi_conn *conn) |
1c83469d MC |
601 | { |
602 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
38e1a8f5 MC |
603 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
604 | struct socket *sock = tcp_sw_conn->sock; | |
1c83469d | 605 | |
57569c37 MC |
606 | /* |
607 | * The iscsi transport class will make sure we are not called in | |
608 | * parallel with start, stop, bind and destroys. However, this can be | |
609 | * called twice if userspace does a stop then a destroy. | |
610 | */ | |
22236961 | 611 | if (!sock) |
1c83469d MC |
612 | return; |
613 | ||
788b71c5 MC |
614 | /* |
615 | * Make sure we start socket shutdown now in case userspace is up | |
616 | * but delayed in releasing the socket. | |
617 | */ | |
618 | kernel_sock_shutdown(sock, SHUT_RDWR); | |
619 | ||
22236961 | 620 | sock_hold(sock->sk); |
c484a50a | 621 | iscsi_sw_tcp_conn_restore_callbacks(conn); |
22236961 | 622 | sock_put(sock->sk); |
1c83469d | 623 | |
f1d26976 MC |
624 | iscsi_suspend_rx(conn); |
625 | ||
57569c37 | 626 | mutex_lock(&tcp_sw_conn->sock_lock); |
38e1a8f5 | 627 | tcp_sw_conn->sock = NULL; |
57569c37 | 628 | mutex_unlock(&tcp_sw_conn->sock_lock); |
22236961 | 629 | sockfd_put(sock); |
1c83469d MC |
630 | } |
631 | ||
38e1a8f5 | 632 | static void iscsi_sw_tcp_conn_destroy(struct iscsi_cls_conn *cls_conn) |
7ba24713 | 633 | { |
5bb0b55a | 634 | struct iscsi_conn *conn = cls_conn->dd_data; |
7ba24713 | 635 | |
38e1a8f5 | 636 | iscsi_sw_tcp_release_conn(conn); |
38e1a8f5 | 637 | iscsi_tcp_conn_teardown(cls_conn); |
5bb0b55a | 638 | } |
7ba24713 | 639 | |
38e1a8f5 | 640 | static void iscsi_sw_tcp_conn_stop(struct iscsi_cls_conn *cls_conn, int flag) |
1c83469d MC |
641 | { |
642 | struct iscsi_conn *conn = cls_conn->dd_data; | |
913e5bf4 | 643 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
38e1a8f5 | 644 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
b64e77f7 | 645 | struct socket *sock = tcp_sw_conn->sock; |
913e5bf4 MC |
646 | |
647 | /* userspace may have goofed up and not bound us */ | |
b64e77f7 | 648 | if (!sock) |
913e5bf4 | 649 | return; |
1c83469d | 650 | |
8c38a295 MC |
651 | sock->sk->sk_err = EIO; |
652 | wake_up_interruptible(sk_sleep(sock->sk)); | |
b64e77f7 | 653 | |
03adb5f9 MC |
654 | /* stop xmit side */ |
655 | iscsi_suspend_tx(conn); | |
656 | ||
657 | /* stop recv side and release socket */ | |
38e1a8f5 | 658 | iscsi_sw_tcp_release_conn(conn); |
03adb5f9 MC |
659 | |
660 | iscsi_conn_stop(cls_conn, flag); | |
1c83469d MC |
661 | } |
662 | ||
5bb0b55a | 663 | static int |
38e1a8f5 MC |
664 | iscsi_sw_tcp_conn_bind(struct iscsi_cls_session *cls_session, |
665 | struct iscsi_cls_conn *cls_conn, uint64_t transport_eph, | |
666 | int is_leading) | |
5bb0b55a MC |
667 | { |
668 | struct iscsi_conn *conn = cls_conn->dd_data; | |
669 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; | |
38e1a8f5 | 670 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
5bb0b55a MC |
671 | struct sock *sk; |
672 | struct socket *sock; | |
673 | int err; | |
7ba24713 | 674 | |
5bb0b55a | 675 | /* lookup for existing socket */ |
264faaaa | 676 | sock = sockfd_lookup((int)transport_eph, &err); |
5bb0b55a | 677 | if (!sock) { |
322d739d MC |
678 | iscsi_conn_printk(KERN_ERR, conn, |
679 | "sockfd_lookup failed %d\n", err); | |
5bb0b55a | 680 | return -EEXIST; |
7ba24713 AA |
681 | } |
682 | ||
f4f82c52 ED |
683 | err = -EINVAL; |
684 | if (!sk_is_tcp(sock->sk)) | |
685 | goto free_socket; | |
686 | ||
5bb0b55a MC |
687 | err = iscsi_conn_bind(cls_session, cls_conn, is_leading); |
688 | if (err) | |
22236961 | 689 | goto free_socket; |
7ba24713 | 690 | |
57569c37 | 691 | mutex_lock(&tcp_sw_conn->sock_lock); |
67a61114 | 692 | /* bind iSCSI connection and socket */ |
38e1a8f5 | 693 | tcp_sw_conn->sock = sock; |
57569c37 | 694 | mutex_unlock(&tcp_sw_conn->sock_lock); |
7ba24713 | 695 | |
67a61114 MC |
696 | /* setup Socket parameters */ |
697 | sk = sock->sk; | |
4a17fd52 | 698 | sk->sk_reuse = SK_CAN_REUSE; |
67a61114 MC |
699 | sk->sk_sndtimeo = 15 * HZ; /* FIXME: make it configurable */ |
700 | sk->sk_allocation = GFP_ATOMIC; | |
98123866 | 701 | sk->sk_use_task_frag = false; |
9e45dd73 | 702 | sk_set_memalloc(sk); |
c0920cd3 | 703 | sock_no_linger(sk); |
7ba24713 | 704 | |
38e1a8f5 | 705 | iscsi_sw_tcp_conn_set_callbacks(conn); |
67a61114 MC |
706 | /* |
707 | * set receive state machine into initial state | |
708 | */ | |
da32dd68 | 709 | iscsi_tcp_hdr_recv_prep(tcp_conn); |
7ba24713 | 710 | return 0; |
22236961 MC |
711 | |
712 | free_socket: | |
713 | sockfd_put(sock); | |
714 | return err; | |
7ba24713 AA |
715 | } |
716 | ||
38e1a8f5 MC |
717 | static int iscsi_sw_tcp_conn_set_param(struct iscsi_cls_conn *cls_conn, |
718 | enum iscsi_param param, char *buf, | |
719 | int buflen) | |
7ba24713 | 720 | { |
7b7232f3 | 721 | struct iscsi_conn *conn = cls_conn->dd_data; |
5bb0b55a | 722 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
38e1a8f5 | 723 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
7ba24713 | 724 | |
7ba24713 | 725 | switch(param) { |
7ba24713 | 726 | case ISCSI_PARAM_HDRDGST_EN: |
5c75b7fc | 727 | iscsi_set_param(cls_conn, param, buf, buflen); |
7ba24713 AA |
728 | break; |
729 | case ISCSI_PARAM_DATADGST_EN: | |
57569c37 MC |
730 | mutex_lock(&tcp_sw_conn->sock_lock); |
731 | if (!tcp_sw_conn->sock) { | |
732 | mutex_unlock(&tcp_sw_conn->sock_lock); | |
733 | return -ENOTCONN; | |
734 | } | |
48b19b79 | 735 | iscsi_set_param(cls_conn, param, buf, buflen); |
57569c37 | 736 | mutex_unlock(&tcp_sw_conn->sock_lock); |
7ba24713 | 737 | break; |
7ba24713 | 738 | case ISCSI_PARAM_MAX_R2T: |
1304be5f | 739 | return iscsi_tcp_set_max_r2t(conn, buf); |
7ba24713 | 740 | default: |
5c75b7fc | 741 | return iscsi_set_param(cls_conn, param, buf, buflen); |
7ba24713 AA |
742 | } |
743 | ||
744 | return 0; | |
745 | } | |
746 | ||
38e1a8f5 MC |
747 | static int iscsi_sw_tcp_conn_get_param(struct iscsi_cls_conn *cls_conn, |
748 | enum iscsi_param param, char *buf) | |
7b8631b5 | 749 | { |
7b7232f3 | 750 | struct iscsi_conn *conn = cls_conn->dd_data; |
57569c37 MC |
751 | struct iscsi_sw_tcp_conn *tcp_sw_conn; |
752 | struct iscsi_tcp_conn *tcp_conn; | |
a79af8a6 | 753 | struct sockaddr_in6 addr; |
bcf3a295 | 754 | struct socket *sock; |
9b2c45d4 | 755 | int rc; |
7b8631b5 MC |
756 | |
757 | switch(param) { | |
fd7255f5 | 758 | case ISCSI_PARAM_CONN_PORT: |
fd7255f5 | 759 | case ISCSI_PARAM_CONN_ADDRESS: |
4bfb8ebf | 760 | case ISCSI_PARAM_LOCAL_PORT: |
659743b0 | 761 | spin_lock_bh(&conn->session->frwd_lock); |
57569c37 | 762 | if (!conn->session->leadconn) { |
659743b0 | 763 | spin_unlock_bh(&conn->session->frwd_lock); |
a79af8a6 MC |
764 | return -ENOTCONN; |
765 | } | |
57569c37 MC |
766 | /* |
767 | * The conn has been setup and bound, so just grab a ref | |
768 | * incase a destroy runs while we are in the net layer. | |
769 | */ | |
770 | iscsi_get_conn(conn->cls_conn); | |
bcf3a295 MM |
771 | spin_unlock_bh(&conn->session->frwd_lock); |
772 | ||
57569c37 MC |
773 | tcp_conn = conn->dd_data; |
774 | tcp_sw_conn = tcp_conn->dd_data; | |
775 | ||
776 | mutex_lock(&tcp_sw_conn->sock_lock); | |
777 | sock = tcp_sw_conn->sock; | |
778 | if (!sock) { | |
779 | rc = -ENOTCONN; | |
780 | goto sock_unlock; | |
781 | } | |
782 | ||
4bfb8ebf | 783 | if (param == ISCSI_PARAM_LOCAL_PORT) |
bcf3a295 | 784 | rc = kernel_getsockname(sock, |
9b2c45d4 | 785 | (struct sockaddr *)&addr); |
4bfb8ebf | 786 | else |
bcf3a295 | 787 | rc = kernel_getpeername(sock, |
9b2c45d4 | 788 | (struct sockaddr *)&addr); |
57569c37 MC |
789 | sock_unlock: |
790 | mutex_unlock(&tcp_sw_conn->sock_lock); | |
791 | iscsi_put_conn(conn->cls_conn); | |
9b2c45d4 | 792 | if (rc < 0) |
a79af8a6 MC |
793 | return rc; |
794 | ||
795 | return iscsi_conn_get_addr_param((struct sockaddr_storage *) | |
796 | &addr, param, buf); | |
fd7255f5 | 797 | default: |
5c75b7fc | 798 | return iscsi_conn_get_param(cls_conn, param, buf); |
fd7255f5 MC |
799 | } |
800 | ||
a79af8a6 MC |
801 | return 0; |
802 | } | |
803 | ||
804 | static int iscsi_sw_tcp_host_get_param(struct Scsi_Host *shost, | |
805 | enum iscsi_host_param param, char *buf) | |
806 | { | |
807 | struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(shost); | |
f484a794 | 808 | struct iscsi_session *session; |
a79af8a6 MC |
809 | struct iscsi_conn *conn; |
810 | struct iscsi_tcp_conn *tcp_conn; | |
811 | struct iscsi_sw_tcp_conn *tcp_sw_conn; | |
812 | struct sockaddr_in6 addr; | |
bcf3a295 | 813 | struct socket *sock; |
9b2c45d4 | 814 | int rc; |
a79af8a6 MC |
815 | |
816 | switch (param) { | |
817 | case ISCSI_HOST_PARAM_IPADDRESS: | |
f484a794 | 818 | session = tcp_sw_host->session; |
eee2b5c8 MC |
819 | if (!session) |
820 | return -ENOTCONN; | |
821 | ||
659743b0 | 822 | spin_lock_bh(&session->frwd_lock); |
a79af8a6 MC |
823 | conn = session->leadconn; |
824 | if (!conn) { | |
659743b0 | 825 | spin_unlock_bh(&session->frwd_lock); |
a79af8a6 MC |
826 | return -ENOTCONN; |
827 | } | |
828 | tcp_conn = conn->dd_data; | |
a79af8a6 | 829 | tcp_sw_conn = tcp_conn->dd_data; |
57569c37 MC |
830 | /* |
831 | * The conn has been setup and bound, so just grab a ref | |
832 | * incase a destroy runs while we are in the net layer. | |
833 | */ | |
834 | iscsi_get_conn(conn->cls_conn); | |
bcf3a295 | 835 | spin_unlock_bh(&session->frwd_lock); |
a79af8a6 | 836 | |
57569c37 MC |
837 | mutex_lock(&tcp_sw_conn->sock_lock); |
838 | sock = tcp_sw_conn->sock; | |
839 | if (!sock) | |
840 | rc = -ENOTCONN; | |
841 | else | |
842 | rc = kernel_getsockname(sock, (struct sockaddr *)&addr); | |
843 | mutex_unlock(&tcp_sw_conn->sock_lock); | |
844 | iscsi_put_conn(conn->cls_conn); | |
9b2c45d4 | 845 | if (rc < 0) |
a79af8a6 MC |
846 | return rc; |
847 | ||
848 | return iscsi_conn_get_addr_param((struct sockaddr_storage *) | |
20054597 NC |
849 | &addr, |
850 | (enum iscsi_param)param, buf); | |
a79af8a6 MC |
851 | default: |
852 | return iscsi_host_get_param(shost, param, buf); | |
853 | } | |
854 | ||
855 | return 0; | |
fd7255f5 MC |
856 | } |
857 | ||
7ba24713 | 858 | static void |
38e1a8f5 MC |
859 | iscsi_sw_tcp_conn_get_stats(struct iscsi_cls_conn *cls_conn, |
860 | struct iscsi_stats *stats) | |
7ba24713 | 861 | { |
7b7232f3 | 862 | struct iscsi_conn *conn = cls_conn->dd_data; |
5bb0b55a | 863 | struct iscsi_tcp_conn *tcp_conn = conn->dd_data; |
38e1a8f5 | 864 | struct iscsi_sw_tcp_conn *tcp_sw_conn = tcp_conn->dd_data; |
7ba24713 | 865 | |
7ba24713 AA |
866 | stats->custom_length = 3; |
867 | strcpy(stats->custom[0].desc, "tx_sendpage_failures"); | |
38e1a8f5 | 868 | stats->custom[0].value = tcp_sw_conn->sendpage_failures_cnt; |
7ba24713 | 869 | strcpy(stats->custom[1].desc, "rx_discontiguous_hdr"); |
38e1a8f5 | 870 | stats->custom[1].value = tcp_sw_conn->discontiguous_hdr_cnt; |
7ba24713 AA |
871 | strcpy(stats->custom[2].desc, "eh_abort_cnt"); |
872 | stats->custom[2].value = conn->eh_abort_cnt; | |
38e1a8f5 MC |
873 | |
874 | iscsi_tcp_conn_get_stats(cls_conn, stats); | |
7ba24713 AA |
875 | } |
876 | ||
5bb0b55a | 877 | static struct iscsi_cls_session * |
38e1a8f5 | 878 | iscsi_sw_tcp_session_create(struct iscsi_endpoint *ep, uint16_t cmds_max, |
5e7facb7 | 879 | uint16_t qdepth, uint32_t initial_cmdsn) |
7ba24713 | 880 | { |
5bb0b55a MC |
881 | struct iscsi_cls_session *cls_session; |
882 | struct iscsi_session *session; | |
a79af8a6 | 883 | struct iscsi_sw_tcp_host *tcp_sw_host; |
06520ede | 884 | struct Scsi_Host *shost; |
25c400db | 885 | int rc; |
7ba24713 | 886 | |
06520ede MC |
887 | if (ep) { |
888 | printk(KERN_ERR "iscsi_tcp: invalid ep %p.\n", ep); | |
75613521 MC |
889 | return NULL; |
890 | } | |
891 | ||
a79af8a6 MC |
892 | shost = iscsi_host_alloc(&iscsi_sw_tcp_sht, |
893 | sizeof(struct iscsi_sw_tcp_host), 1); | |
75613521 | 894 | if (!shost) |
5bb0b55a | 895 | return NULL; |
38e1a8f5 | 896 | shost->transportt = iscsi_sw_tcp_scsi_transport; |
4d108350 | 897 | shost->cmd_per_lun = qdepth; |
75613521 MC |
898 | shost->max_lun = iscsi_max_lun; |
899 | shost->max_id = 0; | |
900 | shost->max_channel = 0; | |
30e9ba9f | 901 | shost->max_cmd_len = SCSI_MAX_VARLEN_CDB_SIZE; |
5b7dfbef | 902 | shost->dma_alignment = 0; |
75613521 | 903 | |
25c400db MC |
904 | rc = iscsi_host_get_max_scsi_cmds(shost, cmds_max); |
905 | if (rc < 0) | |
906 | goto free_host; | |
907 | shost->can_queue = rc; | |
908 | ||
a4804cd6 | 909 | if (iscsi_host_add(shost, NULL)) |
75613521 | 910 | goto free_host; |
75613521 | 911 | |
38e1a8f5 | 912 | cls_session = iscsi_session_setup(&iscsi_sw_tcp_transport, shost, |
b8b9e1b8 | 913 | cmds_max, 0, |
38e1a8f5 MC |
914 | sizeof(struct iscsi_tcp_task) + |
915 | sizeof(struct iscsi_sw_tcp_hdrbuf), | |
7970634b | 916 | initial_cmdsn, 0); |
75613521 MC |
917 | if (!cls_session) |
918 | goto remove_host; | |
919 | session = cls_session->dd_data; | |
7ba24713 | 920 | |
38e1a8f5 | 921 | if (iscsi_tcp_r2tpool_alloc(session)) |
75613521 | 922 | goto remove_session; |
f484a794 MC |
923 | |
924 | /* We are now fully setup so expose the session to sysfs. */ | |
925 | tcp_sw_host = iscsi_host_priv(shost); | |
926 | tcp_sw_host->session = session; | |
5bb0b55a MC |
927 | return cls_session; |
928 | ||
75613521 | 929 | remove_session: |
5bb0b55a | 930 | iscsi_session_teardown(cls_session); |
75613521 | 931 | remove_host: |
31500e90 | 932 | iscsi_host_remove(shost, false); |
75613521 | 933 | free_host: |
a4804cd6 | 934 | iscsi_host_free(shost); |
5bb0b55a MC |
935 | return NULL; |
936 | } | |
937 | ||
38e1a8f5 | 938 | static void iscsi_sw_tcp_session_destroy(struct iscsi_cls_session *cls_session) |
5bb0b55a | 939 | { |
75613521 | 940 | struct Scsi_Host *shost = iscsi_session_to_shost(cls_session); |
54155ed4 NB |
941 | struct iscsi_session *session = cls_session->dd_data; |
942 | ||
943 | if (WARN_ON_ONCE(session->leadconn)) | |
944 | return; | |
75613521 | 945 | |
6f1d64b1 MC |
946 | iscsi_session_remove(cls_session); |
947 | /* | |
948 | * Our get_host_param needs to access the session, so remove the | |
949 | * host from sysfs before freeing the session to make sure userspace | |
950 | * is no longer accessing the callout. | |
951 | */ | |
952 | iscsi_host_remove(shost, false); | |
953 | ||
38e1a8f5 | 954 | iscsi_tcp_r2tpool_free(cls_session->dd_data); |
75613521 | 955 | |
6f1d64b1 | 956 | iscsi_session_free(cls_session); |
a4804cd6 | 957 | iscsi_host_free(shost); |
7ba24713 AA |
958 | } |
959 | ||
587a1f16 | 960 | static umode_t iscsi_sw_tcp_attr_is_visible(int param_type, int param) |
3128c6c7 MC |
961 | { |
962 | switch (param_type) { | |
f27fb2ef MC |
963 | case ISCSI_HOST_PARAM: |
964 | switch (param) { | |
965 | case ISCSI_HOST_PARAM_NETDEV_NAME: | |
966 | case ISCSI_HOST_PARAM_HWADDRESS: | |
967 | case ISCSI_HOST_PARAM_IPADDRESS: | |
968 | case ISCSI_HOST_PARAM_INITIATOR_NAME: | |
969 | return S_IRUGO; | |
970 | default: | |
971 | return 0; | |
972 | } | |
3128c6c7 MC |
973 | case ISCSI_PARAM: |
974 | switch (param) { | |
975 | case ISCSI_PARAM_MAX_RECV_DLENGTH: | |
976 | case ISCSI_PARAM_MAX_XMIT_DLENGTH: | |
977 | case ISCSI_PARAM_HDRDGST_EN: | |
978 | case ISCSI_PARAM_DATADGST_EN: | |
979 | case ISCSI_PARAM_CONN_ADDRESS: | |
980 | case ISCSI_PARAM_CONN_PORT: | |
4bfb8ebf | 981 | case ISCSI_PARAM_LOCAL_PORT: |
3128c6c7 MC |
982 | case ISCSI_PARAM_EXP_STATSN: |
983 | case ISCSI_PARAM_PERSISTENT_ADDRESS: | |
984 | case ISCSI_PARAM_PERSISTENT_PORT: | |
985 | case ISCSI_PARAM_PING_TMO: | |
986 | case ISCSI_PARAM_RECV_TMO: | |
1d063c17 MC |
987 | case ISCSI_PARAM_INITIAL_R2T_EN: |
988 | case ISCSI_PARAM_MAX_R2T: | |
989 | case ISCSI_PARAM_IMM_DATA_EN: | |
990 | case ISCSI_PARAM_FIRST_BURST: | |
991 | case ISCSI_PARAM_MAX_BURST: | |
992 | case ISCSI_PARAM_PDU_INORDER_EN: | |
993 | case ISCSI_PARAM_DATASEQ_INORDER_EN: | |
994 | case ISCSI_PARAM_ERL: | |
995 | case ISCSI_PARAM_TARGET_NAME: | |
996 | case ISCSI_PARAM_TPGT: | |
997 | case ISCSI_PARAM_USERNAME: | |
998 | case ISCSI_PARAM_PASSWORD: | |
999 | case ISCSI_PARAM_USERNAME_IN: | |
1000 | case ISCSI_PARAM_PASSWORD_IN: | |
1001 | case ISCSI_PARAM_FAST_ABORT: | |
1002 | case ISCSI_PARAM_ABORT_TMO: | |
1003 | case ISCSI_PARAM_LU_RESET_TMO: | |
1004 | case ISCSI_PARAM_TGT_RESET_TMO: | |
1005 | case ISCSI_PARAM_IFACE_NAME: | |
1006 | case ISCSI_PARAM_INITIATOR_NAME: | |
3128c6c7 MC |
1007 | return S_IRUGO; |
1008 | default: | |
1009 | return 0; | |
1010 | } | |
1011 | } | |
1012 | ||
1013 | return 0; | |
1014 | } | |
1015 | ||
47c2e30a BVA |
1016 | static int iscsi_sw_tcp_sdev_configure(struct scsi_device *sdev, |
1017 | struct queue_limits *lim) | |
d1d81c01 | 1018 | { |
89d0c804 JW |
1019 | struct iscsi_sw_tcp_host *tcp_sw_host = iscsi_host_priv(sdev->host); |
1020 | struct iscsi_session *session = tcp_sw_host->session; | |
1021 | struct iscsi_conn *conn = session->leadconn; | |
1022 | ||
1023 | if (conn->datadgst_en) | |
1a02f3a7 | 1024 | lim->features |= BLK_FEAT_STABLE_WRITES; |
d1d81c01 MC |
1025 | return 0; |
1026 | } | |
1027 | ||
80602aca | 1028 | static const struct scsi_host_template iscsi_sw_tcp_sht = { |
7974392c | 1029 | .module = THIS_MODULE, |
f4246b33 | 1030 | .name = "iSCSI Initiator over TCP/IP", |
5bb0b55a | 1031 | .queuecommand = iscsi_queuecommand, |
db5ed4df | 1032 | .change_queue_depth = scsi_change_queue_depth, |
25c400db | 1033 | .can_queue = ISCSI_TOTAL_CMDS_MAX, |
66bbe0ce | 1034 | .sg_tablesize = 4096, |
8231f0ed | 1035 | .max_sectors = 0xFFFF, |
5bb0b55a | 1036 | .cmd_per_lun = ISCSI_DEF_CMD_PER_LUN, |
b6a05c82 | 1037 | .eh_timed_out = iscsi_eh_cmd_timed_out, |
5bb0b55a | 1038 | .eh_abort_handler = iscsi_eh_abort, |
843c0a8a | 1039 | .eh_device_reset_handler= iscsi_eh_device_reset, |
309ce156 | 1040 | .eh_target_reset_handler = iscsi_eh_recover_target, |
4af14d11 | 1041 | .dma_boundary = PAGE_SIZE - 1, |
47c2e30a | 1042 | .sdev_configure = iscsi_sw_tcp_sdev_configure, |
5bb0b55a MC |
1043 | .proc_name = "iscsi_tcp", |
1044 | .this_id = -1, | |
c40ecc12 | 1045 | .track_queue_depth = 1, |
db22de3e | 1046 | .cmd_size = sizeof(struct iscsi_cmd), |
5bb0b55a MC |
1047 | }; |
1048 | ||
38e1a8f5 | 1049 | static struct iscsi_transport iscsi_sw_tcp_transport = { |
7ba24713 AA |
1050 | .owner = THIS_MODULE, |
1051 | .name = "tcp", | |
1052 | .caps = CAP_RECOVERY_L0 | CAP_MULTI_R2T | CAP_HDRDGST | |
1053 | | CAP_DATADGST, | |
5bb0b55a | 1054 | /* session management */ |
38e1a8f5 MC |
1055 | .create_session = iscsi_sw_tcp_session_create, |
1056 | .destroy_session = iscsi_sw_tcp_session_destroy, | |
5bb0b55a | 1057 | /* connection management */ |
38e1a8f5 MC |
1058 | .create_conn = iscsi_sw_tcp_conn_create, |
1059 | .bind_conn = iscsi_sw_tcp_conn_bind, | |
1060 | .destroy_conn = iscsi_sw_tcp_conn_destroy, | |
3128c6c7 | 1061 | .attr_is_visible = iscsi_sw_tcp_attr_is_visible, |
38e1a8f5 MC |
1062 | .set_param = iscsi_sw_tcp_conn_set_param, |
1063 | .get_conn_param = iscsi_sw_tcp_conn_get_param, | |
7b8631b5 | 1064 | .get_session_param = iscsi_session_get_param, |
7ba24713 | 1065 | .start_conn = iscsi_conn_start, |
38e1a8f5 | 1066 | .stop_conn = iscsi_sw_tcp_conn_stop, |
0801c242 | 1067 | /* iscsi host params */ |
a79af8a6 | 1068 | .get_host_param = iscsi_sw_tcp_host_get_param, |
0801c242 | 1069 | .set_host_param = iscsi_host_set_param, |
5bb0b55a | 1070 | /* IO */ |
7ba24713 | 1071 | .send_pdu = iscsi_conn_send_pdu, |
38e1a8f5 | 1072 | .get_stats = iscsi_sw_tcp_conn_get_stats, |
e5a7efef | 1073 | /* iscsi task/cmd helpers */ |
fbc514b4 MC |
1074 | .init_task = iscsi_tcp_task_init, |
1075 | .xmit_task = iscsi_tcp_task_xmit, | |
1076 | .cleanup_task = iscsi_tcp_cleanup_task, | |
e5a7efef | 1077 | /* low level pdu helpers */ |
38e1a8f5 MC |
1078 | .xmit_pdu = iscsi_sw_tcp_pdu_xmit, |
1079 | .init_pdu = iscsi_sw_tcp_pdu_init, | |
1080 | .alloc_pdu = iscsi_sw_tcp_pdu_alloc, | |
5bb0b55a | 1081 | /* recovery */ |
30a6c652 | 1082 | .session_recovery_timedout = iscsi_session_recovery_timedout, |
7ba24713 AA |
1083 | }; |
1084 | ||
38e1a8f5 | 1085 | static int __init iscsi_sw_tcp_init(void) |
7ba24713 | 1086 | { |
7ba24713 | 1087 | if (iscsi_max_lun < 1) { |
be2df72e OG |
1088 | printk(KERN_ERR "iscsi_tcp: Invalid max_lun value of %u\n", |
1089 | iscsi_max_lun); | |
7ba24713 AA |
1090 | return -EINVAL; |
1091 | } | |
7ba24713 | 1092 | |
38e1a8f5 MC |
1093 | iscsi_sw_tcp_scsi_transport = iscsi_register_transport( |
1094 | &iscsi_sw_tcp_transport); | |
1095 | if (!iscsi_sw_tcp_scsi_transport) | |
ffbfe925 | 1096 | return -ENODEV; |
7ba24713 | 1097 | |
7b8631b5 | 1098 | return 0; |
7ba24713 AA |
1099 | } |
1100 | ||
38e1a8f5 | 1101 | static void __exit iscsi_sw_tcp_exit(void) |
7ba24713 | 1102 | { |
38e1a8f5 | 1103 | iscsi_unregister_transport(&iscsi_sw_tcp_transport); |
7ba24713 AA |
1104 | } |
1105 | ||
38e1a8f5 MC |
1106 | module_init(iscsi_sw_tcp_init); |
1107 | module_exit(iscsi_sw_tcp_exit); |