scsi_lib.c 66 KB

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