zfcp_aux.c 54 KB

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