scsi_lib.c 90 KB

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