scsi_lib.c 92 KB

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