scsi_lib.c 92 KB

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