scsi_lib.c 79 KB

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