scsi_lib.c 65 KB

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