1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (c) 2010 Cisco Systems, Inc. 4 * 5 * Portions based on tcm_loop_fabric_scsi.c and libfc/fc_fcp.c 6 * 7 * Copyright (c) 2007 Intel Corporation. All rights reserved. 8 * Copyright (c) 2008 Red Hat, Inc. All rights reserved. 9 * Copyright (c) 2008 Mike Christie 10 * Copyright (c) 2009 Rising Tide, Inc. 11 * Copyright (c) 2009 Linux-iSCSI.org 12 * Copyright (c) 2009 Nicholas A. Bellinger <nab@linux-iscsi.org> 13 */ 14 15 /* XXX TBD some includes may be extraneous */ 16 17 #include <linux/module.h> 18 #include <linux/moduleparam.h> 19 #include <linux/utsname.h> 20 #include <linux/init.h> 21 #include <linux/slab.h> 22 #include <linux/kthread.h> 23 #include <linux/types.h> 24 #include <linux/string.h> 25 #include <linux/configfs.h> 26 #include <linux/ctype.h> 27 #include <linux/hash.h> 28 #include <linux/ratelimit.h> 29 #include <asm/unaligned.h> 30 #include <scsi/libfc.h> 31 #include <scsi/fc_encode.h> 32 33 #include <target/target_core_base.h> 34 #include <target/target_core_fabric.h> 35 36 #include "tcm_fc.h" 37 38 /* 39 * Deliver read data back to initiator. 40 * XXX TBD handle resource problems later. 41 */ 42 int ft_queue_data_in(struct se_cmd *se_cmd) 43 { 44 struct ft_cmd *cmd = container_of(se_cmd, struct ft_cmd, se_cmd); 45 struct fc_frame *fp = NULL; 46 struct fc_exch *ep; 47 struct fc_lport *lport; 48 struct scatterlist *sg = NULL; 49 size_t remaining; 50 u32 f_ctl = FC_FC_EX_CTX | FC_FC_REL_OFF; 51 u32 mem_off = 0; 52 u32 fh_off = 0; 53 u32 frame_off = 0; 54 size_t frame_len = 0; 55 size_t mem_len = 0; 56 size_t tlen; 57 size_t off_in_page; 58 struct page *page = NULL; 59 int use_sg; 60 int error; 61 void *page_addr; 62 void *from; 63 void *to = NULL; 64 65 if (cmd->aborted) 66 return 0; 67 68 if (se_cmd->scsi_status == SAM_STAT_TASK_SET_FULL) 69 goto queue_status; 70 71 ep = fc_seq_exch(cmd->seq); 72 lport = ep->lp; 73 cmd->seq = fc_seq_start_next(cmd->seq); 74 75 remaining = se_cmd->data_length; 76 77 /* 78 * Setup to use first mem list entry, unless no data. 79 */ 80 BUG_ON(remaining && !se_cmd->t_data_sg); 81 if (remaining) { 82 sg = se_cmd->t_data_sg; 83 mem_len = sg->length; 84 mem_off = sg->offset; 85 page = sg_page(sg); 86 } 87 88 /* no scatter/gather in skb for odd word length due to fc_seq_send() */ 89 use_sg = !(remaining % 4); 90 91 while (remaining) { 92 struct fc_seq *seq = cmd->seq; 93 94 if (!seq) { 95 pr_debug("%s: Command aborted, xid 0x%x\n", 96 __func__, ep->xid); 97 break; 98 } 99 if (!mem_len) { 100 sg = sg_next(sg); 101 mem_len = min((size_t)sg->length, remaining); 102 mem_off = sg->offset; 103 page = sg_page(sg); 104 } 105 if (!frame_len) { 106 /* 107 * If lport's has capability of Large Send Offload LSO) 108 * , then allow 'frame_len' to be as big as 'lso_max' 109 * if indicated transfer length is >= lport->lso_max 110 */ 111 frame_len = (lport->seq_offload) ? lport->lso_max : 112 cmd->sess->max_frame; 113 frame_len = min(frame_len, remaining); 114 fp = fc_frame_alloc(lport, use_sg ? 0 : frame_len); 115 if (!fp) 116 return -ENOMEM; 117 to = fc_frame_payload_get(fp, 0); 118 fh_off = frame_off; 119 frame_off += frame_len; 120 /* 121 * Setup the frame's max payload which is used by base 122 * driver to indicate HW about max frame size, so that 123 * HW can do fragmentation appropriately based on 124 * "gso_max_size" of underline netdev. 125 */ 126 fr_max_payload(fp) = cmd->sess->max_frame; 127 } 128 tlen = min(mem_len, frame_len); 129 130 if (use_sg) { 131 off_in_page = mem_off; 132 BUG_ON(!page); 133 get_page(page); 134 skb_fill_page_desc(fp_skb(fp), 135 skb_shinfo(fp_skb(fp))->nr_frags, 136 page, off_in_page, tlen); 137 fr_len(fp) += tlen; 138 fp_skb(fp)->data_len += tlen; 139 fp_skb(fp)->truesize += 140 PAGE_SIZE << compound_order(page); 141 } else { 142 BUG_ON(!page); 143 from = kmap_atomic(page + (mem_off >> PAGE_SHIFT)); 144 page_addr = from; 145 from += offset_in_page(mem_off); 146 tlen = min(tlen, (size_t)(PAGE_SIZE - 147 offset_in_page(mem_off))); 148 memcpy(to, from, tlen); 149 kunmap_atomic(page_addr); 150 to += tlen; 151 } 152 153 mem_off += tlen; 154 mem_len -= tlen; 155 frame_len -= tlen; 156 remaining -= tlen; 157 158 if (frame_len && 159 (skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN)) 160 continue; 161 if (!remaining) 162 f_ctl |= FC_FC_END_SEQ; 163 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid, 164 FC_TYPE_FCP, f_ctl, fh_off); 165 error = fc_seq_send(lport, seq, fp); 166 if (error) { 167 pr_info_ratelimited("%s: Failed to send frame %p, " 168 "xid <0x%x>, remaining %zu, " 169 "lso_max <0x%x>\n", 170 __func__, fp, ep->xid, 171 remaining, lport->lso_max); 172 /* 173 * Go ahead and set TASK_SET_FULL status ignoring the 174 * rest of the DataIN, and immediately attempt to 175 * send the response via ft_queue_status() in order 176 * to notify the initiator that it should reduce it's 177 * per LUN queue_depth. 178 */ 179 se_cmd->scsi_status = SAM_STAT_TASK_SET_FULL; 180 break; 181 } 182 } 183 queue_status: 184 return ft_queue_status(se_cmd); 185 } 186 187 static void ft_execute_work(struct work_struct *work) 188 { 189 struct ft_cmd *cmd = container_of(work, struct ft_cmd, work); 190 191 target_execute_cmd(&cmd->se_cmd); 192 } 193 194 /* 195 * Receive write data frame. 196 */ 197 void ft_recv_write_data(struct ft_cmd *cmd, struct fc_frame *fp) 198 { 199 struct se_cmd *se_cmd = &cmd->se_cmd; 200 struct fc_seq *seq = cmd->seq; 201 struct fc_exch *ep; 202 struct fc_lport *lport; 203 struct fc_frame_header *fh; 204 struct scatterlist *sg = NULL; 205 u32 mem_off = 0; 206 u32 rel_off; 207 size_t frame_len; 208 size_t mem_len = 0; 209 size_t tlen; 210 struct page *page = NULL; 211 void *page_addr; 212 void *from; 213 void *to; 214 u32 f_ctl; 215 void *buf; 216 217 fh = fc_frame_header_get(fp); 218 if (!(ntoh24(fh->fh_f_ctl) & FC_FC_REL_OFF)) 219 goto drop; 220 221 f_ctl = ntoh24(fh->fh_f_ctl); 222 ep = fc_seq_exch(seq); 223 lport = ep->lp; 224 if (cmd->was_ddp_setup) { 225 BUG_ON(!ep); 226 BUG_ON(!lport); 227 /* 228 * Since DDP (Large Rx offload) was setup for this request, 229 * payload is expected to be copied directly to user buffers. 230 */ 231 buf = fc_frame_payload_get(fp, 1); 232 if (buf) 233 pr_err("%s: xid 0x%x, f_ctl 0x%x, cmd->sg %p, " 234 "cmd->sg_cnt 0x%x. DDP was setup" 235 " hence not expected to receive frame with " 236 "payload, Frame will be dropped if" 237 "'Sequence Initiative' bit in f_ctl is" 238 "not set\n", __func__, ep->xid, f_ctl, 239 se_cmd->t_data_sg, se_cmd->t_data_nents); 240 /* 241 * Invalidate HW DDP context if it was setup for respective 242 * command. Invalidation of HW DDP context is requited in both 243 * situation (success and error). 244 */ 245 ft_invl_hw_context(cmd); 246 247 /* 248 * If "Sequence Initiative (TSI)" bit set in f_ctl, means last 249 * write data frame is received successfully where payload is 250 * posted directly to user buffer and only the last frame's 251 * header is posted in receive queue. 252 * 253 * If "Sequence Initiative (TSI)" bit is not set, means error 254 * condition w.r.t. DDP, hence drop the packet and let explict 255 * ABORTS from other end of exchange timer trigger the recovery. 256 */ 257 if (f_ctl & FC_FC_SEQ_INIT) 258 goto last_frame; 259 else 260 goto drop; 261 } 262 263 rel_off = ntohl(fh->fh_parm_offset); 264 frame_len = fr_len(fp); 265 if (frame_len <= sizeof(*fh)) 266 goto drop; 267 frame_len -= sizeof(*fh); 268 from = fc_frame_payload_get(fp, 0); 269 if (rel_off >= se_cmd->data_length) 270 goto drop; 271 if (frame_len + rel_off > se_cmd->data_length) 272 frame_len = se_cmd->data_length - rel_off; 273 274 /* 275 * Setup to use first mem list entry, unless no data. 276 */ 277 BUG_ON(frame_len && !se_cmd->t_data_sg); 278 if (frame_len) { 279 sg = se_cmd->t_data_sg; 280 mem_len = sg->length; 281 mem_off = sg->offset; 282 page = sg_page(sg); 283 } 284 285 while (frame_len) { 286 if (!mem_len) { 287 sg = sg_next(sg); 288 mem_len = sg->length; 289 mem_off = sg->offset; 290 page = sg_page(sg); 291 } 292 if (rel_off >= mem_len) { 293 rel_off -= mem_len; 294 mem_len = 0; 295 continue; 296 } 297 mem_off += rel_off; 298 mem_len -= rel_off; 299 rel_off = 0; 300 301 tlen = min(mem_len, frame_len); 302 303 to = kmap_atomic(page + (mem_off >> PAGE_SHIFT)); 304 page_addr = to; 305 to += offset_in_page(mem_off); 306 tlen = min(tlen, (size_t)(PAGE_SIZE - 307 offset_in_page(mem_off))); 308 memcpy(to, from, tlen); 309 kunmap_atomic(page_addr); 310 311 from += tlen; 312 frame_len -= tlen; 313 mem_off += tlen; 314 mem_len -= tlen; 315 cmd->write_data_len += tlen; 316 } 317 last_frame: 318 if (cmd->write_data_len == se_cmd->data_length) { 319 INIT_WORK(&cmd->work, ft_execute_work); 320 queue_work(cmd->sess->tport->tpg->workqueue, &cmd->work); 321 } 322 drop: 323 fc_frame_free(fp); 324 } 325 326 /* 327 * Handle and cleanup any HW specific resources if 328 * received ABORTS, errors, timeouts. 329 */ 330 void ft_invl_hw_context(struct ft_cmd *cmd) 331 { 332 struct fc_seq *seq; 333 struct fc_exch *ep = NULL; 334 struct fc_lport *lport = NULL; 335 336 BUG_ON(!cmd); 337 seq = cmd->seq; 338 339 /* Cleanup the DDP context in HW if DDP was setup */ 340 if (cmd->was_ddp_setup && seq) { 341 ep = fc_seq_exch(seq); 342 if (ep) { 343 lport = ep->lp; 344 if (lport && (ep->xid <= lport->lro_xid)) { 345 /* 346 * "ddp_done" trigger invalidation of HW 347 * specific DDP context 348 */ 349 cmd->write_data_len = lport->tt.ddp_done(lport, 350 ep->xid); 351 352 /* 353 * Resetting same variable to indicate HW's 354 * DDP context has been invalidated to avoid 355 * re_invalidation of same context (context is 356 * identified using ep->xid) 357 */ 358 cmd->was_ddp_setup = 0; 359 } 360 } 361 } 362 } 363