scsi_lib.c 68 KB

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