scsi_lib.c 65 KB

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