scsi_lib.c 77 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046
  1. /*
  2. * Copyright (C) 1999 Eric Youngdale
  3. * Copyright (C) 2014 Christoph Hellwig
  4. *
  5. * SCSI queueing library.
  6. * Initial versions: Eric Youngdale (eric@andante.org).
  7. * Based upon conversations with large numbers
  8. * of people at Linux Expo.
  9. */
  10. #include <linux/bio.h>
  11. #include <linux/bitops.h>
  12. #include <linux/blkdev.h>
  13. #include <linux/completion.h>
  14. #include <linux/kernel.h>
  15. #include <linux/export.h>
  16. #include <linux/mempool.h>
  17. #include <linux/slab.h>
  18. #include <linux/init.h>
  19. #include <linux/pci.h>
  20. #include <linux/delay.h>
  21. #include <linux/hardirq.h>
  22. #include <linux/scatterlist.h>
  23. #include <linux/blk-mq.h>
  24. #include <scsi/scsi.h>
  25. #include <scsi/scsi_cmnd.h>
  26. #include <scsi/scsi_dbg.h>
  27. #include <scsi/scsi_device.h>
  28. #include <scsi/scsi_driver.h>
  29. #include <scsi/scsi_eh.h>
  30. #include <scsi/scsi_host.h>
  31. #include <trace/events/scsi.h>
  32. #include "scsi_priv.h"
  33. #include "scsi_logging.h"
  34. #define SG_MEMPOOL_NR ARRAY_SIZE(scsi_sg_pools)
  35. #define SG_MEMPOOL_SIZE 2
  36. struct scsi_host_sg_pool {
  37. size_t size;
  38. char *name;
  39. struct kmem_cache *slab;
  40. mempool_t *pool;
  41. };
  42. #define SP(x) { x, "sgpool-" __stringify(x) }
  43. #if (SCSI_MAX_SG_SEGMENTS < 32)
  44. #error SCSI_MAX_SG_SEGMENTS is too small (must be 32 or greater)
  45. #endif
  46. static struct scsi_host_sg_pool scsi_sg_pools[] = {
  47. SP(8),
  48. SP(16),
  49. #if (SCSI_MAX_SG_SEGMENTS > 32)
  50. SP(32),
  51. #if (SCSI_MAX_SG_SEGMENTS > 64)
  52. SP(64),
  53. #if (SCSI_MAX_SG_SEGMENTS > 128)
  54. SP(128),
  55. #if (SCSI_MAX_SG_SEGMENTS > 256)
  56. #error SCSI_MAX_SG_SEGMENTS is too large (256 MAX)
  57. #endif
  58. #endif
  59. #endif
  60. #endif
  61. SP(SCSI_MAX_SG_SEGMENTS)
  62. };
  63. #undef SP
  64. struct kmem_cache *scsi_sdb_cache;
  65. /*
  66. * When to reinvoke queueing after a resource shortage. It's 3 msecs to
  67. * not change behaviour from the previous unplug mechanism, experimentation
  68. * may prove this needs changing.
  69. */
  70. #define SCSI_QUEUE_DELAY 3
  71. static void
  72. scsi_set_blocked(struct scsi_cmnd *cmd, int reason)
  73. {
  74. struct Scsi_Host *host = cmd->device->host;
  75. struct scsi_device *device = cmd->device;
  76. struct scsi_target *starget = scsi_target(device);
  77. /*
  78. * Set the appropriate busy bit for the device/host.
  79. *
  80. * If the host/device isn't busy, assume that something actually
  81. * completed, and that we should be able to queue a command now.
  82. *
  83. * Note that the prior mid-layer assumption that any host could
  84. * always queue at least one command is now broken. The mid-layer
  85. * will implement a user specifiable stall (see
  86. * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
  87. * if a command is requeued with no other commands outstanding
  88. * either for the device or for the host.
  89. */
  90. switch (reason) {
  91. case SCSI_MLQUEUE_HOST_BUSY:
  92. atomic_set(&host->host_blocked, host->max_host_blocked);
  93. break;
  94. case SCSI_MLQUEUE_DEVICE_BUSY:
  95. case SCSI_MLQUEUE_EH_RETRY:
  96. atomic_set(&device->device_blocked,
  97. device->max_device_blocked);
  98. break;
  99. case SCSI_MLQUEUE_TARGET_BUSY:
  100. atomic_set(&starget->target_blocked,
  101. starget->max_target_blocked);
  102. break;
  103. }
  104. }
  105. static void scsi_mq_requeue_cmd(struct scsi_cmnd *cmd)
  106. {
  107. struct scsi_device *sdev = cmd->device;
  108. struct request_queue *q = cmd->request->q;
  109. blk_mq_requeue_request(cmd->request);
  110. blk_mq_kick_requeue_list(q);
  111. put_device(&sdev->sdev_gendev);
  112. }
  113. /**
  114. * __scsi_queue_insert - private queue insertion
  115. * @cmd: The SCSI command being requeued
  116. * @reason: The reason for the requeue
  117. * @unbusy: Whether the queue should be unbusied
  118. *
  119. * This is a private queue insertion. The public interface
  120. * scsi_queue_insert() always assumes the queue should be unbusied
  121. * because it's always called before the completion. This function is
  122. * for a requeue after completion, which should only occur in this
  123. * file.
  124. */
  125. static void __scsi_queue_insert(struct scsi_cmnd *cmd, int reason, int unbusy)
  126. {
  127. struct scsi_device *device = cmd->device;
  128. struct request_queue *q = device->request_queue;
  129. unsigned long flags;
  130. SCSI_LOG_MLQUEUE(1, scmd_printk(KERN_INFO, cmd,
  131. "Inserting command %p into mlqueue\n", cmd));
  132. scsi_set_blocked(cmd, reason);
  133. /*
  134. * Decrement the counters, since these commands are no longer
  135. * active on the host/device.
  136. */
  137. if (unbusy)
  138. scsi_device_unbusy(device);
  139. /*
  140. * Requeue this command. It will go before all other commands
  141. * that are already in the queue. Schedule requeue work under
  142. * lock such that the kblockd_schedule_work() call happens
  143. * before blk_cleanup_queue() finishes.
  144. */
  145. cmd->result = 0;
  146. if (q->mq_ops) {
  147. scsi_mq_requeue_cmd(cmd);
  148. return;
  149. }
  150. spin_lock_irqsave(q->queue_lock, flags);
  151. blk_requeue_request(q, cmd->request);
  152. kblockd_schedule_work(&device->requeue_work);
  153. spin_unlock_irqrestore(q->queue_lock, flags);
  154. }
  155. /*
  156. * Function: scsi_queue_insert()
  157. *
  158. * Purpose: Insert a command in the midlevel queue.
  159. *
  160. * Arguments: cmd - command that we are adding to queue.
  161. * reason - why we are inserting command to queue.
  162. *
  163. * Lock status: Assumed that lock is not held upon entry.
  164. *
  165. * Returns: Nothing.
  166. *
  167. * Notes: We do this for one of two cases. Either the host is busy
  168. * and it cannot accept any more commands for the time being,
  169. * or the device returned QUEUE_FULL and can accept no more
  170. * commands.
  171. * Notes: This could be called either from an interrupt context or a
  172. * normal process context.
  173. */
  174. void scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
  175. {
  176. __scsi_queue_insert(cmd, reason, 1);
  177. }
  178. /**
  179. * scsi_execute - insert request and wait for the result
  180. * @sdev: scsi device
  181. * @cmd: scsi command
  182. * @data_direction: data direction
  183. * @buffer: data buffer
  184. * @bufflen: len of buffer
  185. * @sense: optional sense buffer
  186. * @timeout: request timeout in seconds
  187. * @retries: number of times to retry request
  188. * @flags: or into request flags;
  189. * @resid: optional residual length
  190. *
  191. * returns the req->errors value which is the scsi_cmnd result
  192. * field.
  193. */
  194. int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
  195. int data_direction, void *buffer, unsigned bufflen,
  196. unsigned char *sense, int timeout, int retries, u64 flags,
  197. int *resid)
  198. {
  199. struct request *req;
  200. int write = (data_direction == DMA_TO_DEVICE);
  201. int ret = DRIVER_ERROR << 24;
  202. req = blk_get_request(sdev->request_queue, write, __GFP_WAIT);
  203. if (IS_ERR(req))
  204. return ret;
  205. blk_rq_set_block_pc(req);
  206. if (bufflen && blk_rq_map_kern(sdev->request_queue, req,
  207. buffer, bufflen, __GFP_WAIT))
  208. goto out;
  209. req->cmd_len = COMMAND_SIZE(cmd[0]);
  210. memcpy(req->cmd, cmd, req->cmd_len);
  211. req->sense = sense;
  212. req->sense_len = 0;
  213. req->retries = retries;
  214. req->timeout = timeout;
  215. req->cmd_flags |= flags | REQ_QUIET | REQ_PREEMPT;
  216. /*
  217. * head injection *required* here otherwise quiesce won't work
  218. */
  219. blk_execute_rq(req->q, NULL, req, 1);
  220. /*
  221. * Some devices (USB mass-storage in particular) may transfer
  222. * garbage data together with a residue indicating that the data
  223. * is invalid. Prevent the garbage from being misinterpreted
  224. * and prevent security leaks by zeroing out the excess data.
  225. */
  226. if (unlikely(req->resid_len > 0 && req->resid_len <= bufflen))
  227. memset(buffer + (bufflen - req->resid_len), 0, req->resid_len);
  228. if (resid)
  229. *resid = req->resid_len;
  230. ret = req->errors;
  231. out:
  232. blk_put_request(req);
  233. return ret;
  234. }
  235. EXPORT_SYMBOL(scsi_execute);
  236. int scsi_execute_req_flags(struct scsi_device *sdev, const unsigned char *cmd,
  237. int data_direction, void *buffer, unsigned bufflen,
  238. struct scsi_sense_hdr *sshdr, int timeout, int retries,
  239. int *resid, u64 flags)
  240. {
  241. char *sense = NULL;
  242. int result;
  243. if (sshdr) {
  244. sense = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
  245. if (!sense)
  246. return DRIVER_ERROR << 24;
  247. }
  248. result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
  249. sense, timeout, retries, flags, resid);
  250. if (sshdr)
  251. scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
  252. kfree(sense);
  253. return result;
  254. }
  255. EXPORT_SYMBOL(scsi_execute_req_flags);
  256. /*
  257. * Function: scsi_init_cmd_errh()
  258. *
  259. * Purpose: Initialize cmd fields related to error handling.
  260. *
  261. * Arguments: cmd - command that is ready to be queued.
  262. *
  263. * Notes: This function has the job of initializing a number of
  264. * fields related to error handling. Typically this will
  265. * be called once for each command, as required.
  266. */
  267. static void scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  268. {
  269. cmd->serial_number = 0;
  270. scsi_set_resid(cmd, 0);
  271. memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  272. if (cmd->cmd_len == 0)
  273. cmd->cmd_len = scsi_command_size(cmd->cmnd);
  274. }
  275. void scsi_device_unbusy(struct scsi_device *sdev)
  276. {
  277. struct Scsi_Host *shost = sdev->host;
  278. struct scsi_target *starget = scsi_target(sdev);
  279. unsigned long flags;
  280. atomic_dec(&shost->host_busy);
  281. if (starget->can_queue > 0)
  282. atomic_dec(&starget->target_busy);
  283. if (unlikely(scsi_host_in_recovery(shost) &&
  284. (shost->host_failed || shost->host_eh_scheduled))) {
  285. spin_lock_irqsave(shost->host_lock, flags);
  286. scsi_eh_wakeup(shost);
  287. spin_unlock_irqrestore(shost->host_lock, flags);
  288. }
  289. atomic_dec(&sdev->device_busy);
  290. }
  291. static void scsi_kick_queue(struct request_queue *q)
  292. {
  293. if (q->mq_ops)
  294. blk_mq_start_hw_queues(q);
  295. else
  296. blk_run_queue(q);
  297. }
  298. /*
  299. * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  300. * and call blk_run_queue for all the scsi_devices on the target -
  301. * including current_sdev first.
  302. *
  303. * Called with *no* scsi locks held.
  304. */
  305. static void scsi_single_lun_run(struct scsi_device *current_sdev)
  306. {
  307. struct Scsi_Host *shost = current_sdev->host;
  308. struct scsi_device *sdev, *tmp;
  309. struct scsi_target *starget = scsi_target(current_sdev);
  310. unsigned long flags;
  311. spin_lock_irqsave(shost->host_lock, flags);
  312. starget->starget_sdev_user = NULL;
  313. spin_unlock_irqrestore(shost->host_lock, flags);
  314. /*
  315. * Call blk_run_queue for all LUNs on the target, starting with
  316. * current_sdev. We race with others (to set starget_sdev_user),
  317. * but in most cases, we will be first. Ideally, each LU on the
  318. * target would get some limited time or requests on the target.
  319. */
  320. scsi_kick_queue(current_sdev->request_queue);
  321. spin_lock_irqsave(shost->host_lock, flags);
  322. if (starget->starget_sdev_user)
  323. goto out;
  324. list_for_each_entry_safe(sdev, tmp, &starget->devices,
  325. same_target_siblings) {
  326. if (sdev == current_sdev)
  327. continue;
  328. if (scsi_device_get(sdev))
  329. continue;
  330. spin_unlock_irqrestore(shost->host_lock, flags);
  331. scsi_kick_queue(sdev->request_queue);
  332. spin_lock_irqsave(shost->host_lock, flags);
  333. scsi_device_put(sdev);
  334. }
  335. out:
  336. spin_unlock_irqrestore(shost->host_lock, flags);
  337. }
  338. static inline bool scsi_device_is_busy(struct scsi_device *sdev)
  339. {
  340. if (atomic_read(&sdev->device_busy) >= sdev->queue_depth)
  341. return true;
  342. if (atomic_read(&sdev->device_blocked) > 0)
  343. return true;
  344. return false;
  345. }
  346. static inline bool scsi_target_is_busy(struct scsi_target *starget)
  347. {
  348. if (starget->can_queue > 0) {
  349. if (atomic_read(&starget->target_busy) >= starget->can_queue)
  350. return true;
  351. if (atomic_read(&starget->target_blocked) > 0)
  352. return true;
  353. }
  354. return false;
  355. }
  356. static inline bool scsi_host_is_busy(struct Scsi_Host *shost)
  357. {
  358. if (shost->can_queue > 0 &&
  359. atomic_read(&shost->host_busy) >= shost->can_queue)
  360. return true;
  361. if (atomic_read(&shost->host_blocked) > 0)
  362. return true;
  363. if (shost->host_self_blocked)
  364. return true;
  365. return false;
  366. }
  367. static void scsi_starved_list_run(struct Scsi_Host *shost)
  368. {
  369. LIST_HEAD(starved_list);
  370. struct scsi_device *sdev;
  371. unsigned long flags;
  372. spin_lock_irqsave(shost->host_lock, flags);
  373. list_splice_init(&shost->starved_list, &starved_list);
  374. while (!list_empty(&starved_list)) {
  375. struct request_queue *slq;
  376. /*
  377. * As long as shost is accepting commands and we have
  378. * starved queues, call blk_run_queue. scsi_request_fn
  379. * drops the queue_lock and can add us back to the
  380. * starved_list.
  381. *
  382. * host_lock protects the starved_list and starved_entry.
  383. * scsi_request_fn must get the host_lock before checking
  384. * or modifying starved_list or starved_entry.
  385. */
  386. if (scsi_host_is_busy(shost))
  387. break;
  388. sdev = list_entry(starved_list.next,
  389. struct scsi_device, starved_entry);
  390. list_del_init(&sdev->starved_entry);
  391. if (scsi_target_is_busy(scsi_target(sdev))) {
  392. list_move_tail(&sdev->starved_entry,
  393. &shost->starved_list);
  394. continue;
  395. }
  396. /*
  397. * Once we drop the host lock, a racing scsi_remove_device()
  398. * call may remove the sdev from the starved list and destroy
  399. * it and the queue. Mitigate by taking a reference to the
  400. * queue and never touching the sdev again after we drop the
  401. * host lock. Note: if __scsi_remove_device() invokes
  402. * blk_cleanup_queue() before the queue is run from this
  403. * function then blk_run_queue() will return immediately since
  404. * blk_cleanup_queue() marks the queue with QUEUE_FLAG_DYING.
  405. */
  406. slq = sdev->request_queue;
  407. if (!blk_get_queue(slq))
  408. continue;
  409. spin_unlock_irqrestore(shost->host_lock, flags);
  410. scsi_kick_queue(slq);
  411. blk_put_queue(slq);
  412. spin_lock_irqsave(shost->host_lock, flags);
  413. }
  414. /* put any unprocessed entries back */
  415. list_splice(&starved_list, &shost->starved_list);
  416. spin_unlock_irqrestore(shost->host_lock, flags);
  417. }
  418. /*
  419. * Function: scsi_run_queue()
  420. *
  421. * Purpose: Select a proper request queue to serve next
  422. *
  423. * Arguments: q - last request's queue
  424. *
  425. * Returns: Nothing
  426. *
  427. * Notes: The previous command was completely finished, start
  428. * a new one if possible.
  429. */
  430. static void scsi_run_queue(struct request_queue *q)
  431. {
  432. struct scsi_device *sdev = q->queuedata;
  433. if (scsi_target(sdev)->single_lun)
  434. scsi_single_lun_run(sdev);
  435. if (!list_empty(&sdev->host->starved_list))
  436. scsi_starved_list_run(sdev->host);
  437. if (q->mq_ops)
  438. blk_mq_start_stopped_hw_queues(q, false);
  439. else
  440. blk_run_queue(q);
  441. }
  442. void scsi_requeue_run_queue(struct work_struct *work)
  443. {
  444. struct scsi_device *sdev;
  445. struct request_queue *q;
  446. sdev = container_of(work, struct scsi_device, requeue_work);
  447. q = sdev->request_queue;
  448. scsi_run_queue(q);
  449. }
  450. /*
  451. * Function: scsi_requeue_command()
  452. *
  453. * Purpose: Handle post-processing of completed commands.
  454. *
  455. * Arguments: q - queue to operate on
  456. * cmd - command that may need to be requeued.
  457. *
  458. * Returns: Nothing
  459. *
  460. * Notes: After command completion, there may be blocks left
  461. * over which weren't finished by the previous command
  462. * this can be for a number of reasons - the main one is
  463. * I/O errors in the middle of the request, in which case
  464. * we need to request the blocks that come after the bad
  465. * sector.
  466. * Notes: Upon return, cmd is a stale pointer.
  467. */
  468. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  469. {
  470. struct scsi_device *sdev = cmd->device;
  471. struct request *req = cmd->request;
  472. unsigned long flags;
  473. spin_lock_irqsave(q->queue_lock, flags);
  474. blk_unprep_request(req);
  475. req->special = NULL;
  476. scsi_put_command(cmd);
  477. blk_requeue_request(q, req);
  478. spin_unlock_irqrestore(q->queue_lock, flags);
  479. scsi_run_queue(q);
  480. put_device(&sdev->sdev_gendev);
  481. }
  482. void scsi_next_command(struct scsi_cmnd *cmd)
  483. {
  484. struct scsi_device *sdev = cmd->device;
  485. struct request_queue *q = sdev->request_queue;
  486. scsi_put_command(cmd);
  487. scsi_run_queue(q);
  488. put_device(&sdev->sdev_gendev);
  489. }
  490. void scsi_run_host_queues(struct Scsi_Host *shost)
  491. {
  492. struct scsi_device *sdev;
  493. shost_for_each_device(sdev, shost)
  494. scsi_run_queue(sdev->request_queue);
  495. }
  496. static inline unsigned int scsi_sgtable_index(unsigned short nents)
  497. {
  498. unsigned int index;
  499. BUG_ON(nents > SCSI_MAX_SG_SEGMENTS);
  500. if (nents <= 8)
  501. index = 0;
  502. else
  503. index = get_count_order(nents) - 3;
  504. return index;
  505. }
  506. static void scsi_sg_free(struct scatterlist *sgl, unsigned int nents)
  507. {
  508. struct scsi_host_sg_pool *sgp;
  509. sgp = scsi_sg_pools + scsi_sgtable_index(nents);
  510. mempool_free(sgl, sgp->pool);
  511. }
  512. static struct scatterlist *scsi_sg_alloc(unsigned int nents, gfp_t gfp_mask)
  513. {
  514. struct scsi_host_sg_pool *sgp;
  515. sgp = scsi_sg_pools + scsi_sgtable_index(nents);
  516. return mempool_alloc(sgp->pool, gfp_mask);
  517. }
  518. static void scsi_free_sgtable(struct scsi_data_buffer *sdb, bool mq)
  519. {
  520. if (mq && sdb->table.nents <= SCSI_MAX_SG_SEGMENTS)
  521. return;
  522. __sg_free_table(&sdb->table, SCSI_MAX_SG_SEGMENTS, mq, scsi_sg_free);
  523. }
  524. static int scsi_alloc_sgtable(struct scsi_data_buffer *sdb, int nents,
  525. gfp_t gfp_mask, bool mq)
  526. {
  527. struct scatterlist *first_chunk = NULL;
  528. int ret;
  529. BUG_ON(!nents);
  530. if (mq) {
  531. if (nents <= SCSI_MAX_SG_SEGMENTS) {
  532. sdb->table.nents = nents;
  533. sg_init_table(sdb->table.sgl, sdb->table.nents);
  534. return 0;
  535. }
  536. first_chunk = sdb->table.sgl;
  537. }
  538. ret = __sg_alloc_table(&sdb->table, nents, SCSI_MAX_SG_SEGMENTS,
  539. first_chunk, gfp_mask, scsi_sg_alloc);
  540. if (unlikely(ret))
  541. scsi_free_sgtable(sdb, mq);
  542. return ret;
  543. }
  544. static void scsi_uninit_cmd(struct scsi_cmnd *cmd)
  545. {
  546. if (cmd->request->cmd_type == REQ_TYPE_FS) {
  547. struct scsi_driver *drv = scsi_cmd_to_driver(cmd);
  548. if (drv->uninit_command)
  549. drv->uninit_command(cmd);
  550. }
  551. }
  552. static void scsi_mq_free_sgtables(struct scsi_cmnd *cmd)
  553. {
  554. if (cmd->sdb.table.nents)
  555. scsi_free_sgtable(&cmd->sdb, true);
  556. if (cmd->request->next_rq && cmd->request->next_rq->special)
  557. scsi_free_sgtable(cmd->request->next_rq->special, true);
  558. if (scsi_prot_sg_count(cmd))
  559. scsi_free_sgtable(cmd->prot_sdb, true);
  560. }
  561. static void scsi_mq_uninit_cmd(struct scsi_cmnd *cmd)
  562. {
  563. struct scsi_device *sdev = cmd->device;
  564. struct Scsi_Host *shost = sdev->host;
  565. unsigned long flags;
  566. scsi_mq_free_sgtables(cmd);
  567. scsi_uninit_cmd(cmd);
  568. if (shost->use_cmd_list) {
  569. BUG_ON(list_empty(&cmd->list));
  570. spin_lock_irqsave(&sdev->list_lock, flags);
  571. list_del_init(&cmd->list);
  572. spin_unlock_irqrestore(&sdev->list_lock, flags);
  573. }
  574. }
  575. /*
  576. * Function: scsi_release_buffers()
  577. *
  578. * Purpose: Free resources allocate for a scsi_command.
  579. *
  580. * Arguments: cmd - command that we are bailing.
  581. *
  582. * Lock status: Assumed that no lock is held upon entry.
  583. *
  584. * Returns: Nothing
  585. *
  586. * Notes: In the event that an upper level driver rejects a
  587. * command, we must release resources allocated during
  588. * the __init_io() function. Primarily this would involve
  589. * the scatter-gather table.
  590. */
  591. static void scsi_release_buffers(struct scsi_cmnd *cmd)
  592. {
  593. if (cmd->sdb.table.nents)
  594. scsi_free_sgtable(&cmd->sdb, false);
  595. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  596. if (scsi_prot_sg_count(cmd))
  597. scsi_free_sgtable(cmd->prot_sdb, false);
  598. }
  599. static void scsi_release_bidi_buffers(struct scsi_cmnd *cmd)
  600. {
  601. struct scsi_data_buffer *bidi_sdb = cmd->request->next_rq->special;
  602. scsi_free_sgtable(bidi_sdb, false);
  603. kmem_cache_free(scsi_sdb_cache, bidi_sdb);
  604. cmd->request->next_rq->special = NULL;
  605. }
  606. static bool scsi_end_request(struct request *req, int error,
  607. unsigned int bytes, unsigned int bidi_bytes)
  608. {
  609. struct scsi_cmnd *cmd = req->special;
  610. struct scsi_device *sdev = cmd->device;
  611. struct request_queue *q = sdev->request_queue;
  612. if (blk_update_request(req, error, bytes))
  613. return true;
  614. /* Bidi request must be completed as a whole */
  615. if (unlikely(bidi_bytes) &&
  616. blk_update_request(req->next_rq, error, bidi_bytes))
  617. return true;
  618. if (blk_queue_add_random(q))
  619. add_disk_randomness(req->rq_disk);
  620. if (req->mq_ctx) {
  621. /*
  622. * In the MQ case the command gets freed by __blk_mq_end_request,
  623. * so we have to do all cleanup that depends on it earlier.
  624. *
  625. * We also can't kick the queues from irq context, so we
  626. * will have to defer it to a workqueue.
  627. */
  628. scsi_mq_uninit_cmd(cmd);
  629. __blk_mq_end_request(req, error);
  630. if (scsi_target(sdev)->single_lun ||
  631. !list_empty(&sdev->host->starved_list))
  632. kblockd_schedule_work(&sdev->requeue_work);
  633. else
  634. blk_mq_start_stopped_hw_queues(q, true);
  635. put_device(&sdev->sdev_gendev);
  636. } else {
  637. unsigned long flags;
  638. if (bidi_bytes)
  639. scsi_release_bidi_buffers(cmd);
  640. spin_lock_irqsave(q->queue_lock, flags);
  641. blk_finish_request(req, error);
  642. spin_unlock_irqrestore(q->queue_lock, flags);
  643. scsi_release_buffers(cmd);
  644. scsi_next_command(cmd);
  645. }
  646. return false;
  647. }
  648. /**
  649. * __scsi_error_from_host_byte - translate SCSI error code into errno
  650. * @cmd: SCSI command (unused)
  651. * @result: scsi error code
  652. *
  653. * Translate SCSI error code into standard UNIX errno.
  654. * Return values:
  655. * -ENOLINK temporary transport failure
  656. * -EREMOTEIO permanent target failure, do not retry
  657. * -EBADE permanent nexus failure, retry on other path
  658. * -ENOSPC No write space available
  659. * -ENODATA Medium error
  660. * -EIO unspecified I/O error
  661. */
  662. static int __scsi_error_from_host_byte(struct scsi_cmnd *cmd, int result)
  663. {
  664. int error = 0;
  665. switch(host_byte(result)) {
  666. case DID_TRANSPORT_FAILFAST:
  667. error = -ENOLINK;
  668. break;
  669. case DID_TARGET_FAILURE:
  670. set_host_byte(cmd, DID_OK);
  671. error = -EREMOTEIO;
  672. break;
  673. case DID_NEXUS_FAILURE:
  674. set_host_byte(cmd, DID_OK);
  675. error = -EBADE;
  676. break;
  677. case DID_ALLOC_FAILURE:
  678. set_host_byte(cmd, DID_OK);
  679. error = -ENOSPC;
  680. break;
  681. case DID_MEDIUM_ERROR:
  682. set_host_byte(cmd, DID_OK);
  683. error = -ENODATA;
  684. break;
  685. default:
  686. error = -EIO;
  687. break;
  688. }
  689. return error;
  690. }
  691. /*
  692. * Function: scsi_io_completion()
  693. *
  694. * Purpose: Completion processing for block device I/O requests.
  695. *
  696. * Arguments: cmd - command that is finished.
  697. *
  698. * Lock status: Assumed that no lock is held upon entry.
  699. *
  700. * Returns: Nothing
  701. *
  702. * Notes: We will finish off the specified number of sectors. If we
  703. * are done, the command block will be released and the queue
  704. * function will be goosed. If we are not done then we have to
  705. * figure out what to do next:
  706. *
  707. * a) We can call scsi_requeue_command(). The request
  708. * will be unprepared and put back on the queue. Then
  709. * a new command will be created for it. This should
  710. * be used if we made forward progress, or if we want
  711. * to switch from READ(10) to READ(6) for example.
  712. *
  713. * b) We can call __scsi_queue_insert(). The request will
  714. * be put back on the queue and retried using the same
  715. * command as before, possibly after a delay.
  716. *
  717. * c) We can call scsi_end_request() with -EIO to fail
  718. * the remainder of the request.
  719. */
  720. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
  721. {
  722. int result = cmd->result;
  723. struct request_queue *q = cmd->device->request_queue;
  724. struct request *req = cmd->request;
  725. int error = 0;
  726. struct scsi_sense_hdr sshdr;
  727. int sense_valid = 0;
  728. int sense_deferred = 0;
  729. enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
  730. ACTION_DELAYED_RETRY} action;
  731. unsigned long wait_for = (cmd->allowed + 1) * req->timeout;
  732. if (result) {
  733. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  734. if (sense_valid)
  735. sense_deferred = scsi_sense_is_deferred(&sshdr);
  736. }
  737. if (req->cmd_type == REQ_TYPE_BLOCK_PC) { /* SG_IO ioctl from block level */
  738. if (result) {
  739. if (sense_valid && req->sense) {
  740. /*
  741. * SG_IO wants current and deferred errors
  742. */
  743. int len = 8 + cmd->sense_buffer[7];
  744. if (len > SCSI_SENSE_BUFFERSIZE)
  745. len = SCSI_SENSE_BUFFERSIZE;
  746. memcpy(req->sense, cmd->sense_buffer, len);
  747. req->sense_len = len;
  748. }
  749. if (!sense_deferred)
  750. error = __scsi_error_from_host_byte(cmd, result);
  751. }
  752. /*
  753. * __scsi_error_from_host_byte may have reset the host_byte
  754. */
  755. req->errors = cmd->result;
  756. req->resid_len = scsi_get_resid(cmd);
  757. if (scsi_bidi_cmnd(cmd)) {
  758. /*
  759. * Bidi commands Must be complete as a whole,
  760. * both sides at once.
  761. */
  762. req->next_rq->resid_len = scsi_in(cmd)->resid;
  763. if (scsi_end_request(req, 0, blk_rq_bytes(req),
  764. blk_rq_bytes(req->next_rq)))
  765. BUG();
  766. return;
  767. }
  768. } else if (blk_rq_bytes(req) == 0 && result && !sense_deferred) {
  769. /*
  770. * Certain non BLOCK_PC requests are commands that don't
  771. * actually transfer anything (FLUSH), so cannot use
  772. * good_bytes != blk_rq_bytes(req) as the signal for an error.
  773. * This sets the error explicitly for the problem case.
  774. */
  775. error = __scsi_error_from_host_byte(cmd, result);
  776. }
  777. /* no bidi support for !REQ_TYPE_BLOCK_PC yet */
  778. BUG_ON(blk_bidi_rq(req));
  779. /*
  780. * Next deal with any sectors which we were able to correctly
  781. * handle.
  782. */
  783. SCSI_LOG_HLCOMPLETE(1, scmd_printk(KERN_INFO, cmd,
  784. "%u sectors total, %d bytes done.\n",
  785. blk_rq_sectors(req), good_bytes));
  786. /*
  787. * Recovered errors need reporting, but they're always treated
  788. * as success, so fiddle the result code here. For BLOCK_PC
  789. * we already took a copy of the original into rq->errors which
  790. * is what gets returned to the user
  791. */
  792. if (sense_valid && (sshdr.sense_key == RECOVERED_ERROR)) {
  793. /* if ATA PASS-THROUGH INFORMATION AVAILABLE skip
  794. * print since caller wants ATA registers. Only occurs on
  795. * SCSI ATA PASS_THROUGH commands when CK_COND=1
  796. */
  797. if ((sshdr.asc == 0x0) && (sshdr.ascq == 0x1d))
  798. ;
  799. else if (!(req->cmd_flags & REQ_QUIET))
  800. scsi_print_sense("", cmd);
  801. result = 0;
  802. /* BLOCK_PC may have set error */
  803. error = 0;
  804. }
  805. /*
  806. * If we finished all bytes in the request we are done now.
  807. */
  808. if (!scsi_end_request(req, error, good_bytes, 0))
  809. return;
  810. /*
  811. * Kill remainder if no retrys.
  812. */
  813. if (error && scsi_noretry_cmd(cmd)) {
  814. if (scsi_end_request(req, error, blk_rq_bytes(req), 0))
  815. BUG();
  816. return;
  817. }
  818. /*
  819. * If there had been no error, but we have leftover bytes in the
  820. * requeues just queue the command up again.
  821. */
  822. if (result == 0)
  823. goto requeue;
  824. error = __scsi_error_from_host_byte(cmd, result);
  825. if (host_byte(result) == DID_RESET) {
  826. /* Third party bus reset or reset for error recovery
  827. * reasons. Just retry the command and see what
  828. * happens.
  829. */
  830. action = ACTION_RETRY;
  831. } else if (sense_valid && !sense_deferred) {
  832. switch (sshdr.sense_key) {
  833. case UNIT_ATTENTION:
  834. if (cmd->device->removable) {
  835. /* Detected disc change. Set a bit
  836. * and quietly refuse further access.
  837. */
  838. cmd->device->changed = 1;
  839. action = ACTION_FAIL;
  840. } else {
  841. /* Must have been a power glitch, or a
  842. * bus reset. Could not have been a
  843. * media change, so we just retry the
  844. * command and see what happens.
  845. */
  846. action = ACTION_RETRY;
  847. }
  848. break;
  849. case ILLEGAL_REQUEST:
  850. /* If we had an ILLEGAL REQUEST returned, then
  851. * we may have performed an unsupported
  852. * command. The only thing this should be
  853. * would be a ten byte read where only a six
  854. * byte read was supported. Also, on a system
  855. * where READ CAPACITY failed, we may have
  856. * read past the end of the disk.
  857. */
  858. if ((cmd->device->use_10_for_rw &&
  859. sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
  860. (cmd->cmnd[0] == READ_10 ||
  861. cmd->cmnd[0] == WRITE_10)) {
  862. /* This will issue a new 6-byte command. */
  863. cmd->device->use_10_for_rw = 0;
  864. action = ACTION_REPREP;
  865. } else if (sshdr.asc == 0x10) /* DIX */ {
  866. action = ACTION_FAIL;
  867. error = -EILSEQ;
  868. /* INVALID COMMAND OPCODE or INVALID FIELD IN CDB */
  869. } else if (sshdr.asc == 0x20 || sshdr.asc == 0x24) {
  870. action = ACTION_FAIL;
  871. error = -EREMOTEIO;
  872. } else
  873. action = ACTION_FAIL;
  874. break;
  875. case ABORTED_COMMAND:
  876. action = ACTION_FAIL;
  877. if (sshdr.asc == 0x10) /* DIF */
  878. error = -EILSEQ;
  879. break;
  880. case NOT_READY:
  881. /* If the device is in the process of becoming
  882. * ready, or has a temporary blockage, retry.
  883. */
  884. if (sshdr.asc == 0x04) {
  885. switch (sshdr.ascq) {
  886. case 0x01: /* becoming ready */
  887. case 0x04: /* format in progress */
  888. case 0x05: /* rebuild in progress */
  889. case 0x06: /* recalculation in progress */
  890. case 0x07: /* operation in progress */
  891. case 0x08: /* Long write in progress */
  892. case 0x09: /* self test in progress */
  893. case 0x14: /* space allocation in progress */
  894. action = ACTION_DELAYED_RETRY;
  895. break;
  896. default:
  897. action = ACTION_FAIL;
  898. break;
  899. }
  900. } else
  901. action = ACTION_FAIL;
  902. break;
  903. case VOLUME_OVERFLOW:
  904. /* See SSC3rXX or current. */
  905. action = ACTION_FAIL;
  906. break;
  907. default:
  908. action = ACTION_FAIL;
  909. break;
  910. }
  911. } else
  912. action = ACTION_FAIL;
  913. if (action != ACTION_FAIL &&
  914. time_before(cmd->jiffies_at_alloc + wait_for, jiffies))
  915. action = ACTION_FAIL;
  916. switch (action) {
  917. case ACTION_FAIL:
  918. /* Give up and fail the remainder of the request */
  919. if (!(req->cmd_flags & REQ_QUIET)) {
  920. scsi_print_result(cmd);
  921. if (driver_byte(result) & DRIVER_SENSE)
  922. scsi_print_sense("", cmd);
  923. scsi_print_command(cmd);
  924. }
  925. if (!scsi_end_request(req, error, blk_rq_err_bytes(req), 0))
  926. return;
  927. /*FALLTHRU*/
  928. case ACTION_REPREP:
  929. requeue:
  930. /* Unprep the request and put it back at the head of the queue.
  931. * A new command will be prepared and issued.
  932. */
  933. if (q->mq_ops) {
  934. cmd->request->cmd_flags &= ~REQ_DONTPREP;
  935. scsi_mq_uninit_cmd(cmd);
  936. scsi_mq_requeue_cmd(cmd);
  937. } else {
  938. scsi_release_buffers(cmd);
  939. scsi_requeue_command(q, cmd);
  940. }
  941. break;
  942. case ACTION_RETRY:
  943. /* Retry the same command immediately */
  944. __scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY, 0);
  945. break;
  946. case ACTION_DELAYED_RETRY:
  947. /* Retry the same command after a delay */
  948. __scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY, 0);
  949. break;
  950. }
  951. }
  952. static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb,
  953. gfp_t gfp_mask)
  954. {
  955. int count;
  956. /*
  957. * If sg table allocation fails, requeue request later.
  958. */
  959. if (unlikely(scsi_alloc_sgtable(sdb, req->nr_phys_segments,
  960. gfp_mask, req->mq_ctx != NULL)))
  961. return BLKPREP_DEFER;
  962. /*
  963. * Next, walk the list, and fill in the addresses and sizes of
  964. * each segment.
  965. */
  966. count = blk_rq_map_sg(req->q, req, sdb->table.sgl);
  967. BUG_ON(count > sdb->table.nents);
  968. sdb->table.nents = count;
  969. sdb->length = blk_rq_bytes(req);
  970. return BLKPREP_OK;
  971. }
  972. /*
  973. * Function: scsi_init_io()
  974. *
  975. * Purpose: SCSI I/O initialize function.
  976. *
  977. * Arguments: cmd - Command descriptor we wish to initialize
  978. *
  979. * Returns: 0 on success
  980. * BLKPREP_DEFER if the failure is retryable
  981. * BLKPREP_KILL if the failure is fatal
  982. */
  983. int scsi_init_io(struct scsi_cmnd *cmd, gfp_t gfp_mask)
  984. {
  985. struct scsi_device *sdev = cmd->device;
  986. struct request *rq = cmd->request;
  987. bool is_mq = (rq->mq_ctx != NULL);
  988. int error;
  989. BUG_ON(!rq->nr_phys_segments);
  990. error = scsi_init_sgtable(rq, &cmd->sdb, gfp_mask);
  991. if (error)
  992. goto err_exit;
  993. if (blk_bidi_rq(rq)) {
  994. if (!rq->q->mq_ops) {
  995. struct scsi_data_buffer *bidi_sdb =
  996. kmem_cache_zalloc(scsi_sdb_cache, GFP_ATOMIC);
  997. if (!bidi_sdb) {
  998. error = BLKPREP_DEFER;
  999. goto err_exit;
  1000. }
  1001. rq->next_rq->special = bidi_sdb;
  1002. }
  1003. error = scsi_init_sgtable(rq->next_rq, rq->next_rq->special,
  1004. GFP_ATOMIC);
  1005. if (error)
  1006. goto err_exit;
  1007. }
  1008. if (blk_integrity_rq(rq)) {
  1009. struct scsi_data_buffer *prot_sdb = cmd->prot_sdb;
  1010. int ivecs, count;
  1011. BUG_ON(prot_sdb == NULL);
  1012. ivecs = blk_rq_count_integrity_sg(rq->q, rq->bio);
  1013. if (scsi_alloc_sgtable(prot_sdb, ivecs, gfp_mask, is_mq)) {
  1014. error = BLKPREP_DEFER;
  1015. goto err_exit;
  1016. }
  1017. count = blk_rq_map_integrity_sg(rq->q, rq->bio,
  1018. prot_sdb->table.sgl);
  1019. BUG_ON(unlikely(count > ivecs));
  1020. BUG_ON(unlikely(count > queue_max_integrity_segments(rq->q)));
  1021. cmd->prot_sdb = prot_sdb;
  1022. cmd->prot_sdb->table.nents = count;
  1023. }
  1024. return BLKPREP_OK;
  1025. err_exit:
  1026. if (is_mq) {
  1027. scsi_mq_free_sgtables(cmd);
  1028. } else {
  1029. scsi_release_buffers(cmd);
  1030. cmd->request->special = NULL;
  1031. scsi_put_command(cmd);
  1032. put_device(&sdev->sdev_gendev);
  1033. }
  1034. return error;
  1035. }
  1036. EXPORT_SYMBOL(scsi_init_io);
  1037. static struct scsi_cmnd *scsi_get_cmd_from_req(struct scsi_device *sdev,
  1038. struct request *req)
  1039. {
  1040. struct scsi_cmnd *cmd;
  1041. if (!req->special) {
  1042. /* Bail if we can't get a reference to the device */
  1043. if (!get_device(&sdev->sdev_gendev))
  1044. return NULL;
  1045. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  1046. if (unlikely(!cmd)) {
  1047. put_device(&sdev->sdev_gendev);
  1048. return NULL;
  1049. }
  1050. req->special = cmd;
  1051. } else {
  1052. cmd = req->special;
  1053. }
  1054. /* pull a tag out of the request if we have one */
  1055. cmd->tag = req->tag;
  1056. cmd->request = req;
  1057. cmd->cmnd = req->cmd;
  1058. cmd->prot_op = SCSI_PROT_NORMAL;
  1059. return cmd;
  1060. }
  1061. static int scsi_setup_blk_pc_cmnd(struct scsi_device *sdev, struct request *req)
  1062. {
  1063. struct scsi_cmnd *cmd = req->special;
  1064. /*
  1065. * BLOCK_PC requests may transfer data, in which case they must
  1066. * a bio attached to them. Or they might contain a SCSI command
  1067. * that does not transfer data, in which case they may optionally
  1068. * submit a request without an attached bio.
  1069. */
  1070. if (req->bio) {
  1071. int ret = scsi_init_io(cmd, GFP_ATOMIC);
  1072. if (unlikely(ret))
  1073. return ret;
  1074. } else {
  1075. BUG_ON(blk_rq_bytes(req));
  1076. memset(&cmd->sdb, 0, sizeof(cmd->sdb));
  1077. }
  1078. cmd->cmd_len = req->cmd_len;
  1079. cmd->transfersize = blk_rq_bytes(req);
  1080. cmd->allowed = req->retries;
  1081. return BLKPREP_OK;
  1082. }
  1083. /*
  1084. * Setup a REQ_TYPE_FS command. These are simple request from filesystems
  1085. * that still need to be translated to SCSI CDBs from the ULD.
  1086. */
  1087. static int scsi_setup_fs_cmnd(struct scsi_device *sdev, struct request *req)
  1088. {
  1089. struct scsi_cmnd *cmd = req->special;
  1090. if (unlikely(sdev->scsi_dh_data && sdev->scsi_dh_data->scsi_dh
  1091. && sdev->scsi_dh_data->scsi_dh->prep_fn)) {
  1092. int ret = sdev->scsi_dh_data->scsi_dh->prep_fn(sdev, req);
  1093. if (ret != BLKPREP_OK)
  1094. return ret;
  1095. }
  1096. memset(cmd->cmnd, 0, BLK_MAX_CDB);
  1097. return scsi_cmd_to_driver(cmd)->init_command(cmd);
  1098. }
  1099. static int scsi_setup_cmnd(struct scsi_device *sdev, struct request *req)
  1100. {
  1101. struct scsi_cmnd *cmd = req->special;
  1102. if (!blk_rq_bytes(req))
  1103. cmd->sc_data_direction = DMA_NONE;
  1104. else if (rq_data_dir(req) == WRITE)
  1105. cmd->sc_data_direction = DMA_TO_DEVICE;
  1106. else
  1107. cmd->sc_data_direction = DMA_FROM_DEVICE;
  1108. switch (req->cmd_type) {
  1109. case REQ_TYPE_FS:
  1110. return scsi_setup_fs_cmnd(sdev, req);
  1111. case REQ_TYPE_BLOCK_PC:
  1112. return scsi_setup_blk_pc_cmnd(sdev, req);
  1113. default:
  1114. return BLKPREP_KILL;
  1115. }
  1116. }
  1117. static int
  1118. scsi_prep_state_check(struct scsi_device *sdev, struct request *req)
  1119. {
  1120. int ret = BLKPREP_OK;
  1121. /*
  1122. * If the device is not in running state we will reject some
  1123. * or all commands.
  1124. */
  1125. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  1126. switch (sdev->sdev_state) {
  1127. case SDEV_OFFLINE:
  1128. case SDEV_TRANSPORT_OFFLINE:
  1129. /*
  1130. * If the device is offline we refuse to process any
  1131. * commands. The device must be brought online
  1132. * before trying any recovery commands.
  1133. */
  1134. sdev_printk(KERN_ERR, sdev,
  1135. "rejecting I/O to offline device\n");
  1136. ret = BLKPREP_KILL;
  1137. break;
  1138. case SDEV_DEL:
  1139. /*
  1140. * If the device is fully deleted, we refuse to
  1141. * process any commands as well.
  1142. */
  1143. sdev_printk(KERN_ERR, sdev,
  1144. "rejecting I/O to dead device\n");
  1145. ret = BLKPREP_KILL;
  1146. break;
  1147. case SDEV_BLOCK:
  1148. case SDEV_CREATED_BLOCK:
  1149. ret = BLKPREP_DEFER;
  1150. break;
  1151. case SDEV_QUIESCE:
  1152. /*
  1153. * If the devices is blocked we defer normal commands.
  1154. */
  1155. if (!(req->cmd_flags & REQ_PREEMPT))
  1156. ret = BLKPREP_DEFER;
  1157. break;
  1158. default:
  1159. /*
  1160. * For any other not fully online state we only allow
  1161. * special commands. In particular any user initiated
  1162. * command is not allowed.
  1163. */
  1164. if (!(req->cmd_flags & REQ_PREEMPT))
  1165. ret = BLKPREP_KILL;
  1166. break;
  1167. }
  1168. }
  1169. return ret;
  1170. }
  1171. static int
  1172. scsi_prep_return(struct request_queue *q, struct request *req, int ret)
  1173. {
  1174. struct scsi_device *sdev = q->queuedata;
  1175. switch (ret) {
  1176. case BLKPREP_KILL:
  1177. req->errors = DID_NO_CONNECT << 16;
  1178. /* release the command and kill it */
  1179. if (req->special) {
  1180. struct scsi_cmnd *cmd = req->special;
  1181. scsi_release_buffers(cmd);
  1182. scsi_put_command(cmd);
  1183. put_device(&sdev->sdev_gendev);
  1184. req->special = NULL;
  1185. }
  1186. break;
  1187. case BLKPREP_DEFER:
  1188. /*
  1189. * If we defer, the blk_peek_request() returns NULL, but the
  1190. * queue must be restarted, so we schedule a callback to happen
  1191. * shortly.
  1192. */
  1193. if (atomic_read(&sdev->device_busy) == 0)
  1194. blk_delay_queue(q, SCSI_QUEUE_DELAY);
  1195. break;
  1196. default:
  1197. req->cmd_flags |= REQ_DONTPREP;
  1198. }
  1199. return ret;
  1200. }
  1201. static int scsi_prep_fn(struct request_queue *q, struct request *req)
  1202. {
  1203. struct scsi_device *sdev = q->queuedata;
  1204. struct scsi_cmnd *cmd;
  1205. int ret;
  1206. ret = scsi_prep_state_check(sdev, req);
  1207. if (ret != BLKPREP_OK)
  1208. goto out;
  1209. cmd = scsi_get_cmd_from_req(sdev, req);
  1210. if (unlikely(!cmd)) {
  1211. ret = BLKPREP_DEFER;
  1212. goto out;
  1213. }
  1214. ret = scsi_setup_cmnd(sdev, req);
  1215. out:
  1216. return scsi_prep_return(q, req, ret);
  1217. }
  1218. static void scsi_unprep_fn(struct request_queue *q, struct request *req)
  1219. {
  1220. scsi_uninit_cmd(req->special);
  1221. }
  1222. /*
  1223. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1224. * return 0.
  1225. *
  1226. * Called with the queue_lock held.
  1227. */
  1228. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1229. struct scsi_device *sdev)
  1230. {
  1231. unsigned int busy;
  1232. busy = atomic_inc_return(&sdev->device_busy) - 1;
  1233. if (atomic_read(&sdev->device_blocked)) {
  1234. if (busy)
  1235. goto out_dec;
  1236. /*
  1237. * unblock after device_blocked iterates to zero
  1238. */
  1239. if (atomic_dec_return(&sdev->device_blocked) > 0) {
  1240. /*
  1241. * For the MQ case we take care of this in the caller.
  1242. */
  1243. if (!q->mq_ops)
  1244. blk_delay_queue(q, SCSI_QUEUE_DELAY);
  1245. goto out_dec;
  1246. }
  1247. SCSI_LOG_MLQUEUE(3, sdev_printk(KERN_INFO, sdev,
  1248. "unblocking device at zero depth\n"));
  1249. }
  1250. if (busy >= sdev->queue_depth)
  1251. goto out_dec;
  1252. return 1;
  1253. out_dec:
  1254. atomic_dec(&sdev->device_busy);
  1255. return 0;
  1256. }
  1257. /*
  1258. * scsi_target_queue_ready: checks if there we can send commands to target
  1259. * @sdev: scsi device on starget to check.
  1260. */
  1261. static inline int scsi_target_queue_ready(struct Scsi_Host *shost,
  1262. struct scsi_device *sdev)
  1263. {
  1264. struct scsi_target *starget = scsi_target(sdev);
  1265. unsigned int busy;
  1266. if (starget->single_lun) {
  1267. spin_lock_irq(shost->host_lock);
  1268. if (starget->starget_sdev_user &&
  1269. starget->starget_sdev_user != sdev) {
  1270. spin_unlock_irq(shost->host_lock);
  1271. return 0;
  1272. }
  1273. starget->starget_sdev_user = sdev;
  1274. spin_unlock_irq(shost->host_lock);
  1275. }
  1276. if (starget->can_queue <= 0)
  1277. return 1;
  1278. busy = atomic_inc_return(&starget->target_busy) - 1;
  1279. if (atomic_read(&starget->target_blocked) > 0) {
  1280. if (busy)
  1281. goto starved;
  1282. /*
  1283. * unblock after target_blocked iterates to zero
  1284. */
  1285. if (atomic_dec_return(&starget->target_blocked) > 0)
  1286. goto out_dec;
  1287. SCSI_LOG_MLQUEUE(3, starget_printk(KERN_INFO, starget,
  1288. "unblocking target at zero depth\n"));
  1289. }
  1290. if (busy >= starget->can_queue)
  1291. goto starved;
  1292. return 1;
  1293. starved:
  1294. spin_lock_irq(shost->host_lock);
  1295. list_move_tail(&sdev->starved_entry, &shost->starved_list);
  1296. spin_unlock_irq(shost->host_lock);
  1297. out_dec:
  1298. if (starget->can_queue > 0)
  1299. atomic_dec(&starget->target_busy);
  1300. return 0;
  1301. }
  1302. /*
  1303. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1304. * return 0. We must end up running the queue again whenever 0 is
  1305. * returned, else IO can hang.
  1306. */
  1307. static inline int scsi_host_queue_ready(struct request_queue *q,
  1308. struct Scsi_Host *shost,
  1309. struct scsi_device *sdev)
  1310. {
  1311. unsigned int busy;
  1312. if (scsi_host_in_recovery(shost))
  1313. return 0;
  1314. busy = atomic_inc_return(&shost->host_busy) - 1;
  1315. if (atomic_read(&shost->host_blocked) > 0) {
  1316. if (busy)
  1317. goto starved;
  1318. /*
  1319. * unblock after host_blocked iterates to zero
  1320. */
  1321. if (atomic_dec_return(&shost->host_blocked) > 0)
  1322. goto out_dec;
  1323. SCSI_LOG_MLQUEUE(3,
  1324. shost_printk(KERN_INFO, shost,
  1325. "unblocking host at zero depth\n"));
  1326. }
  1327. if (shost->can_queue > 0 && busy >= shost->can_queue)
  1328. goto starved;
  1329. if (shost->host_self_blocked)
  1330. goto starved;
  1331. /* We're OK to process the command, so we can't be starved */
  1332. if (!list_empty(&sdev->starved_entry)) {
  1333. spin_lock_irq(shost->host_lock);
  1334. if (!list_empty(&sdev->starved_entry))
  1335. list_del_init(&sdev->starved_entry);
  1336. spin_unlock_irq(shost->host_lock);
  1337. }
  1338. return 1;
  1339. starved:
  1340. spin_lock_irq(shost->host_lock);
  1341. if (list_empty(&sdev->starved_entry))
  1342. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1343. spin_unlock_irq(shost->host_lock);
  1344. out_dec:
  1345. atomic_dec(&shost->host_busy);
  1346. return 0;
  1347. }
  1348. /*
  1349. * Busy state exporting function for request stacking drivers.
  1350. *
  1351. * For efficiency, no lock is taken to check the busy state of
  1352. * shost/starget/sdev, since the returned value is not guaranteed and
  1353. * may be changed after request stacking drivers call the function,
  1354. * regardless of taking lock or not.
  1355. *
  1356. * When scsi can't dispatch I/Os anymore and needs to kill I/Os scsi
  1357. * needs to return 'not busy'. Otherwise, request stacking drivers
  1358. * may hold requests forever.
  1359. */
  1360. static int scsi_lld_busy(struct request_queue *q)
  1361. {
  1362. struct scsi_device *sdev = q->queuedata;
  1363. struct Scsi_Host *shost;
  1364. if (blk_queue_dying(q))
  1365. return 0;
  1366. shost = sdev->host;
  1367. /*
  1368. * Ignore host/starget busy state.
  1369. * Since block layer does not have a concept of fairness across
  1370. * multiple queues, congestion of host/starget needs to be handled
  1371. * in SCSI layer.
  1372. */
  1373. if (scsi_host_in_recovery(shost) || scsi_device_is_busy(sdev))
  1374. return 1;
  1375. return 0;
  1376. }
  1377. /*
  1378. * Kill a request for a dead device
  1379. */
  1380. static void scsi_kill_request(struct request *req, struct request_queue *q)
  1381. {
  1382. struct scsi_cmnd *cmd = req->special;
  1383. struct scsi_device *sdev;
  1384. struct scsi_target *starget;
  1385. struct Scsi_Host *shost;
  1386. blk_start_request(req);
  1387. scmd_printk(KERN_INFO, cmd, "killing request\n");
  1388. sdev = cmd->device;
  1389. starget = scsi_target(sdev);
  1390. shost = sdev->host;
  1391. scsi_init_cmd_errh(cmd);
  1392. cmd->result = DID_NO_CONNECT << 16;
  1393. atomic_inc(&cmd->device->iorequest_cnt);
  1394. /*
  1395. * SCSI request completion path will do scsi_device_unbusy(),
  1396. * bump busy counts. To bump the counters, we need to dance
  1397. * with the locks as normal issue path does.
  1398. */
  1399. atomic_inc(&sdev->device_busy);
  1400. atomic_inc(&shost->host_busy);
  1401. if (starget->can_queue > 0)
  1402. atomic_inc(&starget->target_busy);
  1403. blk_complete_request(req);
  1404. }
  1405. static void scsi_softirq_done(struct request *rq)
  1406. {
  1407. struct scsi_cmnd *cmd = rq->special;
  1408. unsigned long wait_for = (cmd->allowed + 1) * rq->timeout;
  1409. int disposition;
  1410. INIT_LIST_HEAD(&cmd->eh_entry);
  1411. atomic_inc(&cmd->device->iodone_cnt);
  1412. if (cmd->result)
  1413. atomic_inc(&cmd->device->ioerr_cnt);
  1414. disposition = scsi_decide_disposition(cmd);
  1415. if (disposition != SUCCESS &&
  1416. time_before(cmd->jiffies_at_alloc + wait_for, jiffies)) {
  1417. sdev_printk(KERN_ERR, cmd->device,
  1418. "timing out command, waited %lus\n",
  1419. wait_for/HZ);
  1420. disposition = SUCCESS;
  1421. }
  1422. scsi_log_completion(cmd, disposition);
  1423. switch (disposition) {
  1424. case SUCCESS:
  1425. scsi_finish_command(cmd);
  1426. break;
  1427. case NEEDS_RETRY:
  1428. scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
  1429. break;
  1430. case ADD_TO_MLQUEUE:
  1431. scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
  1432. break;
  1433. default:
  1434. if (!scsi_eh_scmd_add(cmd, 0))
  1435. scsi_finish_command(cmd);
  1436. }
  1437. }
  1438. /**
  1439. * scsi_done - Invoke completion on finished SCSI command.
  1440. * @cmd: The SCSI Command for which a low-level device driver (LLDD) gives
  1441. * ownership back to SCSI Core -- i.e. the LLDD has finished with it.
  1442. *
  1443. * Description: This function is the mid-level's (SCSI Core) interrupt routine,
  1444. * which regains ownership of the SCSI command (de facto) from a LLDD, and
  1445. * calls blk_complete_request() for further processing.
  1446. *
  1447. * This function is interrupt context safe.
  1448. */
  1449. static void scsi_done(struct scsi_cmnd *cmd)
  1450. {
  1451. trace_scsi_dispatch_cmd_done(cmd);
  1452. blk_complete_request(cmd->request);
  1453. }
  1454. /*
  1455. * Function: scsi_request_fn()
  1456. *
  1457. * Purpose: Main strategy routine for SCSI.
  1458. *
  1459. * Arguments: q - Pointer to actual queue.
  1460. *
  1461. * Returns: Nothing
  1462. *
  1463. * Lock status: IO request lock assumed to be held when called.
  1464. */
  1465. static void scsi_request_fn(struct request_queue *q)
  1466. __releases(q->queue_lock)
  1467. __acquires(q->queue_lock)
  1468. {
  1469. struct scsi_device *sdev = q->queuedata;
  1470. struct Scsi_Host *shost;
  1471. struct scsi_cmnd *cmd;
  1472. struct request *req;
  1473. /*
  1474. * To start with, we keep looping until the queue is empty, or until
  1475. * the host is no longer able to accept any more requests.
  1476. */
  1477. shost = sdev->host;
  1478. for (;;) {
  1479. int rtn;
  1480. /*
  1481. * get next queueable request. We do this early to make sure
  1482. * that the request is fully prepared even if we cannot
  1483. * accept it.
  1484. */
  1485. req = blk_peek_request(q);
  1486. if (!req)
  1487. break;
  1488. if (unlikely(!scsi_device_online(sdev))) {
  1489. sdev_printk(KERN_ERR, sdev,
  1490. "rejecting I/O to offline device\n");
  1491. scsi_kill_request(req, q);
  1492. continue;
  1493. }
  1494. if (!scsi_dev_queue_ready(q, sdev))
  1495. break;
  1496. /*
  1497. * Remove the request from the request list.
  1498. */
  1499. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1500. blk_start_request(req);
  1501. spin_unlock_irq(q->queue_lock);
  1502. cmd = req->special;
  1503. if (unlikely(cmd == NULL)) {
  1504. printk(KERN_CRIT "impossible request in %s.\n"
  1505. "please mail a stack trace to "
  1506. "linux-scsi@vger.kernel.org\n",
  1507. __func__);
  1508. blk_dump_rq_flags(req, "foo");
  1509. BUG();
  1510. }
  1511. /*
  1512. * We hit this when the driver is using a host wide
  1513. * tag map. For device level tag maps the queue_depth check
  1514. * in the device ready fn would prevent us from trying
  1515. * to allocate a tag. Since the map is a shared host resource
  1516. * we add the dev to the starved list so it eventually gets
  1517. * a run when a tag is freed.
  1518. */
  1519. if (blk_queue_tagged(q) && !blk_rq_tagged(req)) {
  1520. spin_lock_irq(shost->host_lock);
  1521. if (list_empty(&sdev->starved_entry))
  1522. list_add_tail(&sdev->starved_entry,
  1523. &shost->starved_list);
  1524. spin_unlock_irq(shost->host_lock);
  1525. goto not_ready;
  1526. }
  1527. if (!scsi_target_queue_ready(shost, sdev))
  1528. goto not_ready;
  1529. if (!scsi_host_queue_ready(q, shost, sdev))
  1530. goto host_not_ready;
  1531. /*
  1532. * Finally, initialize any error handling parameters, and set up
  1533. * the timers for timeouts.
  1534. */
  1535. scsi_init_cmd_errh(cmd);
  1536. /*
  1537. * Dispatch the command to the low-level driver.
  1538. */
  1539. cmd->scsi_done = scsi_done;
  1540. rtn = scsi_dispatch_cmd(cmd);
  1541. if (rtn) {
  1542. scsi_queue_insert(cmd, rtn);
  1543. spin_lock_irq(q->queue_lock);
  1544. goto out_delay;
  1545. }
  1546. spin_lock_irq(q->queue_lock);
  1547. }
  1548. return;
  1549. host_not_ready:
  1550. if (scsi_target(sdev)->can_queue > 0)
  1551. atomic_dec(&scsi_target(sdev)->target_busy);
  1552. not_ready:
  1553. /*
  1554. * lock q, handle tag, requeue req, and decrement device_busy. We
  1555. * must return with queue_lock held.
  1556. *
  1557. * Decrementing device_busy without checking it is OK, as all such
  1558. * cases (host limits or settings) should run the queue at some
  1559. * later time.
  1560. */
  1561. spin_lock_irq(q->queue_lock);
  1562. blk_requeue_request(q, req);
  1563. atomic_dec(&sdev->device_busy);
  1564. out_delay:
  1565. if (!atomic_read(&sdev->device_busy) && !scsi_device_blocked(sdev))
  1566. blk_delay_queue(q, SCSI_QUEUE_DELAY);
  1567. }
  1568. static inline int prep_to_mq(int ret)
  1569. {
  1570. switch (ret) {
  1571. case BLKPREP_OK:
  1572. return 0;
  1573. case BLKPREP_DEFER:
  1574. return BLK_MQ_RQ_QUEUE_BUSY;
  1575. default:
  1576. return BLK_MQ_RQ_QUEUE_ERROR;
  1577. }
  1578. }
  1579. static int scsi_mq_prep_fn(struct request *req)
  1580. {
  1581. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1582. struct scsi_device *sdev = req->q->queuedata;
  1583. struct Scsi_Host *shost = sdev->host;
  1584. unsigned char *sense_buf = cmd->sense_buffer;
  1585. struct scatterlist *sg;
  1586. memset(cmd, 0, sizeof(struct scsi_cmnd));
  1587. req->special = cmd;
  1588. cmd->request = req;
  1589. cmd->device = sdev;
  1590. cmd->sense_buffer = sense_buf;
  1591. cmd->tag = req->tag;
  1592. cmd->cmnd = req->cmd;
  1593. cmd->prot_op = SCSI_PROT_NORMAL;
  1594. INIT_LIST_HEAD(&cmd->list);
  1595. INIT_DELAYED_WORK(&cmd->abort_work, scmd_eh_abort_handler);
  1596. cmd->jiffies_at_alloc = jiffies;
  1597. if (shost->use_cmd_list) {
  1598. spin_lock_irq(&sdev->list_lock);
  1599. list_add_tail(&cmd->list, &sdev->cmd_list);
  1600. spin_unlock_irq(&sdev->list_lock);
  1601. }
  1602. sg = (void *)cmd + sizeof(struct scsi_cmnd) + shost->hostt->cmd_size;
  1603. cmd->sdb.table.sgl = sg;
  1604. if (scsi_host_get_prot(shost)) {
  1605. cmd->prot_sdb = (void *)sg +
  1606. min_t(unsigned int,
  1607. shost->sg_tablesize, SCSI_MAX_SG_SEGMENTS) *
  1608. sizeof(struct scatterlist);
  1609. memset(cmd->prot_sdb, 0, sizeof(struct scsi_data_buffer));
  1610. cmd->prot_sdb->table.sgl =
  1611. (struct scatterlist *)(cmd->prot_sdb + 1);
  1612. }
  1613. if (blk_bidi_rq(req)) {
  1614. struct request *next_rq = req->next_rq;
  1615. struct scsi_data_buffer *bidi_sdb = blk_mq_rq_to_pdu(next_rq);
  1616. memset(bidi_sdb, 0, sizeof(struct scsi_data_buffer));
  1617. bidi_sdb->table.sgl =
  1618. (struct scatterlist *)(bidi_sdb + 1);
  1619. next_rq->special = bidi_sdb;
  1620. }
  1621. blk_mq_start_request(req);
  1622. return scsi_setup_cmnd(sdev, req);
  1623. }
  1624. static void scsi_mq_done(struct scsi_cmnd *cmd)
  1625. {
  1626. trace_scsi_dispatch_cmd_done(cmd);
  1627. blk_mq_complete_request(cmd->request);
  1628. }
  1629. static int scsi_queue_rq(struct blk_mq_hw_ctx *hctx, struct request *req,
  1630. bool last)
  1631. {
  1632. struct request_queue *q = req->q;
  1633. struct scsi_device *sdev = q->queuedata;
  1634. struct Scsi_Host *shost = sdev->host;
  1635. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(req);
  1636. int ret;
  1637. int reason;
  1638. ret = prep_to_mq(scsi_prep_state_check(sdev, req));
  1639. if (ret)
  1640. goto out;
  1641. ret = BLK_MQ_RQ_QUEUE_BUSY;
  1642. if (!get_device(&sdev->sdev_gendev))
  1643. goto out;
  1644. if (!scsi_dev_queue_ready(q, sdev))
  1645. goto out_put_device;
  1646. if (!scsi_target_queue_ready(shost, sdev))
  1647. goto out_dec_device_busy;
  1648. if (!scsi_host_queue_ready(q, shost, sdev))
  1649. goto out_dec_target_busy;
  1650. if (!(req->cmd_flags & REQ_DONTPREP)) {
  1651. ret = prep_to_mq(scsi_mq_prep_fn(req));
  1652. if (ret)
  1653. goto out_dec_host_busy;
  1654. req->cmd_flags |= REQ_DONTPREP;
  1655. } else {
  1656. blk_mq_start_request(req);
  1657. }
  1658. if (blk_queue_tagged(q))
  1659. req->cmd_flags |= REQ_QUEUED;
  1660. else
  1661. req->cmd_flags &= ~REQ_QUEUED;
  1662. scsi_init_cmd_errh(cmd);
  1663. cmd->scsi_done = scsi_mq_done;
  1664. reason = scsi_dispatch_cmd(cmd);
  1665. if (reason) {
  1666. scsi_set_blocked(cmd, reason);
  1667. ret = BLK_MQ_RQ_QUEUE_BUSY;
  1668. goto out_dec_host_busy;
  1669. }
  1670. return BLK_MQ_RQ_QUEUE_OK;
  1671. out_dec_host_busy:
  1672. atomic_dec(&shost->host_busy);
  1673. out_dec_target_busy:
  1674. if (scsi_target(sdev)->can_queue > 0)
  1675. atomic_dec(&scsi_target(sdev)->target_busy);
  1676. out_dec_device_busy:
  1677. atomic_dec(&sdev->device_busy);
  1678. out_put_device:
  1679. put_device(&sdev->sdev_gendev);
  1680. out:
  1681. switch (ret) {
  1682. case BLK_MQ_RQ_QUEUE_BUSY:
  1683. blk_mq_stop_hw_queue(hctx);
  1684. if (atomic_read(&sdev->device_busy) == 0 &&
  1685. !scsi_device_blocked(sdev))
  1686. blk_mq_delay_queue(hctx, SCSI_QUEUE_DELAY);
  1687. break;
  1688. case BLK_MQ_RQ_QUEUE_ERROR:
  1689. /*
  1690. * Make sure to release all allocated ressources when
  1691. * we hit an error, as we will never see this command
  1692. * again.
  1693. */
  1694. if (req->cmd_flags & REQ_DONTPREP)
  1695. scsi_mq_uninit_cmd(cmd);
  1696. break;
  1697. default:
  1698. break;
  1699. }
  1700. return ret;
  1701. }
  1702. static enum blk_eh_timer_return scsi_timeout(struct request *req,
  1703. bool reserved)
  1704. {
  1705. if (reserved)
  1706. return BLK_EH_RESET_TIMER;
  1707. return scsi_times_out(req);
  1708. }
  1709. static int scsi_init_request(void *data, struct request *rq,
  1710. unsigned int hctx_idx, unsigned int request_idx,
  1711. unsigned int numa_node)
  1712. {
  1713. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1714. cmd->sense_buffer = kzalloc_node(SCSI_SENSE_BUFFERSIZE, GFP_KERNEL,
  1715. numa_node);
  1716. if (!cmd->sense_buffer)
  1717. return -ENOMEM;
  1718. return 0;
  1719. }
  1720. static void scsi_exit_request(void *data, struct request *rq,
  1721. unsigned int hctx_idx, unsigned int request_idx)
  1722. {
  1723. struct scsi_cmnd *cmd = blk_mq_rq_to_pdu(rq);
  1724. kfree(cmd->sense_buffer);
  1725. }
  1726. static u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1727. {
  1728. struct device *host_dev;
  1729. u64 bounce_limit = 0xffffffff;
  1730. if (shost->unchecked_isa_dma)
  1731. return BLK_BOUNCE_ISA;
  1732. /*
  1733. * Platforms with virtual-DMA translation
  1734. * hardware have no practical limit.
  1735. */
  1736. if (!PCI_DMA_BUS_IS_PHYS)
  1737. return BLK_BOUNCE_ANY;
  1738. host_dev = scsi_get_device(shost);
  1739. if (host_dev && host_dev->dma_mask)
  1740. bounce_limit = (u64)dma_max_pfn(host_dev) << PAGE_SHIFT;
  1741. return bounce_limit;
  1742. }
  1743. static void __scsi_init_queue(struct Scsi_Host *shost, struct request_queue *q)
  1744. {
  1745. struct device *dev = shost->dma_dev;
  1746. /*
  1747. * this limit is imposed by hardware restrictions
  1748. */
  1749. blk_queue_max_segments(q, min_t(unsigned short, shost->sg_tablesize,
  1750. SCSI_MAX_SG_CHAIN_SEGMENTS));
  1751. if (scsi_host_prot_dma(shost)) {
  1752. shost->sg_prot_tablesize =
  1753. min_not_zero(shost->sg_prot_tablesize,
  1754. (unsigned short)SCSI_MAX_PROT_SG_SEGMENTS);
  1755. BUG_ON(shost->sg_prot_tablesize < shost->sg_tablesize);
  1756. blk_queue_max_integrity_segments(q, shost->sg_prot_tablesize);
  1757. }
  1758. blk_queue_max_hw_sectors(q, shost->max_sectors);
  1759. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1760. blk_queue_segment_boundary(q, shost->dma_boundary);
  1761. dma_set_seg_boundary(dev, shost->dma_boundary);
  1762. blk_queue_max_segment_size(q, dma_get_max_seg_size(dev));
  1763. if (!shost->use_clustering)
  1764. q->limits.cluster = 0;
  1765. /*
  1766. * set a reasonable default alignment on word boundaries: the
  1767. * host and device may alter it using
  1768. * blk_queue_update_dma_alignment() later.
  1769. */
  1770. blk_queue_dma_alignment(q, 0x03);
  1771. }
  1772. struct request_queue *__scsi_alloc_queue(struct Scsi_Host *shost,
  1773. request_fn_proc *request_fn)
  1774. {
  1775. struct request_queue *q;
  1776. q = blk_init_queue(request_fn, NULL);
  1777. if (!q)
  1778. return NULL;
  1779. __scsi_init_queue(shost, q);
  1780. return q;
  1781. }
  1782. EXPORT_SYMBOL(__scsi_alloc_queue);
  1783. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1784. {
  1785. struct request_queue *q;
  1786. q = __scsi_alloc_queue(sdev->host, scsi_request_fn);
  1787. if (!q)
  1788. return NULL;
  1789. blk_queue_prep_rq(q, scsi_prep_fn);
  1790. blk_queue_unprep_rq(q, scsi_unprep_fn);
  1791. blk_queue_softirq_done(q, scsi_softirq_done);
  1792. blk_queue_rq_timed_out(q, scsi_times_out);
  1793. blk_queue_lld_busy(q, scsi_lld_busy);
  1794. return q;
  1795. }
  1796. static struct blk_mq_ops scsi_mq_ops = {
  1797. .map_queue = blk_mq_map_queue,
  1798. .queue_rq = scsi_queue_rq,
  1799. .complete = scsi_softirq_done,
  1800. .timeout = scsi_timeout,
  1801. .init_request = scsi_init_request,
  1802. .exit_request = scsi_exit_request,
  1803. };
  1804. struct request_queue *scsi_mq_alloc_queue(struct scsi_device *sdev)
  1805. {
  1806. sdev->request_queue = blk_mq_init_queue(&sdev->host->tag_set);
  1807. if (IS_ERR(sdev->request_queue))
  1808. return NULL;
  1809. sdev->request_queue->queuedata = sdev;
  1810. __scsi_init_queue(sdev->host, sdev->request_queue);
  1811. return sdev->request_queue;
  1812. }
  1813. int scsi_mq_setup_tags(struct Scsi_Host *shost)
  1814. {
  1815. unsigned int cmd_size, sgl_size, tbl_size;
  1816. tbl_size = shost->sg_tablesize;
  1817. if (tbl_size > SCSI_MAX_SG_SEGMENTS)
  1818. tbl_size = SCSI_MAX_SG_SEGMENTS;
  1819. sgl_size = tbl_size * sizeof(struct scatterlist);
  1820. cmd_size = sizeof(struct scsi_cmnd) + shost->hostt->cmd_size + sgl_size;
  1821. if (scsi_host_get_prot(shost))
  1822. cmd_size += sizeof(struct scsi_data_buffer) + sgl_size;
  1823. memset(&shost->tag_set, 0, sizeof(shost->tag_set));
  1824. shost->tag_set.ops = &scsi_mq_ops;
  1825. shost->tag_set.nr_hw_queues = 1;
  1826. shost->tag_set.queue_depth = shost->can_queue;
  1827. shost->tag_set.cmd_size = cmd_size;
  1828. shost->tag_set.numa_node = NUMA_NO_NODE;
  1829. shost->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_SG_MERGE;
  1830. shost->tag_set.driver_data = shost;
  1831. return blk_mq_alloc_tag_set(&shost->tag_set);
  1832. }
  1833. void scsi_mq_destroy_tags(struct Scsi_Host *shost)
  1834. {
  1835. blk_mq_free_tag_set(&shost->tag_set);
  1836. }
  1837. /*
  1838. * Function: scsi_block_requests()
  1839. *
  1840. * Purpose: Utility function used by low-level drivers to prevent further
  1841. * commands from being queued to the device.
  1842. *
  1843. * Arguments: shost - Host in question
  1844. *
  1845. * Returns: Nothing
  1846. *
  1847. * Lock status: No locks are assumed held.
  1848. *
  1849. * Notes: There is no timer nor any other means by which the requests
  1850. * get unblocked other than the low-level driver calling
  1851. * scsi_unblock_requests().
  1852. */
  1853. void scsi_block_requests(struct Scsi_Host *shost)
  1854. {
  1855. shost->host_self_blocked = 1;
  1856. }
  1857. EXPORT_SYMBOL(scsi_block_requests);
  1858. /*
  1859. * Function: scsi_unblock_requests()
  1860. *
  1861. * Purpose: Utility function used by low-level drivers to allow further
  1862. * commands from being queued to the device.
  1863. *
  1864. * Arguments: shost - Host in question
  1865. *
  1866. * Returns: Nothing
  1867. *
  1868. * Lock status: No locks are assumed held.
  1869. *
  1870. * Notes: There is no timer nor any other means by which the requests
  1871. * get unblocked other than the low-level driver calling
  1872. * scsi_unblock_requests().
  1873. *
  1874. * This is done as an API function so that changes to the
  1875. * internals of the scsi mid-layer won't require wholesale
  1876. * changes to drivers that use this feature.
  1877. */
  1878. void scsi_unblock_requests(struct Scsi_Host *shost)
  1879. {
  1880. shost->host_self_blocked = 0;
  1881. scsi_run_host_queues(shost);
  1882. }
  1883. EXPORT_SYMBOL(scsi_unblock_requests);
  1884. int __init scsi_init_queue(void)
  1885. {
  1886. int i;
  1887. scsi_sdb_cache = kmem_cache_create("scsi_data_buffer",
  1888. sizeof(struct scsi_data_buffer),
  1889. 0, 0, NULL);
  1890. if (!scsi_sdb_cache) {
  1891. printk(KERN_ERR "SCSI: can't init scsi sdb cache\n");
  1892. return -ENOMEM;
  1893. }
  1894. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1895. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1896. int size = sgp->size * sizeof(struct scatterlist);
  1897. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1898. SLAB_HWCACHE_ALIGN, NULL);
  1899. if (!sgp->slab) {
  1900. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1901. sgp->name);
  1902. goto cleanup_sdb;
  1903. }
  1904. sgp->pool = mempool_create_slab_pool(SG_MEMPOOL_SIZE,
  1905. sgp->slab);
  1906. if (!sgp->pool) {
  1907. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1908. sgp->name);
  1909. goto cleanup_sdb;
  1910. }
  1911. }
  1912. return 0;
  1913. cleanup_sdb:
  1914. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1915. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1916. if (sgp->pool)
  1917. mempool_destroy(sgp->pool);
  1918. if (sgp->slab)
  1919. kmem_cache_destroy(sgp->slab);
  1920. }
  1921. kmem_cache_destroy(scsi_sdb_cache);
  1922. return -ENOMEM;
  1923. }
  1924. void scsi_exit_queue(void)
  1925. {
  1926. int i;
  1927. kmem_cache_destroy(scsi_sdb_cache);
  1928. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1929. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1930. mempool_destroy(sgp->pool);
  1931. kmem_cache_destroy(sgp->slab);
  1932. }
  1933. }
  1934. /**
  1935. * scsi_mode_select - issue a mode select
  1936. * @sdev: SCSI device to be queried
  1937. * @pf: Page format bit (1 == standard, 0 == vendor specific)
  1938. * @sp: Save page bit (0 == don't save, 1 == save)
  1939. * @modepage: mode page being requested
  1940. * @buffer: request buffer (may not be smaller than eight bytes)
  1941. * @len: length of request buffer.
  1942. * @timeout: command timeout
  1943. * @retries: number of retries before failing
  1944. * @data: returns a structure abstracting the mode header data
  1945. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  1946. * must be SCSI_SENSE_BUFFERSIZE big.
  1947. *
  1948. * Returns zero if successful; negative error number or scsi
  1949. * status on error
  1950. *
  1951. */
  1952. int
  1953. scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
  1954. unsigned char *buffer, int len, int timeout, int retries,
  1955. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  1956. {
  1957. unsigned char cmd[10];
  1958. unsigned char *real_buffer;
  1959. int ret;
  1960. memset(cmd, 0, sizeof(cmd));
  1961. cmd[1] = (pf ? 0x10 : 0) | (sp ? 0x01 : 0);
  1962. if (sdev->use_10_for_ms) {
  1963. if (len > 65535)
  1964. return -EINVAL;
  1965. real_buffer = kmalloc(8 + len, GFP_KERNEL);
  1966. if (!real_buffer)
  1967. return -ENOMEM;
  1968. memcpy(real_buffer + 8, buffer, len);
  1969. len += 8;
  1970. real_buffer[0] = 0;
  1971. real_buffer[1] = 0;
  1972. real_buffer[2] = data->medium_type;
  1973. real_buffer[3] = data->device_specific;
  1974. real_buffer[4] = data->longlba ? 0x01 : 0;
  1975. real_buffer[5] = 0;
  1976. real_buffer[6] = data->block_descriptor_length >> 8;
  1977. real_buffer[7] = data->block_descriptor_length;
  1978. cmd[0] = MODE_SELECT_10;
  1979. cmd[7] = len >> 8;
  1980. cmd[8] = len;
  1981. } else {
  1982. if (len > 255 || data->block_descriptor_length > 255 ||
  1983. data->longlba)
  1984. return -EINVAL;
  1985. real_buffer = kmalloc(4 + len, GFP_KERNEL);
  1986. if (!real_buffer)
  1987. return -ENOMEM;
  1988. memcpy(real_buffer + 4, buffer, len);
  1989. len += 4;
  1990. real_buffer[0] = 0;
  1991. real_buffer[1] = data->medium_type;
  1992. real_buffer[2] = data->device_specific;
  1993. real_buffer[3] = data->block_descriptor_length;
  1994. cmd[0] = MODE_SELECT;
  1995. cmd[4] = len;
  1996. }
  1997. ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
  1998. sshdr, timeout, retries, NULL);
  1999. kfree(real_buffer);
  2000. return ret;
  2001. }
  2002. EXPORT_SYMBOL_GPL(scsi_mode_select);
  2003. /**
  2004. * scsi_mode_sense - issue a mode sense, falling back from 10 to six bytes if necessary.
  2005. * @sdev: SCSI device to be queried
  2006. * @dbd: set if mode sense will allow block descriptors to be returned
  2007. * @modepage: mode page being requested
  2008. * @buffer: request buffer (may not be smaller than eight bytes)
  2009. * @len: length of request buffer.
  2010. * @timeout: command timeout
  2011. * @retries: number of retries before failing
  2012. * @data: returns a structure abstracting the mode header data
  2013. * @sshdr: place to put sense data (or NULL if no sense to be collected).
  2014. * must be SCSI_SENSE_BUFFERSIZE big.
  2015. *
  2016. * Returns zero if unsuccessful, or the header offset (either 4
  2017. * or 8 depending on whether a six or ten byte command was
  2018. * issued) if successful.
  2019. */
  2020. int
  2021. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  2022. unsigned char *buffer, int len, int timeout, int retries,
  2023. struct scsi_mode_data *data, struct scsi_sense_hdr *sshdr)
  2024. {
  2025. unsigned char cmd[12];
  2026. int use_10_for_ms;
  2027. int header_length;
  2028. int result;
  2029. struct scsi_sense_hdr my_sshdr;
  2030. memset(data, 0, sizeof(*data));
  2031. memset(&cmd[0], 0, 12);
  2032. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  2033. cmd[2] = modepage;
  2034. /* caller might not be interested in sense, but we need it */
  2035. if (!sshdr)
  2036. sshdr = &my_sshdr;
  2037. retry:
  2038. use_10_for_ms = sdev->use_10_for_ms;
  2039. if (use_10_for_ms) {
  2040. if (len < 8)
  2041. len = 8;
  2042. cmd[0] = MODE_SENSE_10;
  2043. cmd[8] = len;
  2044. header_length = 8;
  2045. } else {
  2046. if (len < 4)
  2047. len = 4;
  2048. cmd[0] = MODE_SENSE;
  2049. cmd[4] = len;
  2050. header_length = 4;
  2051. }
  2052. memset(buffer, 0, len);
  2053. result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
  2054. sshdr, timeout, retries, NULL);
  2055. /* This code looks awful: what it's doing is making sure an
  2056. * ILLEGAL REQUEST sense return identifies the actual command
  2057. * byte as the problem. MODE_SENSE commands can return
  2058. * ILLEGAL REQUEST if the code page isn't supported */
  2059. if (use_10_for_ms && !scsi_status_is_good(result) &&
  2060. (driver_byte(result) & DRIVER_SENSE)) {
  2061. if (scsi_sense_valid(sshdr)) {
  2062. if ((sshdr->sense_key == ILLEGAL_REQUEST) &&
  2063. (sshdr->asc == 0x20) && (sshdr->ascq == 0)) {
  2064. /*
  2065. * Invalid command operation code
  2066. */
  2067. sdev->use_10_for_ms = 0;
  2068. goto retry;
  2069. }
  2070. }
  2071. }
  2072. if(scsi_status_is_good(result)) {
  2073. if (unlikely(buffer[0] == 0x86 && buffer[1] == 0x0b &&
  2074. (modepage == 6 || modepage == 8))) {
  2075. /* Initio breakage? */
  2076. header_length = 0;
  2077. data->length = 13;
  2078. data->medium_type = 0;
  2079. data->device_specific = 0;
  2080. data->longlba = 0;
  2081. data->block_descriptor_length = 0;
  2082. } else if(use_10_for_ms) {
  2083. data->length = buffer[0]*256 + buffer[1] + 2;
  2084. data->medium_type = buffer[2];
  2085. data->device_specific = buffer[3];
  2086. data->longlba = buffer[4] & 0x01;
  2087. data->block_descriptor_length = buffer[6]*256
  2088. + buffer[7];
  2089. } else {
  2090. data->length = buffer[0] + 1;
  2091. data->medium_type = buffer[1];
  2092. data->device_specific = buffer[2];
  2093. data->block_descriptor_length = buffer[3];
  2094. }
  2095. data->header_length = header_length;
  2096. }
  2097. return result;
  2098. }
  2099. EXPORT_SYMBOL(scsi_mode_sense);
  2100. /**
  2101. * scsi_test_unit_ready - test if unit is ready
  2102. * @sdev: scsi device to change the state of.
  2103. * @timeout: command timeout
  2104. * @retries: number of retries before failing
  2105. * @sshdr_external: Optional pointer to struct scsi_sense_hdr for
  2106. * returning sense. Make sure that this is cleared before passing
  2107. * in.
  2108. *
  2109. * Returns zero if unsuccessful or an error if TUR failed. For
  2110. * removable media, UNIT_ATTENTION sets ->changed flag.
  2111. **/
  2112. int
  2113. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
  2114. struct scsi_sense_hdr *sshdr_external)
  2115. {
  2116. char cmd[] = {
  2117. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  2118. };
  2119. struct scsi_sense_hdr *sshdr;
  2120. int result;
  2121. if (!sshdr_external)
  2122. sshdr = kzalloc(sizeof(*sshdr), GFP_KERNEL);
  2123. else
  2124. sshdr = sshdr_external;
  2125. /* try to eat the UNIT_ATTENTION if there are enough retries */
  2126. do {
  2127. result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
  2128. timeout, retries, NULL);
  2129. if (sdev->removable && scsi_sense_valid(sshdr) &&
  2130. sshdr->sense_key == UNIT_ATTENTION)
  2131. sdev->changed = 1;
  2132. } while (scsi_sense_valid(sshdr) &&
  2133. sshdr->sense_key == UNIT_ATTENTION && --retries);
  2134. if (!sshdr_external)
  2135. kfree(sshdr);
  2136. return result;
  2137. }
  2138. EXPORT_SYMBOL(scsi_test_unit_ready);
  2139. /**
  2140. * scsi_device_set_state - Take the given device through the device state model.
  2141. * @sdev: scsi device to change the state of.
  2142. * @state: state to change to.
  2143. *
  2144. * Returns zero if unsuccessful or an error if the requested
  2145. * transition is illegal.
  2146. */
  2147. int
  2148. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  2149. {
  2150. enum scsi_device_state oldstate = sdev->sdev_state;
  2151. if (state == oldstate)
  2152. return 0;
  2153. switch (state) {
  2154. case SDEV_CREATED:
  2155. switch (oldstate) {
  2156. case SDEV_CREATED_BLOCK:
  2157. break;
  2158. default:
  2159. goto illegal;
  2160. }
  2161. break;
  2162. case SDEV_RUNNING:
  2163. switch (oldstate) {
  2164. case SDEV_CREATED:
  2165. case SDEV_OFFLINE:
  2166. case SDEV_TRANSPORT_OFFLINE:
  2167. case SDEV_QUIESCE:
  2168. case SDEV_BLOCK:
  2169. break;
  2170. default:
  2171. goto illegal;
  2172. }
  2173. break;
  2174. case SDEV_QUIESCE:
  2175. switch (oldstate) {
  2176. case SDEV_RUNNING:
  2177. case SDEV_OFFLINE:
  2178. case SDEV_TRANSPORT_OFFLINE:
  2179. break;
  2180. default:
  2181. goto illegal;
  2182. }
  2183. break;
  2184. case SDEV_OFFLINE:
  2185. case SDEV_TRANSPORT_OFFLINE:
  2186. switch (oldstate) {
  2187. case SDEV_CREATED:
  2188. case SDEV_RUNNING:
  2189. case SDEV_QUIESCE:
  2190. case SDEV_BLOCK:
  2191. break;
  2192. default:
  2193. goto illegal;
  2194. }
  2195. break;
  2196. case SDEV_BLOCK:
  2197. switch (oldstate) {
  2198. case SDEV_RUNNING:
  2199. case SDEV_CREATED_BLOCK:
  2200. break;
  2201. default:
  2202. goto illegal;
  2203. }
  2204. break;
  2205. case SDEV_CREATED_BLOCK:
  2206. switch (oldstate) {
  2207. case SDEV_CREATED:
  2208. break;
  2209. default:
  2210. goto illegal;
  2211. }
  2212. break;
  2213. case SDEV_CANCEL:
  2214. switch (oldstate) {
  2215. case SDEV_CREATED:
  2216. case SDEV_RUNNING:
  2217. case SDEV_QUIESCE:
  2218. case SDEV_OFFLINE:
  2219. case SDEV_TRANSPORT_OFFLINE:
  2220. case SDEV_BLOCK:
  2221. break;
  2222. default:
  2223. goto illegal;
  2224. }
  2225. break;
  2226. case SDEV_DEL:
  2227. switch (oldstate) {
  2228. case SDEV_CREATED:
  2229. case SDEV_RUNNING:
  2230. case SDEV_OFFLINE:
  2231. case SDEV_TRANSPORT_OFFLINE:
  2232. case SDEV_CANCEL:
  2233. case SDEV_CREATED_BLOCK:
  2234. break;
  2235. default:
  2236. goto illegal;
  2237. }
  2238. break;
  2239. }
  2240. sdev->sdev_state = state;
  2241. return 0;
  2242. illegal:
  2243. SCSI_LOG_ERROR_RECOVERY(1,
  2244. sdev_printk(KERN_ERR, sdev,
  2245. "Illegal state transition %s->%s",
  2246. scsi_device_state_name(oldstate),
  2247. scsi_device_state_name(state))
  2248. );
  2249. return -EINVAL;
  2250. }
  2251. EXPORT_SYMBOL(scsi_device_set_state);
  2252. /**
  2253. * sdev_evt_emit - emit a single SCSI device uevent
  2254. * @sdev: associated SCSI device
  2255. * @evt: event to emit
  2256. *
  2257. * Send a single uevent (scsi_event) to the associated scsi_device.
  2258. */
  2259. static void scsi_evt_emit(struct scsi_device *sdev, struct scsi_event *evt)
  2260. {
  2261. int idx = 0;
  2262. char *envp[3];
  2263. switch (evt->evt_type) {
  2264. case SDEV_EVT_MEDIA_CHANGE:
  2265. envp[idx++] = "SDEV_MEDIA_CHANGE=1";
  2266. break;
  2267. case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
  2268. envp[idx++] = "SDEV_UA=INQUIRY_DATA_HAS_CHANGED";
  2269. break;
  2270. case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
  2271. envp[idx++] = "SDEV_UA=CAPACITY_DATA_HAS_CHANGED";
  2272. break;
  2273. case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
  2274. envp[idx++] = "SDEV_UA=THIN_PROVISIONING_SOFT_THRESHOLD_REACHED";
  2275. break;
  2276. case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
  2277. envp[idx++] = "SDEV_UA=MODE_PARAMETERS_CHANGED";
  2278. break;
  2279. case SDEV_EVT_LUN_CHANGE_REPORTED:
  2280. envp[idx++] = "SDEV_UA=REPORTED_LUNS_DATA_HAS_CHANGED";
  2281. break;
  2282. default:
  2283. /* do nothing */
  2284. break;
  2285. }
  2286. envp[idx++] = NULL;
  2287. kobject_uevent_env(&sdev->sdev_gendev.kobj, KOBJ_CHANGE, envp);
  2288. }
  2289. /**
  2290. * sdev_evt_thread - send a uevent for each scsi event
  2291. * @work: work struct for scsi_device
  2292. *
  2293. * Dispatch queued events to their associated scsi_device kobjects
  2294. * as uevents.
  2295. */
  2296. void scsi_evt_thread(struct work_struct *work)
  2297. {
  2298. struct scsi_device *sdev;
  2299. enum scsi_device_event evt_type;
  2300. LIST_HEAD(event_list);
  2301. sdev = container_of(work, struct scsi_device, event_work);
  2302. for (evt_type = SDEV_EVT_FIRST; evt_type <= SDEV_EVT_LAST; evt_type++)
  2303. if (test_and_clear_bit(evt_type, sdev->pending_events))
  2304. sdev_evt_send_simple(sdev, evt_type, GFP_KERNEL);
  2305. while (1) {
  2306. struct scsi_event *evt;
  2307. struct list_head *this, *tmp;
  2308. unsigned long flags;
  2309. spin_lock_irqsave(&sdev->list_lock, flags);
  2310. list_splice_init(&sdev->event_list, &event_list);
  2311. spin_unlock_irqrestore(&sdev->list_lock, flags);
  2312. if (list_empty(&event_list))
  2313. break;
  2314. list_for_each_safe(this, tmp, &event_list) {
  2315. evt = list_entry(this, struct scsi_event, node);
  2316. list_del(&evt->node);
  2317. scsi_evt_emit(sdev, evt);
  2318. kfree(evt);
  2319. }
  2320. }
  2321. }
  2322. /**
  2323. * sdev_evt_send - send asserted event to uevent thread
  2324. * @sdev: scsi_device event occurred on
  2325. * @evt: event to send
  2326. *
  2327. * Assert scsi device event asynchronously.
  2328. */
  2329. void sdev_evt_send(struct scsi_device *sdev, struct scsi_event *evt)
  2330. {
  2331. unsigned long flags;
  2332. #if 0
  2333. /* FIXME: currently this check eliminates all media change events
  2334. * for polled devices. Need to update to discriminate between AN
  2335. * and polled events */
  2336. if (!test_bit(evt->evt_type, sdev->supported_events)) {
  2337. kfree(evt);
  2338. return;
  2339. }
  2340. #endif
  2341. spin_lock_irqsave(&sdev->list_lock, flags);
  2342. list_add_tail(&evt->node, &sdev->event_list);
  2343. schedule_work(&sdev->event_work);
  2344. spin_unlock_irqrestore(&sdev->list_lock, flags);
  2345. }
  2346. EXPORT_SYMBOL_GPL(sdev_evt_send);
  2347. /**
  2348. * sdev_evt_alloc - allocate a new scsi event
  2349. * @evt_type: type of event to allocate
  2350. * @gfpflags: GFP flags for allocation
  2351. *
  2352. * Allocates and returns a new scsi_event.
  2353. */
  2354. struct scsi_event *sdev_evt_alloc(enum scsi_device_event evt_type,
  2355. gfp_t gfpflags)
  2356. {
  2357. struct scsi_event *evt = kzalloc(sizeof(struct scsi_event), gfpflags);
  2358. if (!evt)
  2359. return NULL;
  2360. evt->evt_type = evt_type;
  2361. INIT_LIST_HEAD(&evt->node);
  2362. /* evt_type-specific initialization, if any */
  2363. switch (evt_type) {
  2364. case SDEV_EVT_MEDIA_CHANGE:
  2365. case SDEV_EVT_INQUIRY_CHANGE_REPORTED:
  2366. case SDEV_EVT_CAPACITY_CHANGE_REPORTED:
  2367. case SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED:
  2368. case SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED:
  2369. case SDEV_EVT_LUN_CHANGE_REPORTED:
  2370. default:
  2371. /* do nothing */
  2372. break;
  2373. }
  2374. return evt;
  2375. }
  2376. EXPORT_SYMBOL_GPL(sdev_evt_alloc);
  2377. /**
  2378. * sdev_evt_send_simple - send asserted event to uevent thread
  2379. * @sdev: scsi_device event occurred on
  2380. * @evt_type: type of event to send
  2381. * @gfpflags: GFP flags for allocation
  2382. *
  2383. * Assert scsi device event asynchronously, given an event type.
  2384. */
  2385. void sdev_evt_send_simple(struct scsi_device *sdev,
  2386. enum scsi_device_event evt_type, gfp_t gfpflags)
  2387. {
  2388. struct scsi_event *evt = sdev_evt_alloc(evt_type, gfpflags);
  2389. if (!evt) {
  2390. sdev_printk(KERN_ERR, sdev, "event %d eaten due to OOM\n",
  2391. evt_type);
  2392. return;
  2393. }
  2394. sdev_evt_send(sdev, evt);
  2395. }
  2396. EXPORT_SYMBOL_GPL(sdev_evt_send_simple);
  2397. /**
  2398. * scsi_device_quiesce - Block user issued commands.
  2399. * @sdev: scsi device to quiesce.
  2400. *
  2401. * This works by trying to transition to the SDEV_QUIESCE state
  2402. * (which must be a legal transition). When the device is in this
  2403. * state, only special requests will be accepted, all others will
  2404. * be deferred. Since special requests may also be requeued requests,
  2405. * a successful return doesn't guarantee the device will be
  2406. * totally quiescent.
  2407. *
  2408. * Must be called with user context, may sleep.
  2409. *
  2410. * Returns zero if unsuccessful or an error if not.
  2411. */
  2412. int
  2413. scsi_device_quiesce(struct scsi_device *sdev)
  2414. {
  2415. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  2416. if (err)
  2417. return err;
  2418. scsi_run_queue(sdev->request_queue);
  2419. while (atomic_read(&sdev->device_busy)) {
  2420. msleep_interruptible(200);
  2421. scsi_run_queue(sdev->request_queue);
  2422. }
  2423. return 0;
  2424. }
  2425. EXPORT_SYMBOL(scsi_device_quiesce);
  2426. /**
  2427. * scsi_device_resume - Restart user issued commands to a quiesced device.
  2428. * @sdev: scsi device to resume.
  2429. *
  2430. * Moves the device from quiesced back to running and restarts the
  2431. * queues.
  2432. *
  2433. * Must be called with user context, may sleep.
  2434. */
  2435. void scsi_device_resume(struct scsi_device *sdev)
  2436. {
  2437. /* check if the device state was mutated prior to resume, and if
  2438. * so assume the state is being managed elsewhere (for example
  2439. * device deleted during suspend)
  2440. */
  2441. if (sdev->sdev_state != SDEV_QUIESCE ||
  2442. scsi_device_set_state(sdev, SDEV_RUNNING))
  2443. return;
  2444. scsi_run_queue(sdev->request_queue);
  2445. }
  2446. EXPORT_SYMBOL(scsi_device_resume);
  2447. static void
  2448. device_quiesce_fn(struct scsi_device *sdev, void *data)
  2449. {
  2450. scsi_device_quiesce(sdev);
  2451. }
  2452. void
  2453. scsi_target_quiesce(struct scsi_target *starget)
  2454. {
  2455. starget_for_each_device(starget, NULL, device_quiesce_fn);
  2456. }
  2457. EXPORT_SYMBOL(scsi_target_quiesce);
  2458. static void
  2459. device_resume_fn(struct scsi_device *sdev, void *data)
  2460. {
  2461. scsi_device_resume(sdev);
  2462. }
  2463. void
  2464. scsi_target_resume(struct scsi_target *starget)
  2465. {
  2466. starget_for_each_device(starget, NULL, device_resume_fn);
  2467. }
  2468. EXPORT_SYMBOL(scsi_target_resume);
  2469. /**
  2470. * scsi_internal_device_block - internal function to put a device temporarily into the SDEV_BLOCK state
  2471. * @sdev: device to block
  2472. *
  2473. * Block request made by scsi lld's to temporarily stop all
  2474. * scsi commands on the specified device. Called from interrupt
  2475. * or normal process context.
  2476. *
  2477. * Returns zero if successful or error if not
  2478. *
  2479. * Notes:
  2480. * This routine transitions the device to the SDEV_BLOCK state
  2481. * (which must be a legal transition). When the device is in this
  2482. * state, all commands are deferred until the scsi lld reenables
  2483. * the device with scsi_device_unblock or device_block_tmo fires.
  2484. */
  2485. int
  2486. scsi_internal_device_block(struct scsi_device *sdev)
  2487. {
  2488. struct request_queue *q = sdev->request_queue;
  2489. unsigned long flags;
  2490. int err = 0;
  2491. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  2492. if (err) {
  2493. err = scsi_device_set_state(sdev, SDEV_CREATED_BLOCK);
  2494. if (err)
  2495. return err;
  2496. }
  2497. /*
  2498. * The device has transitioned to SDEV_BLOCK. Stop the
  2499. * block layer from calling the midlayer with this device's
  2500. * request queue.
  2501. */
  2502. if (q->mq_ops) {
  2503. blk_mq_stop_hw_queues(q);
  2504. } else {
  2505. spin_lock_irqsave(q->queue_lock, flags);
  2506. blk_stop_queue(q);
  2507. spin_unlock_irqrestore(q->queue_lock, flags);
  2508. }
  2509. return 0;
  2510. }
  2511. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  2512. /**
  2513. * scsi_internal_device_unblock - resume a device after a block request
  2514. * @sdev: device to resume
  2515. * @new_state: state to set devices to after unblocking
  2516. *
  2517. * Called by scsi lld's or the midlayer to restart the device queue
  2518. * for the previously suspended scsi device. Called from interrupt or
  2519. * normal process context.
  2520. *
  2521. * Returns zero if successful or error if not.
  2522. *
  2523. * Notes:
  2524. * This routine transitions the device to the SDEV_RUNNING state
  2525. * or to one of the offline states (which must be a legal transition)
  2526. * allowing the midlayer to goose the queue for this device.
  2527. */
  2528. int
  2529. scsi_internal_device_unblock(struct scsi_device *sdev,
  2530. enum scsi_device_state new_state)
  2531. {
  2532. struct request_queue *q = sdev->request_queue;
  2533. unsigned long flags;
  2534. /*
  2535. * Try to transition the scsi device to SDEV_RUNNING or one of the
  2536. * offlined states and goose the device queue if successful.
  2537. */
  2538. if ((sdev->sdev_state == SDEV_BLOCK) ||
  2539. (sdev->sdev_state == SDEV_TRANSPORT_OFFLINE))
  2540. sdev->sdev_state = new_state;
  2541. else if (sdev->sdev_state == SDEV_CREATED_BLOCK) {
  2542. if (new_state == SDEV_TRANSPORT_OFFLINE ||
  2543. new_state == SDEV_OFFLINE)
  2544. sdev->sdev_state = new_state;
  2545. else
  2546. sdev->sdev_state = SDEV_CREATED;
  2547. } else if (sdev->sdev_state != SDEV_CANCEL &&
  2548. sdev->sdev_state != SDEV_OFFLINE)
  2549. return -EINVAL;
  2550. if (q->mq_ops) {
  2551. blk_mq_start_stopped_hw_queues(q, false);
  2552. } else {
  2553. spin_lock_irqsave(q->queue_lock, flags);
  2554. blk_start_queue(q);
  2555. spin_unlock_irqrestore(q->queue_lock, flags);
  2556. }
  2557. return 0;
  2558. }
  2559. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  2560. static void
  2561. device_block(struct scsi_device *sdev, void *data)
  2562. {
  2563. scsi_internal_device_block(sdev);
  2564. }
  2565. static int
  2566. target_block(struct device *dev, void *data)
  2567. {
  2568. if (scsi_is_target_device(dev))
  2569. starget_for_each_device(to_scsi_target(dev), NULL,
  2570. device_block);
  2571. return 0;
  2572. }
  2573. void
  2574. scsi_target_block(struct device *dev)
  2575. {
  2576. if (scsi_is_target_device(dev))
  2577. starget_for_each_device(to_scsi_target(dev), NULL,
  2578. device_block);
  2579. else
  2580. device_for_each_child(dev, NULL, target_block);
  2581. }
  2582. EXPORT_SYMBOL_GPL(scsi_target_block);
  2583. static void
  2584. device_unblock(struct scsi_device *sdev, void *data)
  2585. {
  2586. scsi_internal_device_unblock(sdev, *(enum scsi_device_state *)data);
  2587. }
  2588. static int
  2589. target_unblock(struct device *dev, void *data)
  2590. {
  2591. if (scsi_is_target_device(dev))
  2592. starget_for_each_device(to_scsi_target(dev), data,
  2593. device_unblock);
  2594. return 0;
  2595. }
  2596. void
  2597. scsi_target_unblock(struct device *dev, enum scsi_device_state new_state)
  2598. {
  2599. if (scsi_is_target_device(dev))
  2600. starget_for_each_device(to_scsi_target(dev), &new_state,
  2601. device_unblock);
  2602. else
  2603. device_for_each_child(dev, &new_state, target_unblock);
  2604. }
  2605. EXPORT_SYMBOL_GPL(scsi_target_unblock);
  2606. /**
  2607. * scsi_kmap_atomic_sg - find and atomically map an sg-elemnt
  2608. * @sgl: scatter-gather list
  2609. * @sg_count: number of segments in sg
  2610. * @offset: offset in bytes into sg, on return offset into the mapped area
  2611. * @len: bytes to map, on return number of bytes mapped
  2612. *
  2613. * Returns virtual address of the start of the mapped page
  2614. */
  2615. void *scsi_kmap_atomic_sg(struct scatterlist *sgl, int sg_count,
  2616. size_t *offset, size_t *len)
  2617. {
  2618. int i;
  2619. size_t sg_len = 0, len_complete = 0;
  2620. struct scatterlist *sg;
  2621. struct page *page;
  2622. WARN_ON(!irqs_disabled());
  2623. for_each_sg(sgl, sg, sg_count, i) {
  2624. len_complete = sg_len; /* Complete sg-entries */
  2625. sg_len += sg->length;
  2626. if (sg_len > *offset)
  2627. break;
  2628. }
  2629. if (unlikely(i == sg_count)) {
  2630. printk(KERN_ERR "%s: Bytes in sg: %zu, requested offset %zu, "
  2631. "elements %d\n",
  2632. __func__, sg_len, *offset, sg_count);
  2633. WARN_ON(1);
  2634. return NULL;
  2635. }
  2636. /* Offset starting from the beginning of first page in this sg-entry */
  2637. *offset = *offset - len_complete + sg->offset;
  2638. /* Assumption: contiguous pages can be accessed as "page + i" */
  2639. page = nth_page(sg_page(sg), (*offset >> PAGE_SHIFT));
  2640. *offset &= ~PAGE_MASK;
  2641. /* Bytes in this sg-entry from *offset to the end of the page */
  2642. sg_len = PAGE_SIZE - *offset;
  2643. if (*len > sg_len)
  2644. *len = sg_len;
  2645. return kmap_atomic(page);
  2646. }
  2647. EXPORT_SYMBOL(scsi_kmap_atomic_sg);
  2648. /**
  2649. * scsi_kunmap_atomic_sg - atomically unmap a virtual address, previously mapped with scsi_kmap_atomic_sg
  2650. * @virt: virtual address to be unmapped
  2651. */
  2652. void scsi_kunmap_atomic_sg(void *virt)
  2653. {
  2654. kunmap_atomic(virt);
  2655. }
  2656. EXPORT_SYMBOL(scsi_kunmap_atomic_sg);
  2657. void sdev_disable_disk_events(struct scsi_device *sdev)
  2658. {
  2659. atomic_inc(&sdev->disk_events_disable_depth);
  2660. }
  2661. EXPORT_SYMBOL(sdev_disable_disk_events);
  2662. void sdev_enable_disk_events(struct scsi_device *sdev)
  2663. {
  2664. if (WARN_ON_ONCE(atomic_read(&sdev->disk_events_disable_depth) <= 0))
  2665. return;
  2666. atomic_dec(&sdev->disk_events_disable_depth);
  2667. }
  2668. EXPORT_SYMBOL(sdev_enable_disk_events);