1 /* 2 * Copyright(c) 2007 Intel Corporation. All rights reserved. 3 * Copyright(c) 2008 Red Hat, Inc. All rights reserved. 4 * Copyright(c) 2008 Mike Christie 5 * 6 * This program is free software; you can redistribute it and/or modify it 7 * under the terms and conditions of the GNU General Public License, 8 * version 2, as published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 13 * more details. 14 * 15 * You should have received a copy of the GNU General Public License along with 16 * this program; if not, write to the Free Software Foundation, Inc., 17 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Maintained at www.Open-FCoE.org 20 */ 21 22 #include <linux/module.h> 23 #include <linux/delay.h> 24 #include <linux/kernel.h> 25 #include <linux/types.h> 26 #include <linux/spinlock.h> 27 #include <linux/scatterlist.h> 28 #include <linux/err.h> 29 #include <linux/crc32.h> 30 #include <linux/slab.h> 31 32 #include <scsi/scsi_tcq.h> 33 #include <scsi/scsi.h> 34 #include <scsi/scsi_host.h> 35 #include <scsi/scsi_device.h> 36 #include <scsi/scsi_cmnd.h> 37 38 #include <scsi/fc/fc_fc2.h> 39 40 #include <scsi/libfc.h> 41 #include <scsi/fc_encode.h> 42 43 #include "fc_libfc.h" 44 45 static struct kmem_cache *scsi_pkt_cachep; 46 47 /* SRB state definitions */ 48 #define FC_SRB_FREE 0 /* cmd is free */ 49 #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */ 50 #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */ 51 #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */ 52 #define FC_SRB_ABORTED (1 << 3) /* abort acknowledged */ 53 #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */ 54 #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */ 55 #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */ 56 57 #define FC_SRB_READ (1 << 1) 58 #define FC_SRB_WRITE (1 << 0) 59 60 /* 61 * The SCp.ptr should be tested and set under the scsi_pkt_queue lock 62 */ 63 #define CMD_SP(Cmnd) ((struct fc_fcp_pkt *)(Cmnd)->SCp.ptr) 64 #define CMD_ENTRY_STATUS(Cmnd) ((Cmnd)->SCp.have_data_in) 65 #define CMD_COMPL_STATUS(Cmnd) ((Cmnd)->SCp.this_residual) 66 #define CMD_SCSI_STATUS(Cmnd) ((Cmnd)->SCp.Status) 67 #define CMD_RESID_LEN(Cmnd) ((Cmnd)->SCp.buffers_residual) 68 69 /** 70 * struct fc_fcp_internal - FCP layer internal data 71 * @scsi_pkt_pool: Memory pool to draw FCP packets from 72 * @scsi_queue_lock: Protects the scsi_pkt_queue 73 * @scsi_pkt_queue: Current FCP packets 74 * @last_can_queue_ramp_down_time: ramp down time 75 * @last_can_queue_ramp_up_time: ramp up time 76 * @max_can_queue: max can_queue size 77 */ 78 struct fc_fcp_internal { 79 mempool_t *scsi_pkt_pool; 80 spinlock_t scsi_queue_lock; 81 struct list_head scsi_pkt_queue; 82 unsigned long last_can_queue_ramp_down_time; 83 unsigned long last_can_queue_ramp_up_time; 84 int max_can_queue; 85 }; 86 87 #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv) 88 89 /* 90 * function prototypes 91 * FC scsi I/O related functions 92 */ 93 static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *); 94 static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *); 95 static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *); 96 static void fc_fcp_complete_locked(struct fc_fcp_pkt *); 97 static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *); 98 static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *); 99 static void fc_fcp_recovery(struct fc_fcp_pkt *, u8 code); 100 static void fc_fcp_timeout(unsigned long); 101 static void fc_fcp_rec(struct fc_fcp_pkt *); 102 static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *); 103 static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *); 104 static void fc_io_compl(struct fc_fcp_pkt *); 105 106 static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32); 107 static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *); 108 static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *); 109 110 /* 111 * command status codes 112 */ 113 #define FC_COMPLETE 0 114 #define FC_CMD_ABORTED 1 115 #define FC_CMD_RESET 2 116 #define FC_CMD_PLOGO 3 117 #define FC_SNS_RCV 4 118 #define FC_TRANS_ERR 5 119 #define FC_DATA_OVRRUN 6 120 #define FC_DATA_UNDRUN 7 121 #define FC_ERROR 8 122 #define FC_HRD_ERROR 9 123 #define FC_CRC_ERROR 10 124 #define FC_TIMED_OUT 11 125 #define FC_TRANS_RESET 12 126 127 /* 128 * Error recovery timeout values. 129 */ 130 #define FC_SCSI_TM_TOV (10 * HZ) 131 #define FC_HOST_RESET_TIMEOUT (30 * HZ) 132 #define FC_CAN_QUEUE_PERIOD (60 * HZ) 133 134 #define FC_MAX_ERROR_CNT 5 135 #define FC_MAX_RECOV_RETRY 3 136 137 #define FC_FCP_DFLT_QUEUE_DEPTH 32 138 139 /** 140 * fc_fcp_pkt_alloc() - Allocate a fcp_pkt 141 * @lport: The local port that the FCP packet is for 142 * @gfp: GFP flags for allocation 143 * 144 * Return value: fcp_pkt structure or null on allocation failure. 145 * Context: Can be called from process context, no lock is required. 146 */ 147 static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp) 148 { 149 struct fc_fcp_internal *si = fc_get_scsi_internal(lport); 150 struct fc_fcp_pkt *fsp; 151 152 fsp = mempool_alloc(si->scsi_pkt_pool, gfp); 153 if (fsp) { 154 memset(fsp, 0, sizeof(*fsp)); 155 fsp->lp = lport; 156 fsp->xfer_ddp = FC_XID_UNKNOWN; 157 atomic_set(&fsp->ref_cnt, 1); 158 init_timer(&fsp->timer); 159 fsp->timer.data = (unsigned long)fsp; 160 INIT_LIST_HEAD(&fsp->list); 161 spin_lock_init(&fsp->scsi_pkt_lock); 162 } else { 163 per_cpu_ptr(lport->stats, get_cpu())->FcpPktAllocFails++; 164 put_cpu(); 165 } 166 return fsp; 167 } 168 169 /** 170 * fc_fcp_pkt_release() - Release hold on a fcp_pkt 171 * @fsp: The FCP packet to be released 172 * 173 * Context: Can be called from process or interrupt context, 174 * no lock is required. 175 */ 176 static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp) 177 { 178 if (atomic_dec_and_test(&fsp->ref_cnt)) { 179 struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp); 180 181 mempool_free(fsp, si->scsi_pkt_pool); 182 } 183 } 184 185 /** 186 * fc_fcp_pkt_hold() - Hold a fcp_pkt 187 * @fsp: The FCP packet to be held 188 */ 189 static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp) 190 { 191 atomic_inc(&fsp->ref_cnt); 192 } 193 194 /** 195 * fc_fcp_pkt_destroy() - Release hold on a fcp_pkt 196 * @seq: The sequence that the FCP packet is on (required by destructor API) 197 * @fsp: The FCP packet to be released 198 * 199 * This routine is called by a destructor callback in the exch_seq_send() 200 * routine of the libfc Transport Template. The 'struct fc_seq' is a required 201 * argument even though it is not used by this routine. 202 * 203 * Context: No locking required. 204 */ 205 static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp) 206 { 207 fc_fcp_pkt_release(fsp); 208 } 209 210 /** 211 * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count 212 * @fsp: The FCP packet to be locked and incremented 213 * 214 * We should only return error if we return a command to SCSI-ml before 215 * getting a response. This can happen in cases where we send a abort, but 216 * do not wait for the response and the abort and command can be passing 217 * each other on the wire/network-layer. 218 * 219 * Note: this function locks the packet and gets a reference to allow 220 * callers to call the completion function while the lock is held and 221 * not have to worry about the packets refcount. 222 * 223 * TODO: Maybe we should just have callers grab/release the lock and 224 * have a function that they call to verify the fsp and grab a ref if 225 * needed. 226 */ 227 static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp) 228 { 229 spin_lock_bh(&fsp->scsi_pkt_lock); 230 if (fsp->state & FC_SRB_COMPL) { 231 spin_unlock_bh(&fsp->scsi_pkt_lock); 232 return -EPERM; 233 } 234 235 fc_fcp_pkt_hold(fsp); 236 return 0; 237 } 238 239 /** 240 * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its 241 * reference count 242 * @fsp: The FCP packet to be unlocked and decremented 243 */ 244 static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp) 245 { 246 spin_unlock_bh(&fsp->scsi_pkt_lock); 247 fc_fcp_pkt_release(fsp); 248 } 249 250 /** 251 * fc_fcp_timer_set() - Start a timer for a fcp_pkt 252 * @fsp: The FCP packet to start a timer for 253 * @delay: The timeout period in jiffies 254 */ 255 static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay) 256 { 257 if (!(fsp->state & FC_SRB_COMPL)) { 258 mod_timer(&fsp->timer, jiffies + delay); 259 fsp->timer_delay = delay; 260 } 261 } 262 263 static void fc_fcp_abort_done(struct fc_fcp_pkt *fsp) 264 { 265 fsp->state |= FC_SRB_ABORTED; 266 fsp->state &= ~FC_SRB_ABORT_PENDING; 267 268 if (fsp->wait_for_comp) 269 complete(&fsp->tm_done); 270 else 271 fc_fcp_complete_locked(fsp); 272 } 273 274 /** 275 * fc_fcp_send_abort() - Send an abort for exchanges associated with a 276 * fcp_pkt 277 * @fsp: The FCP packet to abort exchanges on 278 */ 279 static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp) 280 { 281 int rc; 282 283 if (!fsp->seq_ptr) 284 return -EINVAL; 285 286 per_cpu_ptr(fsp->lp->stats, get_cpu())->FcpPktAborts++; 287 put_cpu(); 288 289 fsp->state |= FC_SRB_ABORT_PENDING; 290 rc = fsp->lp->tt.seq_exch_abort(fsp->seq_ptr, 0); 291 /* 292 * ->seq_exch_abort() might return -ENXIO if 293 * the sequence is already completed 294 */ 295 if (rc == -ENXIO) { 296 fc_fcp_abort_done(fsp); 297 rc = 0; 298 } 299 return rc; 300 } 301 302 /** 303 * fc_fcp_retry_cmd() - Retry a fcp_pkt 304 * @fsp: The FCP packet to be retried 305 * 306 * Sets the status code to be FC_ERROR and then calls 307 * fc_fcp_complete_locked() which in turn calls fc_io_compl(). 308 * fc_io_compl() will notify the SCSI-ml that the I/O is done. 309 * The SCSI-ml will retry the command. 310 */ 311 static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp, int status_code) 312 { 313 if (fsp->seq_ptr) { 314 fsp->lp->tt.exch_done(fsp->seq_ptr); 315 fsp->seq_ptr = NULL; 316 } 317 318 fsp->state &= ~FC_SRB_ABORT_PENDING; 319 fsp->io_status = 0; 320 fsp->status_code = status_code; 321 fc_fcp_complete_locked(fsp); 322 } 323 324 /** 325 * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context 326 * @fsp: The FCP packet that will manage the DDP frames 327 * @xid: The XID that will be used for the DDP exchange 328 */ 329 void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid) 330 { 331 struct fc_lport *lport; 332 333 lport = fsp->lp; 334 if ((fsp->req_flags & FC_SRB_READ) && 335 (lport->lro_enabled) && (lport->tt.ddp_setup)) { 336 if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd), 337 scsi_sg_count(fsp->cmd))) 338 fsp->xfer_ddp = xid; 339 } 340 } 341 342 /** 343 * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any 344 * DDP related resources for a fcp_pkt 345 * @fsp: The FCP packet that DDP had been used on 346 */ 347 void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp) 348 { 349 struct fc_lport *lport; 350 351 if (!fsp) 352 return; 353 354 if (fsp->xfer_ddp == FC_XID_UNKNOWN) 355 return; 356 357 lport = fsp->lp; 358 if (lport->tt.ddp_done) { 359 fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp); 360 fsp->xfer_ddp = FC_XID_UNKNOWN; 361 } 362 } 363 364 /** 365 * fc_fcp_can_queue_ramp_up() - increases can_queue 366 * @lport: lport to ramp up can_queue 367 */ 368 static void fc_fcp_can_queue_ramp_up(struct fc_lport *lport) 369 { 370 struct fc_fcp_internal *si = fc_get_scsi_internal(lport); 371 unsigned long flags; 372 int can_queue; 373 374 spin_lock_irqsave(lport->host->host_lock, flags); 375 376 if (si->last_can_queue_ramp_up_time && 377 (time_before(jiffies, si->last_can_queue_ramp_up_time + 378 FC_CAN_QUEUE_PERIOD))) 379 goto unlock; 380 381 if (time_before(jiffies, si->last_can_queue_ramp_down_time + 382 FC_CAN_QUEUE_PERIOD)) 383 goto unlock; 384 385 si->last_can_queue_ramp_up_time = jiffies; 386 387 can_queue = lport->host->can_queue << 1; 388 if (can_queue >= si->max_can_queue) { 389 can_queue = si->max_can_queue; 390 si->last_can_queue_ramp_down_time = 0; 391 } 392 lport->host->can_queue = can_queue; 393 shost_printk(KERN_ERR, lport->host, "libfc: increased " 394 "can_queue to %d.\n", can_queue); 395 396 unlock: 397 spin_unlock_irqrestore(lport->host->host_lock, flags); 398 } 399 400 /** 401 * fc_fcp_can_queue_ramp_down() - reduces can_queue 402 * @lport: lport to reduce can_queue 403 * 404 * If we are getting memory allocation failures, then we may 405 * be trying to execute too many commands. We let the running 406 * commands complete or timeout, then try again with a reduced 407 * can_queue. Eventually we will hit the point where we run 408 * on all reserved structs. 409 */ 410 static void fc_fcp_can_queue_ramp_down(struct fc_lport *lport) 411 { 412 struct fc_fcp_internal *si = fc_get_scsi_internal(lport); 413 unsigned long flags; 414 int can_queue; 415 416 spin_lock_irqsave(lport->host->host_lock, flags); 417 418 if (si->last_can_queue_ramp_down_time && 419 (time_before(jiffies, si->last_can_queue_ramp_down_time + 420 FC_CAN_QUEUE_PERIOD))) 421 goto unlock; 422 423 si->last_can_queue_ramp_down_time = jiffies; 424 425 can_queue = lport->host->can_queue; 426 can_queue >>= 1; 427 if (!can_queue) 428 can_queue = 1; 429 lport->host->can_queue = can_queue; 430 431 unlock: 432 spin_unlock_irqrestore(lport->host->host_lock, flags); 433 } 434 435 /* 436 * fc_fcp_frame_alloc() - Allocates fc_frame structure and buffer. 437 * @lport: fc lport struct 438 * @len: payload length 439 * 440 * Allocates fc_frame structure and buffer but if fails to allocate 441 * then reduce can_queue. 442 */ 443 static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport, 444 size_t len) 445 { 446 struct fc_frame *fp; 447 448 fp = fc_frame_alloc(lport, len); 449 if (likely(fp)) 450 return fp; 451 452 per_cpu_ptr(lport->stats, get_cpu())->FcpFrameAllocFails++; 453 put_cpu(); 454 /* error case */ 455 fc_fcp_can_queue_ramp_down(lport); 456 shost_printk(KERN_ERR, lport->host, 457 "libfc: Could not allocate frame, " 458 "reducing can_queue to %d.\n", lport->host->can_queue); 459 return NULL; 460 } 461 462 /** 463 * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target 464 * @fsp: The FCP packet the data is on 465 * @fp: The data frame 466 */ 467 static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 468 { 469 struct scsi_cmnd *sc = fsp->cmd; 470 struct fc_lport *lport = fsp->lp; 471 struct fc_stats *stats; 472 struct fc_frame_header *fh; 473 size_t start_offset; 474 size_t offset; 475 u32 crc; 476 u32 copy_len = 0; 477 size_t len; 478 void *buf; 479 struct scatterlist *sg; 480 u32 nents; 481 u8 host_bcode = FC_COMPLETE; 482 483 fh = fc_frame_header_get(fp); 484 offset = ntohl(fh->fh_parm_offset); 485 start_offset = offset; 486 len = fr_len(fp) - sizeof(*fh); 487 buf = fc_frame_payload_get(fp, 0); 488 489 /* 490 * if this I/O is ddped then clear it and initiate recovery since data 491 * frames are expected to be placed directly in that case. 492 * 493 * Indicate error to scsi-ml because something went wrong with the 494 * ddp handling to get us here. 495 */ 496 if (fsp->xfer_ddp != FC_XID_UNKNOWN) { 497 fc_fcp_ddp_done(fsp); 498 FC_FCP_DBG(fsp, "DDP I/O in fc_fcp_recv_data set ERROR\n"); 499 host_bcode = FC_ERROR; 500 goto err; 501 } 502 if (offset + len > fsp->data_len) { 503 /* this should never happen */ 504 if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) && 505 fc_frame_crc_check(fp)) 506 goto crc_err; 507 FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx " 508 "data_len %x\n", len, offset, fsp->data_len); 509 510 /* Data is corrupted indicate scsi-ml should retry */ 511 host_bcode = FC_DATA_OVRRUN; 512 goto err; 513 } 514 if (offset != fsp->xfer_len) 515 fsp->state |= FC_SRB_DISCONTIG; 516 517 sg = scsi_sglist(sc); 518 nents = scsi_sg_count(sc); 519 520 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) { 521 copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents, 522 &offset, NULL); 523 } else { 524 crc = crc32(~0, (u8 *) fh, sizeof(*fh)); 525 copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents, 526 &offset, &crc); 527 buf = fc_frame_payload_get(fp, 0); 528 if (len % 4) 529 crc = crc32(crc, buf + len, 4 - (len % 4)); 530 531 if (~crc != le32_to_cpu(fr_crc(fp))) { 532 crc_err: 533 stats = per_cpu_ptr(lport->stats, get_cpu()); 534 stats->ErrorFrames++; 535 /* per cpu count, not total count, but OK for limit */ 536 if (stats->InvalidCRCCount++ < FC_MAX_ERROR_CNT) 537 printk(KERN_WARNING "libfc: CRC error on data " 538 "frame for port (%6.6x)\n", 539 lport->port_id); 540 put_cpu(); 541 /* 542 * Assume the frame is total garbage. 543 * We may have copied it over the good part 544 * of the buffer. 545 * If so, we need to retry the entire operation. 546 * Otherwise, ignore it. 547 */ 548 if (fsp->state & FC_SRB_DISCONTIG) { 549 host_bcode = FC_CRC_ERROR; 550 goto err; 551 } 552 return; 553 } 554 } 555 556 if (fsp->xfer_contig_end == start_offset) 557 fsp->xfer_contig_end += copy_len; 558 fsp->xfer_len += copy_len; 559 560 /* 561 * In the very rare event that this data arrived after the response 562 * and completes the transfer, call the completion handler. 563 */ 564 if (unlikely(fsp->state & FC_SRB_RCV_STATUS) && 565 fsp->xfer_len == fsp->data_len - fsp->scsi_resid) 566 fc_fcp_complete_locked(fsp); 567 return; 568 err: 569 fc_fcp_recovery(fsp, host_bcode); 570 } 571 572 /** 573 * fc_fcp_send_data() - Send SCSI data to a target 574 * @fsp: The FCP packet the data is on 575 * @sp: The sequence the data is to be sent on 576 * @offset: The starting offset for this data request 577 * @seq_blen: The burst length for this data request 578 * 579 * Called after receiving a Transfer Ready data descriptor. 580 * If the LLD is capable of sequence offload then send down the 581 * seq_blen amount of data in single frame, otherwise send 582 * multiple frames of the maximum frame payload supported by 583 * the target port. 584 */ 585 static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq, 586 size_t offset, size_t seq_blen) 587 { 588 struct fc_exch *ep; 589 struct scsi_cmnd *sc; 590 struct scatterlist *sg; 591 struct fc_frame *fp = NULL; 592 struct fc_lport *lport = fsp->lp; 593 struct page *page; 594 size_t remaining; 595 size_t t_blen; 596 size_t tlen; 597 size_t sg_bytes; 598 size_t frame_offset, fh_parm_offset; 599 size_t off; 600 int error; 601 void *data = NULL; 602 void *page_addr; 603 int using_sg = lport->sg_supp; 604 u32 f_ctl; 605 606 WARN_ON(seq_blen <= 0); 607 if (unlikely(offset + seq_blen > fsp->data_len)) { 608 /* this should never happen */ 609 FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx " 610 "offset %zx\n", seq_blen, offset); 611 fc_fcp_send_abort(fsp); 612 return 0; 613 } else if (offset != fsp->xfer_len) { 614 /* Out of Order Data Request - no problem, but unexpected. */ 615 FC_FCP_DBG(fsp, "xfer-ready non-contiguous. " 616 "seq_blen %zx offset %zx\n", seq_blen, offset); 617 } 618 619 /* 620 * if LLD is capable of seq_offload then set transport 621 * burst length (t_blen) to seq_blen, otherwise set t_blen 622 * to max FC frame payload previously set in fsp->max_payload. 623 */ 624 t_blen = fsp->max_payload; 625 if (lport->seq_offload) { 626 t_blen = min(seq_blen, (size_t)lport->lso_max); 627 FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n", 628 fsp, seq_blen, lport->lso_max, t_blen); 629 } 630 631 if (t_blen > 512) 632 t_blen &= ~(512 - 1); /* round down to block size */ 633 sc = fsp->cmd; 634 635 remaining = seq_blen; 636 fh_parm_offset = frame_offset = offset; 637 tlen = 0; 638 seq = lport->tt.seq_start_next(seq); 639 f_ctl = FC_FC_REL_OFF; 640 WARN_ON(!seq); 641 642 sg = scsi_sglist(sc); 643 644 while (remaining > 0 && sg) { 645 if (offset >= sg->length) { 646 offset -= sg->length; 647 sg = sg_next(sg); 648 continue; 649 } 650 if (!fp) { 651 tlen = min(t_blen, remaining); 652 653 /* 654 * TODO. Temporary workaround. fc_seq_send() can't 655 * handle odd lengths in non-linear skbs. 656 * This will be the final fragment only. 657 */ 658 if (tlen % 4) 659 using_sg = 0; 660 fp = fc_frame_alloc(lport, using_sg ? 0 : tlen); 661 if (!fp) 662 return -ENOMEM; 663 664 data = fc_frame_header_get(fp) + 1; 665 fh_parm_offset = frame_offset; 666 fr_max_payload(fp) = fsp->max_payload; 667 } 668 669 off = offset + sg->offset; 670 sg_bytes = min(tlen, sg->length - offset); 671 sg_bytes = min(sg_bytes, 672 (size_t) (PAGE_SIZE - (off & ~PAGE_MASK))); 673 page = sg_page(sg) + (off >> PAGE_SHIFT); 674 if (using_sg) { 675 get_page(page); 676 skb_fill_page_desc(fp_skb(fp), 677 skb_shinfo(fp_skb(fp))->nr_frags, 678 page, off & ~PAGE_MASK, sg_bytes); 679 fp_skb(fp)->data_len += sg_bytes; 680 fr_len(fp) += sg_bytes; 681 fp_skb(fp)->truesize += PAGE_SIZE; 682 } else { 683 /* 684 * The scatterlist item may be bigger than PAGE_SIZE, 685 * but we must not cross pages inside the kmap. 686 */ 687 page_addr = kmap_atomic(page); 688 memcpy(data, (char *)page_addr + (off & ~PAGE_MASK), 689 sg_bytes); 690 kunmap_atomic(page_addr); 691 data += sg_bytes; 692 } 693 offset += sg_bytes; 694 frame_offset += sg_bytes; 695 tlen -= sg_bytes; 696 remaining -= sg_bytes; 697 698 if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) && 699 (tlen)) 700 continue; 701 702 /* 703 * Send sequence with transfer sequence initiative in case 704 * this is last FCP frame of the sequence. 705 */ 706 if (remaining == 0) 707 f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ; 708 709 ep = fc_seq_exch(seq); 710 fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid, 711 FC_TYPE_FCP, f_ctl, fh_parm_offset); 712 713 /* 714 * send fragment using for a sequence. 715 */ 716 error = lport->tt.seq_send(lport, seq, fp); 717 if (error) { 718 WARN_ON(1); /* send error should be rare */ 719 return error; 720 } 721 fp = NULL; 722 } 723 fsp->xfer_len += seq_blen; /* premature count? */ 724 return 0; 725 } 726 727 /** 728 * fc_fcp_abts_resp() - Receive an ABTS response 729 * @fsp: The FCP packet that is being aborted 730 * @fp: The response frame 731 */ 732 static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 733 { 734 int ba_done = 1; 735 struct fc_ba_rjt *brp; 736 struct fc_frame_header *fh; 737 738 fh = fc_frame_header_get(fp); 739 switch (fh->fh_r_ctl) { 740 case FC_RCTL_BA_ACC: 741 break; 742 case FC_RCTL_BA_RJT: 743 brp = fc_frame_payload_get(fp, sizeof(*brp)); 744 if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR) 745 break; 746 /* fall thru */ 747 default: 748 /* 749 * we will let the command timeout 750 * and scsi-ml recover in this case, 751 * therefore cleared the ba_done flag. 752 */ 753 ba_done = 0; 754 } 755 756 if (ba_done) 757 fc_fcp_abort_done(fsp); 758 } 759 760 /** 761 * fc_fcp_recv() - Receive an FCP frame 762 * @seq: The sequence the frame is on 763 * @fp: The received frame 764 * @arg: The related FCP packet 765 * 766 * Context: Called from Soft IRQ context. Can not be called 767 * holding the FCP packet list lock. 768 */ 769 static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg) 770 { 771 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg; 772 struct fc_lport *lport = fsp->lp; 773 struct fc_frame_header *fh; 774 struct fcp_txrdy *dd; 775 u8 r_ctl; 776 int rc = 0; 777 778 if (IS_ERR(fp)) { 779 fc_fcp_error(fsp, fp); 780 return; 781 } 782 783 fh = fc_frame_header_get(fp); 784 r_ctl = fh->fh_r_ctl; 785 786 if (lport->state != LPORT_ST_READY) { 787 FC_FCP_DBG(fsp, "lport state %d, ignoring r_ctl %x\n", 788 lport->state, r_ctl); 789 goto out; 790 } 791 if (fc_fcp_lock_pkt(fsp)) 792 goto out; 793 794 if (fh->fh_type == FC_TYPE_BLS) { 795 fc_fcp_abts_resp(fsp, fp); 796 goto unlock; 797 } 798 799 if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING)) { 800 FC_FCP_DBG(fsp, "command aborted, ignoring r_ctl %x\n", r_ctl); 801 goto unlock; 802 } 803 804 if (r_ctl == FC_RCTL_DD_DATA_DESC) { 805 /* 806 * received XFER RDY from the target 807 * need to send data to the target 808 */ 809 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED); 810 dd = fc_frame_payload_get(fp, sizeof(*dd)); 811 WARN_ON(!dd); 812 813 rc = fc_fcp_send_data(fsp, seq, 814 (size_t) ntohl(dd->ft_data_ro), 815 (size_t) ntohl(dd->ft_burst_len)); 816 if (!rc) 817 seq->rec_data = fsp->xfer_len; 818 } else if (r_ctl == FC_RCTL_DD_SOL_DATA) { 819 /* 820 * received a DATA frame 821 * next we will copy the data to the system buffer 822 */ 823 WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */ 824 fc_fcp_recv_data(fsp, fp); 825 seq->rec_data = fsp->xfer_contig_end; 826 } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) { 827 WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED); 828 829 fc_fcp_resp(fsp, fp); 830 } else { 831 FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl); 832 } 833 unlock: 834 fc_fcp_unlock_pkt(fsp); 835 out: 836 fc_frame_free(fp); 837 } 838 839 /** 840 * fc_fcp_resp() - Handler for FCP responses 841 * @fsp: The FCP packet the response is for 842 * @fp: The response frame 843 */ 844 static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 845 { 846 struct fc_frame_header *fh; 847 struct fcp_resp *fc_rp; 848 struct fcp_resp_ext *rp_ex; 849 struct fcp_resp_rsp_info *fc_rp_info; 850 u32 plen; 851 u32 expected_len; 852 u32 respl = 0; 853 u32 snsl = 0; 854 u8 flags = 0; 855 856 plen = fr_len(fp); 857 fh = (struct fc_frame_header *)fr_hdr(fp); 858 if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp))) 859 goto len_err; 860 plen -= sizeof(*fh); 861 fc_rp = (struct fcp_resp *)(fh + 1); 862 fsp->cdb_status = fc_rp->fr_status; 863 flags = fc_rp->fr_flags; 864 fsp->scsi_comp_flags = flags; 865 expected_len = fsp->data_len; 866 867 /* if ddp, update xfer len */ 868 fc_fcp_ddp_done(fsp); 869 870 if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) { 871 rp_ex = (void *)(fc_rp + 1); 872 if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) { 873 if (plen < sizeof(*fc_rp) + sizeof(*rp_ex)) 874 goto len_err; 875 fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1); 876 if (flags & FCP_RSP_LEN_VAL) { 877 respl = ntohl(rp_ex->fr_rsp_len); 878 if ((respl != FCP_RESP_RSP_INFO_LEN4) && 879 (respl != FCP_RESP_RSP_INFO_LEN8)) 880 goto len_err; 881 if (fsp->wait_for_comp) { 882 /* Abuse cdb_status for rsp code */ 883 fsp->cdb_status = fc_rp_info->rsp_code; 884 complete(&fsp->tm_done); 885 /* 886 * tmfs will not have any scsi cmd so 887 * exit here 888 */ 889 return; 890 } 891 } 892 if (flags & FCP_SNS_LEN_VAL) { 893 snsl = ntohl(rp_ex->fr_sns_len); 894 if (snsl > SCSI_SENSE_BUFFERSIZE) 895 snsl = SCSI_SENSE_BUFFERSIZE; 896 memcpy(fsp->cmd->sense_buffer, 897 (char *)fc_rp_info + respl, snsl); 898 } 899 } 900 if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) { 901 if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid)) 902 goto len_err; 903 if (flags & FCP_RESID_UNDER) { 904 fsp->scsi_resid = ntohl(rp_ex->fr_resid); 905 /* 906 * The cmnd->underflow is the minimum number of 907 * bytes that must be transferred for this 908 * command. Provided a sense condition is not 909 * present, make sure the actual amount 910 * transferred is at least the underflow value 911 * or fail. 912 */ 913 if (!(flags & FCP_SNS_LEN_VAL) && 914 (fc_rp->fr_status == 0) && 915 (scsi_bufflen(fsp->cmd) - 916 fsp->scsi_resid) < fsp->cmd->underflow) 917 goto err; 918 expected_len -= fsp->scsi_resid; 919 } else { 920 fsp->status_code = FC_ERROR; 921 } 922 } 923 } 924 fsp->state |= FC_SRB_RCV_STATUS; 925 926 /* 927 * Check for missing or extra data frames. 928 */ 929 if (unlikely(fsp->cdb_status == SAM_STAT_GOOD && 930 fsp->xfer_len != expected_len)) { 931 if (fsp->xfer_len < expected_len) { 932 /* 933 * Some data may be queued locally, 934 * Wait a at least one jiffy to see if it is delivered. 935 * If this expires without data, we may do SRR. 936 */ 937 if (fsp->lp->qfull) { 938 FC_FCP_DBG(fsp, "tgt %6.6x queue busy retry\n", 939 fsp->rport->port_id); 940 return; 941 } 942 FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx data underrun " 943 "len %x, data len %x\n", 944 fsp->rport->port_id, 945 fsp->xfer_len, expected_len, fsp->data_len); 946 fc_fcp_timer_set(fsp, 2); 947 return; 948 } 949 fsp->status_code = FC_DATA_OVRRUN; 950 FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx greater than expected, " 951 "len %x, data len %x\n", 952 fsp->rport->port_id, 953 fsp->xfer_len, expected_len, fsp->data_len); 954 } 955 fc_fcp_complete_locked(fsp); 956 return; 957 958 len_err: 959 FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u " 960 "snsl %u\n", flags, fr_len(fp), respl, snsl); 961 err: 962 fsp->status_code = FC_ERROR; 963 fc_fcp_complete_locked(fsp); 964 } 965 966 /** 967 * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the 968 * fcp_pkt lock held 969 * @fsp: The FCP packet to be completed 970 * 971 * This function may sleep if a timer is pending. The packet lock must be 972 * held, and the host lock must not be held. 973 */ 974 static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp) 975 { 976 struct fc_lport *lport = fsp->lp; 977 struct fc_seq *seq; 978 struct fc_exch *ep; 979 u32 f_ctl; 980 981 if (fsp->state & FC_SRB_ABORT_PENDING) 982 return; 983 984 if (fsp->state & FC_SRB_ABORTED) { 985 if (!fsp->status_code) 986 fsp->status_code = FC_CMD_ABORTED; 987 } else { 988 /* 989 * Test for transport underrun, independent of response 990 * underrun status. 991 */ 992 if (fsp->cdb_status == SAM_STAT_GOOD && 993 fsp->xfer_len < fsp->data_len && !fsp->io_status && 994 (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) || 995 fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) { 996 FC_FCP_DBG(fsp, "data underrun, xfer %zx data %x\n", 997 fsp->xfer_len, fsp->data_len); 998 fsp->status_code = FC_DATA_UNDRUN; 999 } 1000 } 1001 1002 seq = fsp->seq_ptr; 1003 if (seq) { 1004 fsp->seq_ptr = NULL; 1005 if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) { 1006 struct fc_frame *conf_frame; 1007 struct fc_seq *csp; 1008 1009 csp = lport->tt.seq_start_next(seq); 1010 conf_frame = fc_fcp_frame_alloc(fsp->lp, 0); 1011 if (conf_frame) { 1012 f_ctl = FC_FC_SEQ_INIT; 1013 f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ; 1014 ep = fc_seq_exch(seq); 1015 fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL, 1016 ep->did, ep->sid, 1017 FC_TYPE_FCP, f_ctl, 0); 1018 lport->tt.seq_send(lport, csp, conf_frame); 1019 } 1020 } 1021 lport->tt.exch_done(seq); 1022 } 1023 /* 1024 * Some resets driven by SCSI are not I/Os and do not have 1025 * SCSI commands associated with the requests. We should not 1026 * call I/O completion if we do not have a SCSI command. 1027 */ 1028 if (fsp->cmd) 1029 fc_io_compl(fsp); 1030 } 1031 1032 /** 1033 * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt 1034 * @fsp: The FCP packet whose exchanges should be canceled 1035 * @error: The reason for the cancellation 1036 */ 1037 static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error) 1038 { 1039 struct fc_lport *lport = fsp->lp; 1040 1041 if (fsp->seq_ptr) { 1042 lport->tt.exch_done(fsp->seq_ptr); 1043 fsp->seq_ptr = NULL; 1044 } 1045 fsp->status_code = error; 1046 } 1047 1048 /** 1049 * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port 1050 * @lport: The local port whose exchanges should be canceled 1051 * @id: The target's ID 1052 * @lun: The LUN 1053 * @error: The reason for cancellation 1054 * 1055 * If lun or id is -1, they are ignored. 1056 */ 1057 static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id, 1058 unsigned int lun, int error) 1059 { 1060 struct fc_fcp_internal *si = fc_get_scsi_internal(lport); 1061 struct fc_fcp_pkt *fsp; 1062 struct scsi_cmnd *sc_cmd; 1063 unsigned long flags; 1064 1065 spin_lock_irqsave(&si->scsi_queue_lock, flags); 1066 restart: 1067 list_for_each_entry(fsp, &si->scsi_pkt_queue, list) { 1068 sc_cmd = fsp->cmd; 1069 if (id != -1 && scmd_id(sc_cmd) != id) 1070 continue; 1071 1072 if (lun != -1 && sc_cmd->device->lun != lun) 1073 continue; 1074 1075 fc_fcp_pkt_hold(fsp); 1076 spin_unlock_irqrestore(&si->scsi_queue_lock, flags); 1077 1078 spin_lock_bh(&fsp->scsi_pkt_lock); 1079 if (!(fsp->state & FC_SRB_COMPL)) { 1080 fsp->state |= FC_SRB_COMPL; 1081 /* 1082 * TODO: dropping scsi_pkt_lock and then reacquiring 1083 * again around fc_fcp_cleanup_cmd() is required, 1084 * since fc_fcp_cleanup_cmd() calls into 1085 * fc_seq_set_resp() and that func preempts cpu using 1086 * schedule. May be schedule and related code should be 1087 * removed instead of unlocking here to avoid scheduling 1088 * while atomic bug. 1089 */ 1090 spin_unlock_bh(&fsp->scsi_pkt_lock); 1091 1092 fc_fcp_cleanup_cmd(fsp, error); 1093 1094 spin_lock_bh(&fsp->scsi_pkt_lock); 1095 fc_io_compl(fsp); 1096 } 1097 spin_unlock_bh(&fsp->scsi_pkt_lock); 1098 1099 fc_fcp_pkt_release(fsp); 1100 spin_lock_irqsave(&si->scsi_queue_lock, flags); 1101 /* 1102 * while we dropped the lock multiple pkts could 1103 * have been released, so we have to start over. 1104 */ 1105 goto restart; 1106 } 1107 spin_unlock_irqrestore(&si->scsi_queue_lock, flags); 1108 } 1109 1110 /** 1111 * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port 1112 * @lport: The local port whose exchanges are to be aborted 1113 */ 1114 static void fc_fcp_abort_io(struct fc_lport *lport) 1115 { 1116 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR); 1117 } 1118 1119 /** 1120 * fc_fcp_pkt_send() - Send a fcp_pkt 1121 * @lport: The local port to send the FCP packet on 1122 * @fsp: The FCP packet to send 1123 * 1124 * Return: Zero for success and -1 for failure 1125 * Locks: Called without locks held 1126 */ 1127 static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp) 1128 { 1129 struct fc_fcp_internal *si = fc_get_scsi_internal(lport); 1130 unsigned long flags; 1131 int rc; 1132 1133 fsp->cmd->SCp.ptr = (char *)fsp; 1134 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len); 1135 fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK; 1136 1137 int_to_scsilun(fsp->cmd->device->lun, &fsp->cdb_cmd.fc_lun); 1138 memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len); 1139 1140 spin_lock_irqsave(&si->scsi_queue_lock, flags); 1141 list_add_tail(&fsp->list, &si->scsi_pkt_queue); 1142 spin_unlock_irqrestore(&si->scsi_queue_lock, flags); 1143 rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv); 1144 if (unlikely(rc)) { 1145 spin_lock_irqsave(&si->scsi_queue_lock, flags); 1146 fsp->cmd->SCp.ptr = NULL; 1147 list_del(&fsp->list); 1148 spin_unlock_irqrestore(&si->scsi_queue_lock, flags); 1149 } 1150 1151 return rc; 1152 } 1153 1154 /** 1155 * get_fsp_rec_tov() - Helper function to get REC_TOV 1156 * @fsp: the FCP packet 1157 * 1158 * Returns rec tov in jiffies as rpriv->e_d_tov + 1 second 1159 */ 1160 static inline unsigned int get_fsp_rec_tov(struct fc_fcp_pkt *fsp) 1161 { 1162 struct fc_rport_libfc_priv *rpriv = fsp->rport->dd_data; 1163 unsigned int e_d_tov = FC_DEF_E_D_TOV; 1164 1165 if (rpriv && rpriv->e_d_tov > e_d_tov) 1166 e_d_tov = rpriv->e_d_tov; 1167 return msecs_to_jiffies(e_d_tov) + HZ; 1168 } 1169 1170 /** 1171 * fc_fcp_cmd_send() - Send a FCP command 1172 * @lport: The local port to send the command on 1173 * @fsp: The FCP packet the command is on 1174 * @resp: The handler for the response 1175 */ 1176 static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp, 1177 void (*resp)(struct fc_seq *, 1178 struct fc_frame *fp, 1179 void *arg)) 1180 { 1181 struct fc_frame *fp; 1182 struct fc_seq *seq; 1183 struct fc_rport *rport; 1184 struct fc_rport_libfc_priv *rpriv; 1185 const size_t len = sizeof(fsp->cdb_cmd); 1186 int rc = 0; 1187 1188 if (fc_fcp_lock_pkt(fsp)) 1189 return 0; 1190 1191 fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd)); 1192 if (!fp) { 1193 rc = -1; 1194 goto unlock; 1195 } 1196 1197 memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len); 1198 fr_fsp(fp) = fsp; 1199 rport = fsp->rport; 1200 fsp->max_payload = rport->maxframe_size; 1201 rpriv = rport->dd_data; 1202 1203 fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id, 1204 rpriv->local_port->port_id, FC_TYPE_FCP, 1205 FC_FCTL_REQ, 0); 1206 1207 seq = lport->tt.exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy, 1208 fsp, 0); 1209 if (!seq) { 1210 rc = -1; 1211 goto unlock; 1212 } 1213 fsp->seq_ptr = seq; 1214 fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */ 1215 1216 setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp); 1217 if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED) 1218 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp)); 1219 1220 unlock: 1221 fc_fcp_unlock_pkt(fsp); 1222 return rc; 1223 } 1224 1225 /** 1226 * fc_fcp_error() - Handler for FCP layer errors 1227 * @fsp: The FCP packet the error is on 1228 * @fp: The frame that has errored 1229 */ 1230 static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 1231 { 1232 int error = PTR_ERR(fp); 1233 1234 if (fc_fcp_lock_pkt(fsp)) 1235 return; 1236 1237 if (error == -FC_EX_CLOSED) { 1238 fc_fcp_retry_cmd(fsp, FC_ERROR); 1239 goto unlock; 1240 } 1241 1242 /* 1243 * clear abort pending, because the lower layer 1244 * decided to force completion. 1245 */ 1246 fsp->state &= ~FC_SRB_ABORT_PENDING; 1247 fsp->status_code = FC_CMD_PLOGO; 1248 fc_fcp_complete_locked(fsp); 1249 unlock: 1250 fc_fcp_unlock_pkt(fsp); 1251 } 1252 1253 /** 1254 * fc_fcp_pkt_abort() - Abort a fcp_pkt 1255 * @fsp: The FCP packet to abort on 1256 * 1257 * Called to send an abort and then wait for abort completion 1258 */ 1259 static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp) 1260 { 1261 int rc = FAILED; 1262 unsigned long ticks_left; 1263 1264 FC_FCP_DBG(fsp, "pkt abort state %x\n", fsp->state); 1265 if (fc_fcp_send_abort(fsp)) { 1266 FC_FCP_DBG(fsp, "failed to send abort\n"); 1267 return FAILED; 1268 } 1269 1270 if (fsp->state & FC_SRB_ABORTED) { 1271 FC_FCP_DBG(fsp, "target abort cmd completed\n"); 1272 return SUCCESS; 1273 } 1274 1275 init_completion(&fsp->tm_done); 1276 fsp->wait_for_comp = 1; 1277 1278 spin_unlock_bh(&fsp->scsi_pkt_lock); 1279 ticks_left = wait_for_completion_timeout(&fsp->tm_done, 1280 FC_SCSI_TM_TOV); 1281 spin_lock_bh(&fsp->scsi_pkt_lock); 1282 fsp->wait_for_comp = 0; 1283 1284 if (!ticks_left) { 1285 FC_FCP_DBG(fsp, "target abort cmd failed\n"); 1286 } else if (fsp->state & FC_SRB_ABORTED) { 1287 FC_FCP_DBG(fsp, "target abort cmd passed\n"); 1288 rc = SUCCESS; 1289 fc_fcp_complete_locked(fsp); 1290 } 1291 1292 return rc; 1293 } 1294 1295 /** 1296 * fc_lun_reset_send() - Send LUN reset command 1297 * @data: The FCP packet that identifies the LUN to be reset 1298 */ 1299 static void fc_lun_reset_send(unsigned long data) 1300 { 1301 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data; 1302 struct fc_lport *lport = fsp->lp; 1303 1304 if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) { 1305 if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY) 1306 return; 1307 if (fc_fcp_lock_pkt(fsp)) 1308 return; 1309 setup_timer(&fsp->timer, fc_lun_reset_send, (unsigned long)fsp); 1310 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp)); 1311 fc_fcp_unlock_pkt(fsp); 1312 } 1313 } 1314 1315 /** 1316 * fc_lun_reset() - Send a LUN RESET command to a device 1317 * and wait for the reply 1318 * @lport: The local port to sent the command on 1319 * @fsp: The FCP packet that identifies the LUN to be reset 1320 * @id: The SCSI command ID 1321 * @lun: The LUN ID to be reset 1322 */ 1323 static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp, 1324 unsigned int id, unsigned int lun) 1325 { 1326 int rc; 1327 1328 fsp->cdb_cmd.fc_dl = htonl(fsp->data_len); 1329 fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET; 1330 int_to_scsilun(lun, &fsp->cdb_cmd.fc_lun); 1331 1332 fsp->wait_for_comp = 1; 1333 init_completion(&fsp->tm_done); 1334 1335 fc_lun_reset_send((unsigned long)fsp); 1336 1337 /* 1338 * wait for completion of reset 1339 * after that make sure all commands are terminated 1340 */ 1341 rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV); 1342 1343 spin_lock_bh(&fsp->scsi_pkt_lock); 1344 fsp->state |= FC_SRB_COMPL; 1345 spin_unlock_bh(&fsp->scsi_pkt_lock); 1346 1347 del_timer_sync(&fsp->timer); 1348 1349 spin_lock_bh(&fsp->scsi_pkt_lock); 1350 if (fsp->seq_ptr) { 1351 lport->tt.exch_done(fsp->seq_ptr); 1352 fsp->seq_ptr = NULL; 1353 } 1354 fsp->wait_for_comp = 0; 1355 spin_unlock_bh(&fsp->scsi_pkt_lock); 1356 1357 if (!rc) { 1358 FC_SCSI_DBG(lport, "lun reset failed\n"); 1359 return FAILED; 1360 } 1361 1362 /* cdb_status holds the tmf's rsp code */ 1363 if (fsp->cdb_status != FCP_TMF_CMPL) 1364 return FAILED; 1365 1366 FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun); 1367 fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED); 1368 return SUCCESS; 1369 } 1370 1371 /** 1372 * fc_tm_done() - Task Management response handler 1373 * @seq: The sequence that the response is on 1374 * @fp: The response frame 1375 * @arg: The FCP packet the response is for 1376 */ 1377 static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg) 1378 { 1379 struct fc_fcp_pkt *fsp = arg; 1380 struct fc_frame_header *fh; 1381 1382 if (IS_ERR(fp)) { 1383 /* 1384 * If there is an error just let it timeout or wait 1385 * for TMF to be aborted if it timedout. 1386 * 1387 * scsi-eh will escalate for when either happens. 1388 */ 1389 return; 1390 } 1391 1392 if (fc_fcp_lock_pkt(fsp)) 1393 goto out; 1394 1395 /* 1396 * raced with eh timeout handler. 1397 */ 1398 if (!fsp->seq_ptr || !fsp->wait_for_comp) 1399 goto out_unlock; 1400 1401 fh = fc_frame_header_get(fp); 1402 if (fh->fh_type != FC_TYPE_BLS) 1403 fc_fcp_resp(fsp, fp); 1404 fsp->seq_ptr = NULL; 1405 fsp->lp->tt.exch_done(seq); 1406 out_unlock: 1407 fc_fcp_unlock_pkt(fsp); 1408 out: 1409 fc_frame_free(fp); 1410 } 1411 1412 /** 1413 * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port 1414 * @lport: The local port to be cleaned up 1415 */ 1416 static void fc_fcp_cleanup(struct fc_lport *lport) 1417 { 1418 fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR); 1419 } 1420 1421 /** 1422 * fc_fcp_timeout() - Handler for fcp_pkt timeouts 1423 * @data: The FCP packet that has timed out 1424 * 1425 * If REC is supported then just issue it and return. The REC exchange will 1426 * complete or time out and recovery can continue at that point. Otherwise, 1427 * if the response has been received without all the data it has been 1428 * ER_TIMEOUT since the response was received. If the response has not been 1429 * received we see if data was received recently. If it has been then we 1430 * continue waiting, otherwise, we abort the command. 1431 */ 1432 static void fc_fcp_timeout(unsigned long data) 1433 { 1434 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)data; 1435 struct fc_rport *rport = fsp->rport; 1436 struct fc_rport_libfc_priv *rpriv = rport->dd_data; 1437 1438 if (fc_fcp_lock_pkt(fsp)) 1439 return; 1440 1441 if (fsp->cdb_cmd.fc_tm_flags) 1442 goto unlock; 1443 1444 if (fsp->lp->qfull) { 1445 FC_FCP_DBG(fsp, "fcp timeout, resetting timer delay %d\n", 1446 fsp->timer_delay); 1447 setup_timer(&fsp->timer, fc_fcp_timeout, (unsigned long)fsp); 1448 fc_fcp_timer_set(fsp, fsp->timer_delay); 1449 goto unlock; 1450 } 1451 FC_FCP_DBG(fsp, "fcp timeout, delay %d flags %x state %x\n", 1452 fsp->timer_delay, rpriv->flags, fsp->state); 1453 fsp->state |= FC_SRB_FCP_PROCESSING_TMO; 1454 1455 if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED) 1456 fc_fcp_rec(fsp); 1457 else if (fsp->state & FC_SRB_RCV_STATUS) 1458 fc_fcp_complete_locked(fsp); 1459 else 1460 fc_fcp_recovery(fsp, FC_TIMED_OUT); 1461 fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO; 1462 unlock: 1463 fc_fcp_unlock_pkt(fsp); 1464 } 1465 1466 /** 1467 * fc_fcp_rec() - Send a REC ELS request 1468 * @fsp: The FCP packet to send the REC request on 1469 */ 1470 static void fc_fcp_rec(struct fc_fcp_pkt *fsp) 1471 { 1472 struct fc_lport *lport; 1473 struct fc_frame *fp; 1474 struct fc_rport *rport; 1475 struct fc_rport_libfc_priv *rpriv; 1476 1477 lport = fsp->lp; 1478 rport = fsp->rport; 1479 rpriv = rport->dd_data; 1480 if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) { 1481 fsp->status_code = FC_HRD_ERROR; 1482 fsp->io_status = 0; 1483 fc_fcp_complete_locked(fsp); 1484 return; 1485 } 1486 1487 fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec)); 1488 if (!fp) 1489 goto retry; 1490 1491 fr_seq(fp) = fsp->seq_ptr; 1492 fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id, 1493 rpriv->local_port->port_id, FC_TYPE_ELS, 1494 FC_FCTL_REQ, 0); 1495 if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC, 1496 fc_fcp_rec_resp, fsp, 1497 2 * lport->r_a_tov)) { 1498 fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */ 1499 return; 1500 } 1501 retry: 1502 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY) 1503 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp)); 1504 else 1505 fc_fcp_recovery(fsp, FC_TIMED_OUT); 1506 } 1507 1508 /** 1509 * fc_fcp_rec_resp() - Handler for REC ELS responses 1510 * @seq: The sequence the response is on 1511 * @fp: The response frame 1512 * @arg: The FCP packet the response is on 1513 * 1514 * If the response is a reject then the scsi layer will handle 1515 * the timeout. If the response is a LS_ACC then if the I/O was not completed 1516 * set the timeout and return. If the I/O was completed then complete the 1517 * exchange and tell the SCSI layer. 1518 */ 1519 static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg) 1520 { 1521 struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg; 1522 struct fc_els_rec_acc *recp; 1523 struct fc_els_ls_rjt *rjt; 1524 u32 e_stat; 1525 u8 opcode; 1526 u32 offset; 1527 enum dma_data_direction data_dir; 1528 enum fc_rctl r_ctl; 1529 struct fc_rport_libfc_priv *rpriv; 1530 1531 if (IS_ERR(fp)) { 1532 fc_fcp_rec_error(fsp, fp); 1533 return; 1534 } 1535 1536 if (fc_fcp_lock_pkt(fsp)) 1537 goto out; 1538 1539 fsp->recov_retry = 0; 1540 opcode = fc_frame_payload_op(fp); 1541 if (opcode == ELS_LS_RJT) { 1542 rjt = fc_frame_payload_get(fp, sizeof(*rjt)); 1543 switch (rjt->er_reason) { 1544 default: 1545 FC_FCP_DBG(fsp, 1546 "device %x invalid REC reject %d/%d\n", 1547 fsp->rport->port_id, rjt->er_reason, 1548 rjt->er_explan); 1549 /* fall through */ 1550 case ELS_RJT_UNSUP: 1551 FC_FCP_DBG(fsp, "device does not support REC\n"); 1552 rpriv = fsp->rport->dd_data; 1553 /* 1554 * if we do not spport RECs or got some bogus 1555 * reason then resetup timer so we check for 1556 * making progress. 1557 */ 1558 rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED; 1559 break; 1560 case ELS_RJT_LOGIC: 1561 case ELS_RJT_UNAB: 1562 FC_FCP_DBG(fsp, "device %x REC reject %d/%d\n", 1563 fsp->rport->port_id, rjt->er_reason, 1564 rjt->er_explan); 1565 /* 1566 * If response got lost or is stuck in the 1567 * queue somewhere we have no idea if and when 1568 * the response will be received. So quarantine 1569 * the xid and retry the command. 1570 */ 1571 if (rjt->er_explan == ELS_EXPL_OXID_RXID) { 1572 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr); 1573 ep->state |= FC_EX_QUARANTINE; 1574 fsp->state |= FC_SRB_ABORTED; 1575 fc_fcp_retry_cmd(fsp, FC_TRANS_RESET); 1576 break; 1577 } 1578 fc_fcp_recovery(fsp, FC_TRANS_RESET); 1579 break; 1580 } 1581 } else if (opcode == ELS_LS_ACC) { 1582 if (fsp->state & FC_SRB_ABORTED) 1583 goto unlock_out; 1584 1585 data_dir = fsp->cmd->sc_data_direction; 1586 recp = fc_frame_payload_get(fp, sizeof(*recp)); 1587 offset = ntohl(recp->reca_fc4value); 1588 e_stat = ntohl(recp->reca_e_stat); 1589 1590 if (e_stat & ESB_ST_COMPLETE) { 1591 1592 /* 1593 * The exchange is complete. 1594 * 1595 * For output, we must've lost the response. 1596 * For input, all data must've been sent. 1597 * We lost may have lost the response 1598 * (and a confirmation was requested) and maybe 1599 * some data. 1600 * 1601 * If all data received, send SRR 1602 * asking for response. If partial data received, 1603 * or gaps, SRR requests data at start of gap. 1604 * Recovery via SRR relies on in-order-delivery. 1605 */ 1606 if (data_dir == DMA_TO_DEVICE) { 1607 r_ctl = FC_RCTL_DD_CMD_STATUS; 1608 } else if (fsp->xfer_contig_end == offset) { 1609 r_ctl = FC_RCTL_DD_CMD_STATUS; 1610 } else { 1611 offset = fsp->xfer_contig_end; 1612 r_ctl = FC_RCTL_DD_SOL_DATA; 1613 } 1614 fc_fcp_srr(fsp, r_ctl, offset); 1615 } else if (e_stat & ESB_ST_SEQ_INIT) { 1616 /* 1617 * The remote port has the initiative, so just 1618 * keep waiting for it to complete. 1619 */ 1620 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp)); 1621 } else { 1622 1623 /* 1624 * The exchange is incomplete, we have seq. initiative. 1625 * Lost response with requested confirmation, 1626 * lost confirmation, lost transfer ready or 1627 * lost write data. 1628 * 1629 * For output, if not all data was received, ask 1630 * for transfer ready to be repeated. 1631 * 1632 * If we received or sent all the data, send SRR to 1633 * request response. 1634 * 1635 * If we lost a response, we may have lost some read 1636 * data as well. 1637 */ 1638 r_ctl = FC_RCTL_DD_SOL_DATA; 1639 if (data_dir == DMA_TO_DEVICE) { 1640 r_ctl = FC_RCTL_DD_CMD_STATUS; 1641 if (offset < fsp->data_len) 1642 r_ctl = FC_RCTL_DD_DATA_DESC; 1643 } else if (offset == fsp->xfer_contig_end) { 1644 r_ctl = FC_RCTL_DD_CMD_STATUS; 1645 } else if (fsp->xfer_contig_end < offset) { 1646 offset = fsp->xfer_contig_end; 1647 } 1648 fc_fcp_srr(fsp, r_ctl, offset); 1649 } 1650 } 1651 unlock_out: 1652 fc_fcp_unlock_pkt(fsp); 1653 out: 1654 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */ 1655 fc_frame_free(fp); 1656 } 1657 1658 /** 1659 * fc_fcp_rec_error() - Handler for REC errors 1660 * @fsp: The FCP packet the error is on 1661 * @fp: The REC frame 1662 */ 1663 static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 1664 { 1665 int error = PTR_ERR(fp); 1666 1667 if (fc_fcp_lock_pkt(fsp)) 1668 goto out; 1669 1670 switch (error) { 1671 case -FC_EX_CLOSED: 1672 FC_FCP_DBG(fsp, "REC %p fid %6.6x exchange closed\n", 1673 fsp, fsp->rport->port_id); 1674 fc_fcp_retry_cmd(fsp, FC_ERROR); 1675 break; 1676 1677 default: 1678 FC_FCP_DBG(fsp, "REC %p fid %6.6x error unexpected error %d\n", 1679 fsp, fsp->rport->port_id, error); 1680 fsp->status_code = FC_CMD_PLOGO; 1681 /* fall through */ 1682 1683 case -FC_EX_TIMEOUT: 1684 /* 1685 * Assume REC or LS_ACC was lost. 1686 * The exchange manager will have aborted REC, so retry. 1687 */ 1688 FC_FCP_DBG(fsp, "REC %p fid %6.6x exchange timeout retry %d/%d\n", 1689 fsp, fsp->rport->port_id, fsp->recov_retry, 1690 FC_MAX_RECOV_RETRY); 1691 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY) 1692 fc_fcp_rec(fsp); 1693 else 1694 fc_fcp_recovery(fsp, FC_ERROR); 1695 break; 1696 } 1697 fc_fcp_unlock_pkt(fsp); 1698 out: 1699 fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */ 1700 } 1701 1702 /** 1703 * fc_fcp_recovery() - Handler for fcp_pkt recovery 1704 * @fsp: The FCP pkt that needs to be aborted 1705 */ 1706 static void fc_fcp_recovery(struct fc_fcp_pkt *fsp, u8 code) 1707 { 1708 FC_FCP_DBG(fsp, "start recovery code %x\n", code); 1709 fsp->status_code = code; 1710 fsp->cdb_status = 0; 1711 fsp->io_status = 0; 1712 /* 1713 * if this fails then we let the scsi command timer fire and 1714 * scsi-ml escalate. 1715 */ 1716 fc_fcp_send_abort(fsp); 1717 } 1718 1719 /** 1720 * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request) 1721 * @fsp: The FCP packet the SRR is to be sent on 1722 * @r_ctl: The R_CTL field for the SRR request 1723 * This is called after receiving status but insufficient data, or 1724 * when expecting status but the request has timed out. 1725 */ 1726 static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset) 1727 { 1728 struct fc_lport *lport = fsp->lp; 1729 struct fc_rport *rport; 1730 struct fc_rport_libfc_priv *rpriv; 1731 struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr); 1732 struct fc_seq *seq; 1733 struct fcp_srr *srr; 1734 struct fc_frame *fp; 1735 1736 rport = fsp->rport; 1737 rpriv = rport->dd_data; 1738 1739 if (!(rpriv->flags & FC_RP_FLAGS_RETRY) || 1740 rpriv->rp_state != RPORT_ST_READY) 1741 goto retry; /* shouldn't happen */ 1742 fp = fc_fcp_frame_alloc(lport, sizeof(*srr)); 1743 if (!fp) 1744 goto retry; 1745 1746 srr = fc_frame_payload_get(fp, sizeof(*srr)); 1747 memset(srr, 0, sizeof(*srr)); 1748 srr->srr_op = ELS_SRR; 1749 srr->srr_ox_id = htons(ep->oxid); 1750 srr->srr_rx_id = htons(ep->rxid); 1751 srr->srr_r_ctl = r_ctl; 1752 srr->srr_rel_off = htonl(offset); 1753 1754 fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id, 1755 rpriv->local_port->port_id, FC_TYPE_FCP, 1756 FC_FCTL_REQ, 0); 1757 1758 seq = lport->tt.exch_seq_send(lport, fp, fc_fcp_srr_resp, 1759 fc_fcp_pkt_destroy, 1760 fsp, get_fsp_rec_tov(fsp)); 1761 if (!seq) 1762 goto retry; 1763 1764 fsp->recov_seq = seq; 1765 fsp->xfer_len = offset; 1766 fsp->xfer_contig_end = offset; 1767 fsp->state &= ~FC_SRB_RCV_STATUS; 1768 fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */ 1769 return; 1770 retry: 1771 fc_fcp_retry_cmd(fsp, FC_TRANS_RESET); 1772 } 1773 1774 /** 1775 * fc_fcp_srr_resp() - Handler for SRR response 1776 * @seq: The sequence the SRR is on 1777 * @fp: The SRR frame 1778 * @arg: The FCP packet the SRR is on 1779 */ 1780 static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg) 1781 { 1782 struct fc_fcp_pkt *fsp = arg; 1783 struct fc_frame_header *fh; 1784 1785 if (IS_ERR(fp)) { 1786 fc_fcp_srr_error(fsp, fp); 1787 return; 1788 } 1789 1790 if (fc_fcp_lock_pkt(fsp)) 1791 goto out; 1792 1793 fh = fc_frame_header_get(fp); 1794 /* 1795 * BUG? fc_fcp_srr_error calls exch_done which would release 1796 * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT, 1797 * then fc_exch_timeout would be sending an abort. The exch_done 1798 * call by fc_fcp_srr_error would prevent fc_exch.c from seeing 1799 * an abort response though. 1800 */ 1801 if (fh->fh_type == FC_TYPE_BLS) { 1802 fc_fcp_unlock_pkt(fsp); 1803 return; 1804 } 1805 1806 switch (fc_frame_payload_op(fp)) { 1807 case ELS_LS_ACC: 1808 fsp->recov_retry = 0; 1809 fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp)); 1810 break; 1811 case ELS_LS_RJT: 1812 default: 1813 fc_fcp_recovery(fsp, FC_ERROR); 1814 break; 1815 } 1816 fc_fcp_unlock_pkt(fsp); 1817 out: 1818 fsp->lp->tt.exch_done(seq); 1819 fc_frame_free(fp); 1820 } 1821 1822 /** 1823 * fc_fcp_srr_error() - Handler for SRR errors 1824 * @fsp: The FCP packet that the SRR error is on 1825 * @fp: The SRR frame 1826 */ 1827 static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp) 1828 { 1829 if (fc_fcp_lock_pkt(fsp)) 1830 goto out; 1831 switch (PTR_ERR(fp)) { 1832 case -FC_EX_TIMEOUT: 1833 FC_FCP_DBG(fsp, "SRR timeout, retries %d\n", fsp->recov_retry); 1834 if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY) 1835 fc_fcp_rec(fsp); 1836 else 1837 fc_fcp_recovery(fsp, FC_TIMED_OUT); 1838 break; 1839 case -FC_EX_CLOSED: /* e.g., link failure */ 1840 FC_FCP_DBG(fsp, "SRR error, exchange closed\n"); 1841 /* fall through */ 1842 default: 1843 fc_fcp_retry_cmd(fsp, FC_ERROR); 1844 break; 1845 } 1846 fc_fcp_unlock_pkt(fsp); 1847 out: 1848 fsp->lp->tt.exch_done(fsp->recov_seq); 1849 } 1850 1851 /** 1852 * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready 1853 * @lport: The local port to be checked 1854 */ 1855 static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport) 1856 { 1857 /* lock ? */ 1858 return (lport->state == LPORT_ST_READY) && 1859 lport->link_up && !lport->qfull; 1860 } 1861 1862 /** 1863 * fc_queuecommand() - The queuecommand function of the SCSI template 1864 * @shost: The Scsi_Host that the command was issued to 1865 * @cmd: The scsi_cmnd to be executed 1866 * 1867 * This is the i/o strategy routine, called by the SCSI layer. 1868 */ 1869 int fc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc_cmd) 1870 { 1871 struct fc_lport *lport = shost_priv(shost); 1872 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device)); 1873 struct fc_fcp_pkt *fsp; 1874 struct fc_rport_libfc_priv *rpriv; 1875 int rval; 1876 int rc = 0; 1877 struct fc_stats *stats; 1878 1879 rval = fc_remote_port_chkready(rport); 1880 if (rval) { 1881 sc_cmd->result = rval; 1882 sc_cmd->scsi_done(sc_cmd); 1883 return 0; 1884 } 1885 1886 if (!*(struct fc_remote_port **)rport->dd_data) { 1887 /* 1888 * rport is transitioning from blocked/deleted to 1889 * online 1890 */ 1891 sc_cmd->result = DID_IMM_RETRY << 16; 1892 sc_cmd->scsi_done(sc_cmd); 1893 goto out; 1894 } 1895 1896 rpriv = rport->dd_data; 1897 1898 if (!fc_fcp_lport_queue_ready(lport)) { 1899 if (lport->qfull) { 1900 fc_fcp_can_queue_ramp_down(lport); 1901 shost_printk(KERN_ERR, lport->host, 1902 "libfc: queue full, " 1903 "reducing can_queue to %d.\n", 1904 lport->host->can_queue); 1905 } 1906 rc = SCSI_MLQUEUE_HOST_BUSY; 1907 goto out; 1908 } 1909 1910 fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC); 1911 if (fsp == NULL) { 1912 rc = SCSI_MLQUEUE_HOST_BUSY; 1913 goto out; 1914 } 1915 1916 /* 1917 * build the libfc request pkt 1918 */ 1919 fsp->cmd = sc_cmd; /* save the cmd */ 1920 fsp->rport = rport; /* set the remote port ptr */ 1921 1922 /* 1923 * set up the transfer length 1924 */ 1925 fsp->data_len = scsi_bufflen(sc_cmd); 1926 fsp->xfer_len = 0; 1927 1928 /* 1929 * setup the data direction 1930 */ 1931 stats = per_cpu_ptr(lport->stats, get_cpu()); 1932 if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) { 1933 fsp->req_flags = FC_SRB_READ; 1934 stats->InputRequests++; 1935 stats->InputBytes += fsp->data_len; 1936 } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) { 1937 fsp->req_flags = FC_SRB_WRITE; 1938 stats->OutputRequests++; 1939 stats->OutputBytes += fsp->data_len; 1940 } else { 1941 fsp->req_flags = 0; 1942 stats->ControlRequests++; 1943 } 1944 put_cpu(); 1945 1946 /* 1947 * send it to the lower layer 1948 * if we get -1 return then put the request in the pending 1949 * queue. 1950 */ 1951 rval = fc_fcp_pkt_send(lport, fsp); 1952 if (rval != 0) { 1953 fsp->state = FC_SRB_FREE; 1954 fc_fcp_pkt_release(fsp); 1955 rc = SCSI_MLQUEUE_HOST_BUSY; 1956 } 1957 out: 1958 return rc; 1959 } 1960 EXPORT_SYMBOL(fc_queuecommand); 1961 1962 /** 1963 * fc_io_compl() - Handle responses for completed commands 1964 * @fsp: The FCP packet that is complete 1965 * 1966 * Translates fcp_pkt errors to a Linux SCSI errors. 1967 * The fcp packet lock must be held when calling. 1968 */ 1969 static void fc_io_compl(struct fc_fcp_pkt *fsp) 1970 { 1971 struct fc_fcp_internal *si; 1972 struct scsi_cmnd *sc_cmd; 1973 struct fc_lport *lport; 1974 unsigned long flags; 1975 1976 /* release outstanding ddp context */ 1977 fc_fcp_ddp_done(fsp); 1978 1979 fsp->state |= FC_SRB_COMPL; 1980 if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) { 1981 spin_unlock_bh(&fsp->scsi_pkt_lock); 1982 del_timer_sync(&fsp->timer); 1983 spin_lock_bh(&fsp->scsi_pkt_lock); 1984 } 1985 1986 lport = fsp->lp; 1987 si = fc_get_scsi_internal(lport); 1988 1989 /* 1990 * if can_queue ramp down is done then try can_queue ramp up 1991 * since commands are completing now. 1992 */ 1993 if (si->last_can_queue_ramp_down_time) 1994 fc_fcp_can_queue_ramp_up(lport); 1995 1996 sc_cmd = fsp->cmd; 1997 CMD_SCSI_STATUS(sc_cmd) = fsp->cdb_status; 1998 switch (fsp->status_code) { 1999 case FC_COMPLETE: 2000 if (fsp->cdb_status == 0) { 2001 /* 2002 * good I/O status 2003 */ 2004 sc_cmd->result = DID_OK << 16; 2005 if (fsp->scsi_resid) 2006 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid; 2007 } else { 2008 /* 2009 * transport level I/O was ok but scsi 2010 * has non zero status 2011 */ 2012 sc_cmd->result = (DID_OK << 16) | fsp->cdb_status; 2013 } 2014 break; 2015 case FC_ERROR: 2016 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml " 2017 "due to FC_ERROR\n"); 2018 sc_cmd->result = DID_ERROR << 16; 2019 break; 2020 case FC_DATA_UNDRUN: 2021 if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) { 2022 /* 2023 * scsi status is good but transport level 2024 * underrun. 2025 */ 2026 if (fsp->state & FC_SRB_RCV_STATUS) { 2027 sc_cmd->result = DID_OK << 16; 2028 } else { 2029 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml" 2030 " due to FC_DATA_UNDRUN (trans)\n"); 2031 sc_cmd->result = DID_ERROR << 16; 2032 } 2033 } else { 2034 /* 2035 * scsi got underrun, this is an error 2036 */ 2037 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml " 2038 "due to FC_DATA_UNDRUN (scsi)\n"); 2039 CMD_RESID_LEN(sc_cmd) = fsp->scsi_resid; 2040 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status; 2041 } 2042 break; 2043 case FC_DATA_OVRRUN: 2044 /* 2045 * overrun is an error 2046 */ 2047 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml " 2048 "due to FC_DATA_OVRRUN\n"); 2049 sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status; 2050 break; 2051 case FC_CMD_ABORTED: 2052 if (host_byte(sc_cmd->result) == DID_TIME_OUT) 2053 FC_FCP_DBG(fsp, "Returning DID_TIME_OUT to scsi-ml " 2054 "due to FC_CMD_ABORTED\n"); 2055 else { 2056 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml " 2057 "due to FC_CMD_ABORTED\n"); 2058 set_host_byte(sc_cmd, DID_ERROR); 2059 } 2060 sc_cmd->result |= fsp->io_status; 2061 break; 2062 case FC_CMD_RESET: 2063 FC_FCP_DBG(fsp, "Returning DID_RESET to scsi-ml " 2064 "due to FC_CMD_RESET\n"); 2065 sc_cmd->result = (DID_RESET << 16); 2066 break; 2067 case FC_TRANS_RESET: 2068 FC_FCP_DBG(fsp, "Returning DID_SOFT_ERROR to scsi-ml " 2069 "due to FC_TRANS_RESET\n"); 2070 sc_cmd->result = (DID_SOFT_ERROR << 16); 2071 break; 2072 case FC_HRD_ERROR: 2073 FC_FCP_DBG(fsp, "Returning DID_NO_CONNECT to scsi-ml " 2074 "due to FC_HRD_ERROR\n"); 2075 sc_cmd->result = (DID_NO_CONNECT << 16); 2076 break; 2077 case FC_CRC_ERROR: 2078 FC_FCP_DBG(fsp, "Returning DID_PARITY to scsi-ml " 2079 "due to FC_CRC_ERROR\n"); 2080 sc_cmd->result = (DID_PARITY << 16); 2081 break; 2082 case FC_TIMED_OUT: 2083 FC_FCP_DBG(fsp, "Returning DID_BUS_BUSY to scsi-ml " 2084 "due to FC_TIMED_OUT\n"); 2085 sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status; 2086 break; 2087 default: 2088 FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml " 2089 "due to unknown error\n"); 2090 sc_cmd->result = (DID_ERROR << 16); 2091 break; 2092 } 2093 2094 if (lport->state != LPORT_ST_READY && fsp->status_code != FC_COMPLETE) 2095 sc_cmd->result = (DID_TRANSPORT_DISRUPTED << 16); 2096 2097 spin_lock_irqsave(&si->scsi_queue_lock, flags); 2098 list_del(&fsp->list); 2099 sc_cmd->SCp.ptr = NULL; 2100 spin_unlock_irqrestore(&si->scsi_queue_lock, flags); 2101 sc_cmd->scsi_done(sc_cmd); 2102 2103 /* release ref from initial allocation in queue command */ 2104 fc_fcp_pkt_release(fsp); 2105 } 2106 2107 /** 2108 * fc_eh_abort() - Abort a command 2109 * @sc_cmd: The SCSI command to abort 2110 * 2111 * From SCSI host template. 2112 * Send an ABTS to the target device and wait for the response. 2113 */ 2114 int fc_eh_abort(struct scsi_cmnd *sc_cmd) 2115 { 2116 struct fc_fcp_pkt *fsp; 2117 struct fc_lport *lport; 2118 struct fc_fcp_internal *si; 2119 int rc = FAILED; 2120 unsigned long flags; 2121 int rval; 2122 2123 rval = fc_block_scsi_eh(sc_cmd); 2124 if (rval) 2125 return rval; 2126 2127 lport = shost_priv(sc_cmd->device->host); 2128 if (lport->state != LPORT_ST_READY) 2129 return rc; 2130 else if (!lport->link_up) 2131 return rc; 2132 2133 si = fc_get_scsi_internal(lport); 2134 spin_lock_irqsave(&si->scsi_queue_lock, flags); 2135 fsp = CMD_SP(sc_cmd); 2136 if (!fsp) { 2137 /* command completed while scsi eh was setting up */ 2138 spin_unlock_irqrestore(&si->scsi_queue_lock, flags); 2139 return SUCCESS; 2140 } 2141 /* grab a ref so the fsp and sc_cmd cannot be released from under us */ 2142 fc_fcp_pkt_hold(fsp); 2143 spin_unlock_irqrestore(&si->scsi_queue_lock, flags); 2144 2145 if (fc_fcp_lock_pkt(fsp)) { 2146 /* completed while we were waiting for timer to be deleted */ 2147 rc = SUCCESS; 2148 goto release_pkt; 2149 } 2150 2151 rc = fc_fcp_pkt_abort(fsp); 2152 fc_fcp_unlock_pkt(fsp); 2153 2154 release_pkt: 2155 fc_fcp_pkt_release(fsp); 2156 return rc; 2157 } 2158 EXPORT_SYMBOL(fc_eh_abort); 2159 2160 /** 2161 * fc_eh_device_reset() - Reset a single LUN 2162 * @sc_cmd: The SCSI command which identifies the device whose 2163 * LUN is to be reset 2164 * 2165 * Set from SCSI host template. 2166 */ 2167 int fc_eh_device_reset(struct scsi_cmnd *sc_cmd) 2168 { 2169 struct fc_lport *lport; 2170 struct fc_fcp_pkt *fsp; 2171 struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device)); 2172 int rc = FAILED; 2173 int rval; 2174 2175 rval = fc_block_scsi_eh(sc_cmd); 2176 if (rval) 2177 return rval; 2178 2179 lport = shost_priv(sc_cmd->device->host); 2180 2181 if (lport->state != LPORT_ST_READY) 2182 return rc; 2183 2184 FC_SCSI_DBG(lport, "Resetting rport (%6.6x)\n", rport->port_id); 2185 2186 fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO); 2187 if (fsp == NULL) { 2188 printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n"); 2189 goto out; 2190 } 2191 2192 /* 2193 * Build the libfc request pkt. Do not set the scsi cmnd, because 2194 * the sc passed in is not setup for execution like when sent 2195 * through the queuecommand callout. 2196 */ 2197 fsp->rport = rport; /* set the remote port ptr */ 2198 2199 /* 2200 * flush outstanding commands 2201 */ 2202 rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun); 2203 fsp->state = FC_SRB_FREE; 2204 fc_fcp_pkt_release(fsp); 2205 2206 out: 2207 return rc; 2208 } 2209 EXPORT_SYMBOL(fc_eh_device_reset); 2210 2211 /** 2212 * fc_eh_host_reset() - Reset a Scsi_Host. 2213 * @sc_cmd: The SCSI command that identifies the SCSI host to be reset 2214 */ 2215 int fc_eh_host_reset(struct scsi_cmnd *sc_cmd) 2216 { 2217 struct Scsi_Host *shost = sc_cmd->device->host; 2218 struct fc_lport *lport = shost_priv(shost); 2219 unsigned long wait_tmo; 2220 2221 FC_SCSI_DBG(lport, "Resetting host\n"); 2222 2223 fc_block_scsi_eh(sc_cmd); 2224 2225 lport->tt.lport_reset(lport); 2226 wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT; 2227 while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies, 2228 wait_tmo)) 2229 msleep(1000); 2230 2231 if (fc_fcp_lport_queue_ready(lport)) { 2232 shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded " 2233 "on port (%6.6x)\n", lport->port_id); 2234 return SUCCESS; 2235 } else { 2236 shost_printk(KERN_INFO, shost, "libfc: Host reset failed, " 2237 "port (%6.6x) is not ready.\n", 2238 lport->port_id); 2239 return FAILED; 2240 } 2241 } 2242 EXPORT_SYMBOL(fc_eh_host_reset); 2243 2244 /** 2245 * fc_slave_alloc() - Configure the queue depth of a Scsi_Host 2246 * @sdev: The SCSI device that identifies the SCSI host 2247 * 2248 * Configures queue depth based on host's cmd_per_len. If not set 2249 * then we use the libfc default. 2250 */ 2251 int fc_slave_alloc(struct scsi_device *sdev) 2252 { 2253 struct fc_rport *rport = starget_to_rport(scsi_target(sdev)); 2254 2255 if (!rport || fc_remote_port_chkready(rport)) 2256 return -ENXIO; 2257 2258 scsi_change_queue_depth(sdev, FC_FCP_DFLT_QUEUE_DEPTH); 2259 return 0; 2260 } 2261 EXPORT_SYMBOL(fc_slave_alloc); 2262 2263 /** 2264 * fc_fcp_destory() - Tear down the FCP layer for a given local port 2265 * @lport: The local port that no longer needs the FCP layer 2266 */ 2267 void fc_fcp_destroy(struct fc_lport *lport) 2268 { 2269 struct fc_fcp_internal *si = fc_get_scsi_internal(lport); 2270 2271 if (!list_empty(&si->scsi_pkt_queue)) 2272 printk(KERN_ERR "libfc: Leaked SCSI packets when destroying " 2273 "port (%6.6x)\n", lport->port_id); 2274 2275 mempool_destroy(si->scsi_pkt_pool); 2276 kfree(si); 2277 lport->scsi_priv = NULL; 2278 } 2279 EXPORT_SYMBOL(fc_fcp_destroy); 2280 2281 int fc_setup_fcp(void) 2282 { 2283 int rc = 0; 2284 2285 scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt", 2286 sizeof(struct fc_fcp_pkt), 2287 0, SLAB_HWCACHE_ALIGN, NULL); 2288 if (!scsi_pkt_cachep) { 2289 printk(KERN_ERR "libfc: Unable to allocate SRB cache, " 2290 "module load failed!"); 2291 rc = -ENOMEM; 2292 } 2293 2294 return rc; 2295 } 2296 2297 void fc_destroy_fcp(void) 2298 { 2299 if (scsi_pkt_cachep) 2300 kmem_cache_destroy(scsi_pkt_cachep); 2301 } 2302 2303 /** 2304 * fc_fcp_init() - Initialize the FCP layer for a local port 2305 * @lport: The local port to initialize the exchange layer for 2306 */ 2307 int fc_fcp_init(struct fc_lport *lport) 2308 { 2309 int rc; 2310 struct fc_fcp_internal *si; 2311 2312 if (!lport->tt.fcp_cmd_send) 2313 lport->tt.fcp_cmd_send = fc_fcp_cmd_send; 2314 2315 if (!lport->tt.fcp_cleanup) 2316 lport->tt.fcp_cleanup = fc_fcp_cleanup; 2317 2318 if (!lport->tt.fcp_abort_io) 2319 lport->tt.fcp_abort_io = fc_fcp_abort_io; 2320 2321 si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL); 2322 if (!si) 2323 return -ENOMEM; 2324 lport->scsi_priv = si; 2325 si->max_can_queue = lport->host->can_queue; 2326 INIT_LIST_HEAD(&si->scsi_pkt_queue); 2327 spin_lock_init(&si->scsi_queue_lock); 2328 2329 si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep); 2330 if (!si->scsi_pkt_pool) { 2331 rc = -ENOMEM; 2332 goto free_internal; 2333 } 2334 return 0; 2335 2336 free_internal: 2337 kfree(si); 2338 return rc; 2339 } 2340 EXPORT_SYMBOL(fc_fcp_init); 2341