scsi_lib.c 91 KB

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