scsi_lib.c 69 KB

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