scsi_scan.c 54 KB

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