zfcp_scsi.c 26 KB

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