scsi_lib.c 95 KB

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