zfcp_aux.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  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. /* FCP related */
  42. static void zfcp_ns_gid_pn_handler(unsigned long);
  43. /* miscellaneous */
  44. static int zfcp_sg_list_alloc(struct zfcp_sg_list *, size_t);
  45. static void zfcp_sg_list_free(struct zfcp_sg_list *);
  46. static int zfcp_sg_list_copy_from_user(struct zfcp_sg_list *,
  47. void __user *, size_t);
  48. static int zfcp_sg_list_copy_to_user(void __user *,
  49. struct zfcp_sg_list *, size_t);
  50. static long zfcp_cfdc_dev_ioctl(struct file *, unsigned int, unsigned long);
  51. #define ZFCP_CFDC_IOC_MAGIC 0xDD
  52. #define ZFCP_CFDC_IOC \
  53. _IOWR(ZFCP_CFDC_IOC_MAGIC, 0, struct zfcp_cfdc_sense_data)
  54. static const struct file_operations zfcp_cfdc_fops = {
  55. .unlocked_ioctl = zfcp_cfdc_dev_ioctl,
  56. #ifdef CONFIG_COMPAT
  57. .compat_ioctl = zfcp_cfdc_dev_ioctl
  58. #endif
  59. };
  60. static struct miscdevice zfcp_cfdc_misc = {
  61. .minor = ZFCP_CFDC_DEV_MINOR,
  62. .name = ZFCP_CFDC_DEV_NAME,
  63. .fops = &zfcp_cfdc_fops
  64. };
  65. /*********************** KERNEL/MODULE PARAMETERS ***************************/
  66. /* declare driver module init/cleanup functions */
  67. module_init(zfcp_module_init);
  68. MODULE_AUTHOR("IBM Deutschland Entwicklung GmbH - linux390@de.ibm.com");
  69. MODULE_DESCRIPTION
  70. ("FCP (SCSI over Fibre Channel) HBA driver for IBM System z9 and zSeries");
  71. MODULE_LICENSE("GPL");
  72. module_param(device, charp, 0400);
  73. MODULE_PARM_DESC(device, "specify initial device");
  74. module_param(loglevel, uint, 0400);
  75. MODULE_PARM_DESC(loglevel,
  76. "log levels, 8 nibbles: "
  77. "FC ERP QDIO CIO Config FSF SCSI Other, "
  78. "levels: 0=none 1=normal 2=devel 3=trace");
  79. /****************************************************************/
  80. /************** Functions without logging ***********************/
  81. /****************************************************************/
  82. void
  83. _zfcp_hex_dump(char *addr, int count)
  84. {
  85. int i;
  86. for (i = 0; i < count; i++) {
  87. printk("%02x", addr[i]);
  88. if ((i % 4) == 3)
  89. printk(" ");
  90. if ((i % 32) == 31)
  91. printk("\n");
  92. }
  93. if (((i-1) % 32) != 31)
  94. printk("\n");
  95. }
  96. /****************************************************************/
  97. /****** Functions to handle the request ID hash table ********/
  98. /****************************************************************/
  99. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FSF
  100. static int zfcp_reqlist_alloc(struct zfcp_adapter *adapter)
  101. {
  102. int idx;
  103. adapter->req_list = kcalloc(REQUEST_LIST_SIZE, sizeof(struct list_head),
  104. GFP_KERNEL);
  105. if (!adapter->req_list)
  106. return -ENOMEM;
  107. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  108. INIT_LIST_HEAD(&adapter->req_list[idx]);
  109. return 0;
  110. }
  111. static void zfcp_reqlist_free(struct zfcp_adapter *adapter)
  112. {
  113. kfree(adapter->req_list);
  114. }
  115. int zfcp_reqlist_isempty(struct zfcp_adapter *adapter)
  116. {
  117. unsigned int idx;
  118. for (idx = 0; idx < REQUEST_LIST_SIZE; idx++)
  119. if (!list_empty(&adapter->req_list[idx]))
  120. return 0;
  121. return 1;
  122. }
  123. #undef ZFCP_LOG_AREA
  124. /****************************************************************/
  125. /************** Uncategorised Functions *************************/
  126. /****************************************************************/
  127. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_OTHER
  128. /**
  129. * zfcp_device_setup - setup function
  130. * @str: pointer to parameter string
  131. *
  132. * Parse "device=..." parameter string.
  133. */
  134. static int __init
  135. zfcp_device_setup(char *devstr)
  136. {
  137. char *tmp, *str;
  138. size_t len;
  139. if (!devstr)
  140. return 0;
  141. len = strlen(devstr) + 1;
  142. str = kmalloc(len, GFP_KERNEL);
  143. if (!str)
  144. goto err_out;
  145. memcpy(str, devstr, len);
  146. tmp = strchr(str, ',');
  147. if (!tmp)
  148. goto err_out;
  149. *tmp++ = '\0';
  150. strncpy(zfcp_data.init_busid, str, BUS_ID_SIZE);
  151. zfcp_data.init_busid[BUS_ID_SIZE-1] = '\0';
  152. zfcp_data.init_wwpn = simple_strtoull(tmp, &tmp, 0);
  153. if (*tmp++ != ',')
  154. goto err_out;
  155. if (*tmp == '\0')
  156. goto err_out;
  157. zfcp_data.init_fcp_lun = simple_strtoull(tmp, &tmp, 0);
  158. if (*tmp != '\0')
  159. goto err_out;
  160. kfree(str);
  161. return 1;
  162. err_out:
  163. ZFCP_LOG_NORMAL("Parse error for device parameter string %s\n", str);
  164. kfree(str);
  165. return 0;
  166. }
  167. static void __init
  168. zfcp_init_device_configure(void)
  169. {
  170. struct zfcp_adapter *adapter;
  171. struct zfcp_port *port;
  172. struct zfcp_unit *unit;
  173. down(&zfcp_data.config_sema);
  174. read_lock_irq(&zfcp_data.config_lock);
  175. adapter = zfcp_get_adapter_by_busid(zfcp_data.init_busid);
  176. if (adapter)
  177. zfcp_adapter_get(adapter);
  178. read_unlock_irq(&zfcp_data.config_lock);
  179. if (adapter == NULL)
  180. goto out_adapter;
  181. port = zfcp_port_enqueue(adapter, zfcp_data.init_wwpn, 0, 0);
  182. if (!port)
  183. goto out_port;
  184. unit = zfcp_unit_enqueue(port, zfcp_data.init_fcp_lun);
  185. if (!unit)
  186. goto out_unit;
  187. up(&zfcp_data.config_sema);
  188. ccw_device_set_online(adapter->ccw_device);
  189. zfcp_erp_wait(adapter);
  190. down(&zfcp_data.config_sema);
  191. zfcp_unit_put(unit);
  192. out_unit:
  193. zfcp_port_put(port);
  194. out_port:
  195. zfcp_adapter_put(adapter);
  196. out_adapter:
  197. up(&zfcp_data.config_sema);
  198. return;
  199. }
  200. static int calc_alignment(int size)
  201. {
  202. int align = 1;
  203. if (!size)
  204. return 0;
  205. while ((size - align) > 0)
  206. align <<= 1;
  207. return align;
  208. }
  209. static int __init
  210. zfcp_module_init(void)
  211. {
  212. int retval = -ENOMEM;
  213. int size, align;
  214. size = sizeof(struct zfcp_fsf_req_qtcb);
  215. align = calc_alignment(size);
  216. zfcp_data.fsf_req_qtcb_cache =
  217. kmem_cache_create("zfcp_fsf", size, align, 0, NULL);
  218. if (!zfcp_data.fsf_req_qtcb_cache)
  219. goto out;
  220. size = sizeof(struct fsf_status_read_buffer);
  221. align = calc_alignment(size);
  222. zfcp_data.sr_buffer_cache =
  223. kmem_cache_create("zfcp_sr", size, align, 0, NULL);
  224. if (!zfcp_data.sr_buffer_cache)
  225. goto out_sr_cache;
  226. size = sizeof(struct zfcp_gid_pn_data);
  227. align = calc_alignment(size);
  228. zfcp_data.gid_pn_cache =
  229. kmem_cache_create("zfcp_gid", size, align, 0, NULL);
  230. if (!zfcp_data.gid_pn_cache)
  231. goto out_gid_cache;
  232. atomic_set(&zfcp_data.loglevel, loglevel);
  233. /* initialize adapter list */
  234. INIT_LIST_HEAD(&zfcp_data.adapter_list_head);
  235. /* initialize adapters to be removed list head */
  236. INIT_LIST_HEAD(&zfcp_data.adapter_remove_lh);
  237. zfcp_data.scsi_transport_template =
  238. fc_attach_transport(&zfcp_transport_functions);
  239. if (!zfcp_data.scsi_transport_template)
  240. goto out_transport;
  241. retval = misc_register(&zfcp_cfdc_misc);
  242. if (retval != 0) {
  243. ZFCP_LOG_INFO("registration of misc device "
  244. "zfcp_cfdc failed\n");
  245. goto out_misc;
  246. }
  247. ZFCP_LOG_TRACE("major/minor for zfcp_cfdc: %d/%d\n",
  248. ZFCP_CFDC_DEV_MAJOR, zfcp_cfdc_misc.minor);
  249. /* Initialise proc semaphores */
  250. sema_init(&zfcp_data.config_sema, 1);
  251. /* initialise configuration rw lock */
  252. rwlock_init(&zfcp_data.config_lock);
  253. /* setup dynamic I/O */
  254. retval = zfcp_ccw_register();
  255. if (retval) {
  256. ZFCP_LOG_NORMAL("registration with common I/O layer failed\n");
  257. goto out_ccw_register;
  258. }
  259. if (zfcp_device_setup(device))
  260. zfcp_init_device_configure();
  261. goto out;
  262. out_ccw_register:
  263. misc_deregister(&zfcp_cfdc_misc);
  264. out_misc:
  265. fc_release_transport(zfcp_data.scsi_transport_template);
  266. out_transport:
  267. kmem_cache_destroy(zfcp_data.gid_pn_cache);
  268. out_gid_cache:
  269. kmem_cache_destroy(zfcp_data.sr_buffer_cache);
  270. out_sr_cache:
  271. kmem_cache_destroy(zfcp_data.fsf_req_qtcb_cache);
  272. out:
  273. return retval;
  274. }
  275. /*
  276. * function: zfcp_cfdc_dev_ioctl
  277. *
  278. * purpose: Handle control file upload/download transaction via IOCTL
  279. * interface
  280. *
  281. * returns: 0 - Operation completed successfuly
  282. * -ENOTTY - Unknown IOCTL command
  283. * -EINVAL - Invalid sense data record
  284. * -ENXIO - The FCP adapter is not available
  285. * -EOPNOTSUPP - The FCP adapter does not have CFDC support
  286. * -ENOMEM - Insufficient memory
  287. * -EFAULT - User space memory I/O operation fault
  288. * -EPERM - Cannot create or queue FSF request or create SBALs
  289. * -ERESTARTSYS- Received signal (is mapped to EAGAIN by VFS)
  290. */
  291. static long
  292. zfcp_cfdc_dev_ioctl(struct file *file, unsigned int command,
  293. unsigned long buffer)
  294. {
  295. struct zfcp_cfdc_sense_data *sense_data, __user *sense_data_user;
  296. struct zfcp_adapter *adapter = NULL;
  297. struct zfcp_fsf_req *fsf_req = NULL;
  298. struct zfcp_sg_list *sg_list = NULL;
  299. u32 fsf_command, option;
  300. char *bus_id = NULL;
  301. int retval = 0;
  302. sense_data = kmalloc(sizeof(struct zfcp_cfdc_sense_data), GFP_KERNEL);
  303. if (sense_data == NULL) {
  304. retval = -ENOMEM;
  305. goto out;
  306. }
  307. sg_list = kzalloc(sizeof(struct zfcp_sg_list), GFP_KERNEL);
  308. if (sg_list == NULL) {
  309. retval = -ENOMEM;
  310. goto out;
  311. }
  312. if (command != ZFCP_CFDC_IOC) {
  313. ZFCP_LOG_INFO("IOC request code 0x%x invalid\n", command);
  314. retval = -ENOTTY;
  315. goto out;
  316. }
  317. if ((sense_data_user = (void __user *) buffer) == NULL) {
  318. ZFCP_LOG_INFO("sense data record is required\n");
  319. retval = -EINVAL;
  320. goto out;
  321. }
  322. retval = copy_from_user(sense_data, sense_data_user,
  323. sizeof(struct zfcp_cfdc_sense_data));
  324. if (retval) {
  325. retval = -EFAULT;
  326. goto out;
  327. }
  328. if (sense_data->signature != ZFCP_CFDC_SIGNATURE) {
  329. ZFCP_LOG_INFO("invalid sense data request signature 0x%08x\n",
  330. ZFCP_CFDC_SIGNATURE);
  331. retval = -EINVAL;
  332. goto out;
  333. }
  334. switch (sense_data->command) {
  335. case ZFCP_CFDC_CMND_DOWNLOAD_NORMAL:
  336. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  337. option = FSF_CFDC_OPTION_NORMAL_MODE;
  338. break;
  339. case ZFCP_CFDC_CMND_DOWNLOAD_FORCE:
  340. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  341. option = FSF_CFDC_OPTION_FORCE;
  342. break;
  343. case ZFCP_CFDC_CMND_FULL_ACCESS:
  344. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  345. option = FSF_CFDC_OPTION_FULL_ACCESS;
  346. break;
  347. case ZFCP_CFDC_CMND_RESTRICTED_ACCESS:
  348. fsf_command = FSF_QTCB_DOWNLOAD_CONTROL_FILE;
  349. option = FSF_CFDC_OPTION_RESTRICTED_ACCESS;
  350. break;
  351. case ZFCP_CFDC_CMND_UPLOAD:
  352. fsf_command = FSF_QTCB_UPLOAD_CONTROL_FILE;
  353. option = 0;
  354. break;
  355. default:
  356. ZFCP_LOG_INFO("invalid command code 0x%08x\n",
  357. sense_data->command);
  358. retval = -EINVAL;
  359. goto out;
  360. }
  361. bus_id = kmalloc(BUS_ID_SIZE, GFP_KERNEL);
  362. if (bus_id == NULL) {
  363. retval = -ENOMEM;
  364. goto out;
  365. }
  366. snprintf(bus_id, BUS_ID_SIZE, "%d.%d.%04x",
  367. (sense_data->devno >> 24),
  368. (sense_data->devno >> 16) & 0xFF,
  369. (sense_data->devno & 0xFFFF));
  370. read_lock_irq(&zfcp_data.config_lock);
  371. adapter = zfcp_get_adapter_by_busid(bus_id);
  372. if (adapter)
  373. zfcp_adapter_get(adapter);
  374. read_unlock_irq(&zfcp_data.config_lock);
  375. kfree(bus_id);
  376. if (adapter == NULL) {
  377. ZFCP_LOG_INFO("invalid adapter\n");
  378. retval = -ENXIO;
  379. goto out;
  380. }
  381. if (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE) {
  382. retval = zfcp_sg_list_alloc(sg_list,
  383. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  384. if (retval) {
  385. retval = -ENOMEM;
  386. goto out;
  387. }
  388. }
  389. if ((sense_data->command & ZFCP_CFDC_DOWNLOAD) &&
  390. (sense_data->command & ZFCP_CFDC_WITH_CONTROL_FILE)) {
  391. retval = zfcp_sg_list_copy_from_user(
  392. sg_list, &sense_data_user->control_file,
  393. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  394. if (retval) {
  395. retval = -EFAULT;
  396. goto out;
  397. }
  398. }
  399. retval = zfcp_fsf_control_file(adapter, &fsf_req, fsf_command,
  400. option, sg_list);
  401. if (retval)
  402. goto out;
  403. if ((fsf_req->qtcb->prefix.prot_status != FSF_PROT_GOOD) &&
  404. (fsf_req->qtcb->prefix.prot_status != FSF_PROT_FSF_STATUS_PRESENTED)) {
  405. retval = -ENXIO;
  406. goto out;
  407. }
  408. sense_data->fsf_status = fsf_req->qtcb->header.fsf_status;
  409. memcpy(&sense_data->fsf_status_qual,
  410. &fsf_req->qtcb->header.fsf_status_qual,
  411. sizeof(union fsf_status_qual));
  412. memcpy(&sense_data->payloads, &fsf_req->qtcb->bottom.support.els, 256);
  413. retval = copy_to_user(sense_data_user, sense_data,
  414. sizeof(struct zfcp_cfdc_sense_data));
  415. if (retval) {
  416. retval = -EFAULT;
  417. goto out;
  418. }
  419. if (sense_data->command & ZFCP_CFDC_UPLOAD) {
  420. retval = zfcp_sg_list_copy_to_user(
  421. &sense_data_user->control_file, sg_list,
  422. ZFCP_CFDC_MAX_CONTROL_FILE_SIZE);
  423. if (retval) {
  424. retval = -EFAULT;
  425. goto out;
  426. }
  427. }
  428. out:
  429. if (fsf_req != NULL)
  430. zfcp_fsf_req_free(fsf_req);
  431. if ((adapter != NULL) && (retval != -ENXIO))
  432. zfcp_adapter_put(adapter);
  433. if (sg_list != NULL) {
  434. zfcp_sg_list_free(sg_list);
  435. kfree(sg_list);
  436. }
  437. kfree(sense_data);
  438. return retval;
  439. }
  440. /**
  441. * zfcp_sg_list_alloc - create a scatter-gather list of the specified size
  442. * @sg_list: structure describing a scatter gather list
  443. * @size: size of scatter-gather list
  444. * Return: 0 on success, else -ENOMEM
  445. *
  446. * In sg_list->sg a pointer to the created scatter-gather list is returned,
  447. * or NULL if we run out of memory. sg_list->count specifies the number of
  448. * elements of the scatter-gather list. The maximum size of a single element
  449. * in the scatter-gather list is PAGE_SIZE.
  450. */
  451. static int
  452. zfcp_sg_list_alloc(struct zfcp_sg_list *sg_list, size_t size)
  453. {
  454. struct scatterlist *sg;
  455. unsigned int i;
  456. int retval = 0;
  457. void *address;
  458. BUG_ON(sg_list == NULL);
  459. sg_list->count = size >> PAGE_SHIFT;
  460. if (size & ~PAGE_MASK)
  461. sg_list->count++;
  462. sg_list->sg = kcalloc(sg_list->count, sizeof(struct scatterlist),
  463. GFP_KERNEL);
  464. if (sg_list->sg == NULL) {
  465. sg_list->count = 0;
  466. retval = -ENOMEM;
  467. goto out;
  468. }
  469. for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++) {
  470. sg->length = min(size, PAGE_SIZE);
  471. sg->offset = 0;
  472. address = (void *) get_zeroed_page(GFP_KERNEL);
  473. if (address == NULL) {
  474. sg_list->count = i;
  475. zfcp_sg_list_free(sg_list);
  476. retval = -ENOMEM;
  477. goto out;
  478. }
  479. zfcp_address_to_sg(address, sg);
  480. size -= sg->length;
  481. }
  482. out:
  483. return retval;
  484. }
  485. /**
  486. * zfcp_sg_list_free - free memory of a scatter-gather list
  487. * @sg_list: structure describing a scatter-gather list
  488. *
  489. * Memory for each element in the scatter-gather list is freed.
  490. * Finally sg_list->sg is freed itself and sg_list->count is reset.
  491. */
  492. static void
  493. zfcp_sg_list_free(struct zfcp_sg_list *sg_list)
  494. {
  495. struct scatterlist *sg;
  496. unsigned int i;
  497. BUG_ON(sg_list == NULL);
  498. for (i = 0, sg = sg_list->sg; i < sg_list->count; i++, sg++)
  499. free_page((unsigned long) zfcp_sg_to_address(sg));
  500. sg_list->count = 0;
  501. kfree(sg_list->sg);
  502. }
  503. /**
  504. * zfcp_sg_size - determine size of a scatter-gather list
  505. * @sg: array of (struct scatterlist)
  506. * @sg_count: elements in array
  507. * Return: size of entire scatter-gather list
  508. */
  509. static size_t zfcp_sg_size(struct scatterlist *sg, unsigned int sg_count)
  510. {
  511. unsigned int i;
  512. struct scatterlist *p;
  513. size_t size;
  514. size = 0;
  515. for (i = 0, p = sg; i < sg_count; i++, p++) {
  516. BUG_ON(p == NULL);
  517. size += p->length;
  518. }
  519. return size;
  520. }
  521. /**
  522. * zfcp_sg_list_copy_from_user -copy data from user space to scatter-gather list
  523. * @sg_list: structure describing a scatter-gather list
  524. * @user_buffer: pointer to buffer in user space
  525. * @size: number of bytes to be copied
  526. * Return: 0 on success, -EFAULT if copy_from_user fails.
  527. */
  528. static int
  529. zfcp_sg_list_copy_from_user(struct zfcp_sg_list *sg_list,
  530. void __user *user_buffer,
  531. size_t size)
  532. {
  533. struct scatterlist *sg;
  534. unsigned int length;
  535. void *zfcp_buffer;
  536. int retval = 0;
  537. BUG_ON(sg_list == NULL);
  538. if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
  539. return -EFAULT;
  540. for (sg = sg_list->sg; size > 0; sg++) {
  541. length = min((unsigned int)size, sg->length);
  542. zfcp_buffer = zfcp_sg_to_address(sg);
  543. if (copy_from_user(zfcp_buffer, user_buffer, length)) {
  544. retval = -EFAULT;
  545. goto out;
  546. }
  547. user_buffer += length;
  548. size -= length;
  549. }
  550. out:
  551. return retval;
  552. }
  553. /**
  554. * zfcp_sg_list_copy_to_user - copy data from scatter-gather list to user space
  555. * @user_buffer: pointer to buffer in user space
  556. * @sg_list: structure describing a scatter-gather list
  557. * @size: number of bytes to be copied
  558. * Return: 0 on success, -EFAULT if copy_to_user fails
  559. */
  560. static int
  561. zfcp_sg_list_copy_to_user(void __user *user_buffer,
  562. struct zfcp_sg_list *sg_list,
  563. size_t size)
  564. {
  565. struct scatterlist *sg;
  566. unsigned int length;
  567. void *zfcp_buffer;
  568. int retval = 0;
  569. BUG_ON(sg_list == NULL);
  570. if (zfcp_sg_size(sg_list->sg, sg_list->count) < size)
  571. return -EFAULT;
  572. for (sg = sg_list->sg; size > 0; sg++) {
  573. length = min((unsigned int) size, sg->length);
  574. zfcp_buffer = zfcp_sg_to_address(sg);
  575. if (copy_to_user(user_buffer, zfcp_buffer, length)) {
  576. retval = -EFAULT;
  577. goto out;
  578. }
  579. user_buffer += length;
  580. size -= length;
  581. }
  582. out:
  583. return retval;
  584. }
  585. #undef ZFCP_LOG_AREA
  586. /****************************************************************/
  587. /****** Functions for configuration/set-up of structures ********/
  588. /****************************************************************/
  589. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_CONFIG
  590. /**
  591. * zfcp_get_unit_by_lun - find unit in unit list of port by FCP LUN
  592. * @port: pointer to port to search for unit
  593. * @fcp_lun: FCP LUN to search for
  594. * Traverse list of all units of a port and return pointer to a unit
  595. * with the given FCP LUN.
  596. */
  597. struct zfcp_unit *
  598. zfcp_get_unit_by_lun(struct zfcp_port *port, fcp_lun_t fcp_lun)
  599. {
  600. struct zfcp_unit *unit;
  601. int found = 0;
  602. list_for_each_entry(unit, &port->unit_list_head, list) {
  603. if ((unit->fcp_lun == fcp_lun) &&
  604. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status))
  605. {
  606. found = 1;
  607. break;
  608. }
  609. }
  610. return found ? unit : NULL;
  611. }
  612. /**
  613. * zfcp_get_port_by_wwpn - find port in port list of adapter by wwpn
  614. * @adapter: pointer to adapter to search for port
  615. * @wwpn: wwpn to search for
  616. * Traverse list of all ports of an adapter and return pointer to a port
  617. * with the given wwpn.
  618. */
  619. struct zfcp_port *
  620. zfcp_get_port_by_wwpn(struct zfcp_adapter *adapter, wwn_t wwpn)
  621. {
  622. struct zfcp_port *port;
  623. int found = 0;
  624. list_for_each_entry(port, &adapter->port_list_head, list) {
  625. if ((port->wwpn == wwpn) &&
  626. !(atomic_read(&port->status) &
  627. (ZFCP_STATUS_PORT_NO_WWPN | ZFCP_STATUS_COMMON_REMOVE))) {
  628. found = 1;
  629. break;
  630. }
  631. }
  632. return found ? port : NULL;
  633. }
  634. /**
  635. * zfcp_get_port_by_did - find port in port list of adapter by d_id
  636. * @adapter: pointer to adapter to search for port
  637. * @d_id: d_id to search for
  638. * Traverse list of all ports of an adapter and return pointer to a port
  639. * with the given d_id.
  640. */
  641. struct zfcp_port *
  642. zfcp_get_port_by_did(struct zfcp_adapter *adapter, u32 d_id)
  643. {
  644. struct zfcp_port *port;
  645. int found = 0;
  646. list_for_each_entry(port, &adapter->port_list_head, list) {
  647. if ((port->d_id == d_id) &&
  648. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status))
  649. {
  650. found = 1;
  651. break;
  652. }
  653. }
  654. return found ? port : NULL;
  655. }
  656. /**
  657. * zfcp_get_adapter_by_busid - find adpater in adapter list by bus_id
  658. * @bus_id: bus_id to search for
  659. * Traverse list of all adapters and return pointer to an adapter
  660. * with the given bus_id.
  661. */
  662. struct zfcp_adapter *
  663. zfcp_get_adapter_by_busid(char *bus_id)
  664. {
  665. struct zfcp_adapter *adapter;
  666. int found = 0;
  667. list_for_each_entry(adapter, &zfcp_data.adapter_list_head, list) {
  668. if ((strncmp(bus_id, zfcp_get_busid_by_adapter(adapter),
  669. BUS_ID_SIZE) == 0) &&
  670. !atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE,
  671. &adapter->status)){
  672. found = 1;
  673. break;
  674. }
  675. }
  676. return found ? adapter : NULL;
  677. }
  678. /**
  679. * zfcp_unit_enqueue - enqueue unit to unit list of a port.
  680. * @port: pointer to port where unit is added
  681. * @fcp_lun: FCP LUN of unit to be enqueued
  682. * Return: pointer to enqueued unit on success, NULL on error
  683. * Locks: config_sema must be held to serialize changes to the unit list
  684. *
  685. * Sets up some unit internal structures and creates sysfs entry.
  686. */
  687. struct zfcp_unit *
  688. zfcp_unit_enqueue(struct zfcp_port *port, fcp_lun_t fcp_lun)
  689. {
  690. struct zfcp_unit *unit;
  691. /*
  692. * check that there is no unit with this FCP_LUN already in list
  693. * and enqueue it.
  694. * Note: Unlike for the adapter and the port, this is an error
  695. */
  696. read_lock_irq(&zfcp_data.config_lock);
  697. unit = zfcp_get_unit_by_lun(port, fcp_lun);
  698. read_unlock_irq(&zfcp_data.config_lock);
  699. if (unit)
  700. return NULL;
  701. unit = kzalloc(sizeof (struct zfcp_unit), GFP_KERNEL);
  702. if (!unit)
  703. return NULL;
  704. /* initialise reference count stuff */
  705. atomic_set(&unit->refcount, 0);
  706. init_waitqueue_head(&unit->remove_wq);
  707. unit->port = port;
  708. unit->fcp_lun = fcp_lun;
  709. /* setup for sysfs registration */
  710. snprintf(unit->sysfs_device.bus_id, BUS_ID_SIZE, "0x%016llx", fcp_lun);
  711. unit->sysfs_device.parent = &port->sysfs_device;
  712. unit->sysfs_device.release = zfcp_sysfs_unit_release;
  713. dev_set_drvdata(&unit->sysfs_device, unit);
  714. init_waitqueue_head(&unit->scsi_scan_wq);
  715. /* mark unit unusable as long as sysfs registration is not complete */
  716. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  717. if (device_register(&unit->sysfs_device)) {
  718. kfree(unit);
  719. return NULL;
  720. }
  721. if (zfcp_sysfs_unit_create_files(&unit->sysfs_device)) {
  722. device_unregister(&unit->sysfs_device);
  723. return NULL;
  724. }
  725. zfcp_unit_get(unit);
  726. unit->scsi_lun = scsilun_to_int((struct scsi_lun *)&unit->fcp_lun);
  727. write_lock_irq(&zfcp_data.config_lock);
  728. list_add_tail(&unit->list, &port->unit_list_head);
  729. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
  730. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status);
  731. write_unlock_irq(&zfcp_data.config_lock);
  732. port->units++;
  733. zfcp_port_get(port);
  734. return unit;
  735. }
  736. void
  737. zfcp_unit_dequeue(struct zfcp_unit *unit)
  738. {
  739. zfcp_unit_wait(unit);
  740. write_lock_irq(&zfcp_data.config_lock);
  741. list_del(&unit->list);
  742. write_unlock_irq(&zfcp_data.config_lock);
  743. unit->port->units--;
  744. zfcp_port_put(unit->port);
  745. zfcp_sysfs_unit_remove_files(&unit->sysfs_device);
  746. device_unregister(&unit->sysfs_device);
  747. }
  748. /*
  749. * Allocates a combined QTCB/fsf_req buffer for erp actions and fcp/SCSI
  750. * commands.
  751. * It also genrates fcp-nameserver request/response buffer and unsolicited
  752. * status read fsf_req buffers.
  753. *
  754. * locks: must only be called with zfcp_data.config_sema taken
  755. */
  756. static int
  757. zfcp_allocate_low_mem_buffers(struct zfcp_adapter *adapter)
  758. {
  759. adapter->pool.fsf_req_erp =
  760. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ERP_NR,
  761. zfcp_data.fsf_req_qtcb_cache);
  762. if (!adapter->pool.fsf_req_erp)
  763. return -ENOMEM;
  764. adapter->pool.fsf_req_scsi =
  765. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_SCSI_NR,
  766. zfcp_data.fsf_req_qtcb_cache);
  767. if (!adapter->pool.fsf_req_scsi)
  768. return -ENOMEM;
  769. adapter->pool.fsf_req_abort =
  770. mempool_create_slab_pool(ZFCP_POOL_FSF_REQ_ABORT_NR,
  771. zfcp_data.fsf_req_qtcb_cache);
  772. if (!adapter->pool.fsf_req_abort)
  773. return -ENOMEM;
  774. adapter->pool.fsf_req_status_read =
  775. mempool_create_kmalloc_pool(ZFCP_POOL_STATUS_READ_NR,
  776. sizeof(struct zfcp_fsf_req));
  777. if (!adapter->pool.fsf_req_status_read)
  778. return -ENOMEM;
  779. adapter->pool.data_status_read =
  780. mempool_create_slab_pool(ZFCP_POOL_STATUS_READ_NR,
  781. zfcp_data.sr_buffer_cache);
  782. if (!adapter->pool.data_status_read)
  783. return -ENOMEM;
  784. adapter->pool.data_gid_pn =
  785. mempool_create_slab_pool(ZFCP_POOL_DATA_GID_PN_NR,
  786. zfcp_data.gid_pn_cache);
  787. if (!adapter->pool.data_gid_pn)
  788. return -ENOMEM;
  789. return 0;
  790. }
  791. /**
  792. * zfcp_free_low_mem_buffers - free memory pools of an adapter
  793. * @adapter: pointer to zfcp_adapter for which memory pools should be freed
  794. * locking: zfcp_data.config_sema must be held
  795. */
  796. static void
  797. zfcp_free_low_mem_buffers(struct zfcp_adapter *adapter)
  798. {
  799. if (adapter->pool.fsf_req_erp)
  800. mempool_destroy(adapter->pool.fsf_req_erp);
  801. if (adapter->pool.fsf_req_scsi)
  802. mempool_destroy(adapter->pool.fsf_req_scsi);
  803. if (adapter->pool.fsf_req_abort)
  804. mempool_destroy(adapter->pool.fsf_req_abort);
  805. if (adapter->pool.fsf_req_status_read)
  806. mempool_destroy(adapter->pool.fsf_req_status_read);
  807. if (adapter->pool.data_status_read)
  808. mempool_destroy(adapter->pool.data_status_read);
  809. if (adapter->pool.data_gid_pn)
  810. mempool_destroy(adapter->pool.data_gid_pn);
  811. }
  812. static void zfcp_dummy_release(struct device *dev)
  813. {
  814. return;
  815. }
  816. /*
  817. * Enqueues an adapter at the end of the adapter list in the driver data.
  818. * All adapter internal structures are set up.
  819. * Proc-fs entries are also created.
  820. *
  821. * returns: 0 if a new adapter was successfully enqueued
  822. * ZFCP_KNOWN if an adapter with this devno was already present
  823. * -ENOMEM if alloc failed
  824. * locks: config_sema must be held to serialise changes to the adapter list
  825. */
  826. struct zfcp_adapter *
  827. zfcp_adapter_enqueue(struct ccw_device *ccw_device)
  828. {
  829. int retval = 0;
  830. struct zfcp_adapter *adapter;
  831. /*
  832. * Note: It is safe to release the list_lock, as any list changes
  833. * are protected by the config_sema, which must be held to get here
  834. */
  835. /* try to allocate new adapter data structure (zeroed) */
  836. adapter = kzalloc(sizeof (struct zfcp_adapter), GFP_KERNEL);
  837. if (!adapter) {
  838. ZFCP_LOG_INFO("error: allocation of base adapter "
  839. "structure failed\n");
  840. goto out;
  841. }
  842. ccw_device->handler = NULL;
  843. /* save ccw_device pointer */
  844. adapter->ccw_device = ccw_device;
  845. retval = zfcp_qdio_allocate_queues(adapter);
  846. if (retval)
  847. goto queues_alloc_failed;
  848. retval = zfcp_qdio_allocate(adapter);
  849. if (retval)
  850. goto qdio_allocate_failed;
  851. retval = zfcp_allocate_low_mem_buffers(adapter);
  852. if (retval) {
  853. ZFCP_LOG_INFO("error: pool allocation failed\n");
  854. goto failed_low_mem_buffers;
  855. }
  856. /* initialise reference count stuff */
  857. atomic_set(&adapter->refcount, 0);
  858. init_waitqueue_head(&adapter->remove_wq);
  859. /* initialise list of ports */
  860. INIT_LIST_HEAD(&adapter->port_list_head);
  861. /* initialise list of ports to be removed */
  862. INIT_LIST_HEAD(&adapter->port_remove_lh);
  863. /* initialize list of fsf requests */
  864. spin_lock_init(&adapter->req_list_lock);
  865. retval = zfcp_reqlist_alloc(adapter);
  866. if (retval) {
  867. ZFCP_LOG_INFO("request list initialization failed\n");
  868. goto failed_low_mem_buffers;
  869. }
  870. /* initialize debug locks */
  871. spin_lock_init(&adapter->erp_dbf_lock);
  872. spin_lock_init(&adapter->hba_dbf_lock);
  873. spin_lock_init(&adapter->san_dbf_lock);
  874. spin_lock_init(&adapter->scsi_dbf_lock);
  875. retval = zfcp_adapter_debug_register(adapter);
  876. if (retval)
  877. goto debug_register_failed;
  878. /* initialize error recovery stuff */
  879. rwlock_init(&adapter->erp_lock);
  880. sema_init(&adapter->erp_ready_sem, 0);
  881. INIT_LIST_HEAD(&adapter->erp_ready_head);
  882. INIT_LIST_HEAD(&adapter->erp_running_head);
  883. /* initialize abort lock */
  884. rwlock_init(&adapter->abort_lock);
  885. /* initialise some erp stuff */
  886. init_waitqueue_head(&adapter->erp_thread_wqh);
  887. init_waitqueue_head(&adapter->erp_done_wqh);
  888. /* initialize lock of associated request queue */
  889. rwlock_init(&adapter->request_queue.queue_lock);
  890. /* mark adapter unusable as long as sysfs registration is not complete */
  891. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  892. dev_set_drvdata(&ccw_device->dev, adapter);
  893. if (zfcp_sysfs_adapter_create_files(&ccw_device->dev))
  894. goto sysfs_failed;
  895. adapter->generic_services.parent = &adapter->ccw_device->dev;
  896. adapter->generic_services.release = zfcp_dummy_release;
  897. snprintf(adapter->generic_services.bus_id, BUS_ID_SIZE,
  898. "generic_services");
  899. if (device_register(&adapter->generic_services))
  900. goto generic_services_failed;
  901. /* put allocated adapter at list tail */
  902. write_lock_irq(&zfcp_data.config_lock);
  903. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &adapter->status);
  904. list_add_tail(&adapter->list, &zfcp_data.adapter_list_head);
  905. write_unlock_irq(&zfcp_data.config_lock);
  906. zfcp_data.adapters++;
  907. goto out;
  908. generic_services_failed:
  909. zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
  910. sysfs_failed:
  911. zfcp_adapter_debug_unregister(adapter);
  912. debug_register_failed:
  913. dev_set_drvdata(&ccw_device->dev, NULL);
  914. zfcp_reqlist_free(adapter);
  915. failed_low_mem_buffers:
  916. zfcp_free_low_mem_buffers(adapter);
  917. if (qdio_free(ccw_device) != 0)
  918. ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
  919. zfcp_get_busid_by_adapter(adapter));
  920. qdio_allocate_failed:
  921. zfcp_qdio_free_queues(adapter);
  922. queues_alloc_failed:
  923. kfree(adapter);
  924. adapter = NULL;
  925. out:
  926. return adapter;
  927. }
  928. /*
  929. * returns: 0 - struct zfcp_adapter data structure successfully removed
  930. * !0 - struct zfcp_adapter data structure could not be removed
  931. * (e.g. still used)
  932. * locks: adapter list write lock is assumed to be held by caller
  933. */
  934. void
  935. zfcp_adapter_dequeue(struct zfcp_adapter *adapter)
  936. {
  937. int retval = 0;
  938. unsigned long flags;
  939. zfcp_adapter_scsi_unregister(adapter);
  940. device_unregister(&adapter->generic_services);
  941. zfcp_sysfs_adapter_remove_files(&adapter->ccw_device->dev);
  942. dev_set_drvdata(&adapter->ccw_device->dev, NULL);
  943. /* sanity check: no pending FSF requests */
  944. spin_lock_irqsave(&adapter->req_list_lock, flags);
  945. retval = zfcp_reqlist_isempty(adapter);
  946. spin_unlock_irqrestore(&adapter->req_list_lock, flags);
  947. if (!retval) {
  948. ZFCP_LOG_NORMAL("bug: adapter %s (%p) still in use, "
  949. "%i requests outstanding\n",
  950. zfcp_get_busid_by_adapter(adapter), adapter,
  951. atomic_read(&adapter->reqs_active));
  952. retval = -EBUSY;
  953. goto out;
  954. }
  955. zfcp_adapter_debug_unregister(adapter);
  956. /* remove specified adapter data structure from list */
  957. write_lock_irq(&zfcp_data.config_lock);
  958. list_del(&adapter->list);
  959. write_unlock_irq(&zfcp_data.config_lock);
  960. /* decrease number of adapters in list */
  961. zfcp_data.adapters--;
  962. ZFCP_LOG_TRACE("adapter %s (%p) removed from list, "
  963. "%i adapters still in list\n",
  964. zfcp_get_busid_by_adapter(adapter),
  965. adapter, zfcp_data.adapters);
  966. retval = qdio_free(adapter->ccw_device);
  967. if (retval)
  968. ZFCP_LOG_NORMAL("bug: qdio_free for adapter %s failed\n",
  969. zfcp_get_busid_by_adapter(adapter));
  970. zfcp_free_low_mem_buffers(adapter);
  971. /* free memory of adapter data structure and queues */
  972. zfcp_qdio_free_queues(adapter);
  973. zfcp_reqlist_free(adapter);
  974. kfree(adapter->fc_stats);
  975. kfree(adapter->stats_reset_data);
  976. ZFCP_LOG_TRACE("freeing adapter structure\n");
  977. kfree(adapter);
  978. out:
  979. return;
  980. }
  981. /**
  982. * zfcp_port_enqueue - enqueue port to port list of adapter
  983. * @adapter: adapter where remote port is added
  984. * @wwpn: WWPN of the remote port to be enqueued
  985. * @status: initial status for the port
  986. * @d_id: destination id of the remote port to be enqueued
  987. * Return: pointer to enqueued port on success, NULL on error
  988. * Locks: config_sema must be held to serialize changes to the port list
  989. *
  990. * All port internal structures are set up and the sysfs entry is generated.
  991. * d_id is used to enqueue ports with a well known address like the Directory
  992. * Service for nameserver lookup.
  993. */
  994. struct zfcp_port *
  995. zfcp_port_enqueue(struct zfcp_adapter *adapter, wwn_t wwpn, u32 status,
  996. u32 d_id)
  997. {
  998. struct zfcp_port *port;
  999. int check_wwpn;
  1000. check_wwpn = !(status & ZFCP_STATUS_PORT_NO_WWPN);
  1001. /*
  1002. * check that there is no port with this WWPN already in list
  1003. */
  1004. if (check_wwpn) {
  1005. read_lock_irq(&zfcp_data.config_lock);
  1006. port = zfcp_get_port_by_wwpn(adapter, wwpn);
  1007. read_unlock_irq(&zfcp_data.config_lock);
  1008. if (port)
  1009. return NULL;
  1010. }
  1011. port = kzalloc(sizeof (struct zfcp_port), GFP_KERNEL);
  1012. if (!port)
  1013. return NULL;
  1014. /* initialise reference count stuff */
  1015. atomic_set(&port->refcount, 0);
  1016. init_waitqueue_head(&port->remove_wq);
  1017. INIT_LIST_HEAD(&port->unit_list_head);
  1018. INIT_LIST_HEAD(&port->unit_remove_lh);
  1019. port->adapter = adapter;
  1020. if (check_wwpn)
  1021. port->wwpn = wwpn;
  1022. atomic_set_mask(status, &port->status);
  1023. /* setup for sysfs registration */
  1024. if (status & ZFCP_STATUS_PORT_WKA) {
  1025. switch (d_id) {
  1026. case ZFCP_DID_DIRECTORY_SERVICE:
  1027. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1028. "directory");
  1029. break;
  1030. case ZFCP_DID_MANAGEMENT_SERVICE:
  1031. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1032. "management");
  1033. break;
  1034. case ZFCP_DID_KEY_DISTRIBUTION_SERVICE:
  1035. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1036. "key_distribution");
  1037. break;
  1038. case ZFCP_DID_ALIAS_SERVICE:
  1039. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1040. "alias");
  1041. break;
  1042. case ZFCP_DID_TIME_SERVICE:
  1043. snprintf(port->sysfs_device.bus_id, BUS_ID_SIZE,
  1044. "time");
  1045. break;
  1046. default:
  1047. kfree(port);
  1048. return NULL;
  1049. }
  1050. port->d_id = d_id;
  1051. port->sysfs_device.parent = &adapter->generic_services;
  1052. } else {
  1053. snprintf(port->sysfs_device.bus_id,
  1054. BUS_ID_SIZE, "0x%016llx", wwpn);
  1055. port->sysfs_device.parent = &adapter->ccw_device->dev;
  1056. }
  1057. port->sysfs_device.release = zfcp_sysfs_port_release;
  1058. dev_set_drvdata(&port->sysfs_device, port);
  1059. /* mark port unusable as long as sysfs registration is not complete */
  1060. atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  1061. if (device_register(&port->sysfs_device)) {
  1062. kfree(port);
  1063. return NULL;
  1064. }
  1065. if (zfcp_sysfs_port_create_files(&port->sysfs_device, status)) {
  1066. device_unregister(&port->sysfs_device);
  1067. return NULL;
  1068. }
  1069. zfcp_port_get(port);
  1070. write_lock_irq(&zfcp_data.config_lock);
  1071. list_add_tail(&port->list, &adapter->port_list_head);
  1072. atomic_clear_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
  1073. atomic_set_mask(ZFCP_STATUS_COMMON_RUNNING, &port->status);
  1074. if (d_id == ZFCP_DID_DIRECTORY_SERVICE)
  1075. if (!adapter->nameserver_port)
  1076. adapter->nameserver_port = port;
  1077. adapter->ports++;
  1078. write_unlock_irq(&zfcp_data.config_lock);
  1079. zfcp_adapter_get(adapter);
  1080. return port;
  1081. }
  1082. void
  1083. zfcp_port_dequeue(struct zfcp_port *port)
  1084. {
  1085. zfcp_port_wait(port);
  1086. write_lock_irq(&zfcp_data.config_lock);
  1087. list_del(&port->list);
  1088. port->adapter->ports--;
  1089. write_unlock_irq(&zfcp_data.config_lock);
  1090. if (port->rport)
  1091. fc_remote_port_delete(port->rport);
  1092. port->rport = NULL;
  1093. zfcp_adapter_put(port->adapter);
  1094. zfcp_sysfs_port_remove_files(&port->sysfs_device,
  1095. atomic_read(&port->status));
  1096. device_unregister(&port->sysfs_device);
  1097. }
  1098. /* Enqueues a nameserver port */
  1099. int
  1100. zfcp_nameserver_enqueue(struct zfcp_adapter *adapter)
  1101. {
  1102. struct zfcp_port *port;
  1103. port = zfcp_port_enqueue(adapter, 0, ZFCP_STATUS_PORT_WKA,
  1104. ZFCP_DID_DIRECTORY_SERVICE);
  1105. if (!port) {
  1106. ZFCP_LOG_INFO("error: enqueue of nameserver port for "
  1107. "adapter %s failed\n",
  1108. zfcp_get_busid_by_adapter(adapter));
  1109. return -ENXIO;
  1110. }
  1111. zfcp_port_put(port);
  1112. return 0;
  1113. }
  1114. #undef ZFCP_LOG_AREA
  1115. /****************************************************************/
  1116. /******* Fibre Channel Standard related Functions **************/
  1117. /****************************************************************/
  1118. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_FC
  1119. static void
  1120. zfcp_fsf_incoming_els_rscn(struct zfcp_adapter *adapter,
  1121. struct fsf_status_read_buffer *status_buffer)
  1122. {
  1123. struct fcp_rscn_head *fcp_rscn_head;
  1124. struct fcp_rscn_element *fcp_rscn_element;
  1125. struct zfcp_port *port;
  1126. u16 i;
  1127. u16 no_entries;
  1128. u32 range_mask;
  1129. unsigned long flags;
  1130. fcp_rscn_head = (struct fcp_rscn_head *) status_buffer->payload;
  1131. fcp_rscn_element = (struct fcp_rscn_element *) status_buffer->payload;
  1132. /* see FC-FS */
  1133. no_entries = (fcp_rscn_head->payload_len / 4);
  1134. for (i = 1; i < no_entries; i++) {
  1135. /* skip head and start with 1st element */
  1136. fcp_rscn_element++;
  1137. switch (fcp_rscn_element->addr_format) {
  1138. case ZFCP_PORT_ADDRESS:
  1139. range_mask = ZFCP_PORTS_RANGE_PORT;
  1140. break;
  1141. case ZFCP_AREA_ADDRESS:
  1142. range_mask = ZFCP_PORTS_RANGE_AREA;
  1143. break;
  1144. case ZFCP_DOMAIN_ADDRESS:
  1145. range_mask = ZFCP_PORTS_RANGE_DOMAIN;
  1146. break;
  1147. case ZFCP_FABRIC_ADDRESS:
  1148. range_mask = ZFCP_PORTS_RANGE_FABRIC;
  1149. break;
  1150. default:
  1151. ZFCP_LOG_INFO("incoming RSCN with unknown "
  1152. "address format\n");
  1153. continue;
  1154. }
  1155. read_lock_irqsave(&zfcp_data.config_lock, flags);
  1156. list_for_each_entry(port, &adapter->port_list_head, list) {
  1157. if (atomic_test_mask
  1158. (ZFCP_STATUS_PORT_WKA, &port->status))
  1159. continue;
  1160. /* Do we know this port? If not skip it. */
  1161. if (!atomic_test_mask
  1162. (ZFCP_STATUS_PORT_DID_DID, &port->status)) {
  1163. ZFCP_LOG_INFO("incoming RSCN, trying to open "
  1164. "port 0x%016Lx\n", port->wwpn);
  1165. zfcp_erp_port_reopen(port,
  1166. ZFCP_STATUS_COMMON_ERP_FAILED);
  1167. continue;
  1168. }
  1169. /*
  1170. * FIXME: race: d_id might being invalidated
  1171. * (...DID_DID reset)
  1172. */
  1173. if ((port->d_id & range_mask)
  1174. == (fcp_rscn_element->nport_did & range_mask)) {
  1175. ZFCP_LOG_TRACE("reopen did 0x%08x\n",
  1176. fcp_rscn_element->nport_did);
  1177. /*
  1178. * Unfortunately, an RSCN does not specify the
  1179. * type of change a target underwent. We assume
  1180. * that it makes sense to reopen the link.
  1181. * FIXME: Shall we try to find out more about
  1182. * the target and link state before closing it?
  1183. * How to accomplish this? (nameserver?)
  1184. * Where would such code be put in?
  1185. * (inside or outside erp)
  1186. */
  1187. ZFCP_LOG_INFO("incoming RSCN, trying to open "
  1188. "port 0x%016Lx\n", port->wwpn);
  1189. zfcp_test_link(port);
  1190. }
  1191. }
  1192. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  1193. }
  1194. }
  1195. static void
  1196. zfcp_fsf_incoming_els_plogi(struct zfcp_adapter *adapter,
  1197. struct fsf_status_read_buffer *status_buffer)
  1198. {
  1199. struct fsf_plogi *els_plogi;
  1200. struct zfcp_port *port;
  1201. unsigned long flags;
  1202. els_plogi = (struct fsf_plogi *) status_buffer->payload;
  1203. read_lock_irqsave(&zfcp_data.config_lock, flags);
  1204. list_for_each_entry(port, &adapter->port_list_head, list) {
  1205. if (port->wwpn == (*(wwn_t *) &els_plogi->serv_param.wwpn))
  1206. break;
  1207. }
  1208. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  1209. if (!port || (port->wwpn != (*(wwn_t *) &els_plogi->serv_param.wwpn))) {
  1210. ZFCP_LOG_DEBUG("ignored incoming PLOGI for nonexisting port "
  1211. "with d_id 0x%06x on adapter %s\n",
  1212. status_buffer->d_id,
  1213. zfcp_get_busid_by_adapter(adapter));
  1214. } else {
  1215. zfcp_erp_port_forced_reopen(port, 0);
  1216. }
  1217. }
  1218. static void
  1219. zfcp_fsf_incoming_els_logo(struct zfcp_adapter *adapter,
  1220. struct fsf_status_read_buffer *status_buffer)
  1221. {
  1222. struct fcp_logo *els_logo = (struct fcp_logo *) status_buffer->payload;
  1223. struct zfcp_port *port;
  1224. unsigned long flags;
  1225. read_lock_irqsave(&zfcp_data.config_lock, flags);
  1226. list_for_each_entry(port, &adapter->port_list_head, list) {
  1227. if (port->wwpn == els_logo->nport_wwpn)
  1228. break;
  1229. }
  1230. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  1231. if (!port || (port->wwpn != els_logo->nport_wwpn)) {
  1232. ZFCP_LOG_DEBUG("ignored incoming LOGO for nonexisting port "
  1233. "with d_id 0x%06x on adapter %s\n",
  1234. status_buffer->d_id,
  1235. zfcp_get_busid_by_adapter(adapter));
  1236. } else {
  1237. zfcp_erp_port_forced_reopen(port, 0);
  1238. }
  1239. }
  1240. static void
  1241. zfcp_fsf_incoming_els_unknown(struct zfcp_adapter *adapter,
  1242. struct fsf_status_read_buffer *status_buffer)
  1243. {
  1244. ZFCP_LOG_NORMAL("warning: unknown incoming ELS 0x%08x "
  1245. "for adapter %s\n", *(u32 *) (status_buffer->payload),
  1246. zfcp_get_busid_by_adapter(adapter));
  1247. }
  1248. void
  1249. zfcp_fsf_incoming_els(struct zfcp_fsf_req *fsf_req)
  1250. {
  1251. struct fsf_status_read_buffer *status_buffer;
  1252. u32 els_type;
  1253. struct zfcp_adapter *adapter;
  1254. status_buffer = (struct fsf_status_read_buffer *) fsf_req->data;
  1255. els_type = *(u32 *) (status_buffer->payload);
  1256. adapter = fsf_req->adapter;
  1257. zfcp_san_dbf_event_incoming_els(fsf_req);
  1258. if (els_type == LS_PLOGI)
  1259. zfcp_fsf_incoming_els_plogi(adapter, status_buffer);
  1260. else if (els_type == LS_LOGO)
  1261. zfcp_fsf_incoming_els_logo(adapter, status_buffer);
  1262. else if ((els_type & 0xffff0000) == LS_RSCN)
  1263. /* we are only concerned with the command, not the length */
  1264. zfcp_fsf_incoming_els_rscn(adapter, status_buffer);
  1265. else
  1266. zfcp_fsf_incoming_els_unknown(adapter, status_buffer);
  1267. }
  1268. /**
  1269. * zfcp_gid_pn_buffers_alloc - allocate buffers for GID_PN nameserver request
  1270. * @gid_pn: pointer to return pointer to struct zfcp_gid_pn_data
  1271. * @pool: pointer to mempool_t if non-null memory pool is used for allocation
  1272. */
  1273. static int
  1274. zfcp_gid_pn_buffers_alloc(struct zfcp_gid_pn_data **gid_pn, mempool_t *pool)
  1275. {
  1276. struct zfcp_gid_pn_data *data;
  1277. if (pool != NULL) {
  1278. data = mempool_alloc(pool, GFP_ATOMIC);
  1279. if (likely(data != NULL)) {
  1280. data->ct.pool = pool;
  1281. }
  1282. } else {
  1283. data = kmem_cache_alloc(zfcp_data.gid_pn_cache, GFP_ATOMIC);
  1284. }
  1285. if (NULL == data)
  1286. return -ENOMEM;
  1287. memset(data, 0, sizeof(*data));
  1288. data->ct.req = &data->req;
  1289. data->ct.resp = &data->resp;
  1290. data->ct.req_count = data->ct.resp_count = 1;
  1291. zfcp_address_to_sg(&data->ct_iu_req, &data->req);
  1292. zfcp_address_to_sg(&data->ct_iu_resp, &data->resp);
  1293. data->req.length = sizeof(struct ct_iu_gid_pn_req);
  1294. data->resp.length = sizeof(struct ct_iu_gid_pn_resp);
  1295. *gid_pn = data;
  1296. return 0;
  1297. }
  1298. /**
  1299. * zfcp_gid_pn_buffers_free - free buffers for GID_PN nameserver request
  1300. * @gid_pn: pointer to struct zfcp_gid_pn_data which has to be freed
  1301. */
  1302. static void zfcp_gid_pn_buffers_free(struct zfcp_gid_pn_data *gid_pn)
  1303. {
  1304. if (gid_pn->ct.pool)
  1305. mempool_free(gid_pn, gid_pn->ct.pool);
  1306. else
  1307. kmem_cache_free(zfcp_data.gid_pn_cache, gid_pn);
  1308. }
  1309. /**
  1310. * zfcp_ns_gid_pn_request - initiate GID_PN nameserver request
  1311. * @erp_action: pointer to zfcp_erp_action where GID_PN request is needed
  1312. */
  1313. int
  1314. zfcp_ns_gid_pn_request(struct zfcp_erp_action *erp_action)
  1315. {
  1316. int ret;
  1317. struct ct_iu_gid_pn_req *ct_iu_req;
  1318. struct zfcp_gid_pn_data *gid_pn;
  1319. struct zfcp_adapter *adapter = erp_action->adapter;
  1320. ret = zfcp_gid_pn_buffers_alloc(&gid_pn, adapter->pool.data_gid_pn);
  1321. if (ret < 0) {
  1322. ZFCP_LOG_INFO("error: buffer allocation for gid_pn nameserver "
  1323. "request failed for adapter %s\n",
  1324. zfcp_get_busid_by_adapter(adapter));
  1325. goto out;
  1326. }
  1327. /* setup nameserver request */
  1328. ct_iu_req = zfcp_sg_to_address(gid_pn->ct.req);
  1329. ct_iu_req->header.revision = ZFCP_CT_REVISION;
  1330. ct_iu_req->header.gs_type = ZFCP_CT_DIRECTORY_SERVICE;
  1331. ct_iu_req->header.gs_subtype = ZFCP_CT_NAME_SERVER;
  1332. ct_iu_req->header.options = ZFCP_CT_SYNCHRONOUS;
  1333. ct_iu_req->header.cmd_rsp_code = ZFCP_CT_GID_PN;
  1334. ct_iu_req->header.max_res_size = ZFCP_CT_MAX_SIZE;
  1335. ct_iu_req->wwpn = erp_action->port->wwpn;
  1336. /* setup parameters for send generic command */
  1337. gid_pn->ct.port = adapter->nameserver_port;
  1338. gid_pn->ct.handler = zfcp_ns_gid_pn_handler;
  1339. gid_pn->ct.handler_data = (unsigned long) gid_pn;
  1340. gid_pn->ct.timeout = ZFCP_NS_GID_PN_TIMEOUT;
  1341. gid_pn->port = erp_action->port;
  1342. ret = zfcp_fsf_send_ct(&gid_pn->ct, adapter->pool.fsf_req_erp,
  1343. erp_action);
  1344. if (ret) {
  1345. ZFCP_LOG_INFO("error: initiation of gid_pn nameserver request "
  1346. "failed for adapter %s\n",
  1347. zfcp_get_busid_by_adapter(adapter));
  1348. zfcp_gid_pn_buffers_free(gid_pn);
  1349. }
  1350. out:
  1351. return ret;
  1352. }
  1353. /**
  1354. * zfcp_ns_gid_pn_handler - handler for GID_PN nameserver request
  1355. * @data: unsigned long, contains pointer to struct zfcp_gid_pn_data
  1356. */
  1357. static void zfcp_ns_gid_pn_handler(unsigned long data)
  1358. {
  1359. struct zfcp_port *port;
  1360. struct zfcp_send_ct *ct;
  1361. struct ct_iu_gid_pn_req *ct_iu_req;
  1362. struct ct_iu_gid_pn_resp *ct_iu_resp;
  1363. struct zfcp_gid_pn_data *gid_pn;
  1364. gid_pn = (struct zfcp_gid_pn_data *) data;
  1365. port = gid_pn->port;
  1366. ct = &gid_pn->ct;
  1367. ct_iu_req = zfcp_sg_to_address(ct->req);
  1368. ct_iu_resp = zfcp_sg_to_address(ct->resp);
  1369. if (ct->status != 0)
  1370. goto failed;
  1371. if (zfcp_check_ct_response(&ct_iu_resp->header)) {
  1372. /* FIXME: do we need some specific erp entry points */
  1373. atomic_set_mask(ZFCP_STATUS_PORT_INVALID_WWPN, &port->status);
  1374. goto failed;
  1375. }
  1376. /* paranoia */
  1377. if (ct_iu_req->wwpn != port->wwpn) {
  1378. ZFCP_LOG_NORMAL("bug: wwpn 0x%016Lx returned by nameserver "
  1379. "lookup does not match expected wwpn 0x%016Lx "
  1380. "for adapter %s\n", ct_iu_req->wwpn, port->wwpn,
  1381. zfcp_get_busid_by_port(port));
  1382. goto mismatch;
  1383. }
  1384. /* looks like a valid d_id */
  1385. port->d_id = ct_iu_resp->d_id & ZFCP_DID_MASK;
  1386. atomic_set_mask(ZFCP_STATUS_PORT_DID_DID, &port->status);
  1387. ZFCP_LOG_DEBUG("adapter %s: wwpn=0x%016Lx ---> d_id=0x%06x\n",
  1388. zfcp_get_busid_by_port(port), port->wwpn, port->d_id);
  1389. goto out;
  1390. mismatch:
  1391. ZFCP_LOG_DEBUG("CT IUs do not match:\n");
  1392. ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_req,
  1393. sizeof(struct ct_iu_gid_pn_req));
  1394. ZFCP_HEX_DUMP(ZFCP_LOG_LEVEL_DEBUG, (char *) ct_iu_resp,
  1395. sizeof(struct ct_iu_gid_pn_resp));
  1396. failed:
  1397. ZFCP_LOG_NORMAL("warning: failed gid_pn nameserver request for wwpn "
  1398. "0x%016Lx for adapter %s\n",
  1399. port->wwpn, zfcp_get_busid_by_port(port));
  1400. out:
  1401. zfcp_gid_pn_buffers_free(gid_pn);
  1402. return;
  1403. }
  1404. /* reject CT_IU reason codes acc. to FC-GS-4 */
  1405. static const struct zfcp_rc_entry zfcp_ct_rc[] = {
  1406. {0x01, "invalid command code"},
  1407. {0x02, "invalid version level"},
  1408. {0x03, "logical error"},
  1409. {0x04, "invalid CT_IU size"},
  1410. {0x05, "logical busy"},
  1411. {0x07, "protocol error"},
  1412. {0x09, "unable to perform command request"},
  1413. {0x0b, "command not supported"},
  1414. {0x0d, "server not available"},
  1415. {0x0e, "session could not be established"},
  1416. {0xff, "vendor specific error"},
  1417. {0, NULL},
  1418. };
  1419. /* LS_RJT reason codes acc. to FC-FS */
  1420. static const struct zfcp_rc_entry zfcp_ls_rjt_rc[] = {
  1421. {0x01, "invalid LS_Command code"},
  1422. {0x03, "logical error"},
  1423. {0x05, "logical busy"},
  1424. {0x07, "protocol error"},
  1425. {0x09, "unable to perform command request"},
  1426. {0x0b, "command not supported"},
  1427. {0x0e, "command already in progress"},
  1428. {0xff, "vendor specific error"},
  1429. {0, NULL},
  1430. };
  1431. /* reject reason codes according to FC-PH/FC-FS */
  1432. static const struct zfcp_rc_entry zfcp_p_rjt_rc[] = {
  1433. {0x01, "invalid D_ID"},
  1434. {0x02, "invalid S_ID"},
  1435. {0x03, "Nx_Port not available, temporary"},
  1436. {0x04, "Nx_Port not available, permament"},
  1437. {0x05, "class not supported"},
  1438. {0x06, "delimiter usage error"},
  1439. {0x07, "TYPE not supported"},
  1440. {0x08, "invalid Link_Control"},
  1441. {0x09, "invalid R_CTL field"},
  1442. {0x0a, "invalid F_CTL field"},
  1443. {0x0b, "invalid OX_ID"},
  1444. {0x0c, "invalid RX_ID"},
  1445. {0x0d, "invalid SEQ_ID"},
  1446. {0x0e, "invalid DF_CTL"},
  1447. {0x0f, "invalid SEQ_CNT"},
  1448. {0x10, "invalid parameter field"},
  1449. {0x11, "exchange error"},
  1450. {0x12, "protocol error"},
  1451. {0x13, "incorrect length"},
  1452. {0x14, "unsupported ACK"},
  1453. {0x15, "class of service not supported by entity at FFFFFE"},
  1454. {0x16, "login required"},
  1455. {0x17, "excessive sequences attempted"},
  1456. {0x18, "unable to establish exchange"},
  1457. {0x1a, "fabric path not available"},
  1458. {0x1b, "invalid VC_ID (class 4)"},
  1459. {0x1c, "invalid CS_CTL field"},
  1460. {0x1d, "insufficient resources for VC (class 4)"},
  1461. {0x1f, "invalid class of service"},
  1462. {0x20, "preemption request rejected"},
  1463. {0x21, "preemption not enabled"},
  1464. {0x22, "multicast error"},
  1465. {0x23, "multicast error terminate"},
  1466. {0x24, "process login required"},
  1467. {0xff, "vendor specific reject"},
  1468. {0, NULL},
  1469. };
  1470. /**
  1471. * zfcp_rc_description - return description for given reaon code
  1472. * @code: reason code
  1473. * @rc_table: table of reason codes and descriptions
  1474. */
  1475. static const char *
  1476. zfcp_rc_description(u8 code, const struct zfcp_rc_entry *rc_table)
  1477. {
  1478. const char *descr = "unknown reason code";
  1479. do {
  1480. if (code == rc_table->code) {
  1481. descr = rc_table->description;
  1482. break;
  1483. }
  1484. rc_table++;
  1485. } while (rc_table->code && rc_table->description);
  1486. return descr;
  1487. }
  1488. /**
  1489. * zfcp_check_ct_response - evaluate reason code for CT_IU
  1490. * @rjt: response payload to an CT_IU request
  1491. * Return: 0 for accept CT_IU, 1 for reject CT_IU or invlid response code
  1492. */
  1493. int
  1494. zfcp_check_ct_response(struct ct_hdr *rjt)
  1495. {
  1496. if (rjt->cmd_rsp_code == ZFCP_CT_ACCEPT)
  1497. return 0;
  1498. if (rjt->cmd_rsp_code != ZFCP_CT_REJECT) {
  1499. ZFCP_LOG_NORMAL("error: invalid Generic Service command/"
  1500. "response code (0x%04hx)\n",
  1501. rjt->cmd_rsp_code);
  1502. return 1;
  1503. }
  1504. ZFCP_LOG_INFO("Generic Service command rejected\n");
  1505. ZFCP_LOG_INFO("%s (0x%02x, 0x%02x, 0x%02x)\n",
  1506. zfcp_rc_description(rjt->reason_code, zfcp_ct_rc),
  1507. (u32) rjt->reason_code, (u32) rjt->reason_code_expl,
  1508. (u32) rjt->vendor_unique);
  1509. return 1;
  1510. }
  1511. /**
  1512. * zfcp_print_els_rjt - print reject parameter and description for ELS reject
  1513. * @rjt_par: reject parameter acc. to FC-PH/FC-FS
  1514. * @rc_table: table of reason codes and descriptions
  1515. */
  1516. static void
  1517. zfcp_print_els_rjt(struct zfcp_ls_rjt_par *rjt_par,
  1518. const struct zfcp_rc_entry *rc_table)
  1519. {
  1520. ZFCP_LOG_INFO("%s (%02x %02x %02x %02x)\n",
  1521. zfcp_rc_description(rjt_par->reason_code, rc_table),
  1522. (u32) rjt_par->action, (u32) rjt_par->reason_code,
  1523. (u32) rjt_par->reason_expl, (u32) rjt_par->vendor_unique);
  1524. }
  1525. /**
  1526. * zfcp_fsf_handle_els_rjt - evaluate status qualifier/reason code on ELS reject
  1527. * @sq: status qualifier word
  1528. * @rjt_par: reject parameter as described in FC-PH and FC-FS
  1529. * Return: -EROMTEIO for LS_RJT, -EREMCHG for invalid D_ID, -EIO else
  1530. */
  1531. int
  1532. zfcp_handle_els_rjt(u32 sq, struct zfcp_ls_rjt_par *rjt_par)
  1533. {
  1534. int ret = -EIO;
  1535. if (sq == FSF_IOSTAT_NPORT_RJT) {
  1536. ZFCP_LOG_INFO("ELS rejected (P_RJT)\n");
  1537. zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
  1538. /* invalid d_id */
  1539. if (rjt_par->reason_code == 0x01)
  1540. ret = -EREMCHG;
  1541. } else if (sq == FSF_IOSTAT_FABRIC_RJT) {
  1542. ZFCP_LOG_INFO("ELS rejected (F_RJT)\n");
  1543. zfcp_print_els_rjt(rjt_par, zfcp_p_rjt_rc);
  1544. /* invalid d_id */
  1545. if (rjt_par->reason_code == 0x01)
  1546. ret = -EREMCHG;
  1547. } else if (sq == FSF_IOSTAT_LS_RJT) {
  1548. ZFCP_LOG_INFO("ELS rejected (LS_RJT)\n");
  1549. zfcp_print_els_rjt(rjt_par, zfcp_ls_rjt_rc);
  1550. ret = -EREMOTEIO;
  1551. } else
  1552. ZFCP_LOG_INFO("unexpected SQ: 0x%02x\n", sq);
  1553. return ret;
  1554. }
  1555. /**
  1556. * zfcp_plogi_evaluate - evaluate PLOGI playload and copy important fields
  1557. * into zfcp_port structure
  1558. * @port: zfcp_port structure
  1559. * @plogi: plogi payload
  1560. */
  1561. void
  1562. zfcp_plogi_evaluate(struct zfcp_port *port, struct fsf_plogi *plogi)
  1563. {
  1564. port->maxframe_size = plogi->serv_param.common_serv_param[7] |
  1565. ((plogi->serv_param.common_serv_param[6] & 0x0F) << 8);
  1566. if (plogi->serv_param.class1_serv_param[0] & 0x80)
  1567. port->supported_classes |= FC_COS_CLASS1;
  1568. if (plogi->serv_param.class2_serv_param[0] & 0x80)
  1569. port->supported_classes |= FC_COS_CLASS2;
  1570. if (plogi->serv_param.class3_serv_param[0] & 0x80)
  1571. port->supported_classes |= FC_COS_CLASS3;
  1572. if (plogi->serv_param.class4_serv_param[0] & 0x80)
  1573. port->supported_classes |= FC_COS_CLASS4;
  1574. }
  1575. #undef ZFCP_LOG_AREA