zfcp_aux.c 49 KB

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