scsi_lib.c 65 KB

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