zfcp_aux.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. /*
  22. * Driver authors:
  23. * Martin Peschke (originator of the driver)
  24. * Raimund Schroeder
  25. * Aron Zeh
  26. * Wolfgang Taphorn
  27. * Stefan Bader
  28. * Heiko Carstens (kernel 2.6 port of the driver)
  29. * Andreas Herrmann
  30. * Maxim Shchetynin
  31. * Volker Sameske
  32. * Ralph Wuerthner
  33. */
  34. #include "zfcp_ext.h"
  35. /* accumulated log level (module parameter) */
  36. static u32 loglevel = ZFCP_LOG_LEVEL_DEFAULTS;
  37. static char *device;
  38. /*********************** FUNCTION PROTOTYPES *********************************/
  39. /* written against the module interface */
  40. static int __init zfcp_module_init(void);
  41. /* miscellaneous */
  42. static int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
  43. static void zfcp_sg_list_free(struct zfcp_sg_list *);
  44. static int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
  45. void __user *, size_t);
  46. static int zfcp_sg_list_copy_to_user(void __user *,
  47. struct zfcp_sg_list *, size_t);
  48. static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
  49. #define ZFCP_CFDC_IOC_MAGIC 0xDD
  50. #define ZFCP_CFDC_IOC \
  51. _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
  52. static const struct file_operations zfcp_cfdc_fops = {
  53. .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
  54. #ifdef CONFIG_COMPAT
  55. .compat_ioctl = zfcp_cfdc_dev_ioctl
  56. #endif
  57. };
  58. static struct miscdevice zfcp_cfdc_misc = {
  59. .minor = ZFCP_CFDC_DEV_MINOR,
  60. .name = ZFCP_CFDC_DEV_NAME,
  61. .fops = &zfcp_cfdc_fops
  62. };
  63. /*********************** KERNEL/MODULE PARAMETERS ***************************/
  64. /* declare driver module init/cleanup functions */
  65. module_init(zfcp_module_init);
  66. MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
  67. MODULE_DESCRIPTION
  68. ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
  69. MODULE_LICENSE("GPL");
  70. module_param(device, charp, 0400);
  71. MODULE_PARM_DESC(device, "specify initial device");
  72. module_param(loglevel, uint, 0400);
  73. MODULE_PARM_DESC(loglevel,
  74. "log levels, 8 nibbles: "
  75. "FC ERP QDIO CIO Config FSF SCSI Other, "
  76. "levels: 0=none 1=normal 2=devel 3=trace");
  77. /****************************************************************/
  78. /************** Functions without logging ***********************/
  79. /****************************************************************/
  80. void
  81. _zfcp_hex_dump(char *addr, int count)
  82. {
  83. int i;
  84. for (i = 0; i < count; i++) {
  85. printk("%02x", addr[i]);
  86. if ((i % 4) == 3)
  87. printk(" ");
  88. if ((i % 32) == 31)
  89. printk("\n");
  90. }
  91. if (((i-1) % 32) != 31)
  92. printk("\n");
  93. }
  94. /****************************************************************/
  95. /****** Functions to handle the request ID hash table ********/
  96. /****************************************************************/
  97. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF
  98. static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
  99. {
  100. int idx;
  101. adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
  102. GFP_KERNEL);
  103. if (!adapter->req_list)
  104. return -ENOMEM;
  105. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  106. INIT_LIST_HEAD(&adapter->req_list[idx]);
  107. return 0;
  108. }
  109. static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
  110. {
  111. kfree(adapter->req_list);
  112. }
  113. int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
  114. {
  115. unsigned int idx;
  116. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  117. if (!list_empty(&adapter->req_list[idx]))
  118. return 0;
  119. return 1;
  120. }
  121. #undef ZFCP_LOG_AREA
  122. /****************************************************************/
  123. /************** Uncategorised Functions *************************/
  124. /****************************************************************/
  125. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
  126. /**
  127. * zfcp_device_setup - setup function
  128. * @str: pointer to parameter string
  129. *
  130. * Parse "device=..." parameter string.
  131. */
  132. static int __init
  133. zfcp_device_setup(char *devstr)
  134. {
  135. char *tmp, *str;
  136. size_t len;
  137. if (!devstr)
  138. return 0;
  139. len = strlen(devstr) + 1;
  140. str = kmalloc(len, GFP_KERNEL);
  141. if (!str)
  142. goto err_out;
  143. memcpy(str, devstr, len);
  144. tmp = strchr(str, ',');
  145. if (!tmp)
  146. goto err_out;
  147. *tmp++ = '\0';
  148. strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
  149. zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
  150. zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
  151. if (*tmp++ != ',')
  152. goto err_out;
  153. if (*tmp == '\0')
  154. goto err_out;
  155. zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
  156. if (*tmp != '\0')
  157. goto err_out;
  158. kfree(str);
  159. return 1;
  160. err_out:
  161. ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
  162. kfree(str);
  163. return 0;
  164. }
  165. static void __init
  166. zfcp_init_device_configure(void)
  167. {
  168. struct zfcp_adapter *adapter;
  169. struct zfcp_port *port;
  170. struct zfcp_unit *unit;
  171. down(&zfcp_data.config_sema);
  172. read_lock_irq(&zfcp_data.config_lock);
  173. adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
  174. if (adapter)
  175. zfcp_adapter_get(adapter);
  176. read_unlock_irq(&zfcp_data.config_lock);
  177. if (adapter == NULL)
  178. goto out_adapter;
  179. port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
  180. if (!port)
  181. goto out_port;
  182. unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
  183. if (!unit)
  184. goto out_unit;
  185. up(&zfcp_data.config_sema);
  186. ccw_device_set_online(adapter->ccw_device);
  187. zfcp_erp_wait(adapter);
  188. down(&zfcp_data.config_sema);
  189. zfcp_unit_put(unit);
  190. out_unit:
  191. zfcp_port_put(port);
  192. out_port:
  193. zfcp_adapter_put(adapter);
  194. out_adapter:
  195. up(&zfcp_data.config_sema);
  196. return;
  197. }
  198. static int calc_alignment(int size)
  199. {
  200. int align = 1;
  201. if (!size)
  202. return 0;
  203. while ((size - align) > 0)
  204. align <<= 1;
  205. return align;
  206. }
  207. static int __init
  208. zfcp_module_init(void)
  209. {
  210. int retval = -ENOMEM;
  211. int size, align;
  212. size = sizeof(struct zfcp_fsf_req_qtcb);
  213. align = calc_alignment(size);
  214. zfcp_data.fsf_req_qtcb_cache =
  215. kmem_cache_create("zfcp_fsf", size, align, 0, NULL);
  216. if (!zfcp_data.fsf_req_qtcb_cache)
  217. goto out;
  218. size = sizeof(struct fsf_status_read_buffer);
  219. align = calc_alignment(size);
  220. zfcp_data.sr_buffer_cache =
  221. kmem_cache_create("zfcp_sr", size, align, 0, NULL);
  222. if (!zfcp_data.sr_buffer_cache)
  223. goto out_sr_cache;
  224. size = sizeof(struct zfcp_gid_pn_data);
  225. align = calc_alignment(size);
  226. zfcp_data.gid_pn_cache =
  227. kmem_cache_create("zfcp_gid", size, align, 0, NULL);
  228. if (!zfcp_data.gid_pn_cache)
  229. goto out_gid_cache;
  230. atomic_set(&zfcp_data.loglevel, loglevel);
  231. /* initialize adapter list */
  232. INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
  233. /* initialize adapters to be removed list head */
  234. INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
  235. zfcp_data.scsi_transport_template =
  236. fc_attach_transport(&zfcp_transport_functions);
  237. if (!zfcp_data.scsi_transport_template)
  238. goto out_transport;
  239. retval = misc_register(&zfcp_cfdc_misc);
  240. if (retval != 0) {
  241. ZFCP_LOG_INFO("registration of misc device "
  242. "zfcp_cfdc failed\n");
  243. goto out_misc;
  244. }
  245. ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
  246. ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
  247. /* Initialise proc semaphores */
  248. sema_init(&zfcp_data.config_sema, 1);
  249. /* initialise configuration rw lock */
  250. rwlock_init(&zfcp_data.config_lock);
  251. /* setup dynamic I/O */
  252. retval = zfcp_ccw_register();
  253. if (retval) {
  254. ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
  255. goto out_ccw_register;
  256. }
  257. if (zfcp_device_setup(device))
  258. zfcp_init_device_configure();
  259. goto out;
  260. out_ccw_register:
  261. misc_deregister(&zfcp_cfdc_misc);
  262. out_misc:
  263. fc_release_transport(zfcp_data.scsi_transport_template);
  264. out_transport:
  265. kmem_cache_destroy(zfcp_data.gid_pn_cache);
  266. out_gid_cache:
  267. kmem_cache_destroy(zfcp_data.sr_buffer_cache);
  268. out_sr_cache:
  269. kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
  270. out:
  271. return retval;
  272. }
  273. /*
  274. * function: zfcp_cfdc_dev_ioctl
  275. *
  276. * purpose: Handle control file upload/download transaction via IOCTL
  277. * interface
  278. *
  279. * returns: 0 - Operation completed successfuly
  280. * -ENOTTY - Unknown IOCTL command
  281. * -EINVAL - Invalid sense data record
  282. * -ENXIO - The FCP adapter is not available
  283. * -EOPNOTSUPP - The FCP adapter does not have CFDC support
  284. * -ENOMEM - Insufficient memory
  285. * -EFAULT - User space memory I/O operation fault
  286. * -EPERM - Cannot create or queue FSF request or create SBALs
  287. * -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
  288. */
  289. static long
  290. zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
  291. unsigned long buffer)
  292. {
  293. struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
  294. struct zfcp_adapter *adapter = NULL;
  295. struct zfcp_fsf_req *fsf_req = NULL;
  296. struct zfcp_sg_list *sg_list = NULL;
  297. u32 fsf_command, option;
  298. char *bus_id = NULL;
  299. int retval = 0;
  300. sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
  301. if (sense_data == NULL) {
  302. retval = -ENOMEM;
  303. goto out;
  304. }
  305. sg_list = kzalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
  306. if (sg_list == NULL) {
  307. retval = -ENOMEM;
  308. goto out;
  309. }
  310. if (command != ZFCP_CFDC_IOC) {
  311. ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
  312. retval = -ENOTTY;
  313. goto out;
  314. }
  315. if ((sense_data_user = (void __user *) buffer) == NULL) {
  316. ZFCP_LOG_INFO("sense data record is required\n");
  317. retval = -EINVAL;
  318. goto out;
  319. }
  320. retval = copy_from_user(sense_data, sense_data_user,
  321. sizeof(struct zfcp_cfdc_sense_data));
  322. if (retval) {
  323. retval = -EFAULT;
  324. goto out;
  325. }
  326. if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
  327. ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
  328. ZFCP_CFDC_SIGNATURE);
  329. retval = -EINVAL;
  330. goto out;
  331. }
  332. switch (sense_data->command) {
  333. case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
  334. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  335. option = FSF_CFDC_OPTION_NORMAL_MODE;
  336. break;
  337. case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
  338. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  339. option = FSF_CFDC_OPTION_FORCE;
  340. break;
  341. case ZFCP_CFDC_CMND_FULL_ACCESS:
  342. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  343. option = FSF_CFDC_OPTION_FULL_ACCESS;
  344. break;
  345. case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
  346. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  347. option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
  348. break;
  349. case ZFCP_CFDC_CMND_UPLOAD:
  350. fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
  351. option = 0;
  352. break;
  353. default:
  354. ZFCP_LOG_INFO("invalid command code 0x%08x\n",
  355. sense_data->command);
  356. retval = -EINVAL;
  357. goto out;
  358. }
  359. bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  360. if (bus_id == NULL) {
  361. retval = -ENOMEM;
  362. goto out;
  363. }
  364. snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
  365. (sense_data->devno >> 24),
  366. (sense_data->devno >> 16) & 0xFF,
  367. (sense_data->devno & 0xFFFF));
  368. read_lock_irq(&zfcp_data.config_lock);
  369. adapter = zfcp_get_adapter_by_busid(bus_id);
  370. if (adapter)
  371. zfcp_adapter_get(adapter);
  372. read_unlock_irq(&zfcp_data.config_lock);
  373. kfree(bus_id);
  374. if (adapter == NULL) {
  375. ZFCP_LOG_INFO("invalid adapter\n");
  376. retval = -ENXIO;
  377. goto out;
  378. }
  379. if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
  380. retval = zfcp_sg_list_alloc(sg_list,
  381. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  382. if (retval) {
  383. retval = -ENOMEM;
  384. goto out;
  385. }
  386. }
  387. if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
  388. (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
  389. retval = zfcp_sg_list_copy_from_user(
  390. sg_list, &sense_data_user->control_file,
  391. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  392. if (retval) {
  393. retval = -EFAULT;
  394. goto out;
  395. }
  396. }
  397. retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
  398. option, sg_list);
  399. if (retval)
  400. goto out;
  401. if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
  402. (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
  403. retval = -ENXIO;
  404. goto out;
  405. }
  406. sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
  407. memcpy(&sense_data->fsf_status_qual,
  408. &fsf_req->qtcb->header.fsf_status_qual,
  409. sizeof(union fsf_status_qual));
  410. memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
  411. retval = copy_to_user(sense_data_user, sense_data,
  412. sizeof(struct zfcp_cfdc_sense_data));
  413. if (retval) {
  414. retval = -EFAULT;
  415. goto out;
  416. }
  417. if (sense_data->command & ZFCP_CFDC_UPLOAD) {
  418. retval = zfcp_sg_list_copy_to_user(
  419. &sense_data_user->control_file, sg_list,
  420. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  421. if (retval) {
  422. retval = -EFAULT;
  423. goto out;
  424. }
  425. }
  426. out:
  427. if (fsf_req != NULL)
  428. zfcp_fsf_req_free(fsf_req);
  429. if ((adapter != NULL) && (retval != -ENXIO))
  430. zfcp_adapter_put(adapter);
  431. if (sg_list != NULL) {
  432. zfcp_sg_list_free(sg_list);
  433. kfree(sg_list);
  434. }
  435. kfree(sense_data);
  436. return retval;
  437. }
  438. /**
  439. * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
  440. * @sg_list: structure describing a scatter gather list
  441. * @size: size of scatter-gather list
  442. * Return: 0 on success, else -ENOMEM
  443. *
  444. * In sg_list->sg a pointer to the created scatter-gather list is returned,
  445. * or NULL if we run out of memory. sg_list->count specifies the number of
  446. * elements of the scatter-gather list. The maximum size of a single element
  447. * in the scatter-gather list is PAGE_SIZE.
  448. */
  449. static int
  450. zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
  451. {
  452. struct scatterlist *sg;
  453. unsigned int i;
  454. int retval = 0;
  455. void *address;
  456. BUG_ON(sg_list == NULL);
  457. sg_list->count = size >> PAGE_SHIFT;
  458. if (size & ~PAGE_MASK)
  459. sg_list->count++;
  460. sg_list->sg = kcalloc(sg_list->count, sizeof(struct scatterlist),
  461. GFP_KERNEL);
  462. if (sg_list->sg == NULL) {
  463. sg_list->count = 0;
  464. retval = -ENOMEM;
  465. goto out;
  466. }
  467. sg_init_table(sg_list->sg, sg_list->count);
  468. for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
  469. address = (void *) get_zeroed_page(GFP_KERNEL);
  470. if (address == NULL) {
  471. sg_list->count = i;
  472. zfcp_sg_list_free(sg_list);
  473. retval = -ENOMEM;
  474. goto out;
  475. }
  476. zfcp_address_to_sg(address, sg, min(size, PAGE_SIZE));
  477. size -= sg->length;
  478. }
  479. out:
  480. return retval;
  481. }
  482. /**
  483. * zfcp_sg_list_free - free memory of a scatter-gather list
  484. * @sg_list: structure describing a scatter-gather list
  485. *
  486. * Memory for each element in the scatter-gather list is freed.
  487. * Finally sg_list->sg is freed itself and sg_list->count is reset.
  488. */
  489. static void
  490. zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
  491. {
  492. struct scatterlist *sg;
  493. unsigned int i;
  494. BUG_ON(sg_list == NULL);
  495. for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
  496. free_page((unsigned long) zfcp_sg_to_address(sg));
  497. sg_list->count = 0;
  498. kfree(sg_list->sg);
  499. }
  500. /**
  501. * zfcp_sg_size - determine size of a scatter-gather list
  502. * @sg: array of (struct scatterlist)
  503. * @sg_count: elements in array
  504. * Return: size of entire scatter-gather list
  505. */
  506. static size_t zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
  507. {
  508. unsigned int i;
  509. struct scatterlist *p;
  510. size_t size;
  511. size = 0;
  512. for (i = 0, p = sg; i < sg_count; i++, p++) {
  513. BUG_ON(p == NULL);
  514. size += p->length;
  515. }
  516. return size;
  517. }
  518. /**
  519. * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
  520. * @sg_list: structure describing a scatter-gather list
  521. * @user_buffer: pointer to buffer in user space
  522. * @size: number of bytes to be copied
  523. * Return: 0 on success, -EFAULT if copy_from_user fails.
  524. */
  525. static int
  526. zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
  527. void __user *user_buffer,
  528. size_t size)
  529. {
  530. struct scatterlist *sg;
  531. unsigned int length;
  532. void *zfcp_buffer;
  533. int retval = 0;
  534. BUG_ON(sg_list == NULL);
  535. if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
  536. return -EFAULT;
  537. for (sg = sg_list->sg; size > 0; sg++) {
  538. length = min((unsigned int)size, sg->length);
  539. zfcp_buffer = zfcp_sg_to_address(sg);
  540. if (copy_from_user(zfcp_buffer, user_buffer, length)) {
  541. retval = -EFAULT;
  542. goto out;
  543. }
  544. user_buffer += length;
  545. size -= length;
  546. }
  547. out:
  548. return retval;
  549. }
  550. /**
  551. * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
  552. * @user_buffer: pointer to buffer in user space
  553. * @sg_list: structure describing a scatter-gather list
  554. * @size: number of bytes to be copied
  555. * Return: 0 on success, -EFAULT if copy_to_user fails
  556. */
  557. static int
  558. zfcp_sg_list_copy_to_user(void __user *user_buffer,
  559. struct zfcp_sg_list *sg_list,
  560. size_t size)
  561. {
  562. struct scatterlist *sg;
  563. unsigned int length;
  564. void *zfcp_buffer;
  565. int retval = 0;
  566. BUG_ON(sg_list == NULL);
  567. if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
  568. return -EFAULT;
  569. for (sg = sg_list->sg; size > 0; sg++) {
  570. length = min((unsigned int) size, sg->length);
  571. zfcp_buffer = zfcp_sg_to_address(sg);
  572. if (copy_to_user(user_buffer, zfcp_buffer, length)) {
  573. retval = -EFAULT;
  574. goto out;
  575. }
  576. user_buffer += length;
  577. size -= length;
  578. }
  579. out:
  580. return retval;
  581. }
  582. #undef ZFCP_LOG_AREA
  583. /****************************************************************/
  584. /****** Functions for configuration/set-up of structures ********/
  585. /****************************************************************/
  586. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
  587. /**
  588. * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
  589. * @port: pointer to port to search for unit
  590. * @fcp_lun: FCP LUN to search for
  591. * Traverse list of all units of a port and return pointer to a unit
  592. * with the given FCP LUN.
  593. */
  594. struct zfcp_unit *
  595. zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
  596. {
  597. struct zfcp_unit *unit;
  598. int found = 0;
  599. list_for_each_entry(unit, &port->unit_list_head, list) {
  600. if ((unit->fcp_lun == fcp_lun) &&
  601. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
  602. {
  603. found = 1;
  604. break;
  605. }
  606. }
  607. return found ? unit : NULL;
  608. }
  609. /**
  610. * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
  611. * @adapter: pointer to adapter to search for port
  612. * @wwpn: wwpn to search for
  613. * Traverse list of all ports of an adapter and return pointer to a port
  614. * with the given wwpn.
  615. */
  616. struct zfcp_port *
  617. zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
  618. {
  619. struct zfcp_port *port;
  620. int found = 0;
  621. list_for_each_entry(port, &adapter->port_list_head, list) {
  622. if ((port->wwpn == wwpn) &&
  623. !(atomic_read(&port->status) &
  624. (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
  625. found = 1;
  626. break;
  627. }
  628. }
  629. return found ? port : NULL;
  630. }
  631. /**
  632. * zfcp_get_port_by_did - find port in port list of adapter by d_id
  633. * @adapter: pointer to adapter to search for port
  634. * @d_id: d_id to search for
  635. * Traverse list of all ports of an adapter and return pointer to a port
  636. * with the given d_id.
  637. */
  638. struct zfcp_port *
  639. zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
  640. {
  641. struct zfcp_port *port;
  642. int found = 0;
  643. list_for_each_entry(port, &adapter->port_list_head, list) {
  644. if ((port->d_id == d_id) &&
  645. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
  646. {
  647. found = 1;
  648. break;
  649. }
  650. }
  651. return found ? port : NULL;
  652. }
  653. /**
  654. * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
  655. * @bus_id: bus_id to search for
  656. * Traverse list of all adapters and return pointer to an adapter
  657. * with the given bus_id.
  658. */
  659. struct zfcp_adapter *
  660. zfcp_get_adapter_by_busid(char *bus_id)
  661. {
  662. struct zfcp_adapter *adapter;
  663. int found = 0;
  664. list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
  665. if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
  666. BUS_ID_SIZE) == 0) &&
  667. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
  668. &adapter->status)){
  669. found = 1;
  670. break;
  671. }
  672. }
  673. return found ? adapter : NULL;
  674. }
  675. /**
  676. * zfcp_unit_enqueue - enqueue unit to unit list of a port.
  677. * @port: pointer to port where unit is added
  678. * @fcp_lun: FCP LUN of unit to be enqueued
  679. * Return: pointer to enqueued unit on success, NULL on error
  680. * Locks: config_sema must be held to serialize changes to the unit list
  681. *
  682. * Sets up some unit internal structures and creates sysfs entry.
  683. */
  684. struct zfcp_unit *
  685. zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
  686. {
  687. struct zfcp_unit *unit;
  688. /*
  689. * check that there is no unit with this FCP_LUN already in list
  690. * and enqueue it.
  691. * Note: Unlike for the adapter and the port, this is an error
  692. */
  693. read_lock_irq(&zfcp_data.config_lock);
  694. unit = zfcp_get_unit_by_lun(port, fcp_lun);
  695. read_unlock_irq(&zfcp_data.config_lock);
  696. if (unit)
  697. return NULL;
  698. unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
  699. if (!unit)
  700. return NULL;
  701. /* initialise reference count stuff */
  702. atomic_set(&unit->refcount, 0);
  703. init_waitqueue_head(&unit->remove_wq);
  704. unit->port = port;
  705. unit->fcp_lun = fcp_lun;
  706. /* setup for sysfs registration */
  707. snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
  708. unit->sysfs_device.parent = &port->sysfs_device;
  709. unit->sysfs_device.release = zfcp_sysfs_unit_release;
  710. dev_set_drvdata(&unit->sysfs_device, unit);
  711. /* mark unit unusable as long as sysfs registration is not complete */
  712. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  713. spin_lock_init(&unit->latencies.lock);
  714. unit->latencies.write.channel.min = 0xFFFFFFFF;
  715. unit->latencies.write.fabric.min = 0xFFFFFFFF;
  716. unit->latencies.read.channel.min = 0xFFFFFFFF;
  717. unit->latencies.read.fabric.min = 0xFFFFFFFF;
  718. unit->latencies.cmd.channel.min = 0xFFFFFFFF;
  719. unit->latencies.cmd.fabric.min = 0xFFFFFFFF;
  720. if (device_register(&unit->sysfs_device)) {
  721. kfree(unit);
  722. return NULL;
  723. }
  724. if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
  725. device_unregister(&unit->sysfs_device);
  726. return NULL;
  727. }
  728. zfcp_unit_get(unit);
  729. unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
  730. write_lock_irq(&zfcp_data.config_lock);
  731. list_add_tail(&unit->list, &port->unit_list_head);
  732. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  733. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
  734. write_unlock_irq(&zfcp_data.config_lock);
  735. port->units++;
  736. zfcp_port_get(port);
  737. return unit;
  738. }
  739. void
  740. zfcp_unit_dequeue(struct zfcp_unit *unit)
  741. {
  742. zfcp_unit_wait(unit);
  743. write_lock_irq(&zfcp_data.config_lock);
  744. list_del(&unit->list);
  745. write_unlock_irq(&zfcp_data.config_lock);
  746. unit->port->units--;
  747. zfcp_port_put(unit->port);
  748. zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
  749. device_unregister(&unit->sysfs_device);
  750. }
  751. /*
  752. * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
  753. * commands.
  754. * It also genrates fcp-nameserver request/response buffer and unsolicited
  755. * status read fsf_req buffers.
  756. *
  757. * locks: must only be called with zfcp_data.config_sema taken
  758. */
  759. static int
  760. zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
  761. {
  762. adapter->pool.fsf_req_erp =
  763. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
  764. zfcp_data.fsf_req_qtcb_cache);
  765. if (!adapter->pool.fsf_req_erp)
  766. return -ENOMEM;
  767. adapter->pool.fsf_req_scsi =
  768. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
  769. zfcp_data.fsf_req_qtcb_cache);
  770. if (!adapter->pool.fsf_req_scsi)
  771. return -ENOMEM;
  772. adapter->pool.fsf_req_abort =
  773. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
  774. zfcp_data.fsf_req_qtcb_cache);
  775. if (!adapter->pool.fsf_req_abort)
  776. return -ENOMEM;
  777. adapter->pool.fsf_req_status_read =
  778. mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
  779. sizeof(struct zfcp_fsf_req));
  780. if (!adapter->pool.fsf_req_status_read)
  781. return -ENOMEM;
  782. adapter->pool.data_status_read =
  783. mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
  784. zfcp_data.sr_buffer_cache);
  785. if (!adapter->pool.data_status_read)
  786. return -ENOMEM;
  787. adapter->pool.data_gid_pn =
  788. mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
  789. zfcp_data.gid_pn_cache);
  790. if (!adapter->pool.data_gid_pn)
  791. return -ENOMEM;
  792. return 0;
  793. }
  794. /**
  795. * zfcp_free_low_mem_buffers - free memory pools of an adapter
  796. * @adapter: pointer to zfcp_adapter for which memory pools should be freed
  797. * locking: zfcp_data.config_sema must be held
  798. */
  799. static void
  800. zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
  801. {
  802. if (adapter->pool.fsf_req_erp)
  803. mempool_destroy(adapter->pool.fsf_req_erp);
  804. if (adapter->pool.fsf_req_scsi)
  805. mempool_destroy(adapter->pool.fsf_req_scsi);
  806. if (adapter->pool.fsf_req_abort)
  807. mempool_destroy(adapter->pool.fsf_req_abort);
  808. if (adapter->pool.fsf_req_status_read)
  809. mempool_destroy(adapter->pool.fsf_req_status_read);
  810. if (adapter->pool.data_status_read)
  811. mempool_destroy(adapter->pool.data_status_read);
  812. if (adapter->pool.data_gid_pn)
  813. mempool_destroy(adapter->pool.data_gid_pn);
  814. }
  815. static void zfcp_dummy_release(struct device *dev)
  816. {
  817. return;
  818. }
  819. int zfcp_status_read_refill(struct zfcp_adapter *adapter)
  820. {
  821. while (atomic_read(&adapter->stat_miss) > 0)
  822. if (zfcp_fsf_status_read(adapter, ZFCP_WAIT_FOR_SBAL))
  823. break;
  824. else
  825. atomic_dec(&adapter->stat_miss);
  826. if (ZFCP_STATUS_READS_RECOM <= atomic_read(&adapter->stat_miss)) {
  827. zfcp_erp_adapter_reopen(adapter, 0, 103, NULL);
  828. return 1;
  829. }
  830. return 0;
  831. }
  832. static void _zfcp_status_read_scheduler(struct work_struct *work)
  833. {
  834. zfcp_status_read_refill(container_of(work, struct zfcp_adapter,
  835. stat_work));
  836. }
  837. /*
  838. * Enqueues an adapter at the end of the adapter list in the driver data.
  839. * All adapter internal structures are set up.
  840. * Proc-fs entries are also created.
  841. *
  842. * returns: 0 if a new adapter was successfully enqueued
  843. * ZFCP_KNOWN if an adapter with this devno was already present
  844. * -ENOMEM if alloc failed
  845. * locks: config_sema must be held to serialise changes to the adapter list
  846. */
  847. struct zfcp_adapter *
  848. zfcp_adapter_enqueue(struct ccw_device *ccw_device)
  849. {
  850. int retval = 0;
  851. struct zfcp_adapter *adapter;
  852. /*
  853. * Note: It is safe to release the list_lock, as any list changes
  854. * are protected by the config_sema, which must be held to get here
  855. */
  856. /* try to allocate new adapter data structure (zeroed) */
  857. adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
  858. if (!adapter) {
  859. ZFCP_LOG_INFO("error: allocation of base adapter "
  860. "structure failed\n");
  861. goto out;
  862. }
  863. ccw_device->handler = NULL;
  864. /* save ccw_device pointer */
  865. adapter->ccw_device = ccw_device;
  866. retval = zfcp_qdio_allocate_queues(adapter);
  867. if (retval)
  868. goto queues_alloc_failed;
  869. retval = zfcp_qdio_allocate(adapter);
  870. if (retval)
  871. goto qdio_allocate_failed;
  872. retval = zfcp_allocate_low_mem_buffers(adapter);
  873. if (retval) {
  874. ZFCP_LOG_INFO("error: pool allocation failed\n");
  875. goto failed_low_mem_buffers;
  876. }
  877. /* initialise reference count stuff */
  878. atomic_set(&adapter->refcount, 0);
  879. init_waitqueue_head(&adapter->remove_wq);
  880. /* initialise list of ports */
  881. INIT_LIST_HEAD(&adapter->port_list_head);
  882. /* initialise list of ports to be removed */
  883. INIT_LIST_HEAD(&adapter->port_remove_lh);
  884. /* initialize list of fsf requests */
  885. spin_lock_init(&adapter->req_list_lock);
  886. retval = zfcp_reqlist_alloc(adapter);
  887. if (retval) {
  888. ZFCP_LOG_INFO("request list initialization failed\n");
  889. goto failed_low_mem_buffers;
  890. }
  891. /* initialize debug locks */
  892. spin_lock_init(&adapter->hba_dbf_lock);
  893. spin_lock_init(&adapter->san_dbf_lock);
  894. spin_lock_init(&adapter->scsi_dbf_lock);
  895. spin_lock_init(&adapter->rec_dbf_lock);
  896. retval = zfcp_adapter_debug_register(adapter);
  897. if (retval)
  898. goto debug_register_failed;
  899. /* initialize error recovery stuff */
  900. rwlock_init(&adapter->erp_lock);
  901. sema_init(&adapter->erp_ready_sem, 0);
  902. INIT_LIST_HEAD(&adapter->erp_ready_head);
  903. INIT_LIST_HEAD(&adapter->erp_running_head);
  904. /* initialize abort lock */
  905. rwlock_init(&adapter->abort_lock);
  906. /* initialise some erp stuff */
  907. init_waitqueue_head(&adapter->erp_thread_wqh);
  908. init_waitqueue_head(&adapter->erp_done_wqh);
  909. /* initialize lock of associated request queue */
  910. rwlock_init(&adapter->request_queue.queue_lock);
  911. INIT_WORK(&adapter->stat_work, _zfcp_status_read_scheduler);
  912. /* mark adapter unusable as long as sysfs registration is not complete */
  913. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  914. dev_set_drvdata(&ccw_device->dev, adapter);
  915. if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
  916. goto sysfs_failed;
  917. adapter->generic_services.parent = &adapter->ccw_device->dev;
  918. adapter->generic_services.release = zfcp_dummy_release;
  919. snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
  920. "generic_services");
  921. if (device_register(&adapter->generic_services))
  922. goto generic_services_failed;
  923. /* put allocated adapter at list tail */
  924. write_lock_irq(&zfcp_data.config_lock);
  925. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  926. list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
  927. write_unlock_irq(&zfcp_data.config_lock);
  928. zfcp_data.adapters++;
  929. goto out;
  930. generic_services_failed:
  931. zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
  932. sysfs_failed:
  933. zfcp_adapter_debug_unregister(adapter);
  934. debug_register_failed:
  935. dev_set_drvdata(&ccw_device->dev, NULL);
  936. zfcp_reqlist_free(adapter);
  937. failed_low_mem_buffers:
  938. zfcp_free_low_mem_buffers(adapter);
  939. if (qdio_free(ccw_device) != 0)
  940. ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
  941. zfcp_get_busid_by_adapter(adapter));
  942. qdio_allocate_failed:
  943. zfcp_qdio_free_queues(adapter);
  944. queues_alloc_failed:
  945. kfree(adapter);
  946. adapter = NULL;
  947. out:
  948. return adapter;
  949. }
  950. /*
  951. * returns: 0 - struct zfcp_adapter data structure successfully removed
  952. * !0 - struct zfcp_adapter data structure could not be removed
  953. * (e.g. still used)
  954. * locks: adapter list write lock is assumed to be held by caller
  955. */
  956. void
  957. zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
  958. {
  959. int retval = 0;
  960. unsigned long flags;
  961. cancel_work_sync(&adapter->stat_work);
  962. zfcp_adapter_scsi_unregister(adapter);
  963. device_unregister(&adapter->generic_services);
  964. zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
  965. dev_set_drvdata(&adapter->ccw_device->dev, NULL);
  966. /* sanity check: no pending FSF requests */
  967. spin_lock_irqsave(&adapter->req_list_lock, flags);
  968. retval = zfcp_reqlist_isempty(adapter);
  969. spin_unlock_irqrestore(&adapter->req_list_lock, flags);
  970. if (!retval) {
  971. ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
  972. "%i requests outstanding\n",
  973. zfcp_get_busid_by_adapter(adapter), adapter,
  974. atomic_read(&adapter->reqs_active));
  975. retval = -EBUSY;
  976. goto out;
  977. }
  978. zfcp_adapter_debug_unregister(adapter);
  979. /* remove specified adapter data structure from list */
  980. write_lock_irq(&zfcp_data.config_lock);
  981. list_del(&adapter->list);
  982. write_unlock_irq(&zfcp_data.config_lock);
  983. /* decrease number of adapters in list */
  984. zfcp_data.adapters--;
  985. ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
  986. "%i adapters still in list\n",
  987. zfcp_get_busid_by_adapter(adapter),
  988. adapter, zfcp_data.adapters);
  989. retval = qdio_free(adapter->ccw_device);
  990. if (retval)
  991. ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
  992. zfcp_get_busid_by_adapter(adapter));
  993. zfcp_free_low_mem_buffers(adapter);
  994. /* free memory of adapter data structure and queues */
  995. zfcp_qdio_free_queues(adapter);
  996. zfcp_reqlist_free(adapter);
  997. kfree(adapter->fc_stats);
  998. kfree(adapter->stats_reset_data);
  999. ZFCP_LOG_TRACE("freeing adapter structure\n");
  1000. kfree(adapter);
  1001. out:
  1002. return;
  1003. }
  1004. /**
  1005. * zfcp_port_enqueue - enqueue port to port list of adapter
  1006. * @adapter: adapter where remote port is added
  1007. * @wwpn: WWPN of the remote port to be enqueued
  1008. * @status: initial status for the port
  1009. * @d_id: destination id of the remote port to be enqueued
  1010. * Return: pointer to enqueued port on success, NULL on error
  1011. * Locks: config_sema must be held to serialize changes to the port list
  1012. *
  1013. * All port internal structures are set up and the sysfs entry is generated.
  1014. * d_id is used to enqueue ports with a well known address like the Directory
  1015. * Service for nameserver lookup.
  1016. */
  1017. struct zfcp_port *
  1018. zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
  1019. u32 d_id)
  1020. {
  1021. struct zfcp_port *port;
  1022. int check_wwpn;
  1023. check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
  1024. /*
  1025. * check that there is no port with this WWPN already in list
  1026. */
  1027. if (check_wwpn) {
  1028. read_lock_irq(&zfcp_data.config_lock);
  1029. port = zfcp_get_port_by_wwpn(adapter, wwpn);
  1030. read_unlock_irq(&zfcp_data.config_lock);
  1031. if (port)
  1032. return NULL;
  1033. }
  1034. port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
  1035. if (!port)
  1036. return NULL;
  1037. /* initialise reference count stuff */
  1038. atomic_set(&port->refcount, 0);
  1039. init_waitqueue_head(&port->remove_wq);
  1040. INIT_LIST_HEAD(&port->unit_list_head);
  1041. INIT_LIST_HEAD(&port->unit_remove_lh);
  1042. port->adapter = adapter;
  1043. if (check_wwpn)
  1044. port->wwpn = wwpn;
  1045. atomic_set_mask(status, &port->status);
  1046. /* setup for sysfs registration */
  1047. if (status & ZFCP_STATUS_PORT_WKA) {
  1048. switch (d_id) {
  1049. case ZFCP_DID_DIRECTORY_SERVICE:
  1050. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1051. "directory");
  1052. break;
  1053. case ZFCP_DID_MANAGEMENT_SERVICE:
  1054. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1055. "management");
  1056. break;
  1057. case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
  1058. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1059. "key_distribution");
  1060. break;
  1061. case ZFCP_DID_ALIAS_SERVICE:
  1062. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1063. "alias");
  1064. break;
  1065. case ZFCP_DID_TIME_SERVICE:
  1066. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1067. "time");
  1068. break;
  1069. default:
  1070. kfree(port);
  1071. return NULL;
  1072. }
  1073. port->d_id = d_id;
  1074. port->sysfs_device.parent = &adapter->generic_services;
  1075. } else {
  1076. snprintf(port->sysfs_device.bus_id,
  1077. BUS_ID_SIZE, "0x%016llx", wwpn);
  1078. port->sysfs_device.parent = &adapter->ccw_device->dev;
  1079. }
  1080. port->sysfs_device.release = zfcp_sysfs_port_release;
  1081. dev_set_drvdata(&port->sysfs_device, port);
  1082. /* mark port unusable as long as sysfs registration is not complete */
  1083. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  1084. if (device_register(&port->sysfs_device)) {
  1085. kfree(port);
  1086. return NULL;
  1087. }
  1088. if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
  1089. device_unregister(&port->sysfs_device);
  1090. return NULL;
  1091. }
  1092. zfcp_port_get(port);
  1093. write_lock_irq(&zfcp_data.config_lock);
  1094. list_add_tail(&port->list, &adapter->port_list_head);
  1095. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  1096. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
  1097. if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
  1098. if (!adapter->nameserver_port)
  1099. adapter->nameserver_port = port;
  1100. adapter->ports++;
  1101. write_unlock_irq(&zfcp_data.config_lock);
  1102. zfcp_adapter_get(adapter);
  1103. return port;
  1104. }
  1105. void
  1106. zfcp_port_dequeue(struct zfcp_port *port)
  1107. {
  1108. zfcp_port_wait(port);
  1109. write_lock_irq(&zfcp_data.config_lock);
  1110. list_del(&port->list);
  1111. port->adapter->ports--;
  1112. write_unlock_irq(&zfcp_data.config_lock);
  1113. if (port->rport)
  1114. fc_remote_port_delete(port->rport);
  1115. port->rport = NULL;
  1116. zfcp_adapter_put(port->adapter);
  1117. zfcp_sysfs_port_remove_files(&port->sysfs_device,
  1118. atomic_read(&port->status));
  1119. device_unregister(&port->sysfs_device);
  1120. }
  1121. /* Enqueues a nameserver port */
  1122. int
  1123. zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
  1124. {
  1125. struct zfcp_port *port;
  1126. port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
  1127. ZFCP_DID_DIRECTORY_SERVICE);
  1128. if (!port) {
  1129. ZFCP_LOG_INFO("error: enqueue of nameserver port for "
  1130. "adapter %s failed\n",
  1131. zfcp_get_busid_by_adapter(adapter));
  1132. return -ENXIO;
  1133. }
  1134. zfcp_port_put(port);
  1135. return 0;
  1136. }
  1137. #undef ZFCP_LOG_AREA