scsi_lib.c 67 KB

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