scsi_lib.c 65 KB

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