scsi_lib.c 68 KB

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