scsi_scan.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004
  1. /*
  2. * scsi_scan.c
  3. *
  4. * Copyright (C) 2000 Eric Youngdale,
  5. * Copyright (C) 2002 Patrick Mansfield
  6. *
  7. * The general scanning/probing algorithm is as follows, exceptions are
  8. * made to it depending on device specific flags, compilation options, and
  9. * global variable (boot or module load time) settings.
  10. *
  11. * A specific LUN is scanned via an INQUIRY command; if the LUN has a
  12. * device attached, a scsi_device is allocated and setup for it.
  13. *
  14. * For every id of every channel on the given host:
  15. *
  16. * Scan LUN 0; if the target responds to LUN 0 (even if there is no
  17. * device or storage attached to LUN 0):
  18. *
  19. * If LUN 0 has a device attached, allocate and setup a
  20. * scsi_device for it.
  21. *
  22. * If target is SCSI-3 or up, issue a REPORT LUN, and scan
  23. * all of the LUNs returned by the REPORT LUN; else,
  24. * sequentially scan LUNs up until some maximum is reached,
  25. * or a LUN is seen that cannot have a device attached to it.
  26. */
  27. #include <linux/module.h>
  28. #include <linux/moduleparam.h>
  29. #include <linux/init.h>
  30. #include <linux/blkdev.h>
  31. #include <linux/delay.h>
  32. #include <linux/kthread.h>
  33. #include <linux/spinlock.h>
  34. #include <linux/async.h>
  35. #include <linux/slab.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_cmnd.h>
  38. #include <scsi/scsi_device.h>
  39. #include <scsi/scsi_driver.h>
  40. #include <scsi/scsi_devinfo.h>
  41. #include <scsi/scsi_host.h>
  42. #include <scsi/scsi_transport.h>
  43. #include <scsi/scsi_eh.h>
  44. #include "scsi_priv.h"
  45. #include "scsi_logging.h"
  46. #define ALLOC_FAILURE_MSG KERN_ERR "%s: Allocation failure during" \
  47. " SCSI scanning, some SCSI devices might not be configured\n"
  48. /*
  49. * Default timeout
  50. */
  51. #define SCSI_TIMEOUT (2*HZ)
  52. /*
  53. * Prefix values for the SCSI id's (stored in sysfs name field)
  54. */
  55. #define SCSI_UID_SER_NUM 'S'
  56. #define SCSI_UID_UNKNOWN 'Z'
  57. /*
  58. * Return values of some of the scanning functions.
  59. *
  60. * SCSI_SCAN_NO_RESPONSE: no valid response received from the target, this
  61. * includes allocation or general failures preventing IO from being sent.
  62. *
  63. * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is available
  64. * on the given LUN.
  65. *
  66. * SCSI_SCAN_LUN_PRESENT: target responded, and a device is available on a
  67. * given LUN.
  68. */
  69. #define SCSI_SCAN_NO_RESPONSE 0
  70. #define SCSI_SCAN_TARGET_PRESENT 1
  71. #define SCSI_SCAN_LUN_PRESENT 2
  72. static const char *scsi_null_device_strs = "nullnullnullnull";
  73. #define MAX_SCSI_LUNS 512
  74. static u64 max_scsi_luns = MAX_SCSI_LUNS;
  75. module_param_named(max_luns, max_scsi_luns, ullong, S_IRUGO|S_IWUSR);
  76. MODULE_PARM_DESC(max_luns,
  77. "last scsi LUN (should be between 1 and 2^64-1)");
  78. #ifdef CONFIG_SCSI_SCAN_ASYNC
  79. #define SCSI_SCAN_TYPE_DEFAULT "async"
  80. #else
  81. #define SCSI_SCAN_TYPE_DEFAULT "sync"
  82. #endif
  83. char scsi_scan_type[6] = SCSI_SCAN_TYPE_DEFAULT;
  84. module_param_string(scan, scsi_scan_type, sizeof(scsi_scan_type), S_IRUGO);
  85. MODULE_PARM_DESC(scan, "sync, async or none");
  86. /*
  87. * max_scsi_report_luns: the maximum number of LUNS that will be
  88. * returned from the REPORT LUNS command. 8 times this value must
  89. * be allocated. In theory this could be up to an 8 byte value, but
  90. * in practice, the maximum number of LUNs suppored by any device
  91. * is about 16k.
  92. */
  93. static unsigned int max_scsi_report_luns = 511;
  94. module_param_named(max_report_luns, max_scsi_report_luns, uint, S_IRUGO|S_IWUSR);
  95. MODULE_PARM_DESC(max_report_luns,
  96. "REPORT LUNS maximum number of LUNS received (should be"
  97. " between 1 and 16384)");
  98. static unsigned int scsi_inq_timeout = SCSI_TIMEOUT/HZ + 18;
  99. module_param_named(inq_timeout, scsi_inq_timeout, uint, S_IRUGO|S_IWUSR);
  100. MODULE_PARM_DESC(inq_timeout,
  101. "Timeout (in seconds) waiting for devices to answer INQUIRY."
  102. " Default is 20. Some devices may need more; most need less.");
  103. /* This lock protects only this list */
  104. static DEFINE_SPINLOCK(async_scan_lock);
  105. static LIST_HEAD(scanning_hosts);
  106. struct async_scan_data {
  107. struct list_head list;
  108. struct Scsi_Host *shost;
  109. struct completion prev_finished;
  110. };
  111. /**
  112. * scsi_complete_async_scans - Wait for asynchronous scans to complete
  113. *
  114. * When this function returns, any host which started scanning before
  115. * this function was called will have finished its scan. Hosts which
  116. * started scanning after this function was called may or may not have
  117. * finished.
  118. */
  119. int scsi_complete_async_scans(void)
  120. {
  121. struct async_scan_data *data;
  122. do {
  123. if (list_empty(&scanning_hosts))
  124. return 0;
  125. /* If we can't get memory immediately, that's OK. Just
  126. * sleep a little. Even if we never get memory, the async
  127. * scans will finish eventually.
  128. */
  129. data = kmalloc(sizeof(*data), GFP_KERNEL);
  130. if (!data)
  131. msleep(1);
  132. } while (!data);
  133. data->shost = NULL;
  134. init_completion(&data->prev_finished);
  135. spin_lock(&async_scan_lock);
  136. /* Check that there's still somebody else on the list */
  137. if (list_empty(&scanning_hosts))
  138. goto done;
  139. list_add_tail(&data->list, &scanning_hosts);
  140. spin_unlock(&async_scan_lock);
  141. printk(KERN_INFO "scsi: waiting for bus probes to complete ...\n");
  142. wait_for_completion(&data->prev_finished);
  143. spin_lock(&async_scan_lock);
  144. list_del(&data->list);
  145. if (!list_empty(&scanning_hosts)) {
  146. struct async_scan_data *next = list_entry(scanning_hosts.next,
  147. struct async_scan_data, list);
  148. complete(&next->prev_finished);
  149. }
  150. done:
  151. spin_unlock(&async_scan_lock);
  152. kfree(data);
  153. return 0;
  154. }
  155. /**
  156. * scsi_unlock_floptical - unlock device via a special MODE SENSE command
  157. * @sdev: scsi device to send command to
  158. * @result: area to store the result of the MODE SENSE
  159. *
  160. * Description:
  161. * Send a vendor specific MODE SENSE (not a MODE SELECT) command.
  162. * Called for BLIST_KEY devices.
  163. **/
  164. static void scsi_unlock_floptical(struct scsi_device *sdev,
  165. unsigned char *result)
  166. {
  167. unsigned char scsi_cmd[MAX_COMMAND_SIZE];
  168. sdev_printk(KERN_NOTICE, sdev, "unlocking floptical drive\n");
  169. scsi_cmd[0] = MODE_SENSE;
  170. scsi_cmd[1] = 0;
  171. scsi_cmd[2] = 0x2e;
  172. scsi_cmd[3] = 0;
  173. scsi_cmd[4] = 0x2a; /* size */
  174. scsi_cmd[5] = 0;
  175. scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE, result, 0x2a, NULL,
  176. SCSI_TIMEOUT, 3, NULL);
  177. }
  178. /**
  179. * scsi_alloc_sdev - allocate and setup a scsi_Device
  180. * @starget: which target to allocate a &scsi_device for
  181. * @lun: which lun
  182. * @hostdata: usually NULL and set by ->slave_alloc instead
  183. *
  184. * Description:
  185. * Allocate, initialize for io, and return a pointer to a scsi_Device.
  186. * Stores the @shost, @channel, @id, and @lun in the scsi_Device, and
  187. * adds scsi_Device to the appropriate list.
  188. *
  189. * Return value:
  190. * scsi_Device pointer, or NULL on failure.
  191. **/
  192. static struct scsi_device *scsi_alloc_sdev(struct scsi_target *starget,
  193. u64 lun, void *hostdata)
  194. {
  195. struct scsi_device *sdev;
  196. int display_failure_msg = 1, ret;
  197. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  198. extern void scsi_evt_thread(struct work_struct *work);
  199. extern void scsi_requeue_run_queue(struct work_struct *work);
  200. sdev = kzalloc(sizeof(*sdev) + shost->transportt->device_size,
  201. GFP_ATOMIC);
  202. if (!sdev)
  203. goto out;
  204. sdev->vendor = scsi_null_device_strs;
  205. sdev->model = scsi_null_device_strs;
  206. sdev->rev = scsi_null_device_strs;
  207. sdev->host = shost;
  208. sdev->queue_ramp_up_period = SCSI_DEFAULT_RAMP_UP_PERIOD;
  209. sdev->id = starget->id;
  210. sdev->lun = lun;
  211. sdev->channel = starget->channel;
  212. sdev->sdev_state = SDEV_CREATED;
  213. INIT_LIST_HEAD(&sdev->siblings);
  214. INIT_LIST_HEAD(&sdev->same_target_siblings);
  215. INIT_LIST_HEAD(&sdev->cmd_list);
  216. INIT_LIST_HEAD(&sdev->starved_entry);
  217. INIT_LIST_HEAD(&sdev->event_list);
  218. spin_lock_init(&sdev->list_lock);
  219. INIT_WORK(&sdev->event_work, scsi_evt_thread);
  220. INIT_WORK(&sdev->requeue_work, scsi_requeue_run_queue);
  221. sdev->sdev_gendev.parent = get_device(&starget->dev);
  222. sdev->sdev_target = starget;
  223. /* usually NULL and set by ->slave_alloc instead */
  224. sdev->hostdata = hostdata;
  225. /* if the device needs this changing, it may do so in the
  226. * slave_configure function */
  227. sdev->max_device_blocked = SCSI_DEFAULT_DEVICE_BLOCKED;
  228. /*
  229. * Some low level driver could use device->type
  230. */
  231. sdev->type = -1;
  232. /*
  233. * Assume that the device will have handshaking problems,
  234. * and then fix this field later if it turns out it
  235. * doesn't
  236. */
  237. sdev->borken = 1;
  238. if (shost_use_blk_mq(shost))
  239. sdev->request_queue = scsi_mq_alloc_queue(sdev);
  240. else
  241. sdev->request_queue = scsi_alloc_queue(sdev);
  242. if (!sdev->request_queue) {
  243. /* release fn is set up in scsi_sysfs_device_initialise, so
  244. * have to free and put manually here */
  245. put_device(&starget->dev);
  246. kfree(sdev);
  247. goto out;
  248. }
  249. WARN_ON_ONCE(!blk_get_queue(sdev->request_queue));
  250. sdev->request_queue->queuedata = sdev;
  251. if (!shost_use_blk_mq(sdev->host) &&
  252. (shost->bqt || shost->hostt->use_blk_tags)) {
  253. blk_queue_init_tags(sdev->request_queue,
  254. sdev->host->cmd_per_lun, shost->bqt);
  255. }
  256. scsi_change_queue_depth(sdev, sdev->host->cmd_per_lun);
  257. scsi_sysfs_device_initialize(sdev);
  258. if (shost->hostt->slave_alloc) {
  259. ret = shost->hostt->slave_alloc(sdev);
  260. if (ret) {
  261. /*
  262. * if LLDD reports slave not present, don't clutter
  263. * console with alloc failure messages
  264. */
  265. if (ret == -ENXIO)
  266. display_failure_msg = 0;
  267. goto out_device_destroy;
  268. }
  269. }
  270. return sdev;
  271. out_device_destroy:
  272. __scsi_remove_device(sdev);
  273. out:
  274. if (display_failure_msg)
  275. printk(ALLOC_FAILURE_MSG, __func__);
  276. return NULL;
  277. }
  278. static void scsi_target_destroy(struct scsi_target *starget)
  279. {
  280. struct device *dev = &starget->dev;
  281. struct Scsi_Host *shost = dev_to_shost(dev->parent);
  282. unsigned long flags;
  283. starget->state = STARGET_DEL;
  284. transport_destroy_device(dev);
  285. spin_lock_irqsave(shost->host_lock, flags);
  286. if (shost->hostt->target_destroy)
  287. shost->hostt->target_destroy(starget);
  288. list_del_init(&starget->siblings);
  289. spin_unlock_irqrestore(shost->host_lock, flags);
  290. put_device(dev);
  291. }
  292. static void scsi_target_dev_release(struct device *dev)
  293. {
  294. struct device *parent = dev->parent;
  295. struct scsi_target *starget = to_scsi_target(dev);
  296. kfree(starget);
  297. put_device(parent);
  298. }
  299. static struct device_type scsi_target_type = {
  300. .name = "scsi_target",
  301. .release = scsi_target_dev_release,
  302. };
  303. int scsi_is_target_device(const struct device *dev)
  304. {
  305. return dev->type == &scsi_target_type;
  306. }
  307. EXPORT_SYMBOL(scsi_is_target_device);
  308. static struct scsi_target *__scsi_find_target(struct device *parent,
  309. int channel, uint id)
  310. {
  311. struct scsi_target *starget, *found_starget = NULL;
  312. struct Scsi_Host *shost = dev_to_shost(parent);
  313. /*
  314. * Search for an existing target for this sdev.
  315. */
  316. list_for_each_entry(starget, &shost->__targets, siblings) {
  317. if (starget->id == id &&
  318. starget->channel == channel) {
  319. found_starget = starget;
  320. break;
  321. }
  322. }
  323. if (found_starget)
  324. get_device(&found_starget->dev);
  325. return found_starget;
  326. }
  327. /**
  328. * scsi_target_reap_ref_release - remove target from visibility
  329. * @kref: the reap_ref in the target being released
  330. *
  331. * Called on last put of reap_ref, which is the indication that no device
  332. * under this target is visible anymore, so render the target invisible in
  333. * sysfs. Note: we have to be in user context here because the target reaps
  334. * should be done in places where the scsi device visibility is being removed.
  335. */
  336. static void scsi_target_reap_ref_release(struct kref *kref)
  337. {
  338. struct scsi_target *starget
  339. = container_of(kref, struct scsi_target, reap_ref);
  340. /*
  341. * if we get here and the target is still in the CREATED state that
  342. * means it was allocated but never made visible (because a scan
  343. * turned up no LUNs), so don't call device_del() on it.
  344. */
  345. if (starget->state != STARGET_CREATED) {
  346. transport_remove_device(&starget->dev);
  347. device_del(&starget->dev);
  348. }
  349. scsi_target_destroy(starget);
  350. }
  351. static void scsi_target_reap_ref_put(struct scsi_target *starget)
  352. {
  353. kref_put(&starget->reap_ref, scsi_target_reap_ref_release);
  354. }
  355. /**
  356. * scsi_alloc_target - allocate a new or find an existing target
  357. * @parent: parent of the target (need not be a scsi host)
  358. * @channel: target channel number (zero if no channels)
  359. * @id: target id number
  360. *
  361. * Return an existing target if one exists, provided it hasn't already
  362. * gone into STARGET_DEL state, otherwise allocate a new target.
  363. *
  364. * The target is returned with an incremented reference, so the caller
  365. * is responsible for both reaping and doing a last put
  366. */
  367. static struct scsi_target *scsi_alloc_target(struct device *parent,
  368. int channel, uint id)
  369. {
  370. struct Scsi_Host *shost = dev_to_shost(parent);
  371. struct device *dev = NULL;
  372. unsigned long flags;
  373. const int size = sizeof(struct scsi_target)
  374. + shost->transportt->target_size;
  375. struct scsi_target *starget;
  376. struct scsi_target *found_target;
  377. int error, ref_got;
  378. starget = kzalloc(size, GFP_KERNEL);
  379. if (!starget) {
  380. printk(KERN_ERR "%s: allocation failure\n", __func__);
  381. return NULL;
  382. }
  383. dev = &starget->dev;
  384. device_initialize(dev);
  385. kref_init(&starget->reap_ref);
  386. dev->parent = get_device(parent);
  387. dev_set_name(dev, "target%d:%d:%d", shost->host_no, channel, id);
  388. dev->bus = &scsi_bus_type;
  389. dev->type = &scsi_target_type;
  390. starget->id = id;
  391. starget->channel = channel;
  392. starget->can_queue = 0;
  393. INIT_LIST_HEAD(&starget->siblings);
  394. INIT_LIST_HEAD(&starget->devices);
  395. starget->state = STARGET_CREATED;
  396. starget->scsi_level = SCSI_2;
  397. starget->max_target_blocked = SCSI_DEFAULT_TARGET_BLOCKED;
  398. retry:
  399. spin_lock_irqsave(shost->host_lock, flags);
  400. found_target = __scsi_find_target(parent, channel, id);
  401. if (found_target)
  402. goto found;
  403. list_add_tail(&starget->siblings, &shost->__targets);
  404. spin_unlock_irqrestore(shost->host_lock, flags);
  405. /* allocate and add */
  406. transport_setup_device(dev);
  407. if (shost->hostt->target_alloc) {
  408. error = shost->hostt->target_alloc(starget);
  409. if(error) {
  410. dev_printk(KERN_ERR, dev, "target allocation failed, error %d\n", error);
  411. /* don't want scsi_target_reap to do the final
  412. * put because it will be under the host lock */
  413. scsi_target_destroy(starget);
  414. return NULL;
  415. }
  416. }
  417. get_device(dev);
  418. return starget;
  419. found:
  420. /*
  421. * release routine already fired if kref is zero, so if we can still
  422. * take the reference, the target must be alive. If we can't, it must
  423. * be dying and we need to wait for a new target
  424. */
  425. ref_got = kref_get_unless_zero(&found_target->reap_ref);
  426. spin_unlock_irqrestore(shost->host_lock, flags);
  427. if (ref_got) {
  428. put_device(dev);
  429. return found_target;
  430. }
  431. /*
  432. * Unfortunately, we found a dying target; need to wait until it's
  433. * dead before we can get a new one. There is an anomaly here. We
  434. * *should* call scsi_target_reap() to balance the kref_get() of the
  435. * reap_ref above. However, since the target being released, it's
  436. * already invisible and the reap_ref is irrelevant. If we call
  437. * scsi_target_reap() we might spuriously do another device_del() on
  438. * an already invisible target.
  439. */
  440. put_device(&found_target->dev);
  441. /*
  442. * length of time is irrelevant here, we just want to yield the CPU
  443. * for a tick to avoid busy waiting for the target to die.
  444. */
  445. msleep(1);
  446. goto retry;
  447. }
  448. /**
  449. * scsi_target_reap - check to see if target is in use and destroy if not
  450. * @starget: target to be checked
  451. *
  452. * This is used after removing a LUN or doing a last put of the target
  453. * it checks atomically that nothing is using the target and removes
  454. * it if so.
  455. */
  456. void scsi_target_reap(struct scsi_target *starget)
  457. {
  458. /*
  459. * serious problem if this triggers: STARGET_DEL is only set in the if
  460. * the reap_ref drops to zero, so we're trying to do another final put
  461. * on an already released kref
  462. */
  463. BUG_ON(starget->state == STARGET_DEL);
  464. scsi_target_reap_ref_put(starget);
  465. }
  466. /**
  467. * sanitize_inquiry_string - remove non-graphical chars from an INQUIRY result string
  468. * @s: INQUIRY result string to sanitize
  469. * @len: length of the string
  470. *
  471. * Description:
  472. * The SCSI spec says that INQUIRY vendor, product, and revision
  473. * strings must consist entirely of graphic ASCII characters,
  474. * padded on the right with spaces. Since not all devices obey
  475. * this rule, we will replace non-graphic or non-ASCII characters
  476. * with spaces. Exception: a NUL character is interpreted as a
  477. * string terminator, so all the following characters are set to
  478. * spaces.
  479. **/
  480. static void sanitize_inquiry_string(unsigned char *s, int len)
  481. {
  482. int terminated = 0;
  483. for (; len > 0; (--len, ++s)) {
  484. if (*s == 0)
  485. terminated = 1;
  486. if (terminated || *s < 0x20 || *s > 0x7e)
  487. *s = ' ';
  488. }
  489. }
  490. /**
  491. * scsi_probe_lun - probe a single LUN using a SCSI INQUIRY
  492. * @sdev: scsi_device to probe
  493. * @inq_result: area to store the INQUIRY result
  494. * @result_len: len of inq_result
  495. * @bflags: store any bflags found here
  496. *
  497. * Description:
  498. * Probe the lun associated with @req using a standard SCSI INQUIRY;
  499. *
  500. * If the INQUIRY is successful, zero is returned and the
  501. * INQUIRY data is in @inq_result; the scsi_level and INQUIRY length
  502. * are copied to the scsi_device any flags value is stored in *@bflags.
  503. **/
  504. static int scsi_probe_lun(struct scsi_device *sdev, unsigned char *inq_result,
  505. int result_len, int *bflags)
  506. {
  507. unsigned char scsi_cmd[MAX_COMMAND_SIZE];
  508. int first_inquiry_len, try_inquiry_len, next_inquiry_len;
  509. int response_len = 0;
  510. int pass, count, result;
  511. struct scsi_sense_hdr sshdr;
  512. *bflags = 0;
  513. /* Perform up to 3 passes. The first pass uses a conservative
  514. * transfer length of 36 unless sdev->inquiry_len specifies a
  515. * different value. */
  516. first_inquiry_len = sdev->inquiry_len ? sdev->inquiry_len : 36;
  517. try_inquiry_len = first_inquiry_len;
  518. pass = 1;
  519. next_pass:
  520. SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
  521. "scsi scan: INQUIRY pass %d length %d\n",
  522. pass, try_inquiry_len));
  523. /* Each pass gets up to three chances to ignore Unit Attention */
  524. for (count = 0; count < 3; ++count) {
  525. int resid;
  526. memset(scsi_cmd, 0, 6);
  527. scsi_cmd[0] = INQUIRY;
  528. scsi_cmd[4] = (unsigned char) try_inquiry_len;
  529. memset(inq_result, 0, try_inquiry_len);
  530. result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
  531. inq_result, try_inquiry_len, &sshdr,
  532. HZ / 2 + HZ * scsi_inq_timeout, 3,
  533. &resid);
  534. SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
  535. "scsi scan: INQUIRY %s with code 0x%x\n",
  536. result ? "failed" : "successful", result));
  537. if (result) {
  538. /*
  539. * not-ready to ready transition [asc/ascq=0x28/0x0]
  540. * or power-on, reset [asc/ascq=0x29/0x0], continue.
  541. * INQUIRY should not yield UNIT_ATTENTION
  542. * but many buggy devices do so anyway.
  543. */
  544. if ((driver_byte(result) & DRIVER_SENSE) &&
  545. scsi_sense_valid(&sshdr)) {
  546. if ((sshdr.sense_key == UNIT_ATTENTION) &&
  547. ((sshdr.asc == 0x28) ||
  548. (sshdr.asc == 0x29)) &&
  549. (sshdr.ascq == 0))
  550. continue;
  551. }
  552. } else {
  553. /*
  554. * if nothing was transferred, we try
  555. * again. It's a workaround for some USB
  556. * devices.
  557. */
  558. if (resid == try_inquiry_len)
  559. continue;
  560. }
  561. break;
  562. }
  563. if (result == 0) {
  564. sanitize_inquiry_string(&inq_result[8], 8);
  565. sanitize_inquiry_string(&inq_result[16], 16);
  566. sanitize_inquiry_string(&inq_result[32], 4);
  567. response_len = inq_result[4] + 5;
  568. if (response_len > 255)
  569. response_len = first_inquiry_len; /* sanity */
  570. /*
  571. * Get any flags for this device.
  572. *
  573. * XXX add a bflags to scsi_device, and replace the
  574. * corresponding bit fields in scsi_device, so bflags
  575. * need not be passed as an argument.
  576. */
  577. *bflags = scsi_get_device_flags(sdev, &inq_result[8],
  578. &inq_result[16]);
  579. /* When the first pass succeeds we gain information about
  580. * what larger transfer lengths might work. */
  581. if (pass == 1) {
  582. if (BLIST_INQUIRY_36 & *bflags)
  583. next_inquiry_len = 36;
  584. else if (BLIST_INQUIRY_58 & *bflags)
  585. next_inquiry_len = 58;
  586. else if (sdev->inquiry_len)
  587. next_inquiry_len = sdev->inquiry_len;
  588. else
  589. next_inquiry_len = response_len;
  590. /* If more data is available perform the second pass */
  591. if (next_inquiry_len > try_inquiry_len) {
  592. try_inquiry_len = next_inquiry_len;
  593. pass = 2;
  594. goto next_pass;
  595. }
  596. }
  597. } else if (pass == 2) {
  598. sdev_printk(KERN_INFO, sdev,
  599. "scsi scan: %d byte inquiry failed. "
  600. "Consider BLIST_INQUIRY_36 for this device\n",
  601. try_inquiry_len);
  602. /* If this pass failed, the third pass goes back and transfers
  603. * the same amount as we successfully got in the first pass. */
  604. try_inquiry_len = first_inquiry_len;
  605. pass = 3;
  606. goto next_pass;
  607. }
  608. /* If the last transfer attempt got an error, assume the
  609. * peripheral doesn't exist or is dead. */
  610. if (result)
  611. return -EIO;
  612. /* Don't report any more data than the device says is valid */
  613. sdev->inquiry_len = min(try_inquiry_len, response_len);
  614. /*
  615. * XXX Abort if the response length is less than 36? If less than
  616. * 32, the lookup of the device flags (above) could be invalid,
  617. * and it would be possible to take an incorrect action - we do
  618. * not want to hang because of a short INQUIRY. On the flip side,
  619. * if the device is spun down or becoming ready (and so it gives a
  620. * short INQUIRY), an abort here prevents any further use of the
  621. * device, including spin up.
  622. *
  623. * On the whole, the best approach seems to be to assume the first
  624. * 36 bytes are valid no matter what the device says. That's
  625. * better than copying < 36 bytes to the inquiry-result buffer
  626. * and displaying garbage for the Vendor, Product, or Revision
  627. * strings.
  628. */
  629. if (sdev->inquiry_len < 36) {
  630. sdev_printk(KERN_INFO, sdev,
  631. "scsi scan: INQUIRY result too short (%d),"
  632. " using 36\n", sdev->inquiry_len);
  633. sdev->inquiry_len = 36;
  634. }
  635. /*
  636. * Related to the above issue:
  637. *
  638. * XXX Devices (disk or all?) should be sent a TEST UNIT READY,
  639. * and if not ready, sent a START_STOP to start (maybe spin up) and
  640. * then send the INQUIRY again, since the INQUIRY can change after
  641. * a device is initialized.
  642. *
  643. * Ideally, start a device if explicitly asked to do so. This
  644. * assumes that a device is spun up on power on, spun down on
  645. * request, and then spun up on request.
  646. */
  647. /*
  648. * The scanning code needs to know the scsi_level, even if no
  649. * device is attached at LUN 0 (SCSI_SCAN_TARGET_PRESENT) so
  650. * non-zero LUNs can be scanned.
  651. */
  652. sdev->scsi_level = inq_result[2] & 0x07;
  653. if (sdev->scsi_level >= 2 ||
  654. (sdev->scsi_level == 1 && (inq_result[3] & 0x0f) == 1))
  655. sdev->scsi_level++;
  656. sdev->sdev_target->scsi_level = sdev->scsi_level;
  657. /*
  658. * If SCSI-2 or lower, and if the transport requires it,
  659. * store the LUN value in CDB[1].
  660. */
  661. sdev->lun_in_cdb = 0;
  662. if (sdev->scsi_level <= SCSI_2 &&
  663. sdev->scsi_level != SCSI_UNKNOWN &&
  664. !sdev->host->no_scsi2_lun_in_cdb)
  665. sdev->lun_in_cdb = 1;
  666. return 0;
  667. }
  668. /**
  669. * scsi_add_lun - allocate and fully initialze a scsi_device
  670. * @sdev: holds information to be stored in the new scsi_device
  671. * @inq_result: holds the result of a previous INQUIRY to the LUN
  672. * @bflags: black/white list flag
  673. * @async: 1 if this device is being scanned asynchronously
  674. *
  675. * Description:
  676. * Initialize the scsi_device @sdev. Optionally set fields based
  677. * on values in *@bflags.
  678. *
  679. * Return:
  680. * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
  681. * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
  682. **/
  683. static int scsi_add_lun(struct scsi_device *sdev, unsigned char *inq_result,
  684. int *bflags, int async)
  685. {
  686. int ret;
  687. /*
  688. * XXX do not save the inquiry, since it can change underneath us,
  689. * save just vendor/model/rev.
  690. *
  691. * Rather than save it and have an ioctl that retrieves the saved
  692. * value, have an ioctl that executes the same INQUIRY code used
  693. * in scsi_probe_lun, let user level programs doing INQUIRY
  694. * scanning run at their own risk, or supply a user level program
  695. * that can correctly scan.
  696. */
  697. /*
  698. * Copy at least 36 bytes of INQUIRY data, so that we don't
  699. * dereference unallocated memory when accessing the Vendor,
  700. * Product, and Revision strings. Badly behaved devices may set
  701. * the INQUIRY Additional Length byte to a small value, indicating
  702. * these strings are invalid, but often they contain plausible data
  703. * nonetheless. It doesn't matter if the device sent < 36 bytes
  704. * total, since scsi_probe_lun() initializes inq_result with 0s.
  705. */
  706. sdev->inquiry = kmemdup(inq_result,
  707. max_t(size_t, sdev->inquiry_len, 36),
  708. GFP_ATOMIC);
  709. if (sdev->inquiry == NULL)
  710. return SCSI_SCAN_NO_RESPONSE;
  711. sdev->vendor = (char *) (sdev->inquiry + 8);
  712. sdev->model = (char *) (sdev->inquiry + 16);
  713. sdev->rev = (char *) (sdev->inquiry + 32);
  714. if (strncmp(sdev->vendor, "ATA ", 8) == 0) {
  715. /*
  716. * sata emulation layer device. This is a hack to work around
  717. * the SATL power management specifications which state that
  718. * when the SATL detects the device has gone into standby
  719. * mode, it shall respond with NOT READY.
  720. */
  721. sdev->allow_restart = 1;
  722. }
  723. if (*bflags & BLIST_ISROM) {
  724. sdev->type = TYPE_ROM;
  725. sdev->removable = 1;
  726. } else {
  727. sdev->type = (inq_result[0] & 0x1f);
  728. sdev->removable = (inq_result[1] & 0x80) >> 7;
  729. /*
  730. * some devices may respond with wrong type for
  731. * well-known logical units. Force well-known type
  732. * to enumerate them correctly.
  733. */
  734. if (scsi_is_wlun(sdev->lun) && sdev->type != TYPE_WLUN) {
  735. sdev_printk(KERN_WARNING, sdev,
  736. "%s: correcting incorrect peripheral device type 0x%x for W-LUN 0x%16xhN\n",
  737. __func__, sdev->type, (unsigned int)sdev->lun);
  738. sdev->type = TYPE_WLUN;
  739. }
  740. }
  741. if (sdev->type == TYPE_RBC || sdev->type == TYPE_ROM) {
  742. /* RBC and MMC devices can return SCSI-3 compliance and yet
  743. * still not support REPORT LUNS, so make them act as
  744. * BLIST_NOREPORTLUN unless BLIST_REPORTLUN2 is
  745. * specifically set */
  746. if ((*bflags & BLIST_REPORTLUN2) == 0)
  747. *bflags |= BLIST_NOREPORTLUN;
  748. }
  749. /*
  750. * For a peripheral qualifier (PQ) value of 1 (001b), the SCSI
  751. * spec says: The device server is capable of supporting the
  752. * specified peripheral device type on this logical unit. However,
  753. * the physical device is not currently connected to this logical
  754. * unit.
  755. *
  756. * The above is vague, as it implies that we could treat 001 and
  757. * 011 the same. Stay compatible with previous code, and create a
  758. * scsi_device for a PQ of 1
  759. *
  760. * Don't set the device offline here; rather let the upper
  761. * level drivers eval the PQ to decide whether they should
  762. * attach. So remove ((inq_result[0] >> 5) & 7) == 1 check.
  763. */
  764. sdev->inq_periph_qual = (inq_result[0] >> 5) & 7;
  765. sdev->lockable = sdev->removable;
  766. sdev->soft_reset = (inq_result[7] & 1) && ((inq_result[3] & 7) == 2);
  767. if (sdev->scsi_level >= SCSI_3 ||
  768. (sdev->inquiry_len > 56 && inq_result[56] & 0x04))
  769. sdev->ppr = 1;
  770. if (inq_result[7] & 0x60)
  771. sdev->wdtr = 1;
  772. if (inq_result[7] & 0x10)
  773. sdev->sdtr = 1;
  774. sdev_printk(KERN_NOTICE, sdev, "%s %.8s %.16s %.4s PQ: %d "
  775. "ANSI: %d%s\n", scsi_device_type(sdev->type),
  776. sdev->vendor, sdev->model, sdev->rev,
  777. sdev->inq_periph_qual, inq_result[2] & 0x07,
  778. (inq_result[3] & 0x0f) == 1 ? " CCS" : "");
  779. if ((sdev->scsi_level >= SCSI_2) && (inq_result[7] & 2) &&
  780. !(*bflags & BLIST_NOTQ)) {
  781. sdev->tagged_supported = 1;
  782. sdev->simple_tags = 1;
  783. }
  784. /*
  785. * Some devices (Texel CD ROM drives) have handshaking problems
  786. * when used with the Seagate controllers. borken is initialized
  787. * to 1, and then set it to 0 here.
  788. */
  789. if ((*bflags & BLIST_BORKEN) == 0)
  790. sdev->borken = 0;
  791. if (*bflags & BLIST_NO_ULD_ATTACH)
  792. sdev->no_uld_attach = 1;
  793. /*
  794. * Apparently some really broken devices (contrary to the SCSI
  795. * standards) need to be selected without asserting ATN
  796. */
  797. if (*bflags & BLIST_SELECT_NO_ATN)
  798. sdev->select_no_atn = 1;
  799. /*
  800. * Maximum 512 sector transfer length
  801. * broken RA4x00 Compaq Disk Array
  802. */
  803. if (*bflags & BLIST_MAX_512)
  804. blk_queue_max_hw_sectors(sdev->request_queue, 512);
  805. /*
  806. * Some devices may not want to have a start command automatically
  807. * issued when a device is added.
  808. */
  809. if (*bflags & BLIST_NOSTARTONADD)
  810. sdev->no_start_on_add = 1;
  811. if (*bflags & BLIST_SINGLELUN)
  812. scsi_target(sdev)->single_lun = 1;
  813. sdev->use_10_for_rw = 1;
  814. if (*bflags & BLIST_MS_SKIP_PAGE_08)
  815. sdev->skip_ms_page_8 = 1;
  816. if (*bflags & BLIST_MS_SKIP_PAGE_3F)
  817. sdev->skip_ms_page_3f = 1;
  818. if (*bflags & BLIST_USE_10_BYTE_MS)
  819. sdev->use_10_for_ms = 1;
  820. /* some devices don't like REPORT SUPPORTED OPERATION CODES
  821. * and will simply timeout causing sd_mod init to take a very
  822. * very long time */
  823. if (*bflags & BLIST_NO_RSOC)
  824. sdev->no_report_opcodes = 1;
  825. /* set the device running here so that slave configure
  826. * may do I/O */
  827. ret = scsi_device_set_state(sdev, SDEV_RUNNING);
  828. if (ret) {
  829. ret = scsi_device_set_state(sdev, SDEV_BLOCK);
  830. if (ret) {
  831. sdev_printk(KERN_ERR, sdev,
  832. "in wrong state %s to complete scan\n",
  833. scsi_device_state_name(sdev->sdev_state));
  834. return SCSI_SCAN_NO_RESPONSE;
  835. }
  836. }
  837. if (*bflags & BLIST_MS_192_BYTES_FOR_3F)
  838. sdev->use_192_bytes_for_3f = 1;
  839. if (*bflags & BLIST_NOT_LOCKABLE)
  840. sdev->lockable = 0;
  841. if (*bflags & BLIST_RETRY_HWERROR)
  842. sdev->retry_hwerror = 1;
  843. if (*bflags & BLIST_NO_DIF)
  844. sdev->no_dif = 1;
  845. sdev->eh_timeout = SCSI_DEFAULT_EH_TIMEOUT;
  846. if (*bflags & BLIST_TRY_VPD_PAGES)
  847. sdev->try_vpd_pages = 1;
  848. else if (*bflags & BLIST_SKIP_VPD_PAGES)
  849. sdev->skip_vpd_pages = 1;
  850. transport_configure_device(&sdev->sdev_gendev);
  851. if (sdev->host->hostt->slave_configure) {
  852. ret = sdev->host->hostt->slave_configure(sdev);
  853. if (ret) {
  854. /*
  855. * if LLDD reports slave not present, don't clutter
  856. * console with alloc failure messages
  857. */
  858. if (ret != -ENXIO) {
  859. sdev_printk(KERN_ERR, sdev,
  860. "failed to configure device\n");
  861. }
  862. return SCSI_SCAN_NO_RESPONSE;
  863. }
  864. }
  865. if (sdev->scsi_level >= SCSI_3)
  866. scsi_attach_vpd(sdev);
  867. sdev->max_queue_depth = sdev->queue_depth;
  868. /*
  869. * Ok, the device is now all set up, we can
  870. * register it and tell the rest of the kernel
  871. * about it.
  872. */
  873. if (!async && scsi_sysfs_add_sdev(sdev) != 0)
  874. return SCSI_SCAN_NO_RESPONSE;
  875. return SCSI_SCAN_LUN_PRESENT;
  876. }
  877. #ifdef CONFIG_SCSI_LOGGING
  878. /**
  879. * scsi_inq_str - print INQUIRY data from min to max index, strip trailing whitespace
  880. * @buf: Output buffer with at least end-first+1 bytes of space
  881. * @inq: Inquiry buffer (input)
  882. * @first: Offset of string into inq
  883. * @end: Index after last character in inq
  884. */
  885. static unsigned char *scsi_inq_str(unsigned char *buf, unsigned char *inq,
  886. unsigned first, unsigned end)
  887. {
  888. unsigned term = 0, idx;
  889. for (idx = 0; idx + first < end && idx + first < inq[4] + 5; idx++) {
  890. if (inq[idx+first] > ' ') {
  891. buf[idx] = inq[idx+first];
  892. term = idx+1;
  893. } else {
  894. buf[idx] = ' ';
  895. }
  896. }
  897. buf[term] = 0;
  898. return buf;
  899. }
  900. #endif
  901. /**
  902. * scsi_probe_and_add_lun - probe a LUN, if a LUN is found add it
  903. * @starget: pointer to target device structure
  904. * @lun: LUN of target device
  905. * @bflagsp: store bflags here if not NULL
  906. * @sdevp: probe the LUN corresponding to this scsi_device
  907. * @rescan: if nonzero skip some code only needed on first scan
  908. * @hostdata: passed to scsi_alloc_sdev()
  909. *
  910. * Description:
  911. * Call scsi_probe_lun, if a LUN with an attached device is found,
  912. * allocate and set it up by calling scsi_add_lun.
  913. *
  914. * Return:
  915. * SCSI_SCAN_NO_RESPONSE: could not allocate or setup a scsi_device
  916. * SCSI_SCAN_TARGET_PRESENT: target responded, but no device is
  917. * attached at the LUN
  918. * SCSI_SCAN_LUN_PRESENT: a new scsi_device was allocated and initialized
  919. **/
  920. static int scsi_probe_and_add_lun(struct scsi_target *starget,
  921. u64 lun, int *bflagsp,
  922. struct scsi_device **sdevp, int rescan,
  923. void *hostdata)
  924. {
  925. struct scsi_device *sdev;
  926. unsigned char *result;
  927. int bflags, res = SCSI_SCAN_NO_RESPONSE, result_len = 256;
  928. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  929. /*
  930. * The rescan flag is used as an optimization, the first scan of a
  931. * host adapter calls into here with rescan == 0.
  932. */
  933. sdev = scsi_device_lookup_by_target(starget, lun);
  934. if (sdev) {
  935. if (rescan || !scsi_device_created(sdev)) {
  936. SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
  937. "scsi scan: device exists on %s\n",
  938. dev_name(&sdev->sdev_gendev)));
  939. if (sdevp)
  940. *sdevp = sdev;
  941. else
  942. scsi_device_put(sdev);
  943. if (bflagsp)
  944. *bflagsp = scsi_get_device_flags(sdev,
  945. sdev->vendor,
  946. sdev->model);
  947. return SCSI_SCAN_LUN_PRESENT;
  948. }
  949. scsi_device_put(sdev);
  950. } else
  951. sdev = scsi_alloc_sdev(starget, lun, hostdata);
  952. if (!sdev)
  953. goto out;
  954. result = kmalloc(result_len, GFP_ATOMIC |
  955. ((shost->unchecked_isa_dma) ? __GFP_DMA : 0));
  956. if (!result)
  957. goto out_free_sdev;
  958. if (scsi_probe_lun(sdev, result, result_len, &bflags))
  959. goto out_free_result;
  960. if (bflagsp)
  961. *bflagsp = bflags;
  962. /*
  963. * result contains valid SCSI INQUIRY data.
  964. */
  965. if (((result[0] >> 5) == 3) && !(bflags & BLIST_ATTACH_PQ3)) {
  966. /*
  967. * For a Peripheral qualifier 3 (011b), the SCSI
  968. * spec says: The device server is not capable of
  969. * supporting a physical device on this logical
  970. * unit.
  971. *
  972. * For disks, this implies that there is no
  973. * logical disk configured at sdev->lun, but there
  974. * is a target id responding.
  975. */
  976. SCSI_LOG_SCAN_BUS(2, sdev_printk(KERN_INFO, sdev, "scsi scan:"
  977. " peripheral qualifier of 3, device not"
  978. " added\n"))
  979. if (lun == 0) {
  980. SCSI_LOG_SCAN_BUS(1, {
  981. unsigned char vend[9];
  982. unsigned char mod[17];
  983. sdev_printk(KERN_INFO, sdev,
  984. "scsi scan: consider passing scsi_mod."
  985. "dev_flags=%s:%s:0x240 or 0x1000240\n",
  986. scsi_inq_str(vend, result, 8, 16),
  987. scsi_inq_str(mod, result, 16, 32));
  988. });
  989. }
  990. res = SCSI_SCAN_TARGET_PRESENT;
  991. goto out_free_result;
  992. }
  993. /*
  994. * Some targets may set slight variations of PQ and PDT to signal
  995. * that no LUN is present, so don't add sdev in these cases.
  996. * Two specific examples are:
  997. * 1) NetApp targets: return PQ=1, PDT=0x1f
  998. * 2) USB UFI: returns PDT=0x1f, with the PQ bits being "reserved"
  999. * in the UFI 1.0 spec (we cannot rely on reserved bits).
  1000. *
  1001. * References:
  1002. * 1) SCSI SPC-3, pp. 145-146
  1003. * PQ=1: "A peripheral device having the specified peripheral
  1004. * device type is not connected to this logical unit. However, the
  1005. * device server is capable of supporting the specified peripheral
  1006. * device type on this logical unit."
  1007. * PDT=0x1f: "Unknown or no device type"
  1008. * 2) USB UFI 1.0, p. 20
  1009. * PDT=00h Direct-access device (floppy)
  1010. * PDT=1Fh none (no FDD connected to the requested logical unit)
  1011. */
  1012. if (((result[0] >> 5) == 1 || starget->pdt_1f_for_no_lun) &&
  1013. (result[0] & 0x1f) == 0x1f &&
  1014. !scsi_is_wlun(lun)) {
  1015. SCSI_LOG_SCAN_BUS(3, sdev_printk(KERN_INFO, sdev,
  1016. "scsi scan: peripheral device type"
  1017. " of 31, no device added\n"));
  1018. res = SCSI_SCAN_TARGET_PRESENT;
  1019. goto out_free_result;
  1020. }
  1021. res = scsi_add_lun(sdev, result, &bflags, shost->async_scan);
  1022. if (res == SCSI_SCAN_LUN_PRESENT) {
  1023. if (bflags & BLIST_KEY) {
  1024. sdev->lockable = 0;
  1025. scsi_unlock_floptical(sdev, result);
  1026. }
  1027. }
  1028. out_free_result:
  1029. kfree(result);
  1030. out_free_sdev:
  1031. if (res == SCSI_SCAN_LUN_PRESENT) {
  1032. if (sdevp) {
  1033. if (scsi_device_get(sdev) == 0) {
  1034. *sdevp = sdev;
  1035. } else {
  1036. __scsi_remove_device(sdev);
  1037. res = SCSI_SCAN_NO_RESPONSE;
  1038. }
  1039. }
  1040. } else
  1041. __scsi_remove_device(sdev);
  1042. out:
  1043. return res;
  1044. }
  1045. /**
  1046. * scsi_sequential_lun_scan - sequentially scan a SCSI target
  1047. * @starget: pointer to target structure to scan
  1048. * @bflags: black/white list flag for LUN 0
  1049. * @scsi_level: Which version of the standard does this device adhere to
  1050. * @rescan: passed to scsi_probe_add_lun()
  1051. *
  1052. * Description:
  1053. * Generally, scan from LUN 1 (LUN 0 is assumed to already have been
  1054. * scanned) to some maximum lun until a LUN is found with no device
  1055. * attached. Use the bflags to figure out any oddities.
  1056. *
  1057. * Modifies sdevscan->lun.
  1058. **/
  1059. static void scsi_sequential_lun_scan(struct scsi_target *starget,
  1060. int bflags, int scsi_level, int rescan)
  1061. {
  1062. uint max_dev_lun;
  1063. u64 sparse_lun, lun;
  1064. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  1065. SCSI_LOG_SCAN_BUS(3, starget_printk(KERN_INFO, starget,
  1066. "scsi scan: Sequential scan\n"));
  1067. max_dev_lun = min(max_scsi_luns, shost->max_lun);
  1068. /*
  1069. * If this device is known to support sparse multiple units,
  1070. * override the other settings, and scan all of them. Normally,
  1071. * SCSI-3 devices should be scanned via the REPORT LUNS.
  1072. */
  1073. if (bflags & BLIST_SPARSELUN) {
  1074. max_dev_lun = shost->max_lun;
  1075. sparse_lun = 1;
  1076. } else
  1077. sparse_lun = 0;
  1078. /*
  1079. * If less than SCSI_1_CCS, and no special lun scanning, stop
  1080. * scanning; this matches 2.4 behaviour, but could just be a bug
  1081. * (to continue scanning a SCSI_1_CCS device).
  1082. *
  1083. * This test is broken. We might not have any device on lun0 for
  1084. * a sparselun device, and if that's the case then how would we
  1085. * know the real scsi_level, eh? It might make sense to just not
  1086. * scan any SCSI_1 device for non-0 luns, but that check would best
  1087. * go into scsi_alloc_sdev() and just have it return null when asked
  1088. * to alloc an sdev for lun > 0 on an already found SCSI_1 device.
  1089. *
  1090. if ((sdevscan->scsi_level < SCSI_1_CCS) &&
  1091. ((bflags & (BLIST_FORCELUN | BLIST_SPARSELUN | BLIST_MAX5LUN))
  1092. == 0))
  1093. return;
  1094. */
  1095. /*
  1096. * If this device is known to support multiple units, override
  1097. * the other settings, and scan all of them.
  1098. */
  1099. if (bflags & BLIST_FORCELUN)
  1100. max_dev_lun = shost->max_lun;
  1101. /*
  1102. * REGAL CDC-4X: avoid hang after LUN 4
  1103. */
  1104. if (bflags & BLIST_MAX5LUN)
  1105. max_dev_lun = min(5U, max_dev_lun);
  1106. /*
  1107. * Do not scan SCSI-2 or lower device past LUN 7, unless
  1108. * BLIST_LARGELUN.
  1109. */
  1110. if (scsi_level < SCSI_3 && !(bflags & BLIST_LARGELUN))
  1111. max_dev_lun = min(8U, max_dev_lun);
  1112. /*
  1113. * Stop scanning at 255 unless BLIST_SCSI3LUN
  1114. */
  1115. if (!(bflags & BLIST_SCSI3LUN))
  1116. max_dev_lun = min(256U, max_dev_lun);
  1117. /*
  1118. * We have already scanned LUN 0, so start at LUN 1. Keep scanning
  1119. * until we reach the max, or no LUN is found and we are not
  1120. * sparse_lun.
  1121. */
  1122. for (lun = 1; lun < max_dev_lun; ++lun)
  1123. if ((scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan,
  1124. NULL) != SCSI_SCAN_LUN_PRESENT) &&
  1125. !sparse_lun)
  1126. return;
  1127. }
  1128. /**
  1129. * scsilun_to_int - convert a scsi_lun to an int
  1130. * @scsilun: struct scsi_lun to be converted.
  1131. *
  1132. * Description:
  1133. * Convert @scsilun from a struct scsi_lun to a four byte host byte-ordered
  1134. * integer, and return the result. The caller must check for
  1135. * truncation before using this function.
  1136. *
  1137. * Notes:
  1138. * For a description of the LUN format, post SCSI-3 see the SCSI
  1139. * Architecture Model, for SCSI-3 see the SCSI Controller Commands.
  1140. *
  1141. * Given a struct scsi_lun of: d2 04 0b 03 00 00 00 00, this function
  1142. * returns the integer: 0x0b03d204
  1143. *
  1144. * This encoding will return a standard integer LUN for LUNs smaller
  1145. * than 256, which typically use a single level LUN structure with
  1146. * addressing method 0.
  1147. **/
  1148. u64 scsilun_to_int(struct scsi_lun *scsilun)
  1149. {
  1150. int i;
  1151. u64 lun;
  1152. lun = 0;
  1153. for (i = 0; i < sizeof(lun); i += 2)
  1154. lun = lun | (((u64)scsilun->scsi_lun[i] << ((i + 1) * 8)) |
  1155. ((u64)scsilun->scsi_lun[i + 1] << (i * 8)));
  1156. return lun;
  1157. }
  1158. EXPORT_SYMBOL(scsilun_to_int);
  1159. /**
  1160. * int_to_scsilun - reverts an int into a scsi_lun
  1161. * @lun: integer to be reverted
  1162. * @scsilun: struct scsi_lun to be set.
  1163. *
  1164. * Description:
  1165. * Reverts the functionality of the scsilun_to_int, which packed
  1166. * an 8-byte lun value into an int. This routine unpacks the int
  1167. * back into the lun value.
  1168. *
  1169. * Notes:
  1170. * Given an integer : 0x0b03d204, this function returns a
  1171. * struct scsi_lun of: d2 04 0b 03 00 00 00 00
  1172. *
  1173. **/
  1174. void int_to_scsilun(u64 lun, struct scsi_lun *scsilun)
  1175. {
  1176. int i;
  1177. memset(scsilun->scsi_lun, 0, sizeof(scsilun->scsi_lun));
  1178. for (i = 0; i < sizeof(lun); i += 2) {
  1179. scsilun->scsi_lun[i] = (lun >> 8) & 0xFF;
  1180. scsilun->scsi_lun[i+1] = lun & 0xFF;
  1181. lun = lun >> 16;
  1182. }
  1183. }
  1184. EXPORT_SYMBOL(int_to_scsilun);
  1185. /**
  1186. * scsi_report_lun_scan - Scan using SCSI REPORT LUN results
  1187. * @starget: which target
  1188. * @bflags: Zero or a mix of BLIST_NOLUN, BLIST_REPORTLUN2, or BLIST_NOREPORTLUN
  1189. * @rescan: nonzero if we can skip code only needed on first scan
  1190. *
  1191. * Description:
  1192. * Fast scanning for modern (SCSI-3) devices by sending a REPORT LUN command.
  1193. * Scan the resulting list of LUNs by calling scsi_probe_and_add_lun.
  1194. *
  1195. * If BLINK_REPORTLUN2 is set, scan a target that supports more than 8
  1196. * LUNs even if it's older than SCSI-3.
  1197. * If BLIST_NOREPORTLUN is set, return 1 always.
  1198. * If BLIST_NOLUN is set, return 0 always.
  1199. * If starget->no_report_luns is set, return 1 always.
  1200. *
  1201. * Return:
  1202. * 0: scan completed (or no memory, so further scanning is futile)
  1203. * 1: could not scan with REPORT LUN
  1204. **/
  1205. static int scsi_report_lun_scan(struct scsi_target *starget, int bflags,
  1206. int rescan)
  1207. {
  1208. char devname[64];
  1209. unsigned char scsi_cmd[MAX_COMMAND_SIZE];
  1210. unsigned int length;
  1211. u64 lun;
  1212. unsigned int num_luns;
  1213. unsigned int retries;
  1214. int result;
  1215. struct scsi_lun *lunp, *lun_data;
  1216. u8 *data;
  1217. struct scsi_sense_hdr sshdr;
  1218. struct scsi_device *sdev;
  1219. struct Scsi_Host *shost = dev_to_shost(&starget->dev);
  1220. int ret = 0;
  1221. /*
  1222. * Only support SCSI-3 and up devices if BLIST_NOREPORTLUN is not set.
  1223. * Also allow SCSI-2 if BLIST_REPORTLUN2 is set and host adapter does
  1224. * support more than 8 LUNs.
  1225. * Don't attempt if the target doesn't support REPORT LUNS.
  1226. */
  1227. if (bflags & BLIST_NOREPORTLUN)
  1228. return 1;
  1229. if (starget->scsi_level < SCSI_2 &&
  1230. starget->scsi_level != SCSI_UNKNOWN)
  1231. return 1;
  1232. if (starget->scsi_level < SCSI_3 &&
  1233. (!(bflags & BLIST_REPORTLUN2) || shost->max_lun <= 8))
  1234. return 1;
  1235. if (bflags & BLIST_NOLUN)
  1236. return 0;
  1237. if (starget->no_report_luns)
  1238. return 1;
  1239. if (!(sdev = scsi_device_lookup_by_target(starget, 0))) {
  1240. sdev = scsi_alloc_sdev(starget, 0, NULL);
  1241. if (!sdev)
  1242. return 0;
  1243. if (scsi_device_get(sdev)) {
  1244. __scsi_remove_device(sdev);
  1245. return 0;
  1246. }
  1247. }
  1248. sprintf(devname, "host %d channel %d id %d",
  1249. shost->host_no, sdev->channel, sdev->id);
  1250. /*
  1251. * Allocate enough to hold the header (the same size as one scsi_lun)
  1252. * plus the max number of luns we are requesting.
  1253. *
  1254. * Reallocating and trying again (with the exact amount we need)
  1255. * would be nice, but then we need to somehow limit the size
  1256. * allocated based on the available memory and the limits of
  1257. * kmalloc - we don't want a kmalloc() failure of a huge value to
  1258. * prevent us from finding any LUNs on this target.
  1259. */
  1260. length = (max_scsi_report_luns + 1) * sizeof(struct scsi_lun);
  1261. lun_data = kmalloc(length, GFP_ATOMIC |
  1262. (sdev->host->unchecked_isa_dma ? __GFP_DMA : 0));
  1263. if (!lun_data) {
  1264. printk(ALLOC_FAILURE_MSG, __func__);
  1265. goto out;
  1266. }
  1267. scsi_cmd[0] = REPORT_LUNS;
  1268. /*
  1269. * bytes 1 - 5: reserved, set to zero.
  1270. */
  1271. memset(&scsi_cmd[1], 0, 5);
  1272. /*
  1273. * bytes 6 - 9: length of the command.
  1274. */
  1275. scsi_cmd[6] = (unsigned char) (length >> 24) & 0xff;
  1276. scsi_cmd[7] = (unsigned char) (length >> 16) & 0xff;
  1277. scsi_cmd[8] = (unsigned char) (length >> 8) & 0xff;
  1278. scsi_cmd[9] = (unsigned char) length & 0xff;
  1279. scsi_cmd[10] = 0; /* reserved */
  1280. scsi_cmd[11] = 0; /* control */
  1281. /*
  1282. * We can get a UNIT ATTENTION, for example a power on/reset, so
  1283. * retry a few times (like sd.c does for TEST UNIT READY).
  1284. * Experience shows some combinations of adapter/devices get at
  1285. * least two power on/resets.
  1286. *
  1287. * Illegal requests (for devices that do not support REPORT LUNS)
  1288. * should come through as a check condition, and will not generate
  1289. * a retry.
  1290. */
  1291. for (retries = 0; retries < 3; retries++) {
  1292. SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
  1293. "scsi scan: Sending REPORT LUNS to (try %d)\n",
  1294. retries));
  1295. result = scsi_execute_req(sdev, scsi_cmd, DMA_FROM_DEVICE,
  1296. lun_data, length, &sshdr,
  1297. SCSI_TIMEOUT + 4 * HZ, 3, NULL);
  1298. SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
  1299. "scsi scan: REPORT LUNS"
  1300. " %s (try %d) result 0x%x\n",
  1301. result ? "failed" : "successful",
  1302. retries, result));
  1303. if (result == 0)
  1304. break;
  1305. else if (scsi_sense_valid(&sshdr)) {
  1306. if (sshdr.sense_key != UNIT_ATTENTION)
  1307. break;
  1308. }
  1309. }
  1310. if (result) {
  1311. /*
  1312. * The device probably does not support a REPORT LUN command
  1313. */
  1314. ret = 1;
  1315. goto out_err;
  1316. }
  1317. /*
  1318. * Get the length from the first four bytes of lun_data.
  1319. */
  1320. data = (u8 *) lun_data->scsi_lun;
  1321. length = ((data[0] << 24) | (data[1] << 16) |
  1322. (data[2] << 8) | (data[3] << 0));
  1323. num_luns = (length / sizeof(struct scsi_lun));
  1324. if (num_luns > max_scsi_report_luns) {
  1325. sdev_printk(KERN_WARNING, sdev,
  1326. "Only %d (max_scsi_report_luns)"
  1327. " of %d luns reported, try increasing"
  1328. " max_scsi_report_luns.\n",
  1329. max_scsi_report_luns, num_luns);
  1330. num_luns = max_scsi_report_luns;
  1331. }
  1332. SCSI_LOG_SCAN_BUS(3, sdev_printk (KERN_INFO, sdev,
  1333. "scsi scan: REPORT LUN scan\n"));
  1334. /*
  1335. * Scan the luns in lun_data. The entry at offset 0 is really
  1336. * the header, so start at 1 and go up to and including num_luns.
  1337. */
  1338. for (lunp = &lun_data[1]; lunp <= &lun_data[num_luns]; lunp++) {
  1339. lun = scsilun_to_int(lunp);
  1340. if (lun > sdev->host->max_lun) {
  1341. sdev_printk(KERN_WARNING, sdev,
  1342. "lun%llu has a LUN larger than"
  1343. " allowed by the host adapter\n", lun);
  1344. } else {
  1345. int res;
  1346. res = scsi_probe_and_add_lun(starget,
  1347. lun, NULL, NULL, rescan, NULL);
  1348. if (res == SCSI_SCAN_NO_RESPONSE) {
  1349. /*
  1350. * Got some results, but now none, abort.
  1351. */
  1352. sdev_printk(KERN_ERR, sdev,
  1353. "Unexpected response"
  1354. " from lun %llu while scanning, scan"
  1355. " aborted\n", (unsigned long long)lun);
  1356. break;
  1357. }
  1358. }
  1359. }
  1360. out_err:
  1361. kfree(lun_data);
  1362. out:
  1363. scsi_device_put(sdev);
  1364. if (scsi_device_created(sdev))
  1365. /*
  1366. * the sdev we used didn't appear in the report luns scan
  1367. */
  1368. __scsi_remove_device(sdev);
  1369. return ret;
  1370. }
  1371. struct scsi_device *__scsi_add_device(struct Scsi_Host *shost, uint channel,
  1372. uint id, u64 lun, void *hostdata)
  1373. {
  1374. struct scsi_device *sdev = ERR_PTR(-ENODEV);
  1375. struct device *parent = &shost->shost_gendev;
  1376. struct scsi_target *starget;
  1377. if (strncmp(scsi_scan_type, "none", 4) == 0)
  1378. return ERR_PTR(-ENODEV);
  1379. starget = scsi_alloc_target(parent, channel, id);
  1380. if (!starget)
  1381. return ERR_PTR(-ENOMEM);
  1382. scsi_autopm_get_target(starget);
  1383. mutex_lock(&shost->scan_mutex);
  1384. if (!shost->async_scan)
  1385. scsi_complete_async_scans();
  1386. if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
  1387. scsi_probe_and_add_lun(starget, lun, NULL, &sdev, 1, hostdata);
  1388. scsi_autopm_put_host(shost);
  1389. }
  1390. mutex_unlock(&shost->scan_mutex);
  1391. scsi_autopm_put_target(starget);
  1392. /*
  1393. * paired with scsi_alloc_target(). Target will be destroyed unless
  1394. * scsi_probe_and_add_lun made an underlying device visible
  1395. */
  1396. scsi_target_reap(starget);
  1397. put_device(&starget->dev);
  1398. return sdev;
  1399. }
  1400. EXPORT_SYMBOL(__scsi_add_device);
  1401. int scsi_add_device(struct Scsi_Host *host, uint channel,
  1402. uint target, u64 lun)
  1403. {
  1404. struct scsi_device *sdev =
  1405. __scsi_add_device(host, channel, target, lun, NULL);
  1406. if (IS_ERR(sdev))
  1407. return PTR_ERR(sdev);
  1408. scsi_device_put(sdev);
  1409. return 0;
  1410. }
  1411. EXPORT_SYMBOL(scsi_add_device);
  1412. void scsi_rescan_device(struct device *dev)
  1413. {
  1414. struct scsi_driver *drv;
  1415. if (!dev->driver)
  1416. return;
  1417. drv = to_scsi_driver(dev->driver);
  1418. if (try_module_get(drv->owner)) {
  1419. if (drv->rescan)
  1420. drv->rescan(dev);
  1421. module_put(drv->owner);
  1422. }
  1423. }
  1424. EXPORT_SYMBOL(scsi_rescan_device);
  1425. static void __scsi_scan_target(struct device *parent, unsigned int channel,
  1426. unsigned int id, u64 lun, int rescan)
  1427. {
  1428. struct Scsi_Host *shost = dev_to_shost(parent);
  1429. int bflags = 0;
  1430. int res;
  1431. struct scsi_target *starget;
  1432. if (shost->this_id == id)
  1433. /*
  1434. * Don't scan the host adapter
  1435. */
  1436. return;
  1437. starget = scsi_alloc_target(parent, channel, id);
  1438. if (!starget)
  1439. return;
  1440. scsi_autopm_get_target(starget);
  1441. if (lun != SCAN_WILD_CARD) {
  1442. /*
  1443. * Scan for a specific host/chan/id/lun.
  1444. */
  1445. scsi_probe_and_add_lun(starget, lun, NULL, NULL, rescan, NULL);
  1446. goto out_reap;
  1447. }
  1448. /*
  1449. * Scan LUN 0, if there is some response, scan further. Ideally, we
  1450. * would not configure LUN 0 until all LUNs are scanned.
  1451. */
  1452. res = scsi_probe_and_add_lun(starget, 0, &bflags, NULL, rescan, NULL);
  1453. if (res == SCSI_SCAN_LUN_PRESENT || res == SCSI_SCAN_TARGET_PRESENT) {
  1454. if (scsi_report_lun_scan(starget, bflags, rescan) != 0)
  1455. /*
  1456. * The REPORT LUN did not scan the target,
  1457. * do a sequential scan.
  1458. */
  1459. scsi_sequential_lun_scan(starget, bflags,
  1460. starget->scsi_level, rescan);
  1461. }
  1462. out_reap:
  1463. scsi_autopm_put_target(starget);
  1464. /*
  1465. * paired with scsi_alloc_target(): determine if the target has
  1466. * any children at all and if not, nuke it
  1467. */
  1468. scsi_target_reap(starget);
  1469. put_device(&starget->dev);
  1470. }
  1471. /**
  1472. * scsi_scan_target - scan a target id, possibly including all LUNs on the target.
  1473. * @parent: host to scan
  1474. * @channel: channel to scan
  1475. * @id: target id to scan
  1476. * @lun: Specific LUN to scan or SCAN_WILD_CARD
  1477. * @rescan: passed to LUN scanning routines
  1478. *
  1479. * Description:
  1480. * Scan the target id on @parent, @channel, and @id. Scan at least LUN 0,
  1481. * and possibly all LUNs on the target id.
  1482. *
  1483. * First try a REPORT LUN scan, if that does not scan the target, do a
  1484. * sequential scan of LUNs on the target id.
  1485. **/
  1486. void scsi_scan_target(struct device *parent, unsigned int channel,
  1487. unsigned int id, u64 lun, int rescan)
  1488. {
  1489. struct Scsi_Host *shost = dev_to_shost(parent);
  1490. if (strncmp(scsi_scan_type, "none", 4) == 0)
  1491. return;
  1492. mutex_lock(&shost->scan_mutex);
  1493. if (!shost->async_scan)
  1494. scsi_complete_async_scans();
  1495. if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
  1496. __scsi_scan_target(parent, channel, id, lun, rescan);
  1497. scsi_autopm_put_host(shost);
  1498. }
  1499. mutex_unlock(&shost->scan_mutex);
  1500. }
  1501. EXPORT_SYMBOL(scsi_scan_target);
  1502. static void scsi_scan_channel(struct Scsi_Host *shost, unsigned int channel,
  1503. unsigned int id, u64 lun, int rescan)
  1504. {
  1505. uint order_id;
  1506. if (id == SCAN_WILD_CARD)
  1507. for (id = 0; id < shost->max_id; ++id) {
  1508. /*
  1509. * XXX adapter drivers when possible (FCP, iSCSI)
  1510. * could modify max_id to match the current max,
  1511. * not the absolute max.
  1512. *
  1513. * XXX add a shost id iterator, so for example,
  1514. * the FC ID can be the same as a target id
  1515. * without a huge overhead of sparse id's.
  1516. */
  1517. if (shost->reverse_ordering)
  1518. /*
  1519. * Scan from high to low id.
  1520. */
  1521. order_id = shost->max_id - id - 1;
  1522. else
  1523. order_id = id;
  1524. __scsi_scan_target(&shost->shost_gendev, channel,
  1525. order_id, lun, rescan);
  1526. }
  1527. else
  1528. __scsi_scan_target(&shost->shost_gendev, channel,
  1529. id, lun, rescan);
  1530. }
  1531. int scsi_scan_host_selected(struct Scsi_Host *shost, unsigned int channel,
  1532. unsigned int id, u64 lun, int rescan)
  1533. {
  1534. SCSI_LOG_SCAN_BUS(3, shost_printk (KERN_INFO, shost,
  1535. "%s: <%u:%u:%llu>\n",
  1536. __func__, channel, id, lun));
  1537. if (((channel != SCAN_WILD_CARD) && (channel > shost->max_channel)) ||
  1538. ((id != SCAN_WILD_CARD) && (id >= shost->max_id)) ||
  1539. ((lun != SCAN_WILD_CARD) && (lun >= shost->max_lun)))
  1540. return -EINVAL;
  1541. mutex_lock(&shost->scan_mutex);
  1542. if (!shost->async_scan)
  1543. scsi_complete_async_scans();
  1544. if (scsi_host_scan_allowed(shost) && scsi_autopm_get_host(shost) == 0) {
  1545. if (channel == SCAN_WILD_CARD)
  1546. for (channel = 0; channel <= shost->max_channel;
  1547. channel++)
  1548. scsi_scan_channel(shost, channel, id, lun,
  1549. rescan);
  1550. else
  1551. scsi_scan_channel(shost, channel, id, lun, rescan);
  1552. scsi_autopm_put_host(shost);
  1553. }
  1554. mutex_unlock(&shost->scan_mutex);
  1555. return 0;
  1556. }
  1557. static void scsi_sysfs_add_devices(struct Scsi_Host *shost)
  1558. {
  1559. struct scsi_device *sdev;
  1560. shost_for_each_device(sdev, shost) {
  1561. /* target removed before the device could be added */
  1562. if (sdev->sdev_state == SDEV_DEL)
  1563. continue;
  1564. /* If device is already visible, skip adding it to sysfs */
  1565. if (sdev->is_visible)
  1566. continue;
  1567. if (!scsi_host_scan_allowed(shost) ||
  1568. scsi_sysfs_add_sdev(sdev) != 0)
  1569. __scsi_remove_device(sdev);
  1570. }
  1571. }
  1572. /**
  1573. * scsi_prep_async_scan - prepare for an async scan
  1574. * @shost: the host which will be scanned
  1575. * Returns: a cookie to be passed to scsi_finish_async_scan()
  1576. *
  1577. * Tells the midlayer this host is going to do an asynchronous scan.
  1578. * It reserves the host's position in the scanning list and ensures
  1579. * that other asynchronous scans started after this one won't affect the
  1580. * ordering of the discovered devices.
  1581. */
  1582. static struct async_scan_data *scsi_prep_async_scan(struct Scsi_Host *shost)
  1583. {
  1584. struct async_scan_data *data;
  1585. unsigned long flags;
  1586. if (strncmp(scsi_scan_type, "sync", 4) == 0)
  1587. return NULL;
  1588. if (shost->async_scan) {
  1589. shost_printk(KERN_INFO, shost, "%s called twice\n", __func__);
  1590. dump_stack();
  1591. return NULL;
  1592. }
  1593. data = kmalloc(sizeof(*data), GFP_KERNEL);
  1594. if (!data)
  1595. goto err;
  1596. data->shost = scsi_host_get(shost);
  1597. if (!data->shost)
  1598. goto err;
  1599. init_completion(&data->prev_finished);
  1600. mutex_lock(&shost->scan_mutex);
  1601. spin_lock_irqsave(shost->host_lock, flags);
  1602. shost->async_scan = 1;
  1603. spin_unlock_irqrestore(shost->host_lock, flags);
  1604. mutex_unlock(&shost->scan_mutex);
  1605. spin_lock(&async_scan_lock);
  1606. if (list_empty(&scanning_hosts))
  1607. complete(&data->prev_finished);
  1608. list_add_tail(&data->list, &scanning_hosts);
  1609. spin_unlock(&async_scan_lock);
  1610. return data;
  1611. err:
  1612. kfree(data);
  1613. return NULL;
  1614. }
  1615. /**
  1616. * scsi_finish_async_scan - asynchronous scan has finished
  1617. * @data: cookie returned from earlier call to scsi_prep_async_scan()
  1618. *
  1619. * All the devices currently attached to this host have been found.
  1620. * This function announces all the devices it has found to the rest
  1621. * of the system.
  1622. */
  1623. static void scsi_finish_async_scan(struct async_scan_data *data)
  1624. {
  1625. struct Scsi_Host *shost;
  1626. unsigned long flags;
  1627. if (!data)
  1628. return;
  1629. shost = data->shost;
  1630. mutex_lock(&shost->scan_mutex);
  1631. if (!shost->async_scan) {
  1632. shost_printk(KERN_INFO, shost, "%s called twice\n", __func__);
  1633. dump_stack();
  1634. mutex_unlock(&shost->scan_mutex);
  1635. return;
  1636. }
  1637. wait_for_completion(&data->prev_finished);
  1638. scsi_sysfs_add_devices(shost);
  1639. spin_lock_irqsave(shost->host_lock, flags);
  1640. shost->async_scan = 0;
  1641. spin_unlock_irqrestore(shost->host_lock, flags);
  1642. mutex_unlock(&shost->scan_mutex);
  1643. spin_lock(&async_scan_lock);
  1644. list_del(&data->list);
  1645. if (!list_empty(&scanning_hosts)) {
  1646. struct async_scan_data *next = list_entry(scanning_hosts.next,
  1647. struct async_scan_data, list);
  1648. complete(&next->prev_finished);
  1649. }
  1650. spin_unlock(&async_scan_lock);
  1651. scsi_autopm_put_host(shost);
  1652. scsi_host_put(shost);
  1653. kfree(data);
  1654. }
  1655. static void do_scsi_scan_host(struct Scsi_Host *shost)
  1656. {
  1657. if (shost->hostt->scan_finished) {
  1658. unsigned long start = jiffies;
  1659. if (shost->hostt->scan_start)
  1660. shost->hostt->scan_start(shost);
  1661. while (!shost->hostt->scan_finished(shost, jiffies - start))
  1662. msleep(10);
  1663. } else {
  1664. scsi_scan_host_selected(shost, SCAN_WILD_CARD, SCAN_WILD_CARD,
  1665. SCAN_WILD_CARD, 0);
  1666. }
  1667. }
  1668. static void do_scan_async(void *_data, async_cookie_t c)
  1669. {
  1670. struct async_scan_data *data = _data;
  1671. struct Scsi_Host *shost = data->shost;
  1672. do_scsi_scan_host(shost);
  1673. scsi_finish_async_scan(data);
  1674. }
  1675. /**
  1676. * scsi_scan_host - scan the given adapter
  1677. * @shost: adapter to scan
  1678. **/
  1679. void scsi_scan_host(struct Scsi_Host *shost)
  1680. {
  1681. struct async_scan_data *data;
  1682. if (strncmp(scsi_scan_type, "none", 4) == 0)
  1683. return;
  1684. if (scsi_autopm_get_host(shost) < 0)
  1685. return;
  1686. data = scsi_prep_async_scan(shost);
  1687. if (!data) {
  1688. do_scsi_scan_host(shost);
  1689. scsi_autopm_put_host(shost);
  1690. return;
  1691. }
  1692. /* register with the async subsystem so wait_for_device_probe()
  1693. * will flush this work
  1694. */
  1695. async_schedule(do_scan_async, data);
  1696. /* scsi_autopm_put_host(shost) is called in scsi_finish_async_scan() */
  1697. }
  1698. EXPORT_SYMBOL(scsi_scan_host);
  1699. void scsi_forget_host(struct Scsi_Host *shost)
  1700. {
  1701. struct scsi_device *sdev;
  1702. unsigned long flags;
  1703. restart:
  1704. spin_lock_irqsave(shost->host_lock, flags);
  1705. list_for_each_entry(sdev, &shost->__devices, siblings) {
  1706. if (sdev->sdev_state == SDEV_DEL)
  1707. continue;
  1708. spin_unlock_irqrestore(shost->host_lock, flags);
  1709. __scsi_remove_device(sdev);
  1710. goto restart;
  1711. }
  1712. spin_unlock_irqrestore(shost->host_lock, flags);
  1713. }
  1714. /**
  1715. * scsi_get_host_dev - Create a scsi_device that points to the host adapter itself
  1716. * @shost: Host that needs a scsi_device
  1717. *
  1718. * Lock status: None assumed.
  1719. *
  1720. * Returns: The scsi_device or NULL
  1721. *
  1722. * Notes:
  1723. * Attach a single scsi_device to the Scsi_Host - this should
  1724. * be made to look like a "pseudo-device" that points to the
  1725. * HA itself.
  1726. *
  1727. * Note - this device is not accessible from any high-level
  1728. * drivers (including generics), which is probably not
  1729. * optimal. We can add hooks later to attach.
  1730. */
  1731. struct scsi_device *scsi_get_host_dev(struct Scsi_Host *shost)
  1732. {
  1733. struct scsi_device *sdev = NULL;
  1734. struct scsi_target *starget;
  1735. mutex_lock(&shost->scan_mutex);
  1736. if (!scsi_host_scan_allowed(shost))
  1737. goto out;
  1738. starget = scsi_alloc_target(&shost->shost_gendev, 0, shost->this_id);
  1739. if (!starget)
  1740. goto out;
  1741. sdev = scsi_alloc_sdev(starget, 0, NULL);
  1742. if (sdev)
  1743. sdev->borken = 0;
  1744. else
  1745. scsi_target_reap(starget);
  1746. put_device(&starget->dev);
  1747. out:
  1748. mutex_unlock(&shost->scan_mutex);
  1749. return sdev;
  1750. }
  1751. EXPORT_SYMBOL(scsi_get_host_dev);
  1752. /**
  1753. * scsi_free_host_dev - Free a scsi_device that points to the host adapter itself
  1754. * @sdev: Host device to be freed
  1755. *
  1756. * Lock status: None assumed.
  1757. *
  1758. * Returns: Nothing
  1759. */
  1760. void scsi_free_host_dev(struct scsi_device *sdev)
  1761. {
  1762. BUG_ON(sdev->id != sdev->host->this_id);
  1763. __scsi_remove_device(sdev);
  1764. }
  1765. EXPORT_SYMBOL(scsi_free_host_dev);