scsi_lib.c 86 KB

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