789 /*
790 * Function: scsi_io_completion()
791 *
792 * Purpose: Completion processing for block device I/O requests.
793 *
794 * Arguments: cmd – command that is finished.
795 *
796 * Lock status: Assumed that no lock is held upon entry.
797 *
798 * Returns: Nothing
799 *
800 * Notes: This function is matched in terms of capabilities to
801 * the function that created the scatter-gather list.
802 * In other words, if there are no bounce buffers
803 * (the normal case for most drivers), we don’t need
804 * the logic to deal with cleaning up afterwards.
805 *
806 * We must do one of several things here:
807 *
808 * a) Call scsi_end_request. This will finish off the
809 * specified number of sectors. If we are done, the
810 * command block will be released, and the queue
811 * function will be goosed. If we are not done, then
812 * scsi_end_request will directly goose the queue.
813 *
814 * b) We can just use scsi_requeue_command() here. This would
815 * be used if we just wanted to retry, for example.
816 */
817 void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
818 {
819 int result = cmd->result;
820 int this_count = cmd->request_bufflen;
821 request_queue_t *q = cmd->device->request_queue;
822 struct request *req = cmd->request;
823 int clear_errors = 1;
824 struct scsi_sense_hdr sshdr;
825 int sense_valid = 0;
826 int sense_deferred = 0;
827
828 scsi_release_buffers(cmd);
829
830 if (result) {
831 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
832 if (sense_valid)
833 sense_deferred = scsi_sense_is_deferred(&sshdr);
834 }
835
836 if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
837 req->errors = result;
838 if (result) {
839 clear_errors = 0;
840 if (sense_valid && req->sense) {
841 /*
842 * SG_IO wants current and deferred errors
843 */
844 int len = 8 + cmd->sense_buffer[7];
845
846 if (len > SCSI_SENSE_BUFFERSIZE)
847 len = SCSI_SENSE_BUFFERSIZE;
848 memcpy(req->sense, cmd->sense_buffer, len);
849 req->sense_len = len;
850 }
851 }
852 req->data_len = cmd->resid;
853 }
854
855 /*
856 * Next deal with any sectors which we were able to correctly
857 * handle.
858 */
859 SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, "
860 "%d bytes done./n",
861 req->nr_sectors, good_bytes));
862 SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d/n", cmd->use_sg));
863
864 if (clear_errors)
865 req->errors = 0;
866
867 /* A number of bytes were successfully read. If there
868 * are leftovers and there is some kind of error
869 * (result != 0), retry the rest.
870 */
871 if (scsi_end_request(cmd, 1, good_bytes, result == 0) == NULL)
872 return;
873
874 /* good_bytes = 0, or (inclusive) there were leftovers and
875 * result = 0, so scsi_end_request couldn’t retry.
876 */
877 if (sense_valid && !sense_deferred) {
878 switch (sshdr.sense_key) {
879 case UNIT_ATTENTION:
880 if (cmd->device->removable) {
881 /* Detected disc change. Set a bit
882 * and quietly refuse further access.
883 */
884 cmd->device->changed = 1;
885 scsi_end_request(cmd, 0, this_count, 1);
886 return;
887 } else {
888 /* Must have been a power glitch, or a
889 * bus reset. Could not have been a
890 * media change, so we just retry the
891 * request and see what happens.
892 */
893 scsi_requeue_command(q, cmd);
894 return;
895 }
896 break;
897 case ILLEGAL_REQUEST:
898 /* If we had an ILLEGAL REQUEST returned, then
899 * we may have performed an unsupported
900 * command. The only thing this should be
901 * would be a ten byte read where only a six
902 * byte read was supported. Also, on a system
903 * where READ CAPACITY failed, we may have
904 * read past the end of the disk.
905 */
906 if ((cmd->device->use_10_for_rw &&
907 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
908 (cmd->cmnd[0] == READ_10 ||
909 cmd->cmnd[0] == WRITE_10)) {
910 cmd->device->use_10_for_rw = 0;
911 /* This will cause a retry with a
912 * 6-byte command.
913 */
914 scsi_requeue_command(q, cmd);
915 return;
916 } else {
917 scsi_end_request(cmd, 0, this_count, 1);
918 return;
919 }
920 break;
921 case NOT_READY:
922 /* If the device is in the process of becoming
923 * ready, or has a temporary blockage, retry.
924 */
925 if (sshdr.asc == 0x04) {
926 switch (sshdr.ascq) {
927 case 0x01: /* becoming ready */
928 case 0x04: /* format in progress */
929 case 0x05: /* rebuild in progress */
930 case 0x06: /* recalculation in progress */
931 case 0x07: /* operation in progress */
932 case 0x08: /* Long write in progress */
933 case 0x09: /* self test in progress */
934 scsi_requeue_command(q, cmd);
935 return;
936 default:
937 break;
938 }
939 }
940 if (!(req->cmd_flags & REQ_QUIET)) {
941 scmd_printk(KERN_INFO, cmd,
942 "Device not ready: ");
943 scsi_print_sense_hdr("", &sshdr);
944 }
945 scsi_end_request(cmd, 0, this_count, 1);
946 return;
947 case VOLUME_OVERFLOW:
948 if (!(req->cmd_flags & REQ_QUIET)) {
949 scmd_printk(KERN_INFO, cmd,
950 "Volume overflow, CDB: ");
951 __scsi_print_command(cmd->cmnd);
952 scsi_print_sense("", cmd);
953 }
954 /* See SSC3rXX or current. */
955 scsi_end_request(cmd, 0, this_count, 1);
956 return;
957 default:
958 break;
959 }
960 }
961 if (host_byte(result) == DID_RESET) {
962 /* Third party bus reset or reset for error recovery
963 * reasons. Just retry the request and see what
964 * happens.
965 */
966 scsi_requeue_command(q, cmd);
967 return;
968 }
969 if (result) {
970 if (!(req->cmd_flags & REQ_QUIET)) {
971 scsi_print_result(cmd);
972 if (driver_byte(result) & DRIVER_SENSE)
973 scsi_print_sense("", cmd);
974 }
975 }
976 scsi_end_request(cmd, 0, this_count, !result);
977 }
看看花儿冲破北疆漫漫寒冬,妖娆绽放;