scsi_lib.c 53 KB

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