scsi_lib.c 67 KB

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