scsi_lib.c 65 KB

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