scsi_lib.c 83 KB

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