1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185 |
- #include <linux/module.h>
- #include <linux/sched.h>
- #include <linux/gfp.h>
- #include <linux/timer.h>
- #include <linux/string.h>
- #include <linux/kernel.h>
- #include <linux/freezer.h>
- #include <linux/kthread.h>
- #include <linux/interrupt.h>
- #include <linux/blkdev.h>
- #include <linux/delay.h>
- #include <scsi/scsi.h>
- #include <scsi/scsi_cmnd.h>
- #include <scsi/scsi_dbg.h>
- #include <scsi/scsi_device.h>
- #include <scsi/scsi_eh.h>
- #include <scsi/scsi_transport.h>
- #include <scsi/scsi_host.h>
- #include <scsi/scsi_ioctl.h>
- #include "scsi_priv.h"
- #include "scsi_logging.h"
- #include "scsi_transport_api.h"
- #include <trace/events/scsi.h>
- #define SENSE_TIMEOUT (10*HZ)
- #define BUS_RESET_SETTLE_TIME (10)
- #define HOST_RESET_SETTLE_TIME (10)
- static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
- void scsi_eh_wakeup(struct Scsi_Host *shost)
- {
- if (shost->host_busy == shost->host_failed) {
- trace_scsi_eh_wakeup(shost);
- wake_up_process(shost->ehandler);
- SCSI_LOG_ERROR_RECOVERY(5,
- printk("Waking error handler thread\n"));
- }
- }
- void scsi_schedule_eh(struct Scsi_Host *shost)
- {
- unsigned long flags;
- spin_lock_irqsave(shost->host_lock, flags);
- if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
- scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
- shost->host_eh_scheduled++;
- scsi_eh_wakeup(shost);
- }
- spin_unlock_irqrestore(shost->host_lock, flags);
- }
- EXPORT_SYMBOL_GPL(scsi_schedule_eh);
- int scsi_eh_scmd_add(struct scsi_cmnd *scmd, int eh_flag)
- {
- struct Scsi_Host *shost = scmd->device->host;
- unsigned long flags;
- int ret = 0;
- if (!shost->ehandler)
- return 0;
- spin_lock_irqsave(shost->host_lock, flags);
- if (scsi_host_set_state(shost, SHOST_RECOVERY))
- if (scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY))
- goto out_unlock;
- ret = 1;
- scmd->eh_eflags |= eh_flag;
- list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
- shost->host_failed++;
- scsi_eh_wakeup(shost);
- out_unlock:
- spin_unlock_irqrestore(shost->host_lock, flags);
- return ret;
- }
- enum blk_eh_timer_return scsi_times_out(struct request *req)
- {
- struct scsi_cmnd *scmd = req->special;
- enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
- struct Scsi_Host *host = scmd->device->host;
- trace_scsi_dispatch_cmd_timeout(scmd);
- scsi_log_completion(scmd, TIMEOUT_ERROR);
- if (host->transportt->eh_timed_out)
- rtn = host->transportt->eh_timed_out(scmd);
- else if (host->hostt->eh_timed_out)
- rtn = host->hostt->eh_timed_out(scmd);
- if (unlikely(rtn == BLK_EH_NOT_HANDLED &&
- !scsi_eh_scmd_add(scmd, SCSI_EH_CANCEL_CMD))) {
- scmd->result |= DID_TIME_OUT << 16;
- rtn = BLK_EH_HANDLED;
- }
- return rtn;
- }
- int scsi_block_when_processing_errors(struct scsi_device *sdev)
- {
- int online;
- wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
- online = scsi_device_online(sdev);
- SCSI_LOG_ERROR_RECOVERY(5, printk("%s: rtn: %d\n", __func__,
- online));
- return online;
- }
- EXPORT_SYMBOL(scsi_block_when_processing_errors);
- #ifdef CONFIG_SCSI_LOGGING
- static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
- struct list_head *work_q)
- {
- struct scsi_cmnd *scmd;
- struct scsi_device *sdev;
- int total_failures = 0;
- int cmd_failed = 0;
- int cmd_cancel = 0;
- int devices_failed = 0;
- shost_for_each_device(sdev, shost) {
- list_for_each_entry(scmd, work_q, eh_entry) {
- if (scmd->device == sdev) {
- ++total_failures;
- if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD)
- ++cmd_cancel;
- else
- ++cmd_failed;
- }
- }
- if (cmd_cancel || cmd_failed) {
- SCSI_LOG_ERROR_RECOVERY(3,
- sdev_printk(KERN_INFO, sdev,
- "%s: cmds failed: %d, cancel: %d\n",
- __func__, cmd_failed,
- cmd_cancel));
- cmd_cancel = 0;
- cmd_failed = 0;
- ++devices_failed;
- }
- }
- SCSI_LOG_ERROR_RECOVERY(2, printk("Total of %d commands on %d"
- " devices require eh work\n",
- total_failures, devices_failed));
- }
- #endif
- static int scsi_check_sense(struct scsi_cmnd *scmd)
- {
- struct scsi_device *sdev = scmd->device;
- struct scsi_sense_hdr sshdr;
- if (! scsi_command_normalize_sense(scmd, &sshdr))
- return FAILED;
- if (scsi_sense_is_deferred(&sshdr))
- return NEEDS_RETRY;
- if (sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh &&
- sdev->scsi_dh_data->scsi_dh->check_sense) {
- int rc;
- rc = sdev->scsi_dh_data->scsi_dh->check_sense(sdev, &sshdr);
- if (rc != SCSI_RETURN_NOT_HANDLED)
- return rc;
-
- }
-
- if (sshdr.response_code == 0x70) {
-
- if (scmd->sense_buffer[2] & 0xe0)
- return SUCCESS;
- } else {
-
- if ((sshdr.additional_length > 3) &&
- (scmd->sense_buffer[8] == 0x4) &&
- (scmd->sense_buffer[11] & 0xe0))
- return SUCCESS;
- }
- switch (sshdr.sense_key) {
- case NO_SENSE:
- return SUCCESS;
- case RECOVERED_ERROR:
- return SUCCESS;
- case ABORTED_COMMAND:
- if (sshdr.asc == 0x10)
- return SUCCESS;
- return NEEDS_RETRY;
- case NOT_READY:
- case UNIT_ATTENTION:
-
- if (scmd->device->expecting_cc_ua) {
- scmd->device->expecting_cc_ua = 0;
- return NEEDS_RETRY;
- }
-
- if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
- return NEEDS_RETRY;
-
- if (scmd->device->allow_restart &&
- (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
- return FAILED;
- if (sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
- scmd_printk(KERN_WARNING, scmd,
- "Warning! Received an indication that the "
- "LUN assignments on this target have "
- "changed. The Linux SCSI layer does not "
- "automatically remap LUN assignments.\n");
- else if (sshdr.asc == 0x3f)
- scmd_printk(KERN_WARNING, scmd,
- "Warning! Received an indication that the "
- "operating parameters on this target have "
- "changed. The Linux SCSI layer does not "
- "automatically adjust these parameters.\n");
- if (sshdr.asc == 0x38 && sshdr.ascq == 0x07)
- scmd_printk(KERN_WARNING, scmd,
- "Warning! Received an indication that the "
- "LUN reached a thin provisioning soft "
- "threshold.\n");
-
- return SUCCESS;
-
- case COPY_ABORTED:
- case VOLUME_OVERFLOW:
- case MISCOMPARE:
- case BLANK_CHECK:
- case DATA_PROTECT:
- return TARGET_ERROR;
- case MEDIUM_ERROR:
- if (sshdr.asc == 0x11 ||
- sshdr.asc == 0x13 ||
- sshdr.asc == 0x14) {
- return TARGET_ERROR;
- }
- return NEEDS_RETRY;
- case HARDWARE_ERROR:
- if (scmd->device->retry_hwerror)
- return ADD_TO_MLQUEUE;
- else
- return TARGET_ERROR;
- case ILLEGAL_REQUEST:
- default:
- return SUCCESS;
- }
- }
- static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
- {
- struct scsi_host_template *sht = sdev->host->hostt;
- struct scsi_device *tmp_sdev;
- if (!sht->change_queue_depth ||
- sdev->queue_depth >= sdev->max_queue_depth)
- return;
- if (time_before(jiffies,
- sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
- return;
- if (time_before(jiffies,
- sdev->last_queue_full_time + sdev->queue_ramp_up_period))
- return;
-
- shost_for_each_device(tmp_sdev, sdev->host) {
- if (tmp_sdev->channel != sdev->channel ||
- tmp_sdev->id != sdev->id ||
- tmp_sdev->queue_depth == sdev->max_queue_depth)
- continue;
-
- sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1,
- SCSI_QDEPTH_RAMP_UP);
- sdev->last_queue_ramp_up = jiffies;
- }
- }
- static void scsi_handle_queue_full(struct scsi_device *sdev)
- {
- struct scsi_host_template *sht = sdev->host->hostt;
- struct scsi_device *tmp_sdev;
- if (!sht->change_queue_depth)
- return;
- shost_for_each_device(tmp_sdev, sdev->host) {
- if (tmp_sdev->channel != sdev->channel ||
- tmp_sdev->id != sdev->id)
- continue;
-
- sht->change_queue_depth(tmp_sdev, tmp_sdev->queue_depth - 1,
- SCSI_QDEPTH_QFULL);
- }
- }
- static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
- {
-
- if (host_byte(scmd->result) == DID_RESET) {
-
- return scsi_check_sense(scmd);
- }
- if (host_byte(scmd->result) != DID_OK)
- return FAILED;
-
- if (msg_byte(scmd->result) != COMMAND_COMPLETE)
- return FAILED;
-
- switch (status_byte(scmd->result)) {
- case GOOD:
- scsi_handle_queue_ramp_up(scmd->device);
- case COMMAND_TERMINATED:
- return SUCCESS;
- case CHECK_CONDITION:
- return scsi_check_sense(scmd);
- case CONDITION_GOOD:
- case INTERMEDIATE_GOOD:
- case INTERMEDIATE_C_GOOD:
-
- return SUCCESS;
- case RESERVATION_CONFLICT:
- if (scmd->cmnd[0] == TEST_UNIT_READY)
-
- return SUCCESS;
-
- return FAILED;
- case QUEUE_FULL:
- scsi_handle_queue_full(scmd->device);
-
- case BUSY:
- return NEEDS_RETRY;
- default:
- return FAILED;
- }
- return FAILED;
- }
- static void scsi_eh_done(struct scsi_cmnd *scmd)
- {
- struct completion *eh_action;
- SCSI_LOG_ERROR_RECOVERY(3,
- printk("%s scmd: %p result: %x\n",
- __func__, scmd, scmd->result));
- eh_action = scmd->device->host->eh_action;
- if (eh_action)
- complete(eh_action);
- }
- static int scsi_try_host_reset(struct scsi_cmnd *scmd)
- {
- unsigned long flags;
- int rtn;
- struct Scsi_Host *host = scmd->device->host;
- struct scsi_host_template *hostt = host->hostt;
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Host RST\n",
- __func__));
- if (!hostt->eh_host_reset_handler)
- return FAILED;
- rtn = hostt->eh_host_reset_handler(scmd);
- if (rtn == SUCCESS) {
- if (!hostt->skip_settle_delay)
- ssleep(HOST_RESET_SETTLE_TIME);
- spin_lock_irqsave(host->host_lock, flags);
- scsi_report_bus_reset(host, scmd_channel(scmd));
- spin_unlock_irqrestore(host->host_lock, flags);
- }
- return rtn;
- }
- static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
- {
- unsigned long flags;
- int rtn;
- struct Scsi_Host *host = scmd->device->host;
- struct scsi_host_template *hostt = host->hostt;
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Snd Bus RST\n",
- __func__));
- if (!hostt->eh_bus_reset_handler)
- return FAILED;
- rtn = hostt->eh_bus_reset_handler(scmd);
- if (rtn == SUCCESS) {
- if (!hostt->skip_settle_delay)
- ssleep(BUS_RESET_SETTLE_TIME);
- spin_lock_irqsave(host->host_lock, flags);
- scsi_report_bus_reset(host, scmd_channel(scmd));
- spin_unlock_irqrestore(host->host_lock, flags);
- }
- return rtn;
- }
- static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
- {
- sdev->was_reset = 1;
- sdev->expecting_cc_ua = 1;
- }
- static int scsi_try_target_reset(struct scsi_cmnd *scmd)
- {
- unsigned long flags;
- int rtn;
- struct Scsi_Host *host = scmd->device->host;
- struct scsi_host_template *hostt = host->hostt;
- if (!hostt->eh_target_reset_handler)
- return FAILED;
- rtn = hostt->eh_target_reset_handler(scmd);
- if (rtn == SUCCESS) {
- spin_lock_irqsave(host->host_lock, flags);
- __starget_for_each_device(scsi_target(scmd->device), NULL,
- __scsi_report_device_reset);
- spin_unlock_irqrestore(host->host_lock, flags);
- }
- return rtn;
- }
- static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
- {
- int rtn;
- struct scsi_host_template *hostt = scmd->device->host->hostt;
- if (!hostt->eh_device_reset_handler)
- return FAILED;
- rtn = hostt->eh_device_reset_handler(scmd);
- if (rtn == SUCCESS)
- __scsi_report_device_reset(scmd->device, NULL);
- return rtn;
- }
- static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt, struct scsi_cmnd *scmd)
- {
- if (!hostt->eh_abort_handler)
- return FAILED;
- return hostt->eh_abort_handler(scmd);
- }
- static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
- {
- if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
- if (scsi_try_bus_device_reset(scmd) != SUCCESS)
- if (scsi_try_target_reset(scmd) != SUCCESS)
- if (scsi_try_bus_reset(scmd) != SUCCESS)
- scsi_try_host_reset(scmd);
- }
- void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
- unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
- {
- struct scsi_device *sdev = scmd->device;
-
- ses->cmd_len = scmd->cmd_len;
- ses->cmnd = scmd->cmnd;
- ses->data_direction = scmd->sc_data_direction;
- ses->sdb = scmd->sdb;
- ses->next_rq = scmd->request->next_rq;
- ses->result = scmd->result;
- ses->underflow = scmd->underflow;
- ses->prot_op = scmd->prot_op;
- scmd->prot_op = SCSI_PROT_NORMAL;
- scmd->cmnd = ses->eh_cmnd;
- memset(scmd->cmnd, 0, BLK_MAX_CDB);
- memset(&scmd->sdb, 0, sizeof(scmd->sdb));
- scmd->request->next_rq = NULL;
- if (sense_bytes) {
- scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
- sense_bytes);
- sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
- scmd->sdb.length);
- scmd->sdb.table.sgl = &ses->sense_sgl;
- scmd->sc_data_direction = DMA_FROM_DEVICE;
- scmd->sdb.table.nents = 1;
- scmd->cmnd[0] = REQUEST_SENSE;
- scmd->cmnd[4] = scmd->sdb.length;
- scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
- } else {
- scmd->sc_data_direction = DMA_NONE;
- if (cmnd) {
- BUG_ON(cmnd_size > BLK_MAX_CDB);
- memcpy(scmd->cmnd, cmnd, cmnd_size);
- scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
- }
- }
- scmd->underflow = 0;
- if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
- scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
- (sdev->lun << 5 & 0xe0);
-
- memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
- }
- EXPORT_SYMBOL(scsi_eh_prep_cmnd);
- void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
- {
-
- scmd->cmd_len = ses->cmd_len;
- scmd->cmnd = ses->cmnd;
- scmd->sc_data_direction = ses->data_direction;
- scmd->sdb = ses->sdb;
- scmd->request->next_rq = ses->next_rq;
- scmd->result = ses->result;
- scmd->underflow = ses->underflow;
- scmd->prot_op = ses->prot_op;
- }
- EXPORT_SYMBOL(scsi_eh_restore_cmnd);
- static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
- int cmnd_size, int timeout, unsigned sense_bytes)
- {
- struct scsi_device *sdev = scmd->device;
- struct Scsi_Host *shost = sdev->host;
- DECLARE_COMPLETION_ONSTACK(done);
- unsigned long timeleft;
- struct scsi_eh_save ses;
- int rtn;
- scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
- shost->eh_action = &done;
- scsi_log_send(scmd);
- scmd->scsi_done = scsi_eh_done;
- shost->hostt->queuecommand(shost, scmd);
- timeleft = wait_for_completion_timeout(&done, timeout);
- shost->eh_action = NULL;
- scsi_log_completion(scmd, SUCCESS);
- SCSI_LOG_ERROR_RECOVERY(3,
- printk("%s: scmd: %p, timeleft: %ld\n",
- __func__, scmd, timeleft));
-
- if (timeleft) {
- rtn = scsi_eh_completed_normally(scmd);
- SCSI_LOG_ERROR_RECOVERY(3,
- printk("%s: scsi_eh_completed_normally %x\n",
- __func__, rtn));
- switch (rtn) {
- case SUCCESS:
- case NEEDS_RETRY:
- case FAILED:
- case TARGET_ERROR:
- break;
- case ADD_TO_MLQUEUE:
- rtn = NEEDS_RETRY;
- break;
- default:
- rtn = FAILED;
- break;
- }
- } else {
- scsi_abort_eh_cmnd(scmd);
- rtn = FAILED;
- }
- scsi_eh_restore_cmnd(scmd, &ses);
- return rtn;
- }
- static int scsi_request_sense(struct scsi_cmnd *scmd)
- {
- return scsi_send_eh_cmnd(scmd, NULL, 0, SENSE_TIMEOUT, ~0);
- }
- void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
- {
- scmd->device->host->host_failed--;
- scmd->eh_eflags = 0;
- list_move_tail(&scmd->eh_entry, done_q);
- }
- EXPORT_SYMBOL(scsi_eh_finish_cmd);
- int scsi_eh_get_sense(struct list_head *work_q,
- struct list_head *done_q)
- {
- struct scsi_cmnd *scmd, *next;
- int rtn;
- list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
- if ((scmd->eh_eflags & SCSI_EH_CANCEL_CMD) ||
- SCSI_SENSE_VALID(scmd))
- continue;
- SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
- "%s: requesting sense\n",
- current->comm));
- rtn = scsi_request_sense(scmd);
- if (rtn != SUCCESS)
- continue;
- SCSI_LOG_ERROR_RECOVERY(3, printk("sense requested for %p"
- " result %x\n", scmd,
- scmd->result));
- SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense("bh", scmd));
- rtn = scsi_decide_disposition(scmd);
-
- if (rtn == SUCCESS)
-
- scmd->retries = scmd->allowed;
- else if (rtn != NEEDS_RETRY)
- continue;
- scsi_eh_finish_cmd(scmd, done_q);
- }
- return list_empty(work_q);
- }
- EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
- static int scsi_eh_tur(struct scsi_cmnd *scmd)
- {
- static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
- int retry_cnt = 1, rtn;
- retry_tur:
- rtn = scsi_send_eh_cmnd(scmd, tur_command, 6, SENSE_TIMEOUT, 0);
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: scmd %p rtn %x\n",
- __func__, scmd, rtn));
- switch (rtn) {
- case NEEDS_RETRY:
- if (retry_cnt--)
- goto retry_tur;
-
- case SUCCESS:
- return 0;
- default:
- return 1;
- }
- }
- static int scsi_eh_test_devices(struct list_head *cmd_list,
- struct list_head *work_q,
- struct list_head *done_q, int try_stu)
- {
- struct scsi_cmnd *scmd, *next;
- struct scsi_device *sdev;
- int finish_cmds;
- while (!list_empty(cmd_list)) {
- scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
- sdev = scmd->device;
- finish_cmds = !scsi_device_online(scmd->device) ||
- (try_stu && !scsi_eh_try_stu(scmd) &&
- !scsi_eh_tur(scmd)) ||
- !scsi_eh_tur(scmd);
- list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
- if (scmd->device == sdev) {
- if (finish_cmds)
- scsi_eh_finish_cmd(scmd, done_q);
- else
- list_move_tail(&scmd->eh_entry, work_q);
- }
- }
- return list_empty(work_q);
- }
- static int scsi_eh_abort_cmds(struct list_head *work_q,
- struct list_head *done_q)
- {
- struct scsi_cmnd *scmd, *next;
- LIST_HEAD(check_list);
- int rtn;
- list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
- if (!(scmd->eh_eflags & SCSI_EH_CANCEL_CMD))
- continue;
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting cmd:"
- "0x%p\n", current->comm,
- scmd));
- rtn = scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd);
- if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
- scmd->eh_eflags &= ~SCSI_EH_CANCEL_CMD;
- if (rtn == FAST_IO_FAIL)
- scsi_eh_finish_cmd(scmd, done_q);
- else
- list_move_tail(&scmd->eh_entry, &check_list);
- } else
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: aborting"
- " cmd failed:"
- "0x%p\n",
- current->comm,
- scmd));
- }
- return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
- }
- static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
- {
- static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
- if (scmd->device->allow_restart) {
- int i, rtn = NEEDS_RETRY;
- for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
- rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
- if (rtn == SUCCESS)
- return 0;
- }
- return 1;
- }
-
- static int scsi_eh_stu(struct Scsi_Host *shost,
- struct list_head *work_q,
- struct list_head *done_q)
- {
- struct scsi_cmnd *scmd, *stu_scmd, *next;
- struct scsi_device *sdev;
- shost_for_each_device(sdev, shost) {
- stu_scmd = NULL;
- list_for_each_entry(scmd, work_q, eh_entry)
- if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
- scsi_check_sense(scmd) == FAILED ) {
- stu_scmd = scmd;
- break;
- }
- if (!stu_scmd)
- continue;
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending START_UNIT to sdev:"
- " 0x%p\n", current->comm, sdev));
- if (!scsi_eh_try_stu(stu_scmd)) {
- if (!scsi_device_online(sdev) ||
- !scsi_eh_tur(stu_scmd)) {
- list_for_each_entry_safe(scmd, next,
- work_q, eh_entry) {
- if (scmd->device == sdev)
- scsi_eh_finish_cmd(scmd, done_q);
- }
- }
- } else {
- SCSI_LOG_ERROR_RECOVERY(3,
- printk("%s: START_UNIT failed to sdev:"
- " 0x%p\n", current->comm, sdev));
- }
- }
- return list_empty(work_q);
- }
- static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
- struct list_head *work_q,
- struct list_head *done_q)
- {
- struct scsi_cmnd *scmd, *bdr_scmd, *next;
- struct scsi_device *sdev;
- int rtn;
- shost_for_each_device(sdev, shost) {
- bdr_scmd = NULL;
- list_for_each_entry(scmd, work_q, eh_entry)
- if (scmd->device == sdev) {
- bdr_scmd = scmd;
- break;
- }
- if (!bdr_scmd)
- continue;
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BDR sdev:"
- " 0x%p\n", current->comm,
- sdev));
- rtn = scsi_try_bus_device_reset(bdr_scmd);
- if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
- if (!scsi_device_online(sdev) ||
- rtn == FAST_IO_FAIL ||
- !scsi_eh_tur(bdr_scmd)) {
- list_for_each_entry_safe(scmd, next,
- work_q, eh_entry) {
- if (scmd->device == sdev)
- scsi_eh_finish_cmd(scmd,
- done_q);
- }
- }
- } else {
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BDR"
- " failed sdev:"
- "0x%p\n",
- current->comm,
- sdev));
- }
- }
- return list_empty(work_q);
- }
- static int scsi_eh_target_reset(struct Scsi_Host *shost,
- struct list_head *work_q,
- struct list_head *done_q)
- {
- LIST_HEAD(tmp_list);
- LIST_HEAD(check_list);
- list_splice_init(work_q, &tmp_list);
- while (!list_empty(&tmp_list)) {
- struct scsi_cmnd *next, *scmd;
- int rtn;
- unsigned int id;
- scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
- id = scmd_id(scmd);
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending target reset "
- "to target %d\n",
- current->comm, id));
- rtn = scsi_try_target_reset(scmd);
- if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Target reset"
- " failed target: "
- "%d\n",
- current->comm, id));
- list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
- if (scmd_id(scmd) != id)
- continue;
- if (rtn == SUCCESS)
- list_move_tail(&scmd->eh_entry, &check_list);
- else if (rtn == FAST_IO_FAIL)
- scsi_eh_finish_cmd(scmd, done_q);
- else
-
- list_move(&scmd->eh_entry, work_q);
- }
- }
- return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
- }
- static int scsi_eh_bus_reset(struct Scsi_Host *shost,
- struct list_head *work_q,
- struct list_head *done_q)
- {
- struct scsi_cmnd *scmd, *chan_scmd, *next;
- LIST_HEAD(check_list);
- unsigned int channel;
- int rtn;
-
- for (channel = 0; channel <= shost->max_channel; channel++) {
- chan_scmd = NULL;
- list_for_each_entry(scmd, work_q, eh_entry) {
- if (channel == scmd_channel(scmd)) {
- chan_scmd = scmd;
- break;
-
- }
- }
- if (!chan_scmd)
- continue;
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending BRST chan:"
- " %d\n", current->comm,
- channel));
- rtn = scsi_try_bus_reset(chan_scmd);
- if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
- list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
- if (channel == scmd_channel(scmd)) {
- if (rtn == FAST_IO_FAIL)
- scsi_eh_finish_cmd(scmd,
- done_q);
- else
- list_move_tail(&scmd->eh_entry,
- &check_list);
- }
- }
- } else {
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: BRST"
- " failed chan: %d\n",
- current->comm,
- channel));
- }
- }
- return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
- }
- static int scsi_eh_host_reset(struct list_head *work_q,
- struct list_head *done_q)
- {
- struct scsi_cmnd *scmd, *next;
- LIST_HEAD(check_list);
- int rtn;
- if (!list_empty(work_q)) {
- scmd = list_entry(work_q->next,
- struct scsi_cmnd, eh_entry);
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: Sending HRST\n"
- , current->comm));
- rtn = scsi_try_host_reset(scmd);
- if (rtn == SUCCESS) {
- list_splice_init(work_q, &check_list);
- } else if (rtn == FAST_IO_FAIL) {
- list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
- scsi_eh_finish_cmd(scmd, done_q);
- }
- } else {
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: HRST"
- " failed\n",
- current->comm));
- }
- }
- return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
- }
- static void scsi_eh_offline_sdevs(struct list_head *work_q,
- struct list_head *done_q)
- {
- struct scsi_cmnd *scmd, *next;
- list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
- sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
- "not ready after error recovery\n");
- scsi_device_set_state(scmd->device, SDEV_OFFLINE);
- if (scmd->eh_eflags & SCSI_EH_CANCEL_CMD) {
-
- }
- scsi_eh_finish_cmd(scmd, done_q);
- }
- return;
- }
- int scsi_noretry_cmd(struct scsi_cmnd *scmd)
- {
- switch (host_byte(scmd->result)) {
- case DID_OK:
- break;
- case DID_BUS_BUSY:
- return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
- case DID_PARITY:
- return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
- case DID_ERROR:
- if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
- status_byte(scmd->result) == RESERVATION_CONFLICT)
- return 0;
-
- case DID_SOFT_ERROR:
- return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
- }
- switch (status_byte(scmd->result)) {
- case CHECK_CONDITION:
-
- if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
- scmd->request->cmd_type == REQ_TYPE_BLOCK_PC)
- return 1;
- }
- return 0;
- }
- int scsi_decide_disposition(struct scsi_cmnd *scmd)
- {
- int rtn;
-
- if (!scsi_device_online(scmd->device)) {
- SCSI_LOG_ERROR_RECOVERY(5, printk("%s: device offline - report"
- " as SUCCESS\n",
- __func__));
- return SUCCESS;
- }
-
- switch (host_byte(scmd->result)) {
- case DID_PASSTHROUGH:
-
- scmd->result &= 0xff00ffff;
- return SUCCESS;
- case DID_OK:
-
- break;
- case DID_NO_CONNECT:
- case DID_BAD_TARGET:
- case DID_ABORT:
-
- return SUCCESS;
-
- case DID_SOFT_ERROR:
- goto maybe_retry;
- case DID_IMM_RETRY:
- return NEEDS_RETRY;
- case DID_REQUEUE:
- return ADD_TO_MLQUEUE;
- case DID_TRANSPORT_DISRUPTED:
-
- goto maybe_retry;
- case DID_TRANSPORT_FAILFAST:
-
- return SUCCESS;
- case DID_ERROR:
- if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
- status_byte(scmd->result) == RESERVATION_CONFLICT)
-
- break;
-
- case DID_BUS_BUSY:
- case DID_PARITY:
- goto maybe_retry;
- case DID_TIME_OUT:
-
- if ((scmd->cmnd[0] == TEST_UNIT_READY ||
- scmd->cmnd[0] == INQUIRY)) {
- return SUCCESS;
- } else {
- return FAILED;
- }
- case DID_RESET:
- return SUCCESS;
- default:
- return FAILED;
- }
-
- if (msg_byte(scmd->result) != COMMAND_COMPLETE)
- return FAILED;
-
- switch (status_byte(scmd->result)) {
- case QUEUE_FULL:
- scsi_handle_queue_full(scmd->device);
-
- case BUSY:
-
- return ADD_TO_MLQUEUE;
- case GOOD:
- scsi_handle_queue_ramp_up(scmd->device);
- case COMMAND_TERMINATED:
- return SUCCESS;
- case TASK_ABORTED:
- goto maybe_retry;
- case CHECK_CONDITION:
- rtn = scsi_check_sense(scmd);
- if (rtn == NEEDS_RETRY)
- goto maybe_retry;
- else if (rtn == TARGET_ERROR) {
-
- scmd->result |= (DID_TARGET_FAILURE << 16);
- rtn = SUCCESS;
- }
-
- return rtn;
- case CONDITION_GOOD:
- case INTERMEDIATE_GOOD:
- case INTERMEDIATE_C_GOOD:
- case ACA_ACTIVE:
-
- return SUCCESS;
- case RESERVATION_CONFLICT:
- sdev_printk(KERN_INFO, scmd->device,
- "reservation conflict\n");
- scmd->result |= (DID_NEXUS_FAILURE << 16);
- return SUCCESS;
- default:
- return FAILED;
- }
- return FAILED;
- maybe_retry:
-
- if ((++scmd->retries) <= scmd->allowed
- && !scsi_noretry_cmd(scmd)) {
- return NEEDS_RETRY;
- } else {
-
- return SUCCESS;
- }
- }
- static void eh_lock_door_done(struct request *req, int uptodate)
- {
- __blk_put_request(req->q, req);
- }
- static void scsi_eh_lock_door(struct scsi_device *sdev)
- {
- struct request *req;
-
- req = blk_get_request(sdev->request_queue, READ, GFP_KERNEL);
- req->cmd[0] = ALLOW_MEDIUM_REMOVAL;
- req->cmd[1] = 0;
- req->cmd[2] = 0;
- req->cmd[3] = 0;
- req->cmd[4] = SCSI_REMOVAL_PREVENT;
- req->cmd[5] = 0;
- req->cmd_len = COMMAND_SIZE(req->cmd[0]);
- req->cmd_type = REQ_TYPE_BLOCK_PC;
- req->cmd_flags |= REQ_QUIET;
- req->timeout = 10 * HZ;
- req->retries = 5;
- blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
- }
- static void scsi_restart_operations(struct Scsi_Host *shost)
- {
- struct scsi_device *sdev;
- unsigned long flags;
-
- shost_for_each_device(sdev, shost) {
- if (scsi_device_online(sdev) && sdev->locked)
- scsi_eh_lock_door(sdev);
- }
-
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: waking up host to restart\n",
- __func__));
- spin_lock_irqsave(shost->host_lock, flags);
- if (scsi_host_set_state(shost, SHOST_RUNNING))
- if (scsi_host_set_state(shost, SHOST_CANCEL))
- BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
- spin_unlock_irqrestore(shost->host_lock, flags);
- wake_up(&shost->host_wait);
-
- scsi_run_host_queues(shost);
- }
- void scsi_eh_ready_devs(struct Scsi_Host *shost,
- struct list_head *work_q,
- struct list_head *done_q)
- {
- if (!scsi_eh_stu(shost, work_q, done_q))
- if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
- if (!scsi_eh_target_reset(shost, work_q, done_q))
- if (!scsi_eh_bus_reset(shost, work_q, done_q))
- if (!scsi_eh_host_reset(work_q, done_q))
- scsi_eh_offline_sdevs(work_q,
- done_q);
- }
- EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
- void scsi_eh_flush_done_q(struct list_head *done_q)
- {
- struct scsi_cmnd *scmd, *next;
- list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
- list_del_init(&scmd->eh_entry);
- if (scsi_device_online(scmd->device) &&
- !scsi_noretry_cmd(scmd) &&
- (++scmd->retries <= scmd->allowed)) {
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush"
- " retry cmd: %p\n",
- current->comm,
- scmd));
- scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
- } else {
-
- if (!scmd->result)
- scmd->result |= (DRIVER_TIMEOUT << 24);
- SCSI_LOG_ERROR_RECOVERY(3, printk("%s: flush finish"
- " cmd: %p\n",
- current->comm, scmd));
- scsi_finish_command(scmd);
- }
- }
- }
- EXPORT_SYMBOL(scsi_eh_flush_done_q);
- static void scsi_unjam_host(struct Scsi_Host *shost)
- {
- unsigned long flags;
- LIST_HEAD(eh_work_q);
- LIST_HEAD(eh_done_q);
- spin_lock_irqsave(shost->host_lock, flags);
- list_splice_init(&shost->eh_cmd_q, &eh_work_q);
- spin_unlock_irqrestore(shost->host_lock, flags);
- SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
- if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
- if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
- scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
- scsi_eh_flush_done_q(&eh_done_q);
- }
- int scsi_error_handler(void *data)
- {
- struct Scsi_Host *shost = data;
-
- set_current_state(TASK_INTERRUPTIBLE);
- while (!kthread_should_stop()) {
- if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
- shost->host_failed != shost->host_busy) {
- SCSI_LOG_ERROR_RECOVERY(1,
- printk("Error handler scsi_eh_%d sleeping\n",
- shost->host_no));
- schedule();
- set_current_state(TASK_INTERRUPTIBLE);
- continue;
- }
- __set_current_state(TASK_RUNNING);
- SCSI_LOG_ERROR_RECOVERY(1,
- printk("Error handler scsi_eh_%d waking up\n",
- shost->host_no));
-
- if (scsi_autopm_get_host(shost) != 0) {
- SCSI_LOG_ERROR_RECOVERY(1,
- printk(KERN_ERR "Error handler scsi_eh_%d "
- "unable to autoresume\n",
- shost->host_no));
- continue;
- }
- if (shost->transportt->eh_strategy_handler)
- shost->transportt->eh_strategy_handler(shost);
- else
- scsi_unjam_host(shost);
-
- scsi_restart_operations(shost);
- scsi_autopm_put_host(shost);
- set_current_state(TASK_INTERRUPTIBLE);
- }
- __set_current_state(TASK_RUNNING);
- SCSI_LOG_ERROR_RECOVERY(1,
- printk("Error handler scsi_eh_%d exiting\n", shost->host_no));
- shost->ehandler = NULL;
- return 0;
- }
- void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
- {
- struct scsi_device *sdev;
- __shost_for_each_device(sdev, shost) {
- if (channel == sdev_channel(sdev))
- __scsi_report_device_reset(sdev, NULL);
- }
- }
- EXPORT_SYMBOL(scsi_report_bus_reset);
- void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
- {
- struct scsi_device *sdev;
- __shost_for_each_device(sdev, shost) {
- if (channel == sdev_channel(sdev) &&
- target == sdev_id(sdev))
- __scsi_report_device_reset(sdev, NULL);
- }
- }
- EXPORT_SYMBOL(scsi_report_device_reset);
- static void
- scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
- {
- }
- int
- scsi_reset_provider(struct scsi_device *dev, int flag)
- {
- struct scsi_cmnd *scmd;
- struct Scsi_Host *shost = dev->host;
- struct request req;
- unsigned long flags;
- int rtn;
- if (scsi_autopm_get_host(shost) < 0)
- return FAILED;
- scmd = scsi_get_command(dev, GFP_KERNEL);
- blk_rq_init(NULL, &req);
- scmd->request = &req;
- scmd->cmnd = req.cmd;
- scmd->scsi_done = scsi_reset_provider_done_command;
- memset(&scmd->sdb, 0, sizeof(scmd->sdb));
- scmd->cmd_len = 0;
- scmd->sc_data_direction = DMA_BIDIRECTIONAL;
- spin_lock_irqsave(shost->host_lock, flags);
- shost->tmf_in_progress = 1;
- spin_unlock_irqrestore(shost->host_lock, flags);
- switch (flag) {
- case SCSI_TRY_RESET_DEVICE:
- rtn = scsi_try_bus_device_reset(scmd);
- if (rtn == SUCCESS)
- break;
-
- case SCSI_TRY_RESET_TARGET:
- rtn = scsi_try_target_reset(scmd);
- if (rtn == SUCCESS)
- break;
-
- case SCSI_TRY_RESET_BUS:
- rtn = scsi_try_bus_reset(scmd);
- if (rtn == SUCCESS)
- break;
-
- case SCSI_TRY_RESET_HOST:
- rtn = scsi_try_host_reset(scmd);
- break;
- default:
- rtn = FAILED;
- }
- spin_lock_irqsave(shost->host_lock, flags);
- shost->tmf_in_progress = 0;
- spin_unlock_irqrestore(shost->host_lock, flags);
-
- SCSI_LOG_ERROR_RECOVERY(3,
- printk("%s: waking up host to restart after TMF\n",
- __func__));
- wake_up(&shost->host_wait);
- scsi_run_host_queues(shost);
- scsi_next_command(scmd);
- scsi_autopm_put_host(shost);
- return rtn;
- }
- EXPORT_SYMBOL(scsi_reset_provider);
- int scsi_normalize_sense(const u8 *sense_buffer, int sb_len,
- struct scsi_sense_hdr *sshdr)
- {
- if (!sense_buffer || !sb_len)
- return 0;
- memset(sshdr, 0, sizeof(struct scsi_sense_hdr));
- sshdr->response_code = (sense_buffer[0] & 0x7f);
- if (!scsi_sense_valid(sshdr))
- return 0;
- if (sshdr->response_code >= 0x72) {
-
- if (sb_len > 1)
- sshdr->sense_key = (sense_buffer[1] & 0xf);
- if (sb_len > 2)
- sshdr->asc = sense_buffer[2];
- if (sb_len > 3)
- sshdr->ascq = sense_buffer[3];
- if (sb_len > 7)
- sshdr->additional_length = sense_buffer[7];
- } else {
-
- if (sb_len > 2)
- sshdr->sense_key = (sense_buffer[2] & 0xf);
- if (sb_len > 7) {
- sb_len = (sb_len < (sense_buffer[7] + 8)) ?
- sb_len : (sense_buffer[7] + 8);
- if (sb_len > 12)
- sshdr->asc = sense_buffer[12];
- if (sb_len > 13)
- sshdr->ascq = sense_buffer[13];
- }
- }
- return 1;
- }
- EXPORT_SYMBOL(scsi_normalize_sense);
- int scsi_command_normalize_sense(struct scsi_cmnd *cmd,
- struct scsi_sense_hdr *sshdr)
- {
- return scsi_normalize_sense(cmd->sense_buffer,
- SCSI_SENSE_BUFFERSIZE, sshdr);
- }
- EXPORT_SYMBOL(scsi_command_normalize_sense);
- const u8 * scsi_sense_desc_find(const u8 * sense_buffer, int sb_len,
- int desc_type)
- {
- int add_sen_len, add_len, desc_len, k;
- const u8 * descp;
- if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
- return NULL;
- if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
- return NULL;
- add_sen_len = (add_sen_len < (sb_len - 8)) ?
- add_sen_len : (sb_len - 8);
- descp = &sense_buffer[8];
- for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
- descp += desc_len;
- add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
- desc_len = add_len + 2;
- if (descp[0] == desc_type)
- return descp;
- if (add_len < 0)
- break;
- }
- return NULL;
- }
- EXPORT_SYMBOL(scsi_sense_desc_find);
- int scsi_get_sense_info_fld(const u8 * sense_buffer, int sb_len,
- u64 * info_out)
- {
- int j;
- const u8 * ucp;
- u64 ull;
- if (sb_len < 7)
- return 0;
- switch (sense_buffer[0] & 0x7f) {
- case 0x70:
- case 0x71:
- if (sense_buffer[0] & 0x80) {
- *info_out = (sense_buffer[3] << 24) +
- (sense_buffer[4] << 16) +
- (sense_buffer[5] << 8) + sense_buffer[6];
- return 1;
- } else
- return 0;
- case 0x72:
- case 0x73:
- ucp = scsi_sense_desc_find(sense_buffer, sb_len,
- 0 );
- if (ucp && (0xa == ucp[1])) {
- ull = 0;
- for (j = 0; j < 8; ++j) {
- if (j > 0)
- ull <<= 8;
- ull |= ucp[4 + j];
- }
- *info_out = ull;
- return 1;
- } else
- return 0;
- default:
- return 0;
- }
- }
- EXPORT_SYMBOL(scsi_get_sense_info_fld);
- void scsi_build_sense_buffer(int desc, u8 *buf, u8 key, u8 asc, u8 ascq)
- {
- if (desc) {
- buf[0] = 0x72;
- buf[1] = key;
- buf[2] = asc;
- buf[3] = ascq;
- buf[7] = 0;
- } else {
- buf[0] = 0x70;
- buf[2] = key;
- buf[7] = 0xa;
- buf[12] = asc;
- buf[13] = ascq;
- }
- }
- EXPORT_SYMBOL(scsi_build_sense_buffer);
|