zfcp_aux.c 55 KB

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