scsi_lib.c 82 KB

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