scsi_lib.c 65 KB

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