scsi_lib.c 65 KB

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