scsi_lib.c 86 KB

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