scsi_lib.c 93 KB

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