scsi_lib.c 65 KB

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