zfcp_scsi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  1. /*
  2. *
  3. * linux/drivers/s390/scsi/zfcp_scsi.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_LOG_AREA ZFCP_LOG_AREA_SCSI
  32. #define ZFCP_SCSI_REVISION "$Revision: 1.74 $"
  33. #include "zfcp_ext.h"
  34. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  35. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  36. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  37. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  38. void (*done) (struct scsi_cmnd *));
  39. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  40. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  41. static int zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *);
  42. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  43. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  44. struct scsi_cmnd *);
  45. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int, scsi_id_t,
  46. scsi_lun_t);
  47. static struct zfcp_port *zfcp_port_lookup(struct zfcp_adapter *, int,
  48. scsi_id_t);
  49. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  50. struct scsi_transport_template *zfcp_transport_template;
  51. struct zfcp_data zfcp_data = {
  52. .scsi_host_template = {
  53. name: ZFCP_NAME,
  54. proc_name: "zfcp",
  55. proc_info: NULL,
  56. detect: NULL,
  57. slave_alloc: zfcp_scsi_slave_alloc,
  58. slave_configure: zfcp_scsi_slave_configure,
  59. slave_destroy: zfcp_scsi_slave_destroy,
  60. queuecommand: zfcp_scsi_queuecommand,
  61. eh_abort_handler: zfcp_scsi_eh_abort_handler,
  62. eh_device_reset_handler: zfcp_scsi_eh_device_reset_handler,
  63. eh_bus_reset_handler: zfcp_scsi_eh_bus_reset_handler,
  64. eh_host_reset_handler: zfcp_scsi_eh_host_reset_handler,
  65. /* FIXME(openfcp): Tune */
  66. can_queue: 4096,
  67. this_id: 0,
  68. /*
  69. * FIXME:
  70. * one less? can zfcp_create_sbale cope with it?
  71. */
  72. sg_tablesize: ZFCP_MAX_SBALES_PER_REQ,
  73. cmd_per_lun: 1,
  74. unchecked_isa_dma: 0,
  75. use_clustering: 1,
  76. sdev_attrs: zfcp_sysfs_sdev_attrs,
  77. },
  78. .driver_version = ZFCP_VERSION,
  79. /* rest initialised with zeros */
  80. };
  81. /* Find start of Response Information in FCP response unit*/
  82. char *
  83. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  84. {
  85. char *fcp_rsp_info_ptr;
  86. fcp_rsp_info_ptr =
  87. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  88. return fcp_rsp_info_ptr;
  89. }
  90. /* Find start of Sense Information in FCP response unit*/
  91. char *
  92. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  93. {
  94. char *fcp_sns_info_ptr;
  95. fcp_sns_info_ptr =
  96. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  97. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  98. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  99. fcp_rsp_iu->fcp_rsp_len;
  100. return fcp_sns_info_ptr;
  101. }
  102. fcp_dl_t *
  103. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  104. {
  105. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  106. fcp_dl_t *fcp_dl_addr;
  107. fcp_dl_addr = (fcp_dl_t *)
  108. ((unsigned char *) fcp_cmd +
  109. sizeof (struct fcp_cmnd_iu) + additional_length);
  110. /*
  111. * fcp_dl_addr = start address of fcp_cmnd structure +
  112. * size of fixed part + size of dynamically sized add_dcp_cdb field
  113. * SEE FCP-2 documentation
  114. */
  115. return fcp_dl_addr;
  116. }
  117. fcp_dl_t
  118. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  119. {
  120. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  121. }
  122. void
  123. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  124. {
  125. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  126. }
  127. /*
  128. * note: it's a bit-or operation not an assignment
  129. * regarding the specified byte
  130. */
  131. static inline void
  132. set_byte(u32 * result, char status, char pos)
  133. {
  134. *result |= status << (pos * 8);
  135. }
  136. void
  137. set_host_byte(u32 * result, char status)
  138. {
  139. set_byte(result, status, 2);
  140. }
  141. void
  142. set_driver_byte(u32 * result, char status)
  143. {
  144. set_byte(result, status, 3);
  145. }
  146. /*
  147. * function: zfcp_scsi_slave_alloc
  148. *
  149. * purpose:
  150. *
  151. * returns:
  152. */
  153. static int
  154. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  155. {
  156. struct zfcp_adapter *adapter;
  157. struct zfcp_unit *unit;
  158. unsigned long flags;
  159. int retval = -ENODEV;
  160. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  161. if (!adapter)
  162. goto out;
  163. read_lock_irqsave(&zfcp_data.config_lock, flags);
  164. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  165. if (unit) {
  166. sdp->hostdata = unit;
  167. unit->device = sdp;
  168. zfcp_unit_get(unit);
  169. retval = 0;
  170. }
  171. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  172. out:
  173. return retval;
  174. }
  175. /*
  176. * function: zfcp_scsi_slave_destroy
  177. *
  178. * purpose:
  179. *
  180. * returns:
  181. */
  182. static void
  183. zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  184. {
  185. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  186. if (unit) {
  187. sdpnt->hostdata = NULL;
  188. unit->device = NULL;
  189. zfcp_unit_put(unit);
  190. } else {
  191. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  192. "address %p\n", sdpnt);
  193. }
  194. }
  195. /*
  196. * called from scsi midlayer to allow finetuning of a device.
  197. */
  198. static int
  199. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  200. {
  201. if (sdp->tagged_supported)
  202. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  203. else
  204. scsi_adjust_queue_depth(sdp, 0, 1);
  205. return 0;
  206. }
  207. /**
  208. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  209. * @scpnt: pointer to struct scsi_cmnd where result is set
  210. * @result: result to be set in scpnt (e.g. DID_ERROR)
  211. */
  212. static void
  213. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  214. {
  215. set_host_byte(&scpnt->result, result);
  216. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  217. zfcp_scsi_dbf_event_result("fail", 4,
  218. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  219. scpnt);
  220. /* return directly */
  221. scpnt->scsi_done(scpnt);
  222. }
  223. /**
  224. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  225. * zfcp_scsi_command_sync
  226. * @adapter: adapter where scsi command is issued
  227. * @unit: unit to which scsi command is sent
  228. * @scpnt: scsi command to be sent
  229. * @timer: timer to be started if request is successfully initiated
  230. *
  231. * Note: In scsi_done function must be set in scpnt.
  232. */
  233. int
  234. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  235. struct scsi_cmnd *scpnt, struct timer_list *timer)
  236. {
  237. int tmp;
  238. int retval;
  239. retval = 0;
  240. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  241. BUG_ON(scpnt->scsi_done == NULL);
  242. if (unlikely(NULL == unit)) {
  243. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  244. goto out;
  245. }
  246. if (unlikely(
  247. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  248. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  249. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  250. "0x%016Lx on adapter %s\n",
  251. unit->fcp_lun, unit->port->wwpn,
  252. zfcp_get_busid_by_adapter(adapter));
  253. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  254. goto out;
  255. }
  256. if (unlikely(
  257. !atomic_test_mask(ZFCP_STATUS_COMMON_UNBLOCKED, &unit->status))) {
  258. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  259. "on port 0x%016Lx in recovery\n",
  260. zfcp_get_busid_by_unit(unit),
  261. unit->fcp_lun, unit->port->wwpn);
  262. retval = SCSI_MLQUEUE_DEVICE_BUSY;
  263. goto out;
  264. }
  265. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, timer,
  266. ZFCP_REQ_AUTO_CLEANUP);
  267. if (unlikely(tmp < 0)) {
  268. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  269. retval = SCSI_MLQUEUE_HOST_BUSY;
  270. }
  271. out:
  272. return retval;
  273. }
  274. void
  275. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  276. {
  277. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  278. complete(wait);
  279. }
  280. /**
  281. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  282. * @unit: unit where command is sent to
  283. * @scpnt: scsi command to be sent
  284. * @timer: timer to be started if request is successfully initiated
  285. * Return: 0
  286. *
  287. * Errors are indicated in scpnt->result
  288. */
  289. int
  290. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  291. struct timer_list *timer)
  292. {
  293. int ret;
  294. DECLARE_COMPLETION(wait);
  295. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  296. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  297. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt, timer);
  298. if (ret == 0)
  299. wait_for_completion(&wait);
  300. scpnt->SCp.ptr = NULL;
  301. return 0;
  302. }
  303. /*
  304. * function: zfcp_scsi_queuecommand
  305. *
  306. * purpose: enqueues a SCSI command to the specified target device
  307. *
  308. * returns: 0 - success, SCSI command enqueued
  309. * !0 - failure
  310. */
  311. int
  312. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  313. void (*done) (struct scsi_cmnd *))
  314. {
  315. struct zfcp_unit *unit;
  316. struct zfcp_adapter *adapter;
  317. /* reset the status for this request */
  318. scpnt->result = 0;
  319. scpnt->host_scribble = NULL;
  320. scpnt->scsi_done = done;
  321. /*
  322. * figure out adapter and target device
  323. * (stored there by zfcp_scsi_slave_alloc)
  324. */
  325. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  326. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  327. return zfcp_scsi_command_async(adapter, unit, scpnt, NULL);
  328. }
  329. /*
  330. * function: zfcp_unit_lookup
  331. *
  332. * purpose:
  333. *
  334. * returns:
  335. *
  336. * context:
  337. */
  338. static struct zfcp_unit *
  339. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id,
  340. scsi_lun_t lun)
  341. {
  342. struct zfcp_port *port;
  343. struct zfcp_unit *unit, *retval = NULL;
  344. list_for_each_entry(port, &adapter->port_list_head, list) {
  345. if (!port->rport || (id != port->rport->scsi_target_id))
  346. continue;
  347. list_for_each_entry(unit, &port->unit_list_head, list) {
  348. if (lun == unit->scsi_lun) {
  349. retval = unit;
  350. goto out;
  351. }
  352. }
  353. }
  354. out:
  355. return retval;
  356. }
  357. static struct zfcp_port *
  358. zfcp_port_lookup(struct zfcp_adapter *adapter, int channel, scsi_id_t id)
  359. {
  360. struct zfcp_port *port;
  361. list_for_each_entry(port, &adapter->port_list_head, list) {
  362. if (port->rport && (id == port->rport->scsi_target_id))
  363. return port;
  364. }
  365. return (struct zfcp_port *) NULL;
  366. }
  367. /**
  368. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  369. * @scpnt: pointer to scsi_cmnd to be aborted
  370. * Return: SUCCESS - command has been aborted and cleaned up in internal
  371. * bookkeeping, SCSI stack won't be called for aborted command
  372. * FAILED - otherwise
  373. *
  374. * We do not need to care for a SCSI command which completes normally
  375. * but late during this abort routine runs. We are allowed to return
  376. * late commands to the SCSI stack. It tracks the state of commands and
  377. * will handle late commands. (Usually, the normal completion of late
  378. * commands is ignored with respect to the running abort operation.)
  379. */
  380. int
  381. zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  382. {
  383. struct Scsi_Host *scsi_host;
  384. struct zfcp_adapter *adapter;
  385. struct zfcp_unit *unit;
  386. int retval = SUCCESS;
  387. struct zfcp_fsf_req *new_fsf_req = NULL;
  388. struct zfcp_fsf_req *old_fsf_req;
  389. unsigned long flags;
  390. scsi_host = scpnt->device->host;
  391. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  392. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  393. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  394. scpnt, zfcp_get_busid_by_adapter(adapter));
  395. /* avoid race condition between late normal completion and abort */
  396. write_lock_irqsave(&adapter->abort_lock, flags);
  397. /*
  398. * Check whether command has just completed and can not be aborted.
  399. * Even if the command has just been completed late, we can access
  400. * scpnt since the SCSI stack does not release it at least until
  401. * this routine returns. (scpnt is parameter passed to this routine
  402. * and must not disappear during abort even on late completion.)
  403. */
  404. old_fsf_req = (struct zfcp_fsf_req *) scpnt->host_scribble;
  405. if (!old_fsf_req) {
  406. write_unlock_irqrestore(&adapter->abort_lock, flags);
  407. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, new_fsf_req);
  408. retval = SUCCESS;
  409. goto out;
  410. }
  411. old_fsf_req->data = 0;
  412. old_fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  413. /* don't access old_fsf_req after releasing the abort_lock */
  414. write_unlock_irqrestore(&adapter->abort_lock, flags);
  415. /* call FSF routine which does the abort */
  416. new_fsf_req = zfcp_fsf_abort_fcp_command((unsigned long) old_fsf_req,
  417. adapter, unit, 0);
  418. if (!new_fsf_req) {
  419. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  420. retval = FAILED;
  421. goto out;
  422. }
  423. /* wait for completion of abort */
  424. __wait_event(new_fsf_req->completion_wq,
  425. new_fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  426. /* status should be valid since signals were not permitted */
  427. if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  428. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, new_fsf_req);
  429. retval = SUCCESS;
  430. } else if (new_fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  431. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, new_fsf_req);
  432. retval = SUCCESS;
  433. } else {
  434. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, new_fsf_req);
  435. retval = FAILED;
  436. }
  437. zfcp_fsf_req_free(new_fsf_req);
  438. out:
  439. return retval;
  440. }
  441. /*
  442. * function: zfcp_scsi_eh_device_reset_handler
  443. *
  444. * purpose:
  445. *
  446. * returns:
  447. */
  448. int
  449. zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  450. {
  451. int retval;
  452. struct zfcp_unit *unit = (struct zfcp_unit *) scpnt->device->hostdata;
  453. if (!unit) {
  454. ZFCP_LOG_NORMAL("bug: Tried reset for nonexistent unit\n");
  455. retval = SUCCESS;
  456. goto out;
  457. }
  458. ZFCP_LOG_NORMAL("resetting unit 0x%016Lx\n", unit->fcp_lun);
  459. /*
  460. * If we do not know whether the unit supports 'logical unit reset'
  461. * then try 'logical unit reset' and proceed with 'target reset'
  462. * if 'logical unit reset' fails.
  463. * If the unit is known not to support 'logical unit reset' then
  464. * skip 'logical unit reset' and try 'target reset' immediately.
  465. */
  466. if (!atomic_test_mask(ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  467. &unit->status)) {
  468. retval = zfcp_task_management_function(unit,
  469. FCP_LOGICAL_UNIT_RESET,
  470. scpnt);
  471. if (retval) {
  472. ZFCP_LOG_DEBUG("unit reset failed (unit=%p)\n", unit);
  473. if (retval == -ENOTSUPP)
  474. atomic_set_mask
  475. (ZFCP_STATUS_UNIT_NOTSUPPUNITRESET,
  476. &unit->status);
  477. /* fall through and try 'target reset' next */
  478. } else {
  479. ZFCP_LOG_DEBUG("unit reset succeeded (unit=%p)\n",
  480. unit);
  481. /* avoid 'target reset' */
  482. retval = SUCCESS;
  483. goto out;
  484. }
  485. }
  486. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  487. if (retval) {
  488. ZFCP_LOG_DEBUG("target reset failed (unit=%p)\n", unit);
  489. retval = FAILED;
  490. } else {
  491. ZFCP_LOG_DEBUG("target reset succeeded (unit=%p)\n", unit);
  492. retval = SUCCESS;
  493. }
  494. out:
  495. return retval;
  496. }
  497. static int
  498. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  499. struct scsi_cmnd *scpnt)
  500. {
  501. struct zfcp_adapter *adapter = unit->port->adapter;
  502. struct zfcp_fsf_req *fsf_req;
  503. int retval = 0;
  504. /* issue task management function */
  505. fsf_req = zfcp_fsf_send_fcp_command_task_management
  506. (adapter, unit, tm_flags, 0);
  507. if (!fsf_req) {
  508. ZFCP_LOG_INFO("error: creation of task management request "
  509. "failed for unit 0x%016Lx on port 0x%016Lx on "
  510. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  511. zfcp_get_busid_by_adapter(adapter));
  512. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  513. retval = -ENOMEM;
  514. goto out;
  515. }
  516. __wait_event(fsf_req->completion_wq,
  517. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  518. /*
  519. * check completion status of task management function
  520. */
  521. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  522. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  523. retval = -EIO;
  524. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  525. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  526. retval = -ENOTSUPP;
  527. } else
  528. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  529. zfcp_fsf_req_free(fsf_req);
  530. out:
  531. return retval;
  532. }
  533. /**
  534. * zfcp_scsi_eh_bus_reset_handler - reset bus (reopen adapter)
  535. */
  536. int
  537. zfcp_scsi_eh_bus_reset_handler(struct scsi_cmnd *scpnt)
  538. {
  539. struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
  540. struct zfcp_adapter *adapter = unit->port->adapter;
  541. ZFCP_LOG_NORMAL("bus reset because of problems with "
  542. "unit 0x%016Lx\n", unit->fcp_lun);
  543. zfcp_erp_adapter_reopen(adapter, 0);
  544. zfcp_erp_wait(adapter);
  545. return SUCCESS;
  546. }
  547. /**
  548. * zfcp_scsi_eh_host_reset_handler - reset host (reopen adapter)
  549. */
  550. int
  551. zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  552. {
  553. struct zfcp_unit *unit = (struct zfcp_unit*) scpnt->device->hostdata;
  554. struct zfcp_adapter *adapter = unit->port->adapter;
  555. ZFCP_LOG_NORMAL("host reset because of problems with "
  556. "unit 0x%016Lx\n", unit->fcp_lun);
  557. zfcp_erp_adapter_reopen(adapter, 0);
  558. zfcp_erp_wait(adapter);
  559. return SUCCESS;
  560. }
  561. /*
  562. * function:
  563. *
  564. * purpose:
  565. *
  566. * returns:
  567. */
  568. int
  569. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  570. {
  571. int retval = 0;
  572. static unsigned int unique_id = 0;
  573. /* register adapter as SCSI host with mid layer of SCSI stack */
  574. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  575. sizeof (struct zfcp_adapter *));
  576. if (!adapter->scsi_host) {
  577. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  578. "for adapter %s ",
  579. zfcp_get_busid_by_adapter(adapter));
  580. retval = -EIO;
  581. goto out;
  582. }
  583. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  584. /* tell the SCSI stack some characteristics of this adapter */
  585. adapter->scsi_host->max_id = 1;
  586. adapter->scsi_host->max_lun = 1;
  587. adapter->scsi_host->max_channel = 0;
  588. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  589. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  590. adapter->scsi_host->transportt = zfcp_transport_template;
  591. /*
  592. * Reverse mapping of the host number to avoid race condition
  593. */
  594. adapter->scsi_host_no = adapter->scsi_host->host_no;
  595. /*
  596. * save a pointer to our own adapter data structure within
  597. * hostdata field of SCSI host data structure
  598. */
  599. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  600. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  601. scsi_host_put(adapter->scsi_host);
  602. retval = -EIO;
  603. goto out;
  604. }
  605. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  606. out:
  607. return retval;
  608. }
  609. /*
  610. * function:
  611. *
  612. * purpose:
  613. *
  614. * returns:
  615. */
  616. void
  617. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  618. {
  619. struct Scsi_Host *shost;
  620. struct zfcp_port *port;
  621. shost = adapter->scsi_host;
  622. if (!shost)
  623. return;
  624. read_lock_irq(&zfcp_data.config_lock);
  625. list_for_each_entry(port, &adapter->port_list_head, list)
  626. if (port->rport)
  627. port->rport = NULL;
  628. read_unlock_irq(&zfcp_data.config_lock);
  629. fc_remove_host(shost);
  630. scsi_remove_host(shost);
  631. scsi_host_put(shost);
  632. adapter->scsi_host = NULL;
  633. adapter->scsi_host_no = 0;
  634. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  635. return;
  636. }
  637. void
  638. zfcp_fsf_start_scsi_er_timer(struct zfcp_adapter *adapter)
  639. {
  640. adapter->scsi_er_timer.function = zfcp_fsf_scsi_er_timeout_handler;
  641. adapter->scsi_er_timer.data = (unsigned long) adapter;
  642. adapter->scsi_er_timer.expires = jiffies + ZFCP_SCSI_ER_TIMEOUT;
  643. add_timer(&adapter->scsi_er_timer);
  644. }
  645. /*
  646. * Support functions for FC transport class
  647. */
  648. static void
  649. zfcp_get_port_id(struct scsi_target *starget)
  650. {
  651. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  652. struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
  653. struct zfcp_port *port;
  654. unsigned long flags;
  655. read_lock_irqsave(&zfcp_data.config_lock, flags);
  656. port = zfcp_port_lookup(adapter, starget->channel, starget->id);
  657. if (port)
  658. fc_starget_port_id(starget) = port->d_id;
  659. else
  660. fc_starget_port_id(starget) = -1;
  661. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  662. }
  663. static void
  664. zfcp_get_port_name(struct scsi_target *starget)
  665. {
  666. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  667. struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
  668. struct zfcp_port *port;
  669. unsigned long flags;
  670. read_lock_irqsave(&zfcp_data.config_lock, flags);
  671. port = zfcp_port_lookup(adapter, starget->channel, starget->id);
  672. if (port)
  673. fc_starget_port_name(starget) = port->wwpn;
  674. else
  675. fc_starget_port_name(starget) = -1;
  676. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  677. }
  678. static void
  679. zfcp_get_node_name(struct scsi_target *starget)
  680. {
  681. struct Scsi_Host *shost = dev_to_shost(starget->dev.parent);
  682. struct zfcp_adapter *adapter = (struct zfcp_adapter *)shost->hostdata[0];
  683. struct zfcp_port *port;
  684. unsigned long flags;
  685. read_lock_irqsave(&zfcp_data.config_lock, flags);
  686. port = zfcp_port_lookup(adapter, starget->channel, starget->id);
  687. if (port)
  688. fc_starget_node_name(starget) = port->wwnn;
  689. else
  690. fc_starget_node_name(starget) = -1;
  691. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  692. }
  693. struct fc_function_template zfcp_transport_functions = {
  694. .get_starget_port_id = zfcp_get_port_id,
  695. .get_starget_port_name = zfcp_get_port_name,
  696. .get_starget_node_name = zfcp_get_node_name,
  697. .show_starget_port_id = 1,
  698. .show_starget_port_name = 1,
  699. .show_starget_node_name = 1,
  700. .show_rport_supported_classes = 1,
  701. .show_host_node_name = 1,
  702. .show_host_port_name = 1,
  703. .show_host_supported_classes = 1,
  704. .show_host_maxframe_size = 1,
  705. .show_host_serial_number = 1,
  706. .show_host_speed = 1,
  707. .show_host_port_id = 1,
  708. };
  709. /**
  710. * ZFCP_DEFINE_SCSI_ATTR
  711. * @_name: name of show attribute
  712. * @_format: format string
  713. * @_value: value to print
  714. *
  715. * Generates attribute for a unit.
  716. */
  717. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  718. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  719. char *buf) \
  720. { \
  721. struct scsi_device *sdev; \
  722. struct zfcp_unit *unit; \
  723. \
  724. sdev = to_scsi_device(dev); \
  725. unit = sdev->hostdata; \
  726. return sprintf(buf, _format, _value); \
  727. } \
  728. \
  729. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  730. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  731. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  732. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  733. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  734. &dev_attr_fcp_lun,
  735. &dev_attr_wwpn,
  736. &dev_attr_hba_id,
  737. NULL
  738. };
  739. #undef ZFCP_LOG_AREA