dasd_alias.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PAV alias management for the DASD ECKD discipline
  4. *
  5. * Copyright IBM Corp. 2007
  6. * Author(s): Stefan Weinhuber <wein@de.ibm.com>
  7. */
  8. #include <linux/list.h>
  9. #include <linux/slab.h>
  10. #include <asm/ebcdic.h>
  11. #include "dasd_int.h"
  12. #include "dasd_eckd.h"
  13. /*
  14. * General concept of alias management:
  15. * - PAV and DASD alias management is specific to the eckd discipline.
  16. * - A device is connected to an lcu as long as the device exists.
  17. * dasd_alias_make_device_known_to_lcu will be called wenn the
  18. * device is checked by the eckd discipline and
  19. * dasd_alias_disconnect_device_from_lcu will be called
  20. * before the device is deleted.
  21. * - The dasd_alias_add_device / dasd_alias_remove_device
  22. * functions mark the point when a device is 'ready for service'.
  23. * - A summary unit check is a rare occasion, but it is mandatory to
  24. * support it. It requires some complex recovery actions before the
  25. * devices can be used again (see dasd_alias_handle_summary_unit_check).
  26. * - dasd_alias_get_start_dev will find an alias device that can be used
  27. * instead of the base device and does some (very simple) load balancing.
  28. * This is the function that gets called for each I/O, so when improving
  29. * something, this function should get faster or better, the rest has just
  30. * to be correct.
  31. */
  32. static void summary_unit_check_handling_work(struct work_struct *);
  33. static void lcu_update_work(struct work_struct *);
  34. static int _schedule_lcu_update(struct alias_lcu *, struct dasd_device *);
  35. static struct alias_root aliastree = {
  36. .serverlist = LIST_HEAD_INIT(aliastree.serverlist),
  37. .lock = __SPIN_LOCK_UNLOCKED(aliastree.lock),
  38. };
  39. static struct alias_server *_find_server(struct dasd_uid *uid)
  40. {
  41. struct alias_server *pos;
  42. list_for_each_entry(pos, &aliastree.serverlist, server) {
  43. if (!strncmp(pos->uid.vendor, uid->vendor,
  44. sizeof(uid->vendor))
  45. && !strncmp(pos->uid.serial, uid->serial,
  46. sizeof(uid->serial)))
  47. return pos;
  48. }
  49. return NULL;
  50. }
  51. static struct alias_lcu *_find_lcu(struct alias_server *server,
  52. struct dasd_uid *uid)
  53. {
  54. struct alias_lcu *pos;
  55. list_for_each_entry(pos, &server->lculist, lcu) {
  56. if (pos->uid.ssid == uid->ssid)
  57. return pos;
  58. }
  59. return NULL;
  60. }
  61. static struct alias_pav_group *_find_group(struct alias_lcu *lcu,
  62. struct dasd_uid *uid)
  63. {
  64. struct alias_pav_group *pos;
  65. __u8 search_unit_addr;
  66. /* for hyper pav there is only one group */
  67. if (lcu->pav == HYPER_PAV) {
  68. if (list_empty(&lcu->grouplist))
  69. return NULL;
  70. else
  71. return list_first_entry(&lcu->grouplist,
  72. struct alias_pav_group, group);
  73. }
  74. /* for base pav we have to find the group that matches the base */
  75. if (uid->type == UA_BASE_DEVICE)
  76. search_unit_addr = uid->real_unit_addr;
  77. else
  78. search_unit_addr = uid->base_unit_addr;
  79. list_for_each_entry(pos, &lcu->grouplist, group) {
  80. if (pos->uid.base_unit_addr == search_unit_addr &&
  81. !strncmp(pos->uid.vduit, uid->vduit, sizeof(uid->vduit)))
  82. return pos;
  83. }
  84. return NULL;
  85. }
  86. static struct alias_server *_allocate_server(struct dasd_uid *uid)
  87. {
  88. struct alias_server *server;
  89. server = kzalloc_obj(*server);
  90. if (!server)
  91. return ERR_PTR(-ENOMEM);
  92. memcpy(server->uid.vendor, uid->vendor, sizeof(uid->vendor));
  93. memcpy(server->uid.serial, uid->serial, sizeof(uid->serial));
  94. INIT_LIST_HEAD(&server->server);
  95. INIT_LIST_HEAD(&server->lculist);
  96. return server;
  97. }
  98. static void _free_server(struct alias_server *server)
  99. {
  100. kfree(server);
  101. }
  102. static struct alias_lcu *_allocate_lcu(struct dasd_uid *uid)
  103. {
  104. struct alias_lcu *lcu;
  105. lcu = kzalloc_obj(*lcu);
  106. if (!lcu)
  107. return ERR_PTR(-ENOMEM);
  108. lcu->uac = kzalloc_obj(*(lcu->uac), GFP_KERNEL | GFP_DMA);
  109. if (!lcu->uac)
  110. goto out_err1;
  111. lcu->rsu_cqr = kzalloc_obj(*lcu->rsu_cqr, GFP_KERNEL | GFP_DMA);
  112. if (!lcu->rsu_cqr)
  113. goto out_err2;
  114. lcu->rsu_cqr->cpaddr = kzalloc_obj(struct ccw1, GFP_KERNEL | GFP_DMA);
  115. if (!lcu->rsu_cqr->cpaddr)
  116. goto out_err3;
  117. lcu->rsu_cqr->data = kzalloc(16, GFP_KERNEL | GFP_DMA);
  118. if (!lcu->rsu_cqr->data)
  119. goto out_err4;
  120. memcpy(lcu->uid.vendor, uid->vendor, sizeof(uid->vendor));
  121. memcpy(lcu->uid.serial, uid->serial, sizeof(uid->serial));
  122. lcu->uid.ssid = uid->ssid;
  123. lcu->pav = NO_PAV;
  124. lcu->flags = NEED_UAC_UPDATE | UPDATE_PENDING;
  125. INIT_LIST_HEAD(&lcu->lcu);
  126. INIT_LIST_HEAD(&lcu->inactive_devices);
  127. INIT_LIST_HEAD(&lcu->active_devices);
  128. INIT_LIST_HEAD(&lcu->grouplist);
  129. INIT_WORK(&lcu->suc_data.worker, summary_unit_check_handling_work);
  130. INIT_DELAYED_WORK(&lcu->ruac_data.dwork, lcu_update_work);
  131. spin_lock_init(&lcu->lock);
  132. init_completion(&lcu->lcu_setup);
  133. return lcu;
  134. out_err4:
  135. kfree(lcu->rsu_cqr->cpaddr);
  136. out_err3:
  137. kfree(lcu->rsu_cqr);
  138. out_err2:
  139. kfree(lcu->uac);
  140. out_err1:
  141. kfree(lcu);
  142. return ERR_PTR(-ENOMEM);
  143. }
  144. static void _free_lcu(struct alias_lcu *lcu)
  145. {
  146. kfree(lcu->rsu_cqr->data);
  147. kfree(lcu->rsu_cqr->cpaddr);
  148. kfree(lcu->rsu_cqr);
  149. kfree(lcu->uac);
  150. kfree(lcu);
  151. }
  152. /*
  153. * This is the function that will allocate all the server and lcu data,
  154. * so this function must be called first for a new device.
  155. * If the return value is 1, the lcu was already known before, if it
  156. * is 0, this is a new lcu.
  157. * Negative return code indicates that something went wrong (e.g. -ENOMEM)
  158. */
  159. int dasd_alias_make_device_known_to_lcu(struct dasd_device *device)
  160. {
  161. struct dasd_eckd_private *private = device->private;
  162. unsigned long flags;
  163. struct alias_server *server, *newserver;
  164. struct alias_lcu *lcu, *newlcu;
  165. struct dasd_uid uid;
  166. device->discipline->get_uid(device, &uid);
  167. spin_lock_irqsave(&aliastree.lock, flags);
  168. server = _find_server(&uid);
  169. if (!server) {
  170. spin_unlock_irqrestore(&aliastree.lock, flags);
  171. newserver = _allocate_server(&uid);
  172. if (IS_ERR(newserver))
  173. return PTR_ERR(newserver);
  174. spin_lock_irqsave(&aliastree.lock, flags);
  175. server = _find_server(&uid);
  176. if (!server) {
  177. list_add(&newserver->server, &aliastree.serverlist);
  178. server = newserver;
  179. } else {
  180. /* someone was faster */
  181. _free_server(newserver);
  182. }
  183. }
  184. lcu = _find_lcu(server, &uid);
  185. if (!lcu) {
  186. spin_unlock_irqrestore(&aliastree.lock, flags);
  187. newlcu = _allocate_lcu(&uid);
  188. if (IS_ERR(newlcu))
  189. return PTR_ERR(newlcu);
  190. spin_lock_irqsave(&aliastree.lock, flags);
  191. lcu = _find_lcu(server, &uid);
  192. if (!lcu) {
  193. list_add(&newlcu->lcu, &server->lculist);
  194. lcu = newlcu;
  195. } else {
  196. /* someone was faster */
  197. _free_lcu(newlcu);
  198. }
  199. }
  200. spin_lock(&lcu->lock);
  201. list_add(&device->alias_list, &lcu->inactive_devices);
  202. private->lcu = lcu;
  203. spin_unlock(&lcu->lock);
  204. spin_unlock_irqrestore(&aliastree.lock, flags);
  205. return 0;
  206. }
  207. /*
  208. * This function removes a device from the scope of alias management.
  209. * The complicated part is to make sure that it is not in use by
  210. * any of the workers. If necessary cancel the work.
  211. */
  212. void dasd_alias_disconnect_device_from_lcu(struct dasd_device *device)
  213. {
  214. struct dasd_eckd_private *private = device->private;
  215. unsigned long flags;
  216. struct alias_lcu *lcu;
  217. struct alias_server *server;
  218. int was_pending;
  219. struct dasd_uid uid;
  220. lcu = private->lcu;
  221. /* nothing to do if already disconnected */
  222. if (!lcu)
  223. return;
  224. device->discipline->get_uid(device, &uid);
  225. spin_lock_irqsave(&lcu->lock, flags);
  226. /* make sure that the workers don't use this device */
  227. if (device == lcu->suc_data.device) {
  228. spin_unlock_irqrestore(&lcu->lock, flags);
  229. cancel_work_sync(&lcu->suc_data.worker);
  230. spin_lock_irqsave(&lcu->lock, flags);
  231. if (device == lcu->suc_data.device) {
  232. dasd_put_device(device);
  233. lcu->suc_data.device = NULL;
  234. }
  235. }
  236. was_pending = 0;
  237. if (device == lcu->ruac_data.device) {
  238. spin_unlock_irqrestore(&lcu->lock, flags);
  239. was_pending = 1;
  240. cancel_delayed_work_sync(&lcu->ruac_data.dwork);
  241. spin_lock_irqsave(&lcu->lock, flags);
  242. if (device == lcu->ruac_data.device) {
  243. dasd_put_device(device);
  244. lcu->ruac_data.device = NULL;
  245. }
  246. }
  247. private->lcu = NULL;
  248. spin_unlock_irqrestore(&lcu->lock, flags);
  249. spin_lock_irqsave(&aliastree.lock, flags);
  250. spin_lock(&lcu->lock);
  251. list_del_init(&device->alias_list);
  252. if (list_empty(&lcu->grouplist) &&
  253. list_empty(&lcu->active_devices) &&
  254. list_empty(&lcu->inactive_devices)) {
  255. list_del(&lcu->lcu);
  256. spin_unlock(&lcu->lock);
  257. _free_lcu(lcu);
  258. lcu = NULL;
  259. } else {
  260. if (was_pending)
  261. _schedule_lcu_update(lcu, NULL);
  262. spin_unlock(&lcu->lock);
  263. }
  264. server = _find_server(&uid);
  265. if (server && list_empty(&server->lculist)) {
  266. list_del(&server->server);
  267. _free_server(server);
  268. }
  269. spin_unlock_irqrestore(&aliastree.lock, flags);
  270. }
  271. /*
  272. * This function assumes that the unit address configuration stored
  273. * in the lcu is up to date and will update the device uid before
  274. * adding it to a pav group.
  275. */
  276. static int _add_device_to_lcu(struct alias_lcu *lcu,
  277. struct dasd_device *device,
  278. struct dasd_device *pos)
  279. {
  280. struct dasd_eckd_private *private = device->private;
  281. struct alias_pav_group *group;
  282. struct dasd_uid uid;
  283. spin_lock(get_ccwdev_lock(device->cdev));
  284. private->uid.type = lcu->uac->unit[private->uid.real_unit_addr].ua_type;
  285. private->uid.base_unit_addr =
  286. lcu->uac->unit[private->uid.real_unit_addr].base_ua;
  287. uid = private->uid;
  288. spin_unlock(get_ccwdev_lock(device->cdev));
  289. /* if we have no PAV anyway, we don't need to bother with PAV groups */
  290. if (lcu->pav == NO_PAV) {
  291. list_move(&device->alias_list, &lcu->active_devices);
  292. return 0;
  293. }
  294. group = _find_group(lcu, &uid);
  295. if (!group) {
  296. group = kzalloc_obj(*group, GFP_ATOMIC);
  297. if (!group)
  298. return -ENOMEM;
  299. memcpy(group->uid.vendor, uid.vendor, sizeof(uid.vendor));
  300. memcpy(group->uid.serial, uid.serial, sizeof(uid.serial));
  301. group->uid.ssid = uid.ssid;
  302. if (uid.type == UA_BASE_DEVICE)
  303. group->uid.base_unit_addr = uid.real_unit_addr;
  304. else
  305. group->uid.base_unit_addr = uid.base_unit_addr;
  306. memcpy(group->uid.vduit, uid.vduit, sizeof(uid.vduit));
  307. INIT_LIST_HEAD(&group->group);
  308. INIT_LIST_HEAD(&group->baselist);
  309. INIT_LIST_HEAD(&group->aliaslist);
  310. list_add(&group->group, &lcu->grouplist);
  311. }
  312. if (uid.type == UA_BASE_DEVICE)
  313. list_move(&device->alias_list, &group->baselist);
  314. else
  315. list_move(&device->alias_list, &group->aliaslist);
  316. private->pavgroup = group;
  317. return 0;
  318. };
  319. static void _remove_device_from_lcu(struct alias_lcu *lcu,
  320. struct dasd_device *device)
  321. {
  322. struct dasd_eckd_private *private = device->private;
  323. struct alias_pav_group *group;
  324. list_move(&device->alias_list, &lcu->inactive_devices);
  325. group = private->pavgroup;
  326. if (!group)
  327. return;
  328. private->pavgroup = NULL;
  329. if (list_empty(&group->baselist) && list_empty(&group->aliaslist)) {
  330. list_del(&group->group);
  331. kfree(group);
  332. return;
  333. }
  334. if (group->next == device)
  335. group->next = NULL;
  336. };
  337. static int
  338. suborder_not_supported(struct dasd_ccw_req *cqr)
  339. {
  340. char *sense;
  341. char reason;
  342. char msg_format;
  343. char msg_no;
  344. /*
  345. * intrc values ENODEV, ENOLINK and EPERM
  346. * will be optained from sleep_on to indicate that no
  347. * IO operation can be started
  348. */
  349. if (cqr->intrc == -ENODEV)
  350. return 1;
  351. if (cqr->intrc == -ENOLINK)
  352. return 1;
  353. if (cqr->intrc == -EPERM)
  354. return 1;
  355. sense = dasd_get_sense(&cqr->irb);
  356. if (!sense)
  357. return 0;
  358. reason = sense[0];
  359. msg_format = (sense[7] & 0xF0);
  360. msg_no = (sense[7] & 0x0F);
  361. /* command reject, Format 0 MSG 4 - invalid parameter */
  362. if ((reason == 0x80) && (msg_format == 0x00) && (msg_no == 0x04))
  363. return 1;
  364. return 0;
  365. }
  366. static int read_unit_address_configuration(struct dasd_device *device,
  367. struct alias_lcu *lcu)
  368. {
  369. struct dasd_psf_prssd_data *prssdp;
  370. struct dasd_ccw_req *cqr;
  371. struct ccw1 *ccw;
  372. int rc;
  373. unsigned long flags;
  374. cqr = dasd_smalloc_request(DASD_ECKD_MAGIC, 1 /* PSF */ + 1 /* RSSD */,
  375. (sizeof(struct dasd_psf_prssd_data)),
  376. device, NULL);
  377. if (IS_ERR(cqr))
  378. return PTR_ERR(cqr);
  379. cqr->startdev = device;
  380. cqr->memdev = device;
  381. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  382. cqr->retries = 10;
  383. cqr->expires = 20 * HZ;
  384. /* Prepare for Read Subsystem Data */
  385. prssdp = (struct dasd_psf_prssd_data *) cqr->data;
  386. memset(prssdp, 0, sizeof(struct dasd_psf_prssd_data));
  387. prssdp->order = PSF_ORDER_PRSSD;
  388. prssdp->suborder = 0x0e; /* Read unit address configuration */
  389. /* all other bytes of prssdp must be zero */
  390. ccw = cqr->cpaddr;
  391. ccw->cmd_code = DASD_ECKD_CCW_PSF;
  392. ccw->count = sizeof(struct dasd_psf_prssd_data);
  393. ccw->flags |= CCW_FLAG_CC;
  394. ccw->cda = virt_to_dma32(prssdp);
  395. /* Read Subsystem Data - feature codes */
  396. memset(lcu->uac, 0, sizeof(*(lcu->uac)));
  397. ccw++;
  398. ccw->cmd_code = DASD_ECKD_CCW_RSSD;
  399. ccw->count = sizeof(*(lcu->uac));
  400. ccw->cda = virt_to_dma32(lcu->uac);
  401. cqr->buildclk = get_tod_clock();
  402. cqr->status = DASD_CQR_FILLED;
  403. /* need to unset flag here to detect race with summary unit check */
  404. spin_lock_irqsave(&lcu->lock, flags);
  405. lcu->flags &= ~NEED_UAC_UPDATE;
  406. spin_unlock_irqrestore(&lcu->lock, flags);
  407. rc = dasd_sleep_on(cqr);
  408. if (!rc)
  409. goto out;
  410. if (suborder_not_supported(cqr)) {
  411. /* suborder not supported or device unusable for IO */
  412. rc = -EOPNOTSUPP;
  413. } else {
  414. /* IO failed but should be retried */
  415. spin_lock_irqsave(&lcu->lock, flags);
  416. lcu->flags |= NEED_UAC_UPDATE;
  417. spin_unlock_irqrestore(&lcu->lock, flags);
  418. }
  419. out:
  420. dasd_sfree_request(cqr, cqr->memdev);
  421. return rc;
  422. }
  423. static int _lcu_update(struct dasd_device *refdev, struct alias_lcu *lcu)
  424. {
  425. unsigned long flags;
  426. struct alias_pav_group *pavgroup, *tempgroup;
  427. struct dasd_device *device, *tempdev;
  428. int i, rc;
  429. struct dasd_eckd_private *private;
  430. spin_lock_irqsave(&lcu->lock, flags);
  431. list_for_each_entry_safe(pavgroup, tempgroup, &lcu->grouplist, group) {
  432. list_for_each_entry_safe(device, tempdev, &pavgroup->baselist,
  433. alias_list) {
  434. list_move(&device->alias_list, &lcu->active_devices);
  435. private = device->private;
  436. private->pavgroup = NULL;
  437. }
  438. list_for_each_entry_safe(device, tempdev, &pavgroup->aliaslist,
  439. alias_list) {
  440. list_move(&device->alias_list, &lcu->active_devices);
  441. private = device->private;
  442. private->pavgroup = NULL;
  443. }
  444. list_del(&pavgroup->group);
  445. kfree(pavgroup);
  446. }
  447. spin_unlock_irqrestore(&lcu->lock, flags);
  448. rc = read_unit_address_configuration(refdev, lcu);
  449. if (rc)
  450. return rc;
  451. spin_lock_irqsave(&lcu->lock, flags);
  452. /*
  453. * there is another update needed skip the remaining handling
  454. * the data might already be outdated
  455. * but especially do not add the device to an LCU with pending
  456. * update
  457. */
  458. if (lcu->flags & NEED_UAC_UPDATE)
  459. goto out;
  460. lcu->pav = NO_PAV;
  461. for (i = 0; i < MAX_DEVICES_PER_LCU; ++i) {
  462. switch (lcu->uac->unit[i].ua_type) {
  463. case UA_BASE_PAV_ALIAS:
  464. lcu->pav = BASE_PAV;
  465. break;
  466. case UA_HYPER_PAV_ALIAS:
  467. lcu->pav = HYPER_PAV;
  468. break;
  469. }
  470. if (lcu->pav != NO_PAV)
  471. break;
  472. }
  473. list_for_each_entry_safe(device, tempdev, &lcu->active_devices,
  474. alias_list) {
  475. _add_device_to_lcu(lcu, device, refdev);
  476. }
  477. out:
  478. spin_unlock_irqrestore(&lcu->lock, flags);
  479. return 0;
  480. }
  481. static void lcu_update_work(struct work_struct *work)
  482. {
  483. struct alias_lcu *lcu;
  484. struct read_uac_work_data *ruac_data;
  485. struct dasd_device *device;
  486. unsigned long flags;
  487. int rc;
  488. ruac_data = container_of(work, struct read_uac_work_data, dwork.work);
  489. lcu = container_of(ruac_data, struct alias_lcu, ruac_data);
  490. device = ruac_data->device;
  491. rc = _lcu_update(device, lcu);
  492. /*
  493. * Need to check flags again, as there could have been another
  494. * prepare_update or a new device a new device while we were still
  495. * processing the data
  496. */
  497. spin_lock_irqsave(&lcu->lock, flags);
  498. if ((rc && (rc != -EOPNOTSUPP)) || (lcu->flags & NEED_UAC_UPDATE)) {
  499. DBF_DEV_EVENT(DBF_WARNING, device, "could not update"
  500. " alias data in lcu (rc = %d), retry later", rc);
  501. if (!schedule_delayed_work(&lcu->ruac_data.dwork, 30*HZ))
  502. dasd_put_device(device);
  503. } else {
  504. dasd_put_device(device);
  505. lcu->ruac_data.device = NULL;
  506. lcu->flags &= ~UPDATE_PENDING;
  507. }
  508. spin_unlock_irqrestore(&lcu->lock, flags);
  509. }
  510. static int _schedule_lcu_update(struct alias_lcu *lcu,
  511. struct dasd_device *device)
  512. {
  513. struct dasd_device *usedev = NULL;
  514. struct alias_pav_group *group;
  515. lcu->flags |= NEED_UAC_UPDATE;
  516. if (lcu->ruac_data.device) {
  517. /* already scheduled or running */
  518. return 0;
  519. }
  520. if (device && !list_empty(&device->alias_list))
  521. usedev = device;
  522. if (!usedev && !list_empty(&lcu->grouplist)) {
  523. group = list_first_entry(&lcu->grouplist,
  524. struct alias_pav_group, group);
  525. if (!list_empty(&group->baselist))
  526. usedev = list_first_entry(&group->baselist,
  527. struct dasd_device,
  528. alias_list);
  529. else if (!list_empty(&group->aliaslist))
  530. usedev = list_first_entry(&group->aliaslist,
  531. struct dasd_device,
  532. alias_list);
  533. }
  534. if (!usedev && !list_empty(&lcu->active_devices)) {
  535. usedev = list_first_entry(&lcu->active_devices,
  536. struct dasd_device, alias_list);
  537. }
  538. /*
  539. * if we haven't found a proper device yet, give up for now, the next
  540. * device that will be set active will trigger an lcu update
  541. */
  542. if (!usedev)
  543. return -EINVAL;
  544. dasd_get_device(usedev);
  545. lcu->ruac_data.device = usedev;
  546. if (!schedule_delayed_work(&lcu->ruac_data.dwork, 0))
  547. dasd_put_device(usedev);
  548. return 0;
  549. }
  550. int dasd_alias_add_device(struct dasd_device *device)
  551. {
  552. struct dasd_eckd_private *private = device->private;
  553. __u8 uaddr = private->uid.real_unit_addr;
  554. struct alias_lcu *lcu = private->lcu;
  555. unsigned long flags;
  556. int rc;
  557. rc = 0;
  558. spin_lock_irqsave(&lcu->lock, flags);
  559. /*
  560. * Check if device and lcu type differ. If so, the uac data may be
  561. * outdated and needs to be updated.
  562. */
  563. if (private->uid.type != lcu->uac->unit[uaddr].ua_type) {
  564. lcu->flags |= UPDATE_PENDING;
  565. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  566. "uid type mismatch - trigger rescan");
  567. }
  568. if (!(lcu->flags & UPDATE_PENDING)) {
  569. rc = _add_device_to_lcu(lcu, device, device);
  570. if (rc)
  571. lcu->flags |= UPDATE_PENDING;
  572. }
  573. if (lcu->flags & UPDATE_PENDING) {
  574. list_move(&device->alias_list, &lcu->active_devices);
  575. private->pavgroup = NULL;
  576. _schedule_lcu_update(lcu, device);
  577. }
  578. spin_unlock_irqrestore(&lcu->lock, flags);
  579. return rc;
  580. }
  581. int dasd_alias_update_add_device(struct dasd_device *device)
  582. {
  583. struct dasd_eckd_private *private = device->private;
  584. private->lcu->flags |= UPDATE_PENDING;
  585. return dasd_alias_add_device(device);
  586. }
  587. int dasd_alias_remove_device(struct dasd_device *device)
  588. {
  589. struct dasd_eckd_private *private = device->private;
  590. struct alias_lcu *lcu = private->lcu;
  591. unsigned long flags;
  592. /* nothing to do if already removed */
  593. if (!lcu)
  594. return 0;
  595. spin_lock_irqsave(&lcu->lock, flags);
  596. _remove_device_from_lcu(lcu, device);
  597. spin_unlock_irqrestore(&lcu->lock, flags);
  598. return 0;
  599. }
  600. struct dasd_device *dasd_alias_get_start_dev(struct dasd_device *base_device)
  601. {
  602. struct dasd_eckd_private *alias_priv, *private = base_device->private;
  603. struct alias_lcu *lcu = private->lcu;
  604. struct dasd_device *alias_device;
  605. struct alias_pav_group *group;
  606. unsigned long flags;
  607. if (!lcu)
  608. return NULL;
  609. if (lcu->pav == NO_PAV ||
  610. lcu->flags & (NEED_UAC_UPDATE | UPDATE_PENDING))
  611. return NULL;
  612. if (unlikely(!(private->features.feature[8] & 0x01))) {
  613. /*
  614. * PAV enabled but prefix not, very unlikely
  615. * seems to be a lost pathgroup
  616. * use base device to do IO
  617. */
  618. DBF_DEV_EVENT(DBF_ERR, base_device, "%s",
  619. "Prefix not enabled with PAV enabled\n");
  620. return NULL;
  621. }
  622. spin_lock_irqsave(&lcu->lock, flags);
  623. group = private->pavgroup;
  624. if (!group) {
  625. spin_unlock_irqrestore(&lcu->lock, flags);
  626. return NULL;
  627. }
  628. alias_device = group->next;
  629. if (!alias_device) {
  630. if (list_empty(&group->aliaslist)) {
  631. spin_unlock_irqrestore(&lcu->lock, flags);
  632. return NULL;
  633. } else {
  634. alias_device = list_first_entry(&group->aliaslist,
  635. struct dasd_device,
  636. alias_list);
  637. }
  638. }
  639. if (list_is_last(&alias_device->alias_list, &group->aliaslist))
  640. group->next = list_first_entry(&group->aliaslist,
  641. struct dasd_device, alias_list);
  642. else
  643. group->next = list_first_entry(&alias_device->alias_list,
  644. struct dasd_device, alias_list);
  645. spin_unlock_irqrestore(&lcu->lock, flags);
  646. alias_priv = alias_device->private;
  647. if ((alias_priv->count < private->count) && !alias_device->stopped &&
  648. !test_bit(DASD_FLAG_OFFLINE, &alias_device->flags))
  649. return alias_device;
  650. else
  651. return NULL;
  652. }
  653. /*
  654. * Summary unit check handling depends on the way alias devices
  655. * are handled so it is done here rather then in dasd_eckd.c
  656. */
  657. static int reset_summary_unit_check(struct alias_lcu *lcu,
  658. struct dasd_device *device,
  659. char reason)
  660. {
  661. struct dasd_ccw_req *cqr;
  662. int rc = 0;
  663. struct ccw1 *ccw;
  664. cqr = lcu->rsu_cqr;
  665. memcpy((char *) &cqr->magic, "ECKD", 4);
  666. ASCEBC((char *) &cqr->magic, 4);
  667. ccw = cqr->cpaddr;
  668. ccw->cmd_code = DASD_ECKD_CCW_RSCK;
  669. ccw->flags = CCW_FLAG_SLI;
  670. ccw->count = 16;
  671. ccw->cda = virt_to_dma32(cqr->data);
  672. ((char *)cqr->data)[0] = reason;
  673. clear_bit(DASD_CQR_FLAGS_USE_ERP, &cqr->flags);
  674. cqr->retries = 255; /* set retry counter to enable basic ERP */
  675. cqr->startdev = device;
  676. cqr->memdev = device;
  677. cqr->block = NULL;
  678. cqr->expires = 5 * HZ;
  679. cqr->buildclk = get_tod_clock();
  680. cqr->status = DASD_CQR_FILLED;
  681. rc = dasd_sleep_on_immediatly(cqr);
  682. return rc;
  683. }
  684. static void _restart_all_base_devices_on_lcu(struct alias_lcu *lcu)
  685. {
  686. struct alias_pav_group *pavgroup;
  687. struct dasd_device *device;
  688. struct dasd_eckd_private *private;
  689. /* active and inactive list can contain alias as well as base devices */
  690. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  691. private = device->private;
  692. if (private->uid.type != UA_BASE_DEVICE)
  693. continue;
  694. dasd_schedule_block_bh(device->block);
  695. dasd_schedule_device_bh(device);
  696. }
  697. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  698. private = device->private;
  699. if (private->uid.type != UA_BASE_DEVICE)
  700. continue;
  701. dasd_schedule_block_bh(device->block);
  702. dasd_schedule_device_bh(device);
  703. }
  704. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  705. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  706. dasd_schedule_block_bh(device->block);
  707. dasd_schedule_device_bh(device);
  708. }
  709. }
  710. }
  711. static void flush_all_alias_devices_on_lcu(struct alias_lcu *lcu)
  712. {
  713. struct alias_pav_group *pavgroup;
  714. struct dasd_device *device, *temp;
  715. struct dasd_eckd_private *private;
  716. unsigned long flags;
  717. LIST_HEAD(active);
  718. /*
  719. * Problem here ist that dasd_flush_device_queue may wait
  720. * for termination of a request to complete. We can't keep
  721. * the lcu lock during that time, so we must assume that
  722. * the lists may have changed.
  723. * Idea: first gather all active alias devices in a separate list,
  724. * then flush the first element of this list unlocked, and afterwards
  725. * check if it is still on the list before moving it to the
  726. * active_devices list.
  727. */
  728. spin_lock_irqsave(&lcu->lock, flags);
  729. list_for_each_entry_safe(device, temp, &lcu->active_devices,
  730. alias_list) {
  731. private = device->private;
  732. if (private->uid.type == UA_BASE_DEVICE)
  733. continue;
  734. list_move(&device->alias_list, &active);
  735. }
  736. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  737. list_splice_init(&pavgroup->aliaslist, &active);
  738. }
  739. while (!list_empty(&active)) {
  740. device = list_first_entry(&active, struct dasd_device,
  741. alias_list);
  742. spin_unlock_irqrestore(&lcu->lock, flags);
  743. dasd_flush_device_queue(device);
  744. spin_lock_irqsave(&lcu->lock, flags);
  745. /*
  746. * only move device around if it wasn't moved away while we
  747. * were waiting for the flush
  748. */
  749. if (device == list_first_entry(&active,
  750. struct dasd_device, alias_list)) {
  751. list_move(&device->alias_list, &lcu->active_devices);
  752. private = device->private;
  753. private->pavgroup = NULL;
  754. }
  755. }
  756. spin_unlock_irqrestore(&lcu->lock, flags);
  757. }
  758. static void _stop_all_devices_on_lcu(struct alias_lcu *lcu)
  759. {
  760. struct alias_pav_group *pavgroup;
  761. struct dasd_device *device;
  762. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  763. spin_lock(get_ccwdev_lock(device->cdev));
  764. dasd_device_set_stop_bits(device, DASD_STOPPED_SU);
  765. spin_unlock(get_ccwdev_lock(device->cdev));
  766. }
  767. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  768. spin_lock(get_ccwdev_lock(device->cdev));
  769. dasd_device_set_stop_bits(device, DASD_STOPPED_SU);
  770. spin_unlock(get_ccwdev_lock(device->cdev));
  771. }
  772. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  773. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  774. spin_lock(get_ccwdev_lock(device->cdev));
  775. dasd_device_set_stop_bits(device, DASD_STOPPED_SU);
  776. spin_unlock(get_ccwdev_lock(device->cdev));
  777. }
  778. list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
  779. spin_lock(get_ccwdev_lock(device->cdev));
  780. dasd_device_set_stop_bits(device, DASD_STOPPED_SU);
  781. spin_unlock(get_ccwdev_lock(device->cdev));
  782. }
  783. }
  784. }
  785. static void _unstop_all_devices_on_lcu(struct alias_lcu *lcu)
  786. {
  787. struct alias_pav_group *pavgroup;
  788. struct dasd_device *device;
  789. list_for_each_entry(device, &lcu->active_devices, alias_list) {
  790. spin_lock(get_ccwdev_lock(device->cdev));
  791. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  792. spin_unlock(get_ccwdev_lock(device->cdev));
  793. }
  794. list_for_each_entry(device, &lcu->inactive_devices, alias_list) {
  795. spin_lock(get_ccwdev_lock(device->cdev));
  796. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  797. spin_unlock(get_ccwdev_lock(device->cdev));
  798. }
  799. list_for_each_entry(pavgroup, &lcu->grouplist, group) {
  800. list_for_each_entry(device, &pavgroup->baselist, alias_list) {
  801. spin_lock(get_ccwdev_lock(device->cdev));
  802. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  803. spin_unlock(get_ccwdev_lock(device->cdev));
  804. }
  805. list_for_each_entry(device, &pavgroup->aliaslist, alias_list) {
  806. spin_lock(get_ccwdev_lock(device->cdev));
  807. dasd_device_remove_stop_bits(device, DASD_STOPPED_SU);
  808. spin_unlock(get_ccwdev_lock(device->cdev));
  809. }
  810. }
  811. }
  812. static void summary_unit_check_handling_work(struct work_struct *work)
  813. {
  814. struct alias_lcu *lcu;
  815. struct summary_unit_check_work_data *suc_data;
  816. unsigned long flags;
  817. struct dasd_device *device;
  818. suc_data = container_of(work, struct summary_unit_check_work_data,
  819. worker);
  820. lcu = container_of(suc_data, struct alias_lcu, suc_data);
  821. device = suc_data->device;
  822. /* 1. flush alias devices */
  823. flush_all_alias_devices_on_lcu(lcu);
  824. /* 2. reset summary unit check */
  825. spin_lock_irqsave(get_ccwdev_lock(device->cdev), flags);
  826. dasd_device_remove_stop_bits(device,
  827. (DASD_STOPPED_SU | DASD_STOPPED_PENDING));
  828. spin_unlock_irqrestore(get_ccwdev_lock(device->cdev), flags);
  829. reset_summary_unit_check(lcu, device, suc_data->reason);
  830. spin_lock_irqsave(&lcu->lock, flags);
  831. _unstop_all_devices_on_lcu(lcu);
  832. _restart_all_base_devices_on_lcu(lcu);
  833. /* 3. read new alias configuration */
  834. _schedule_lcu_update(lcu, device);
  835. lcu->suc_data.device = NULL;
  836. dasd_put_device(device);
  837. spin_unlock_irqrestore(&lcu->lock, flags);
  838. }
  839. void dasd_alias_handle_summary_unit_check(struct work_struct *work)
  840. {
  841. struct dasd_device *device = container_of(work, struct dasd_device,
  842. suc_work);
  843. struct dasd_eckd_private *private = device->private;
  844. struct alias_lcu *lcu;
  845. unsigned long flags;
  846. lcu = private->lcu;
  847. if (!lcu) {
  848. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  849. "device not ready to handle summary"
  850. " unit check (no lcu structure)");
  851. goto out;
  852. }
  853. spin_lock_irqsave(&lcu->lock, flags);
  854. /* If this device is about to be removed just return and wait for
  855. * the next interrupt on a different device
  856. */
  857. if (list_empty(&device->alias_list)) {
  858. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  859. "device is in offline processing,"
  860. " don't do summary unit check handling");
  861. goto out_unlock;
  862. }
  863. if (lcu->suc_data.device) {
  864. /* already scheduled or running */
  865. DBF_DEV_EVENT(DBF_WARNING, device, "%s",
  866. "previous instance of summary unit check worker"
  867. " still pending");
  868. goto out_unlock;
  869. }
  870. _stop_all_devices_on_lcu(lcu);
  871. /* prepare for lcu_update */
  872. lcu->flags |= NEED_UAC_UPDATE | UPDATE_PENDING;
  873. lcu->suc_data.reason = private->suc_reason;
  874. lcu->suc_data.device = device;
  875. dasd_get_device(device);
  876. if (!schedule_work(&lcu->suc_data.worker))
  877. dasd_put_device(device);
  878. out_unlock:
  879. spin_unlock_irqrestore(&lcu->lock, flags);
  880. out:
  881. clear_bit(DASD_FLAG_SUC, &device->flags);
  882. dasd_put_device(device);
  883. };