scsi_lib.c 65 KB

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