scsi_lib.c 76 KB

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