scsi_lib.c 81 KB

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