scsi_lib.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023
  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/blkdev.h>
  11. #include <linux/completion.h>
  12. #include <linux/kernel.h>
  13. #include <linux/mempool.h>
  14. #include <linux/slab.h>
  15. #include <linux/init.h>
  16. #include <linux/pci.h>
  17. #include <linux/delay.h>
  18. #include <scsi/scsi.h>
  19. #include <scsi/scsi_dbg.h>
  20. #include <scsi/scsi_device.h>
  21. #include <scsi/scsi_driver.h>
  22. #include <scsi/scsi_eh.h>
  23. #include <scsi/scsi_host.h>
  24. #include <scsi/scsi_request.h>
  25. #include "scsi_priv.h"
  26. #include "scsi_logging.h"
  27. #define SG_MEMPOOL_NR (sizeof(scsi_sg_pools)/sizeof(struct scsi_host_sg_pool))
  28. #define SG_MEMPOOL_SIZE 32
  29. struct scsi_host_sg_pool {
  30. size_t size;
  31. char *name;
  32. kmem_cache_t *slab;
  33. mempool_t *pool;
  34. };
  35. #if (SCSI_MAX_PHYS_SEGMENTS < 32)
  36. #error SCSI_MAX_PHYS_SEGMENTS is too small
  37. #endif
  38. #define SP(x) { x, "sgpool-" #x }
  39. struct scsi_host_sg_pool scsi_sg_pools[] = {
  40. SP(8),
  41. SP(16),
  42. SP(32),
  43. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  44. SP(64),
  45. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  46. SP(128),
  47. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  48. SP(256),
  49. #if (SCSI_MAX_PHYS_SEGMENTS > 256)
  50. #error SCSI_MAX_PHYS_SEGMENTS is too large
  51. #endif
  52. #endif
  53. #endif
  54. #endif
  55. };
  56. #undef SP
  57. /*
  58. * Function: scsi_insert_special_req()
  59. *
  60. * Purpose: Insert pre-formed request into request queue.
  61. *
  62. * Arguments: sreq - request that is ready to be queued.
  63. * at_head - boolean. True if we should insert at head
  64. * of queue, false if we should insert at tail.
  65. *
  66. * Lock status: Assumed that lock is not held upon entry.
  67. *
  68. * Returns: Nothing
  69. *
  70. * Notes: This function is called from character device and from
  71. * ioctl types of functions where the caller knows exactly
  72. * what SCSI command needs to be issued. The idea is that
  73. * we merely inject the command into the queue (at the head
  74. * for now), and then call the queue request function to actually
  75. * process it.
  76. */
  77. int scsi_insert_special_req(struct scsi_request *sreq, int at_head)
  78. {
  79. /*
  80. * Because users of this function are apt to reuse requests with no
  81. * modification, we have to sanitise the request flags here
  82. */
  83. sreq->sr_request->flags &= ~REQ_DONTPREP;
  84. blk_insert_request(sreq->sr_device->request_queue, sreq->sr_request,
  85. at_head, sreq, 0);
  86. return 0;
  87. }
  88. /*
  89. * Function: scsi_queue_insert()
  90. *
  91. * Purpose: Insert a command in the midlevel queue.
  92. *
  93. * Arguments: cmd - command that we are adding to queue.
  94. * reason - why we are inserting command to queue.
  95. *
  96. * Lock status: Assumed that lock is not held upon entry.
  97. *
  98. * Returns: Nothing.
  99. *
  100. * Notes: We do this for one of two cases. Either the host is busy
  101. * and it cannot accept any more commands for the time being,
  102. * or the device returned QUEUE_FULL and can accept no more
  103. * commands.
  104. * Notes: This could be called either from an interrupt context or a
  105. * normal process context.
  106. */
  107. int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
  108. {
  109. struct Scsi_Host *host = cmd->device->host;
  110. struct scsi_device *device = cmd->device;
  111. SCSI_LOG_MLQUEUE(1,
  112. printk("Inserting command %p into mlqueue\n", cmd));
  113. /*
  114. * We are inserting the command into the ml queue. First, we
  115. * cancel the timer, so it doesn't time out.
  116. */
  117. scsi_delete_timer(cmd);
  118. /*
  119. * Next, set the appropriate busy bit for the device/host.
  120. *
  121. * If the host/device isn't busy, assume that something actually
  122. * completed, and that we should be able to queue a command now.
  123. *
  124. * Note that the prior mid-layer assumption that any host could
  125. * always queue at least one command is now broken. The mid-layer
  126. * will implement a user specifiable stall (see
  127. * scsi_host.max_host_blocked and scsi_device.max_device_blocked)
  128. * if a command is requeued with no other commands outstanding
  129. * either for the device or for the host.
  130. */
  131. if (reason == SCSI_MLQUEUE_HOST_BUSY)
  132. host->host_blocked = host->max_host_blocked;
  133. else if (reason == SCSI_MLQUEUE_DEVICE_BUSY)
  134. device->device_blocked = device->max_device_blocked;
  135. /*
  136. * Register the fact that we own the thing for now.
  137. */
  138. cmd->state = SCSI_STATE_MLQUEUE;
  139. cmd->owner = SCSI_OWNER_MIDLEVEL;
  140. /*
  141. * Decrement the counters, since these commands are no longer
  142. * active on the host/device.
  143. */
  144. scsi_device_unbusy(device);
  145. /*
  146. * Insert this command at the head of the queue for it's device.
  147. * It will go before all other commands that are already in the queue.
  148. *
  149. * NOTE: there is magic here about the way the queue is plugged if
  150. * we have no outstanding commands.
  151. *
  152. * Although this *doesn't* plug the queue, it does call the request
  153. * function. The SCSI request function detects the blocked condition
  154. * and plugs the queue appropriately.
  155. */
  156. blk_insert_request(device->request_queue, cmd->request, 1, cmd, 1);
  157. return 0;
  158. }
  159. /*
  160. * Function: scsi_do_req
  161. *
  162. * Purpose: Queue a SCSI request
  163. *
  164. * Arguments: sreq - command descriptor.
  165. * cmnd - actual SCSI command to be performed.
  166. * buffer - data buffer.
  167. * bufflen - size of data buffer.
  168. * done - completion function to be run.
  169. * timeout - how long to let it run before timeout.
  170. * retries - number of retries we allow.
  171. *
  172. * Lock status: No locks held upon entry.
  173. *
  174. * Returns: Nothing.
  175. *
  176. * Notes: This function is only used for queueing requests for things
  177. * like ioctls and character device requests - this is because
  178. * we essentially just inject a request into the queue for the
  179. * device.
  180. *
  181. * In order to support the scsi_device_quiesce function, we
  182. * now inject requests on the *head* of the device queue
  183. * rather than the tail.
  184. */
  185. void scsi_do_req(struct scsi_request *sreq, const void *cmnd,
  186. void *buffer, unsigned bufflen,
  187. void (*done)(struct scsi_cmnd *),
  188. int timeout, int retries)
  189. {
  190. /*
  191. * If the upper level driver is reusing these things, then
  192. * we should release the low-level block now. Another one will
  193. * be allocated later when this request is getting queued.
  194. */
  195. __scsi_release_request(sreq);
  196. /*
  197. * Our own function scsi_done (which marks the host as not busy,
  198. * disables the timeout counter, etc) will be called by us or by the
  199. * scsi_hosts[host].queuecommand() function needs to also call
  200. * the completion function for the high level driver.
  201. */
  202. memcpy(sreq->sr_cmnd, cmnd, sizeof(sreq->sr_cmnd));
  203. sreq->sr_bufflen = bufflen;
  204. sreq->sr_buffer = buffer;
  205. sreq->sr_allowed = retries;
  206. sreq->sr_done = done;
  207. sreq->sr_timeout_per_command = timeout;
  208. if (sreq->sr_cmd_len == 0)
  209. sreq->sr_cmd_len = COMMAND_SIZE(sreq->sr_cmnd[0]);
  210. /*
  211. * head injection *required* here otherwise quiesce won't work
  212. */
  213. scsi_insert_special_req(sreq, 1);
  214. }
  215. EXPORT_SYMBOL(scsi_do_req);
  216. static void scsi_wait_done(struct scsi_cmnd *cmd)
  217. {
  218. struct request *req = cmd->request;
  219. struct request_queue *q = cmd->device->request_queue;
  220. unsigned long flags;
  221. req->rq_status = RQ_SCSI_DONE; /* Busy, but indicate request done */
  222. spin_lock_irqsave(q->queue_lock, flags);
  223. if (blk_rq_tagged(req))
  224. blk_queue_end_tag(q, req);
  225. spin_unlock_irqrestore(q->queue_lock, flags);
  226. if (req->waiting)
  227. complete(req->waiting);
  228. }
  229. /* This is the end routine we get to if a command was never attached
  230. * to the request. Simply complete the request without changing
  231. * rq_status; this will cause a DRIVER_ERROR. */
  232. static void scsi_wait_req_end_io(struct request *req)
  233. {
  234. BUG_ON(!req->waiting);
  235. complete(req->waiting);
  236. }
  237. void scsi_wait_req(struct scsi_request *sreq, const void *cmnd, void *buffer,
  238. unsigned bufflen, int timeout, int retries)
  239. {
  240. DECLARE_COMPLETION(wait);
  241. sreq->sr_request->waiting = &wait;
  242. sreq->sr_request->rq_status = RQ_SCSI_BUSY;
  243. sreq->sr_request->end_io = scsi_wait_req_end_io;
  244. scsi_do_req(sreq, cmnd, buffer, bufflen, scsi_wait_done,
  245. timeout, retries);
  246. wait_for_completion(&wait);
  247. sreq->sr_request->waiting = NULL;
  248. if (sreq->sr_request->rq_status != RQ_SCSI_DONE)
  249. sreq->sr_result |= (DRIVER_ERROR << 24);
  250. __scsi_release_request(sreq);
  251. }
  252. EXPORT_SYMBOL(scsi_wait_req);
  253. /*
  254. * Function: scsi_init_cmd_errh()
  255. *
  256. * Purpose: Initialize cmd fields related to error handling.
  257. *
  258. * Arguments: cmd - command that is ready to be queued.
  259. *
  260. * Returns: Nothing
  261. *
  262. * Notes: This function has the job of initializing a number of
  263. * fields related to error handling. Typically this will
  264. * be called once for each command, as required.
  265. */
  266. static int scsi_init_cmd_errh(struct scsi_cmnd *cmd)
  267. {
  268. cmd->owner = SCSI_OWNER_MIDLEVEL;
  269. cmd->serial_number = 0;
  270. cmd->serial_number_at_timeout = 0;
  271. cmd->abort_reason = 0;
  272. memset(cmd->sense_buffer, 0, sizeof cmd->sense_buffer);
  273. if (cmd->cmd_len == 0)
  274. cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
  275. /*
  276. * We need saved copies of a number of fields - this is because
  277. * error handling may need to overwrite these with different values
  278. * to run different commands, and once error handling is complete,
  279. * we will need to restore these values prior to running the actual
  280. * command.
  281. */
  282. cmd->old_use_sg = cmd->use_sg;
  283. cmd->old_cmd_len = cmd->cmd_len;
  284. cmd->sc_old_data_direction = cmd->sc_data_direction;
  285. cmd->old_underflow = cmd->underflow;
  286. memcpy(cmd->data_cmnd, cmd->cmnd, sizeof(cmd->cmnd));
  287. cmd->buffer = cmd->request_buffer;
  288. cmd->bufflen = cmd->request_bufflen;
  289. cmd->internal_timeout = NORMAL_TIMEOUT;
  290. cmd->abort_reason = 0;
  291. return 1;
  292. }
  293. /*
  294. * Function: scsi_setup_cmd_retry()
  295. *
  296. * Purpose: Restore the command state for a retry
  297. *
  298. * Arguments: cmd - command to be restored
  299. *
  300. * Returns: Nothing
  301. *
  302. * Notes: Immediately prior to retrying a command, we need
  303. * to restore certain fields that we saved above.
  304. */
  305. void scsi_setup_cmd_retry(struct scsi_cmnd *cmd)
  306. {
  307. memcpy(cmd->cmnd, cmd->data_cmnd, sizeof(cmd->data_cmnd));
  308. cmd->request_buffer = cmd->buffer;
  309. cmd->request_bufflen = cmd->bufflen;
  310. cmd->use_sg = cmd->old_use_sg;
  311. cmd->cmd_len = cmd->old_cmd_len;
  312. cmd->sc_data_direction = cmd->sc_old_data_direction;
  313. cmd->underflow = cmd->old_underflow;
  314. }
  315. void scsi_device_unbusy(struct scsi_device *sdev)
  316. {
  317. struct Scsi_Host *shost = sdev->host;
  318. unsigned long flags;
  319. spin_lock_irqsave(shost->host_lock, flags);
  320. shost->host_busy--;
  321. if (unlikely(test_bit(SHOST_RECOVERY, &shost->shost_state) &&
  322. shost->host_failed))
  323. scsi_eh_wakeup(shost);
  324. spin_unlock(shost->host_lock);
  325. spin_lock(sdev->request_queue->queue_lock);
  326. sdev->device_busy--;
  327. spin_unlock_irqrestore(sdev->request_queue->queue_lock, flags);
  328. }
  329. /*
  330. * Called for single_lun devices on IO completion. Clear starget_sdev_user,
  331. * and call blk_run_queue for all the scsi_devices on the target -
  332. * including current_sdev first.
  333. *
  334. * Called with *no* scsi locks held.
  335. */
  336. static void scsi_single_lun_run(struct scsi_device *current_sdev)
  337. {
  338. struct Scsi_Host *shost = current_sdev->host;
  339. struct scsi_device *sdev, *tmp;
  340. struct scsi_target *starget = scsi_target(current_sdev);
  341. unsigned long flags;
  342. spin_lock_irqsave(shost->host_lock, flags);
  343. starget->starget_sdev_user = NULL;
  344. spin_unlock_irqrestore(shost->host_lock, flags);
  345. /*
  346. * Call blk_run_queue for all LUNs on the target, starting with
  347. * current_sdev. We race with others (to set starget_sdev_user),
  348. * but in most cases, we will be first. Ideally, each LU on the
  349. * target would get some limited time or requests on the target.
  350. */
  351. blk_run_queue(current_sdev->request_queue);
  352. spin_lock_irqsave(shost->host_lock, flags);
  353. if (starget->starget_sdev_user)
  354. goto out;
  355. list_for_each_entry_safe(sdev, tmp, &starget->devices,
  356. same_target_siblings) {
  357. if (sdev == current_sdev)
  358. continue;
  359. if (scsi_device_get(sdev))
  360. continue;
  361. spin_unlock_irqrestore(shost->host_lock, flags);
  362. blk_run_queue(sdev->request_queue);
  363. spin_lock_irqsave(shost->host_lock, flags);
  364. scsi_device_put(sdev);
  365. }
  366. out:
  367. spin_unlock_irqrestore(shost->host_lock, flags);
  368. }
  369. /*
  370. * Function: scsi_run_queue()
  371. *
  372. * Purpose: Select a proper request queue to serve next
  373. *
  374. * Arguments: q - last request's queue
  375. *
  376. * Returns: Nothing
  377. *
  378. * Notes: The previous command was completely finished, start
  379. * a new one if possible.
  380. */
  381. static void scsi_run_queue(struct request_queue *q)
  382. {
  383. struct scsi_device *sdev = q->queuedata;
  384. struct Scsi_Host *shost = sdev->host;
  385. unsigned long flags;
  386. if (sdev->single_lun)
  387. scsi_single_lun_run(sdev);
  388. spin_lock_irqsave(shost->host_lock, flags);
  389. while (!list_empty(&shost->starved_list) &&
  390. !shost->host_blocked && !shost->host_self_blocked &&
  391. !((shost->can_queue > 0) &&
  392. (shost->host_busy >= shost->can_queue))) {
  393. /*
  394. * As long as shost is accepting commands and we have
  395. * starved queues, call blk_run_queue. scsi_request_fn
  396. * drops the queue_lock and can add us back to the
  397. * starved_list.
  398. *
  399. * host_lock protects the starved_list and starved_entry.
  400. * scsi_request_fn must get the host_lock before checking
  401. * or modifying starved_list or starved_entry.
  402. */
  403. sdev = list_entry(shost->starved_list.next,
  404. struct scsi_device, starved_entry);
  405. list_del_init(&sdev->starved_entry);
  406. spin_unlock_irqrestore(shost->host_lock, flags);
  407. blk_run_queue(sdev->request_queue);
  408. spin_lock_irqsave(shost->host_lock, flags);
  409. if (unlikely(!list_empty(&sdev->starved_entry)))
  410. /*
  411. * sdev lost a race, and was put back on the
  412. * starved list. This is unlikely but without this
  413. * in theory we could loop forever.
  414. */
  415. break;
  416. }
  417. spin_unlock_irqrestore(shost->host_lock, flags);
  418. blk_run_queue(q);
  419. }
  420. /*
  421. * Function: scsi_requeue_command()
  422. *
  423. * Purpose: Handle post-processing of completed commands.
  424. *
  425. * Arguments: q - queue to operate on
  426. * cmd - command that may need to be requeued.
  427. *
  428. * Returns: Nothing
  429. *
  430. * Notes: After command completion, there may be blocks left
  431. * over which weren't finished by the previous command
  432. * this can be for a number of reasons - the main one is
  433. * I/O errors in the middle of the request, in which case
  434. * we need to request the blocks that come after the bad
  435. * sector.
  436. */
  437. static void scsi_requeue_command(struct request_queue *q, struct scsi_cmnd *cmd)
  438. {
  439. cmd->request->flags &= ~REQ_DONTPREP;
  440. blk_insert_request(q, cmd->request, 1, cmd, 1);
  441. scsi_run_queue(q);
  442. }
  443. void scsi_next_command(struct scsi_cmnd *cmd)
  444. {
  445. struct request_queue *q = cmd->device->request_queue;
  446. scsi_put_command(cmd);
  447. scsi_run_queue(q);
  448. }
  449. void scsi_run_host_queues(struct Scsi_Host *shost)
  450. {
  451. struct scsi_device *sdev;
  452. shost_for_each_device(sdev, shost)
  453. scsi_run_queue(sdev->request_queue);
  454. }
  455. /*
  456. * Function: scsi_end_request()
  457. *
  458. * Purpose: Post-processing of completed commands (usually invoked at end
  459. * of upper level post-processing and scsi_io_completion).
  460. *
  461. * Arguments: cmd - command that is complete.
  462. * uptodate - 1 if I/O indicates success, <= 0 for I/O error.
  463. * bytes - number of bytes of completed I/O
  464. * requeue - indicates whether we should requeue leftovers.
  465. *
  466. * Lock status: Assumed that lock is not held upon entry.
  467. *
  468. * Returns: cmd if requeue done or required, NULL otherwise
  469. *
  470. * Notes: This is called for block device requests in order to
  471. * mark some number of sectors as complete.
  472. *
  473. * We are guaranteeing that the request queue will be goosed
  474. * at some point during this call.
  475. */
  476. static struct scsi_cmnd *scsi_end_request(struct scsi_cmnd *cmd, int uptodate,
  477. int bytes, int requeue)
  478. {
  479. request_queue_t *q = cmd->device->request_queue;
  480. struct request *req = cmd->request;
  481. unsigned long flags;
  482. /*
  483. * If there are blocks left over at the end, set up the command
  484. * to queue the remainder of them.
  485. */
  486. if (end_that_request_chunk(req, uptodate, bytes)) {
  487. int leftover = (req->hard_nr_sectors << 9);
  488. if (blk_pc_request(req))
  489. leftover = req->data_len;
  490. /* kill remainder if no retrys */
  491. if (!uptodate && blk_noretry_request(req))
  492. end_that_request_chunk(req, 0, leftover);
  493. else {
  494. if (requeue)
  495. /*
  496. * Bleah. Leftovers again. Stick the
  497. * leftovers in the front of the
  498. * queue, and goose the queue again.
  499. */
  500. scsi_requeue_command(q, cmd);
  501. return cmd;
  502. }
  503. }
  504. add_disk_randomness(req->rq_disk);
  505. spin_lock_irqsave(q->queue_lock, flags);
  506. if (blk_rq_tagged(req))
  507. blk_queue_end_tag(q, req);
  508. end_that_request_last(req);
  509. spin_unlock_irqrestore(q->queue_lock, flags);
  510. /*
  511. * This will goose the queue request function at the end, so we don't
  512. * need to worry about launching another command.
  513. */
  514. scsi_next_command(cmd);
  515. return NULL;
  516. }
  517. static struct scatterlist *scsi_alloc_sgtable(struct scsi_cmnd *cmd, int gfp_mask)
  518. {
  519. struct scsi_host_sg_pool *sgp;
  520. struct scatterlist *sgl;
  521. BUG_ON(!cmd->use_sg);
  522. switch (cmd->use_sg) {
  523. case 1 ... 8:
  524. cmd->sglist_len = 0;
  525. break;
  526. case 9 ... 16:
  527. cmd->sglist_len = 1;
  528. break;
  529. case 17 ... 32:
  530. cmd->sglist_len = 2;
  531. break;
  532. #if (SCSI_MAX_PHYS_SEGMENTS > 32)
  533. case 33 ... 64:
  534. cmd->sglist_len = 3;
  535. break;
  536. #if (SCSI_MAX_PHYS_SEGMENTS > 64)
  537. case 65 ... 128:
  538. cmd->sglist_len = 4;
  539. break;
  540. #if (SCSI_MAX_PHYS_SEGMENTS > 128)
  541. case 129 ... 256:
  542. cmd->sglist_len = 5;
  543. break;
  544. #endif
  545. #endif
  546. #endif
  547. default:
  548. return NULL;
  549. }
  550. sgp = scsi_sg_pools + cmd->sglist_len;
  551. sgl = mempool_alloc(sgp->pool, gfp_mask);
  552. if (sgl)
  553. memset(sgl, 0, sgp->size);
  554. return sgl;
  555. }
  556. static void scsi_free_sgtable(struct scatterlist *sgl, int index)
  557. {
  558. struct scsi_host_sg_pool *sgp;
  559. BUG_ON(index > SG_MEMPOOL_NR);
  560. sgp = scsi_sg_pools + index;
  561. mempool_free(sgl, sgp->pool);
  562. }
  563. /*
  564. * Function: scsi_release_buffers()
  565. *
  566. * Purpose: Completion processing for block device I/O requests.
  567. *
  568. * Arguments: cmd - command that we are bailing.
  569. *
  570. * Lock status: Assumed that no lock is held upon entry.
  571. *
  572. * Returns: Nothing
  573. *
  574. * Notes: In the event that an upper level driver rejects a
  575. * command, we must release resources allocated during
  576. * the __init_io() function. Primarily this would involve
  577. * the scatter-gather table, and potentially any bounce
  578. * buffers.
  579. */
  580. static void scsi_release_buffers(struct scsi_cmnd *cmd)
  581. {
  582. struct request *req = cmd->request;
  583. /*
  584. * Free up any indirection buffers we allocated for DMA purposes.
  585. */
  586. if (cmd->use_sg)
  587. scsi_free_sgtable(cmd->request_buffer, cmd->sglist_len);
  588. else if (cmd->request_buffer != req->buffer)
  589. kfree(cmd->request_buffer);
  590. /*
  591. * Zero these out. They now point to freed memory, and it is
  592. * dangerous to hang onto the pointers.
  593. */
  594. cmd->buffer = NULL;
  595. cmd->bufflen = 0;
  596. cmd->request_buffer = NULL;
  597. cmd->request_bufflen = 0;
  598. }
  599. /*
  600. * Function: scsi_io_completion()
  601. *
  602. * Purpose: Completion processing for block device I/O requests.
  603. *
  604. * Arguments: cmd - command that is finished.
  605. *
  606. * Lock status: Assumed that no lock is held upon entry.
  607. *
  608. * Returns: Nothing
  609. *
  610. * Notes: This function is matched in terms of capabilities to
  611. * the function that created the scatter-gather list.
  612. * In other words, if there are no bounce buffers
  613. * (the normal case for most drivers), we don't need
  614. * the logic to deal with cleaning up afterwards.
  615. *
  616. * We must do one of several things here:
  617. *
  618. * a) Call scsi_end_request. This will finish off the
  619. * specified number of sectors. If we are done, the
  620. * command block will be released, and the queue
  621. * function will be goosed. If we are not done, then
  622. * scsi_end_request will directly goose the queue.
  623. *
  624. * b) We can just use scsi_requeue_command() here. This would
  625. * be used if we just wanted to retry, for example.
  626. */
  627. void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes,
  628. unsigned int block_bytes)
  629. {
  630. int result = cmd->result;
  631. int this_count = cmd->bufflen;
  632. request_queue_t *q = cmd->device->request_queue;
  633. struct request *req = cmd->request;
  634. int clear_errors = 1;
  635. struct scsi_sense_hdr sshdr;
  636. int sense_valid = 0;
  637. int sense_deferred = 0;
  638. if (blk_complete_barrier_rq(q, req, good_bytes >> 9))
  639. return;
  640. /*
  641. * Free up any indirection buffers we allocated for DMA purposes.
  642. * For the case of a READ, we need to copy the data out of the
  643. * bounce buffer and into the real buffer.
  644. */
  645. if (cmd->use_sg)
  646. scsi_free_sgtable(cmd->buffer, cmd->sglist_len);
  647. else if (cmd->buffer != req->buffer) {
  648. if (rq_data_dir(req) == READ) {
  649. unsigned long flags;
  650. char *to = bio_kmap_irq(req->bio, &flags);
  651. memcpy(to, cmd->buffer, cmd->bufflen);
  652. bio_kunmap_irq(to, &flags);
  653. }
  654. kfree(cmd->buffer);
  655. }
  656. if (result) {
  657. sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
  658. if (sense_valid)
  659. sense_deferred = scsi_sense_is_deferred(&sshdr);
  660. }
  661. if (blk_pc_request(req)) { /* SG_IO ioctl from block level */
  662. req->errors = result;
  663. if (result) {
  664. clear_errors = 0;
  665. if (sense_valid && req->sense) {
  666. /*
  667. * SG_IO wants current and deferred errors
  668. */
  669. int len = 8 + cmd->sense_buffer[7];
  670. if (len > SCSI_SENSE_BUFFERSIZE)
  671. len = SCSI_SENSE_BUFFERSIZE;
  672. memcpy(req->sense, cmd->sense_buffer, len);
  673. req->sense_len = len;
  674. }
  675. } else
  676. req->data_len = cmd->resid;
  677. }
  678. /*
  679. * Zero these out. They now point to freed memory, and it is
  680. * dangerous to hang onto the pointers.
  681. */
  682. cmd->buffer = NULL;
  683. cmd->bufflen = 0;
  684. cmd->request_buffer = NULL;
  685. cmd->request_bufflen = 0;
  686. /*
  687. * Next deal with any sectors which we were able to correctly
  688. * handle.
  689. */
  690. if (good_bytes >= 0) {
  691. SCSI_LOG_HLCOMPLETE(1, printk("%ld sectors total, %d bytes done.\n",
  692. req->nr_sectors, good_bytes));
  693. SCSI_LOG_HLCOMPLETE(1, printk("use_sg is %d\n", cmd->use_sg));
  694. if (clear_errors)
  695. req->errors = 0;
  696. /*
  697. * If multiple sectors are requested in one buffer, then
  698. * they will have been finished off by the first command.
  699. * If not, then we have a multi-buffer command.
  700. *
  701. * If block_bytes != 0, it means we had a medium error
  702. * of some sort, and that we want to mark some number of
  703. * sectors as not uptodate. Thus we want to inhibit
  704. * requeueing right here - we will requeue down below
  705. * when we handle the bad sectors.
  706. */
  707. cmd = scsi_end_request(cmd, 1, good_bytes, result == 0);
  708. /*
  709. * If the command completed without error, then either finish off the
  710. * rest of the command, or start a new one.
  711. */
  712. if (result == 0 || cmd == NULL ) {
  713. return;
  714. }
  715. }
  716. /*
  717. * Now, if we were good little boys and girls, Santa left us a request
  718. * sense buffer. We can extract information from this, so we
  719. * can choose a block to remap, etc.
  720. */
  721. if (sense_valid && !sense_deferred) {
  722. switch (sshdr.sense_key) {
  723. case UNIT_ATTENTION:
  724. if (cmd->device->removable) {
  725. /* detected disc change. set a bit
  726. * and quietly refuse further access.
  727. */
  728. cmd->device->changed = 1;
  729. cmd = scsi_end_request(cmd, 0,
  730. this_count, 1);
  731. return;
  732. } else {
  733. /*
  734. * Must have been a power glitch, or a
  735. * bus reset. Could not have been a
  736. * media change, so we just retry the
  737. * request and see what happens.
  738. */
  739. scsi_requeue_command(q, cmd);
  740. return;
  741. }
  742. break;
  743. case ILLEGAL_REQUEST:
  744. /*
  745. * If we had an ILLEGAL REQUEST returned, then we may
  746. * have performed an unsupported command. The only
  747. * thing this should be would be a ten byte read where
  748. * only a six byte read was supported. Also, on a
  749. * system where READ CAPACITY failed, we may have read
  750. * past the end of the disk.
  751. */
  752. if (cmd->device->use_10_for_rw &&
  753. (cmd->cmnd[0] == READ_10 ||
  754. cmd->cmnd[0] == WRITE_10)) {
  755. cmd->device->use_10_for_rw = 0;
  756. /*
  757. * This will cause a retry with a 6-byte
  758. * command.
  759. */
  760. scsi_requeue_command(q, cmd);
  761. result = 0;
  762. } else {
  763. cmd = scsi_end_request(cmd, 0, this_count, 1);
  764. return;
  765. }
  766. break;
  767. case NOT_READY:
  768. /*
  769. * If the device is in the process of becoming ready,
  770. * retry.
  771. */
  772. if (sshdr.asc == 0x04 && sshdr.ascq == 0x01) {
  773. scsi_requeue_command(q, cmd);
  774. return;
  775. }
  776. printk(KERN_INFO "Device %s not ready.\n",
  777. req->rq_disk ? req->rq_disk->disk_name : "");
  778. cmd = scsi_end_request(cmd, 0, this_count, 1);
  779. return;
  780. case VOLUME_OVERFLOW:
  781. printk(KERN_INFO "Volume overflow <%d %d %d %d> CDB: ",
  782. cmd->device->host->host_no,
  783. (int)cmd->device->channel,
  784. (int)cmd->device->id, (int)cmd->device->lun);
  785. __scsi_print_command(cmd->data_cmnd);
  786. scsi_print_sense("", cmd);
  787. cmd = scsi_end_request(cmd, 0, block_bytes, 1);
  788. return;
  789. default:
  790. break;
  791. }
  792. } /* driver byte != 0 */
  793. if (host_byte(result) == DID_RESET) {
  794. /*
  795. * Third party bus reset or reset for error
  796. * recovery reasons. Just retry the request
  797. * and see what happens.
  798. */
  799. scsi_requeue_command(q, cmd);
  800. return;
  801. }
  802. if (result) {
  803. printk(KERN_INFO "SCSI error : <%d %d %d %d> return code "
  804. "= 0x%x\n", cmd->device->host->host_no,
  805. cmd->device->channel,
  806. cmd->device->id,
  807. cmd->device->lun, result);
  808. if (driver_byte(result) & DRIVER_SENSE)
  809. scsi_print_sense("", cmd);
  810. /*
  811. * Mark a single buffer as not uptodate. Queue the remainder.
  812. * We sometimes get this cruft in the event that a medium error
  813. * isn't properly reported.
  814. */
  815. block_bytes = req->hard_cur_sectors << 9;
  816. if (!block_bytes)
  817. block_bytes = req->data_len;
  818. cmd = scsi_end_request(cmd, 0, block_bytes, 1);
  819. }
  820. }
  821. EXPORT_SYMBOL(scsi_io_completion);
  822. /*
  823. * Function: scsi_init_io()
  824. *
  825. * Purpose: SCSI I/O initialize function.
  826. *
  827. * Arguments: cmd - Command descriptor we wish to initialize
  828. *
  829. * Returns: 0 on success
  830. * BLKPREP_DEFER if the failure is retryable
  831. * BLKPREP_KILL if the failure is fatal
  832. */
  833. static int scsi_init_io(struct scsi_cmnd *cmd)
  834. {
  835. struct request *req = cmd->request;
  836. struct scatterlist *sgpnt;
  837. int count;
  838. /*
  839. * if this is a rq->data based REQ_BLOCK_PC, setup for a non-sg xfer
  840. */
  841. if ((req->flags & REQ_BLOCK_PC) && !req->bio) {
  842. cmd->request_bufflen = req->data_len;
  843. cmd->request_buffer = req->data;
  844. req->buffer = req->data;
  845. cmd->use_sg = 0;
  846. return 0;
  847. }
  848. /*
  849. * we used to not use scatter-gather for single segment request,
  850. * but now we do (it makes highmem I/O easier to support without
  851. * kmapping pages)
  852. */
  853. cmd->use_sg = req->nr_phys_segments;
  854. /*
  855. * if sg table allocation fails, requeue request later.
  856. */
  857. sgpnt = scsi_alloc_sgtable(cmd, GFP_ATOMIC);
  858. if (unlikely(!sgpnt)) {
  859. req->flags |= REQ_SPECIAL;
  860. return BLKPREP_DEFER;
  861. }
  862. cmd->request_buffer = (char *) sgpnt;
  863. cmd->request_bufflen = req->nr_sectors << 9;
  864. if (blk_pc_request(req))
  865. cmd->request_bufflen = req->data_len;
  866. req->buffer = NULL;
  867. /*
  868. * Next, walk the list, and fill in the addresses and sizes of
  869. * each segment.
  870. */
  871. count = blk_rq_map_sg(req->q, req, cmd->request_buffer);
  872. /*
  873. * mapped well, send it off
  874. */
  875. if (likely(count <= cmd->use_sg)) {
  876. cmd->use_sg = count;
  877. return 0;
  878. }
  879. printk(KERN_ERR "Incorrect number of segments after building list\n");
  880. printk(KERN_ERR "counted %d, received %d\n", count, cmd->use_sg);
  881. printk(KERN_ERR "req nr_sec %lu, cur_nr_sec %u\n", req->nr_sectors,
  882. req->current_nr_sectors);
  883. /* release the command and kill it */
  884. scsi_release_buffers(cmd);
  885. scsi_put_command(cmd);
  886. return BLKPREP_KILL;
  887. }
  888. static int scsi_prepare_flush_fn(request_queue_t *q, struct request *rq)
  889. {
  890. struct scsi_device *sdev = q->queuedata;
  891. struct scsi_driver *drv;
  892. if (sdev->sdev_state == SDEV_RUNNING) {
  893. drv = *(struct scsi_driver **) rq->rq_disk->private_data;
  894. if (drv->prepare_flush)
  895. return drv->prepare_flush(q, rq);
  896. }
  897. return 0;
  898. }
  899. static void scsi_end_flush_fn(request_queue_t *q, struct request *rq)
  900. {
  901. struct scsi_device *sdev = q->queuedata;
  902. struct request *flush_rq = rq->end_io_data;
  903. struct scsi_driver *drv;
  904. if (flush_rq->errors) {
  905. printk("scsi: barrier error, disabling flush support\n");
  906. blk_queue_ordered(q, QUEUE_ORDERED_NONE);
  907. }
  908. if (sdev->sdev_state == SDEV_RUNNING) {
  909. drv = *(struct scsi_driver **) rq->rq_disk->private_data;
  910. drv->end_flush(q, rq);
  911. }
  912. }
  913. static int scsi_issue_flush_fn(request_queue_t *q, struct gendisk *disk,
  914. sector_t *error_sector)
  915. {
  916. struct scsi_device *sdev = q->queuedata;
  917. struct scsi_driver *drv;
  918. if (sdev->sdev_state != SDEV_RUNNING)
  919. return -ENXIO;
  920. drv = *(struct scsi_driver **) disk->private_data;
  921. if (drv->issue_flush)
  922. return drv->issue_flush(&sdev->sdev_gendev, error_sector);
  923. return -EOPNOTSUPP;
  924. }
  925. static int scsi_prep_fn(struct request_queue *q, struct request *req)
  926. {
  927. struct scsi_device *sdev = q->queuedata;
  928. struct scsi_cmnd *cmd;
  929. int specials_only = 0;
  930. /*
  931. * Just check to see if the device is online. If it isn't, we
  932. * refuse to process any commands. The device must be brought
  933. * online before trying any recovery commands
  934. */
  935. if (unlikely(!scsi_device_online(sdev))) {
  936. printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to offline device\n",
  937. sdev->host->host_no, sdev->id, sdev->lun);
  938. return BLKPREP_KILL;
  939. }
  940. if (unlikely(sdev->sdev_state != SDEV_RUNNING)) {
  941. /* OK, we're not in a running state don't prep
  942. * user commands */
  943. if (sdev->sdev_state == SDEV_DEL) {
  944. /* Device is fully deleted, no commands
  945. * at all allowed down */
  946. printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to dead device\n",
  947. sdev->host->host_no, sdev->id, sdev->lun);
  948. return BLKPREP_KILL;
  949. }
  950. /* OK, we only allow special commands (i.e. not
  951. * user initiated ones */
  952. specials_only = sdev->sdev_state;
  953. }
  954. /*
  955. * Find the actual device driver associated with this command.
  956. * The SPECIAL requests are things like character device or
  957. * ioctls, which did not originate from ll_rw_blk. Note that
  958. * the special field is also used to indicate the cmd for
  959. * the remainder of a partially fulfilled request that can
  960. * come up when there is a medium error. We have to treat
  961. * these two cases differently. We differentiate by looking
  962. * at request->cmd, as this tells us the real story.
  963. */
  964. if (req->flags & REQ_SPECIAL) {
  965. struct scsi_request *sreq = req->special;
  966. if (sreq->sr_magic == SCSI_REQ_MAGIC) {
  967. cmd = scsi_get_command(sreq->sr_device, GFP_ATOMIC);
  968. if (unlikely(!cmd))
  969. goto defer;
  970. scsi_init_cmd_from_req(cmd, sreq);
  971. } else
  972. cmd = req->special;
  973. } else if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  974. if(unlikely(specials_only)) {
  975. if(specials_only == SDEV_QUIESCE ||
  976. specials_only == SDEV_BLOCK)
  977. return BLKPREP_DEFER;
  978. printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to device being removed\n",
  979. sdev->host->host_no, sdev->id, sdev->lun);
  980. return BLKPREP_KILL;
  981. }
  982. /*
  983. * Now try and find a command block that we can use.
  984. */
  985. if (!req->special) {
  986. cmd = scsi_get_command(sdev, GFP_ATOMIC);
  987. if (unlikely(!cmd))
  988. goto defer;
  989. } else
  990. cmd = req->special;
  991. /* pull a tag out of the request if we have one */
  992. cmd->tag = req->tag;
  993. } else {
  994. blk_dump_rq_flags(req, "SCSI bad req");
  995. return BLKPREP_KILL;
  996. }
  997. /* note the overloading of req->special. When the tag
  998. * is active it always means cmd. If the tag goes
  999. * back for re-queueing, it may be reset */
  1000. req->special = cmd;
  1001. cmd->request = req;
  1002. /*
  1003. * FIXME: drop the lock here because the functions below
  1004. * expect to be called without the queue lock held. Also,
  1005. * previously, we dequeued the request before dropping the
  1006. * lock. We hope REQ_STARTED prevents anything untoward from
  1007. * happening now.
  1008. */
  1009. if (req->flags & (REQ_CMD | REQ_BLOCK_PC)) {
  1010. struct scsi_driver *drv;
  1011. int ret;
  1012. /*
  1013. * This will do a couple of things:
  1014. * 1) Fill in the actual SCSI command.
  1015. * 2) Fill in any other upper-level specific fields
  1016. * (timeout).
  1017. *
  1018. * If this returns 0, it means that the request failed
  1019. * (reading past end of disk, reading offline device,
  1020. * etc). This won't actually talk to the device, but
  1021. * some kinds of consistency checking may cause the
  1022. * request to be rejected immediately.
  1023. */
  1024. /*
  1025. * This sets up the scatter-gather table (allocating if
  1026. * required).
  1027. */
  1028. ret = scsi_init_io(cmd);
  1029. if (ret) /* BLKPREP_KILL return also releases the command */
  1030. return ret;
  1031. /*
  1032. * Initialize the actual SCSI command for this request.
  1033. */
  1034. drv = *(struct scsi_driver **)req->rq_disk->private_data;
  1035. if (unlikely(!drv->init_command(cmd))) {
  1036. scsi_release_buffers(cmd);
  1037. scsi_put_command(cmd);
  1038. return BLKPREP_KILL;
  1039. }
  1040. }
  1041. /*
  1042. * The request is now prepped, no need to come back here
  1043. */
  1044. req->flags |= REQ_DONTPREP;
  1045. return BLKPREP_OK;
  1046. defer:
  1047. /* If we defer, the elv_next_request() returns NULL, but the
  1048. * queue must be restarted, so we plug here if no returning
  1049. * command will automatically do that. */
  1050. if (sdev->device_busy == 0)
  1051. blk_plug_device(q);
  1052. return BLKPREP_DEFER;
  1053. }
  1054. /*
  1055. * scsi_dev_queue_ready: if we can send requests to sdev, return 1 else
  1056. * return 0.
  1057. *
  1058. * Called with the queue_lock held.
  1059. */
  1060. static inline int scsi_dev_queue_ready(struct request_queue *q,
  1061. struct scsi_device *sdev)
  1062. {
  1063. if (sdev->device_busy >= sdev->queue_depth)
  1064. return 0;
  1065. if (sdev->device_busy == 0 && sdev->device_blocked) {
  1066. /*
  1067. * unblock after device_blocked iterates to zero
  1068. */
  1069. if (--sdev->device_blocked == 0) {
  1070. SCSI_LOG_MLQUEUE(3,
  1071. printk("scsi%d (%d:%d) unblocking device at"
  1072. " zero depth\n", sdev->host->host_no,
  1073. sdev->id, sdev->lun));
  1074. } else {
  1075. blk_plug_device(q);
  1076. return 0;
  1077. }
  1078. }
  1079. if (sdev->device_blocked)
  1080. return 0;
  1081. return 1;
  1082. }
  1083. /*
  1084. * scsi_host_queue_ready: if we can send requests to shost, return 1 else
  1085. * return 0. We must end up running the queue again whenever 0 is
  1086. * returned, else IO can hang.
  1087. *
  1088. * Called with host_lock held.
  1089. */
  1090. static inline int scsi_host_queue_ready(struct request_queue *q,
  1091. struct Scsi_Host *shost,
  1092. struct scsi_device *sdev)
  1093. {
  1094. if (test_bit(SHOST_RECOVERY, &shost->shost_state))
  1095. return 0;
  1096. if (shost->host_busy == 0 && shost->host_blocked) {
  1097. /*
  1098. * unblock after host_blocked iterates to zero
  1099. */
  1100. if (--shost->host_blocked == 0) {
  1101. SCSI_LOG_MLQUEUE(3,
  1102. printk("scsi%d unblocking host at zero depth\n",
  1103. shost->host_no));
  1104. } else {
  1105. blk_plug_device(q);
  1106. return 0;
  1107. }
  1108. }
  1109. if ((shost->can_queue > 0 && shost->host_busy >= shost->can_queue) ||
  1110. shost->host_blocked || shost->host_self_blocked) {
  1111. if (list_empty(&sdev->starved_entry))
  1112. list_add_tail(&sdev->starved_entry, &shost->starved_list);
  1113. return 0;
  1114. }
  1115. /* We're OK to process the command, so we can't be starved */
  1116. if (!list_empty(&sdev->starved_entry))
  1117. list_del_init(&sdev->starved_entry);
  1118. return 1;
  1119. }
  1120. /*
  1121. * Kill requests for a dead device
  1122. */
  1123. static void scsi_kill_requests(request_queue_t *q)
  1124. {
  1125. struct request *req;
  1126. while ((req = elv_next_request(q)) != NULL) {
  1127. blkdev_dequeue_request(req);
  1128. req->flags |= REQ_QUIET;
  1129. while (end_that_request_first(req, 0, req->nr_sectors))
  1130. ;
  1131. end_that_request_last(req);
  1132. }
  1133. }
  1134. /*
  1135. * Function: scsi_request_fn()
  1136. *
  1137. * Purpose: Main strategy routine for SCSI.
  1138. *
  1139. * Arguments: q - Pointer to actual queue.
  1140. *
  1141. * Returns: Nothing
  1142. *
  1143. * Lock status: IO request lock assumed to be held when called.
  1144. */
  1145. static void scsi_request_fn(struct request_queue *q)
  1146. {
  1147. struct scsi_device *sdev = q->queuedata;
  1148. struct Scsi_Host *shost;
  1149. struct scsi_cmnd *cmd;
  1150. struct request *req;
  1151. if (!sdev) {
  1152. printk("scsi: killing requests for dead queue\n");
  1153. scsi_kill_requests(q);
  1154. return;
  1155. }
  1156. if(!get_device(&sdev->sdev_gendev))
  1157. /* We must be tearing the block queue down already */
  1158. return;
  1159. /*
  1160. * To start with, we keep looping until the queue is empty, or until
  1161. * the host is no longer able to accept any more requests.
  1162. */
  1163. shost = sdev->host;
  1164. while (!blk_queue_plugged(q)) {
  1165. int rtn;
  1166. /*
  1167. * get next queueable request. We do this early to make sure
  1168. * that the request is fully prepared even if we cannot
  1169. * accept it.
  1170. */
  1171. req = elv_next_request(q);
  1172. if (!req || !scsi_dev_queue_ready(q, sdev))
  1173. break;
  1174. if (unlikely(!scsi_device_online(sdev))) {
  1175. printk(KERN_ERR "scsi%d (%d:%d): rejecting I/O to offline device\n",
  1176. sdev->host->host_no, sdev->id, sdev->lun);
  1177. blkdev_dequeue_request(req);
  1178. req->flags |= REQ_QUIET;
  1179. while (end_that_request_first(req, 0, req->nr_sectors))
  1180. ;
  1181. end_that_request_last(req);
  1182. continue;
  1183. }
  1184. /*
  1185. * Remove the request from the request list.
  1186. */
  1187. if (!(blk_queue_tagged(q) && !blk_queue_start_tag(q, req)))
  1188. blkdev_dequeue_request(req);
  1189. sdev->device_busy++;
  1190. spin_unlock(q->queue_lock);
  1191. spin_lock(shost->host_lock);
  1192. if (!scsi_host_queue_ready(q, shost, sdev))
  1193. goto not_ready;
  1194. if (sdev->single_lun) {
  1195. if (scsi_target(sdev)->starget_sdev_user &&
  1196. scsi_target(sdev)->starget_sdev_user != sdev)
  1197. goto not_ready;
  1198. scsi_target(sdev)->starget_sdev_user = sdev;
  1199. }
  1200. shost->host_busy++;
  1201. /*
  1202. * XXX(hch): This is rather suboptimal, scsi_dispatch_cmd will
  1203. * take the lock again.
  1204. */
  1205. spin_unlock_irq(shost->host_lock);
  1206. cmd = req->special;
  1207. if (unlikely(cmd == NULL)) {
  1208. printk(KERN_CRIT "impossible request in %s.\n"
  1209. "please mail a stack trace to "
  1210. "linux-scsi@vger.kernel.org",
  1211. __FUNCTION__);
  1212. BUG();
  1213. }
  1214. /*
  1215. * Finally, initialize any error handling parameters, and set up
  1216. * the timers for timeouts.
  1217. */
  1218. scsi_init_cmd_errh(cmd);
  1219. /*
  1220. * Dispatch the command to the low-level driver.
  1221. */
  1222. rtn = scsi_dispatch_cmd(cmd);
  1223. spin_lock_irq(q->queue_lock);
  1224. if(rtn) {
  1225. /* we're refusing the command; because of
  1226. * the way locks get dropped, we need to
  1227. * check here if plugging is required */
  1228. if(sdev->device_busy == 0)
  1229. blk_plug_device(q);
  1230. break;
  1231. }
  1232. }
  1233. goto out;
  1234. not_ready:
  1235. spin_unlock_irq(shost->host_lock);
  1236. /*
  1237. * lock q, handle tag, requeue req, and decrement device_busy. We
  1238. * must return with queue_lock held.
  1239. *
  1240. * Decrementing device_busy without checking it is OK, as all such
  1241. * cases (host limits or settings) should run the queue at some
  1242. * later time.
  1243. */
  1244. spin_lock_irq(q->queue_lock);
  1245. blk_requeue_request(q, req);
  1246. sdev->device_busy--;
  1247. if(sdev->device_busy == 0)
  1248. blk_plug_device(q);
  1249. out:
  1250. /* must be careful here...if we trigger the ->remove() function
  1251. * we cannot be holding the q lock */
  1252. spin_unlock_irq(q->queue_lock);
  1253. put_device(&sdev->sdev_gendev);
  1254. spin_lock_irq(q->queue_lock);
  1255. }
  1256. u64 scsi_calculate_bounce_limit(struct Scsi_Host *shost)
  1257. {
  1258. struct device *host_dev;
  1259. u64 bounce_limit = 0xffffffff;
  1260. if (shost->unchecked_isa_dma)
  1261. return BLK_BOUNCE_ISA;
  1262. /*
  1263. * Platforms with virtual-DMA translation
  1264. * hardware have no practical limit.
  1265. */
  1266. if (!PCI_DMA_BUS_IS_PHYS)
  1267. return BLK_BOUNCE_ANY;
  1268. host_dev = scsi_get_device(shost);
  1269. if (host_dev && host_dev->dma_mask)
  1270. bounce_limit = *host_dev->dma_mask;
  1271. return bounce_limit;
  1272. }
  1273. EXPORT_SYMBOL(scsi_calculate_bounce_limit);
  1274. struct request_queue *scsi_alloc_queue(struct scsi_device *sdev)
  1275. {
  1276. struct Scsi_Host *shost = sdev->host;
  1277. struct request_queue *q;
  1278. q = blk_init_queue(scsi_request_fn, NULL);
  1279. if (!q)
  1280. return NULL;
  1281. blk_queue_prep_rq(q, scsi_prep_fn);
  1282. blk_queue_max_hw_segments(q, shost->sg_tablesize);
  1283. blk_queue_max_phys_segments(q, SCSI_MAX_PHYS_SEGMENTS);
  1284. blk_queue_max_sectors(q, shost->max_sectors);
  1285. blk_queue_bounce_limit(q, scsi_calculate_bounce_limit(shost));
  1286. blk_queue_segment_boundary(q, shost->dma_boundary);
  1287. blk_queue_issue_flush_fn(q, scsi_issue_flush_fn);
  1288. /*
  1289. * ordered tags are superior to flush ordering
  1290. */
  1291. if (shost->ordered_tag)
  1292. blk_queue_ordered(q, QUEUE_ORDERED_TAG);
  1293. else if (shost->ordered_flush) {
  1294. blk_queue_ordered(q, QUEUE_ORDERED_FLUSH);
  1295. q->prepare_flush_fn = scsi_prepare_flush_fn;
  1296. q->end_flush_fn = scsi_end_flush_fn;
  1297. }
  1298. if (!shost->use_clustering)
  1299. clear_bit(QUEUE_FLAG_CLUSTER, &q->queue_flags);
  1300. return q;
  1301. }
  1302. void scsi_free_queue(struct request_queue *q)
  1303. {
  1304. blk_cleanup_queue(q);
  1305. }
  1306. /*
  1307. * Function: scsi_block_requests()
  1308. *
  1309. * Purpose: Utility function used by low-level drivers to prevent further
  1310. * commands from being queued to the device.
  1311. *
  1312. * Arguments: shost - Host in question
  1313. *
  1314. * Returns: Nothing
  1315. *
  1316. * Lock status: No locks are assumed held.
  1317. *
  1318. * Notes: There is no timer nor any other means by which the requests
  1319. * get unblocked other than the low-level driver calling
  1320. * scsi_unblock_requests().
  1321. */
  1322. void scsi_block_requests(struct Scsi_Host *shost)
  1323. {
  1324. shost->host_self_blocked = 1;
  1325. }
  1326. EXPORT_SYMBOL(scsi_block_requests);
  1327. /*
  1328. * Function: scsi_unblock_requests()
  1329. *
  1330. * Purpose: Utility function used by low-level drivers to allow further
  1331. * commands from being queued to the device.
  1332. *
  1333. * Arguments: shost - Host in question
  1334. *
  1335. * Returns: Nothing
  1336. *
  1337. * Lock status: No locks are assumed held.
  1338. *
  1339. * Notes: There is no timer nor any other means by which the requests
  1340. * get unblocked other than the low-level driver calling
  1341. * scsi_unblock_requests().
  1342. *
  1343. * This is done as an API function so that changes to the
  1344. * internals of the scsi mid-layer won't require wholesale
  1345. * changes to drivers that use this feature.
  1346. */
  1347. void scsi_unblock_requests(struct Scsi_Host *shost)
  1348. {
  1349. shost->host_self_blocked = 0;
  1350. scsi_run_host_queues(shost);
  1351. }
  1352. EXPORT_SYMBOL(scsi_unblock_requests);
  1353. int __init scsi_init_queue(void)
  1354. {
  1355. int i;
  1356. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1357. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1358. int size = sgp->size * sizeof(struct scatterlist);
  1359. sgp->slab = kmem_cache_create(sgp->name, size, 0,
  1360. SLAB_HWCACHE_ALIGN, NULL, NULL);
  1361. if (!sgp->slab) {
  1362. printk(KERN_ERR "SCSI: can't init sg slab %s\n",
  1363. sgp->name);
  1364. }
  1365. sgp->pool = mempool_create(SG_MEMPOOL_SIZE,
  1366. mempool_alloc_slab, mempool_free_slab,
  1367. sgp->slab);
  1368. if (!sgp->pool) {
  1369. printk(KERN_ERR "SCSI: can't init sg mempool %s\n",
  1370. sgp->name);
  1371. }
  1372. }
  1373. return 0;
  1374. }
  1375. void scsi_exit_queue(void)
  1376. {
  1377. int i;
  1378. for (i = 0; i < SG_MEMPOOL_NR; i++) {
  1379. struct scsi_host_sg_pool *sgp = scsi_sg_pools + i;
  1380. mempool_destroy(sgp->pool);
  1381. kmem_cache_destroy(sgp->slab);
  1382. }
  1383. }
  1384. /**
  1385. * __scsi_mode_sense - issue a mode sense, falling back from 10 to
  1386. * six bytes if necessary.
  1387. * @sreq: SCSI request to fill in with the MODE_SENSE
  1388. * @dbd: set if mode sense will allow block descriptors to be returned
  1389. * @modepage: mode page being requested
  1390. * @buffer: request buffer (may not be smaller than eight bytes)
  1391. * @len: length of request buffer.
  1392. * @timeout: command timeout
  1393. * @retries: number of retries before failing
  1394. * @data: returns a structure abstracting the mode header data
  1395. *
  1396. * Returns zero if unsuccessful, or the header offset (either 4
  1397. * or 8 depending on whether a six or ten byte command was
  1398. * issued) if successful.
  1399. **/
  1400. int
  1401. __scsi_mode_sense(struct scsi_request *sreq, int dbd, int modepage,
  1402. unsigned char *buffer, int len, int timeout, int retries,
  1403. struct scsi_mode_data *data) {
  1404. unsigned char cmd[12];
  1405. int use_10_for_ms;
  1406. int header_length;
  1407. memset(data, 0, sizeof(*data));
  1408. memset(&cmd[0], 0, 12);
  1409. cmd[1] = dbd & 0x18; /* allows DBD and LLBA bits */
  1410. cmd[2] = modepage;
  1411. retry:
  1412. use_10_for_ms = sreq->sr_device->use_10_for_ms;
  1413. if (use_10_for_ms) {
  1414. if (len < 8)
  1415. len = 8;
  1416. cmd[0] = MODE_SENSE_10;
  1417. cmd[8] = len;
  1418. header_length = 8;
  1419. } else {
  1420. if (len < 4)
  1421. len = 4;
  1422. cmd[0] = MODE_SENSE;
  1423. cmd[4] = len;
  1424. header_length = 4;
  1425. }
  1426. sreq->sr_cmd_len = 0;
  1427. memset(sreq->sr_sense_buffer, 0, sizeof(sreq->sr_sense_buffer));
  1428. sreq->sr_data_direction = DMA_FROM_DEVICE;
  1429. memset(buffer, 0, len);
  1430. scsi_wait_req(sreq, cmd, buffer, len, timeout, retries);
  1431. /* This code looks awful: what it's doing is making sure an
  1432. * ILLEGAL REQUEST sense return identifies the actual command
  1433. * byte as the problem. MODE_SENSE commands can return
  1434. * ILLEGAL REQUEST if the code page isn't supported */
  1435. if (use_10_for_ms && !scsi_status_is_good(sreq->sr_result) &&
  1436. (driver_byte(sreq->sr_result) & DRIVER_SENSE)) {
  1437. struct scsi_sense_hdr sshdr;
  1438. if (scsi_request_normalize_sense(sreq, &sshdr)) {
  1439. if ((sshdr.sense_key == ILLEGAL_REQUEST) &&
  1440. (sshdr.asc == 0x20) && (sshdr.ascq == 0)) {
  1441. /*
  1442. * Invalid command operation code
  1443. */
  1444. sreq->sr_device->use_10_for_ms = 0;
  1445. goto retry;
  1446. }
  1447. }
  1448. }
  1449. if(scsi_status_is_good(sreq->sr_result)) {
  1450. data->header_length = header_length;
  1451. if(use_10_for_ms) {
  1452. data->length = buffer[0]*256 + buffer[1] + 2;
  1453. data->medium_type = buffer[2];
  1454. data->device_specific = buffer[3];
  1455. data->longlba = buffer[4] & 0x01;
  1456. data->block_descriptor_length = buffer[6]*256
  1457. + buffer[7];
  1458. } else {
  1459. data->length = buffer[0] + 1;
  1460. data->medium_type = buffer[1];
  1461. data->device_specific = buffer[2];
  1462. data->block_descriptor_length = buffer[3];
  1463. }
  1464. }
  1465. return sreq->sr_result;
  1466. }
  1467. EXPORT_SYMBOL(__scsi_mode_sense);
  1468. /**
  1469. * scsi_mode_sense - issue a mode sense, falling back from 10 to
  1470. * six bytes if necessary.
  1471. * @sdev: scsi device to send command to.
  1472. * @dbd: set if mode sense will disable block descriptors in the return
  1473. * @modepage: mode page being requested
  1474. * @buffer: request buffer (may not be smaller than eight bytes)
  1475. * @len: length of request buffer.
  1476. * @timeout: command timeout
  1477. * @retries: number of retries before failing
  1478. *
  1479. * Returns zero if unsuccessful, or the header offset (either 4
  1480. * or 8 depending on whether a six or ten byte command was
  1481. * issued) if successful.
  1482. **/
  1483. int
  1484. scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
  1485. unsigned char *buffer, int len, int timeout, int retries,
  1486. struct scsi_mode_data *data)
  1487. {
  1488. struct scsi_request *sreq = scsi_allocate_request(sdev, GFP_KERNEL);
  1489. int ret;
  1490. if (!sreq)
  1491. return -1;
  1492. ret = __scsi_mode_sense(sreq, dbd, modepage, buffer, len,
  1493. timeout, retries, data);
  1494. scsi_release_request(sreq);
  1495. return ret;
  1496. }
  1497. EXPORT_SYMBOL(scsi_mode_sense);
  1498. int
  1499. scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries)
  1500. {
  1501. struct scsi_request *sreq;
  1502. char cmd[] = {
  1503. TEST_UNIT_READY, 0, 0, 0, 0, 0,
  1504. };
  1505. int result;
  1506. sreq = scsi_allocate_request(sdev, GFP_KERNEL);
  1507. if (!sreq)
  1508. return -ENOMEM;
  1509. sreq->sr_data_direction = DMA_NONE;
  1510. scsi_wait_req(sreq, cmd, NULL, 0, timeout, retries);
  1511. if ((driver_byte(sreq->sr_result) & DRIVER_SENSE) && sdev->removable) {
  1512. struct scsi_sense_hdr sshdr;
  1513. if ((scsi_request_normalize_sense(sreq, &sshdr)) &&
  1514. ((sshdr.sense_key == UNIT_ATTENTION) ||
  1515. (sshdr.sense_key == NOT_READY))) {
  1516. sdev->changed = 1;
  1517. sreq->sr_result = 0;
  1518. }
  1519. }
  1520. result = sreq->sr_result;
  1521. scsi_release_request(sreq);
  1522. return result;
  1523. }
  1524. EXPORT_SYMBOL(scsi_test_unit_ready);
  1525. /**
  1526. * scsi_device_set_state - Take the given device through the device
  1527. * state model.
  1528. * @sdev: scsi device to change the state of.
  1529. * @state: state to change to.
  1530. *
  1531. * Returns zero if unsuccessful or an error if the requested
  1532. * transition is illegal.
  1533. **/
  1534. int
  1535. scsi_device_set_state(struct scsi_device *sdev, enum scsi_device_state state)
  1536. {
  1537. enum scsi_device_state oldstate = sdev->sdev_state;
  1538. if (state == oldstate)
  1539. return 0;
  1540. switch (state) {
  1541. case SDEV_CREATED:
  1542. /* There are no legal states that come back to
  1543. * created. This is the manually initialised start
  1544. * state */
  1545. goto illegal;
  1546. case SDEV_RUNNING:
  1547. switch (oldstate) {
  1548. case SDEV_CREATED:
  1549. case SDEV_OFFLINE:
  1550. case SDEV_QUIESCE:
  1551. case SDEV_BLOCK:
  1552. break;
  1553. default:
  1554. goto illegal;
  1555. }
  1556. break;
  1557. case SDEV_QUIESCE:
  1558. switch (oldstate) {
  1559. case SDEV_RUNNING:
  1560. case SDEV_OFFLINE:
  1561. break;
  1562. default:
  1563. goto illegal;
  1564. }
  1565. break;
  1566. case SDEV_OFFLINE:
  1567. switch (oldstate) {
  1568. case SDEV_CREATED:
  1569. case SDEV_RUNNING:
  1570. case SDEV_QUIESCE:
  1571. case SDEV_BLOCK:
  1572. break;
  1573. default:
  1574. goto illegal;
  1575. }
  1576. break;
  1577. case SDEV_BLOCK:
  1578. switch (oldstate) {
  1579. case SDEV_CREATED:
  1580. case SDEV_RUNNING:
  1581. break;
  1582. default:
  1583. goto illegal;
  1584. }
  1585. break;
  1586. case SDEV_CANCEL:
  1587. switch (oldstate) {
  1588. case SDEV_CREATED:
  1589. case SDEV_RUNNING:
  1590. case SDEV_OFFLINE:
  1591. case SDEV_BLOCK:
  1592. break;
  1593. default:
  1594. goto illegal;
  1595. }
  1596. break;
  1597. case SDEV_DEL:
  1598. switch (oldstate) {
  1599. case SDEV_CANCEL:
  1600. break;
  1601. default:
  1602. goto illegal;
  1603. }
  1604. break;
  1605. }
  1606. sdev->sdev_state = state;
  1607. return 0;
  1608. illegal:
  1609. SCSI_LOG_ERROR_RECOVERY(1,
  1610. dev_printk(KERN_ERR, &sdev->sdev_gendev,
  1611. "Illegal state transition %s->%s\n",
  1612. scsi_device_state_name(oldstate),
  1613. scsi_device_state_name(state))
  1614. );
  1615. return -EINVAL;
  1616. }
  1617. EXPORT_SYMBOL(scsi_device_set_state);
  1618. /**
  1619. * scsi_device_quiesce - Block user issued commands.
  1620. * @sdev: scsi device to quiesce.
  1621. *
  1622. * This works by trying to transition to the SDEV_QUIESCE state
  1623. * (which must be a legal transition). When the device is in this
  1624. * state, only special requests will be accepted, all others will
  1625. * be deferred. Since special requests may also be requeued requests,
  1626. * a successful return doesn't guarantee the device will be
  1627. * totally quiescent.
  1628. *
  1629. * Must be called with user context, may sleep.
  1630. *
  1631. * Returns zero if unsuccessful or an error if not.
  1632. **/
  1633. int
  1634. scsi_device_quiesce(struct scsi_device *sdev)
  1635. {
  1636. int err = scsi_device_set_state(sdev, SDEV_QUIESCE);
  1637. if (err)
  1638. return err;
  1639. scsi_run_queue(sdev->request_queue);
  1640. while (sdev->device_busy) {
  1641. msleep_interruptible(200);
  1642. scsi_run_queue(sdev->request_queue);
  1643. }
  1644. return 0;
  1645. }
  1646. EXPORT_SYMBOL(scsi_device_quiesce);
  1647. /**
  1648. * scsi_device_resume - Restart user issued commands to a quiesced device.
  1649. * @sdev: scsi device to resume.
  1650. *
  1651. * Moves the device from quiesced back to running and restarts the
  1652. * queues.
  1653. *
  1654. * Must be called with user context, may sleep.
  1655. **/
  1656. void
  1657. scsi_device_resume(struct scsi_device *sdev)
  1658. {
  1659. if(scsi_device_set_state(sdev, SDEV_RUNNING))
  1660. return;
  1661. scsi_run_queue(sdev->request_queue);
  1662. }
  1663. EXPORT_SYMBOL(scsi_device_resume);
  1664. static void
  1665. device_quiesce_fn(struct scsi_device *sdev, void *data)
  1666. {
  1667. scsi_device_quiesce(sdev);
  1668. }
  1669. void
  1670. scsi_target_quiesce(struct scsi_target *starget)
  1671. {
  1672. starget_for_each_device(starget, NULL, device_quiesce_fn);
  1673. }
  1674. EXPORT_SYMBOL(scsi_target_quiesce);
  1675. static void
  1676. device_resume_fn(struct scsi_device *sdev, void *data)
  1677. {
  1678. scsi_device_resume(sdev);
  1679. }
  1680. void
  1681. scsi_target_resume(struct scsi_target *starget)
  1682. {
  1683. starget_for_each_device(starget, NULL, device_resume_fn);
  1684. }
  1685. EXPORT_SYMBOL(scsi_target_resume);
  1686. /**
  1687. * scsi_internal_device_block - internal function to put a device
  1688. * temporarily into the SDEV_BLOCK state
  1689. * @sdev: device to block
  1690. *
  1691. * Block request made by scsi lld's to temporarily stop all
  1692. * scsi commands on the specified device. Called from interrupt
  1693. * or normal process context.
  1694. *
  1695. * Returns zero if successful or error if not
  1696. *
  1697. * Notes:
  1698. * This routine transitions the device to the SDEV_BLOCK state
  1699. * (which must be a legal transition). When the device is in this
  1700. * state, all commands are deferred until the scsi lld reenables
  1701. * the device with scsi_device_unblock or device_block_tmo fires.
  1702. * This routine assumes the host_lock is held on entry.
  1703. **/
  1704. int
  1705. scsi_internal_device_block(struct scsi_device *sdev)
  1706. {
  1707. request_queue_t *q = sdev->request_queue;
  1708. unsigned long flags;
  1709. int err = 0;
  1710. err = scsi_device_set_state(sdev, SDEV_BLOCK);
  1711. if (err)
  1712. return err;
  1713. /*
  1714. * The device has transitioned to SDEV_BLOCK. Stop the
  1715. * block layer from calling the midlayer with this device's
  1716. * request queue.
  1717. */
  1718. spin_lock_irqsave(q->queue_lock, flags);
  1719. blk_stop_queue(q);
  1720. spin_unlock_irqrestore(q->queue_lock, flags);
  1721. return 0;
  1722. }
  1723. EXPORT_SYMBOL_GPL(scsi_internal_device_block);
  1724. /**
  1725. * scsi_internal_device_unblock - resume a device after a block request
  1726. * @sdev: device to resume
  1727. *
  1728. * Called by scsi lld's or the midlayer to restart the device queue
  1729. * for the previously suspended scsi device. Called from interrupt or
  1730. * normal process context.
  1731. *
  1732. * Returns zero if successful or error if not.
  1733. *
  1734. * Notes:
  1735. * This routine transitions the device to the SDEV_RUNNING state
  1736. * (which must be a legal transition) allowing the midlayer to
  1737. * goose the queue for this device. This routine assumes the
  1738. * host_lock is held upon entry.
  1739. **/
  1740. int
  1741. scsi_internal_device_unblock(struct scsi_device *sdev)
  1742. {
  1743. request_queue_t *q = sdev->request_queue;
  1744. int err;
  1745. unsigned long flags;
  1746. /*
  1747. * Try to transition the scsi device to SDEV_RUNNING
  1748. * and goose the device queue if successful.
  1749. */
  1750. err = scsi_device_set_state(sdev, SDEV_RUNNING);
  1751. if (err)
  1752. return err;
  1753. spin_lock_irqsave(q->queue_lock, flags);
  1754. blk_start_queue(q);
  1755. spin_unlock_irqrestore(q->queue_lock, flags);
  1756. return 0;
  1757. }
  1758. EXPORT_SYMBOL_GPL(scsi_internal_device_unblock);
  1759. static void
  1760. device_block(struct scsi_device *sdev, void *data)
  1761. {
  1762. scsi_internal_device_block(sdev);
  1763. }
  1764. static int
  1765. target_block(struct device *dev, void *data)
  1766. {
  1767. if (scsi_is_target_device(dev))
  1768. starget_for_each_device(to_scsi_target(dev), NULL,
  1769. device_block);
  1770. return 0;
  1771. }
  1772. void
  1773. scsi_target_block(struct device *dev)
  1774. {
  1775. if (scsi_is_target_device(dev))
  1776. starget_for_each_device(to_scsi_target(dev), NULL,
  1777. device_block);
  1778. else
  1779. device_for_each_child(dev, NULL, target_block);
  1780. }
  1781. EXPORT_SYMBOL_GPL(scsi_target_block);
  1782. static void
  1783. device_unblock(struct scsi_device *sdev, void *data)
  1784. {
  1785. scsi_internal_device_unblock(sdev);
  1786. }
  1787. static int
  1788. target_unblock(struct device *dev, void *data)
  1789. {
  1790. if (scsi_is_target_device(dev))
  1791. starget_for_each_device(to_scsi_target(dev), NULL,
  1792. device_unblock);
  1793. return 0;
  1794. }
  1795. void
  1796. scsi_target_unblock(struct device *dev)
  1797. {
  1798. if (scsi_is_target_device(dev))
  1799. starget_for_each_device(to_scsi_target(dev), NULL,
  1800. device_unblock);
  1801. else
  1802. device_for_each_child(dev, NULL, target_unblock);
  1803. }
  1804. EXPORT_SYMBOL_GPL(scsi_target_unblock);