scsi_lib.c 65 KB

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