scsi_lib.c 65 KB

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