scsi_lib.c 66 KB

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