zfcp_scsi.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*
  2. * This file is part of the zfcp device driver for
  3. * FCP adapters for IBM System z9 and zSeries.
  4. *
  5. * (C) Copyright IBM Corp. 2002, 2006
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #define ZFCP_LOG_AREA ZFCP_LOG_AREA_SCSI
  22. #include "zfcp_ext.h"
  23. #include <asm/atomic.h>
  24. static void zfcp_scsi_slave_destroy(struct scsi_device *sdp);
  25. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp);
  26. static int zfcp_scsi_slave_configure(struct scsi_device *sdp);
  27. static int zfcp_scsi_queuecommand(struct scsi_cmnd *,
  28. void (*done) (struct scsi_cmnd *));
  29. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *);
  30. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *);
  31. static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *);
  32. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *);
  33. static int zfcp_task_management_function(struct zfcp_unit *, u8,
  34. struct scsi_cmnd *);
  35. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *, int,
  36. unsigned int, unsigned int);
  37. static struct device_attribute *zfcp_sysfs_sdev_attrs[];
  38. struct zfcp_data zfcp_data = {
  39. .scsi_host_template = {
  40. .name = ZFCP_NAME,
  41. .module = THIS_MODULE,
  42. .proc_name = "zfcp",
  43. .slave_alloc = zfcp_scsi_slave_alloc,
  44. .slave_configure = zfcp_scsi_slave_configure,
  45. .slave_destroy = zfcp_scsi_slave_destroy,
  46. .queuecommand = zfcp_scsi_queuecommand,
  47. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  48. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  49. .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
  50. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  51. .can_queue = 4096,
  52. .this_id = -1,
  53. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  54. .cmd_per_lun = 1,
  55. .use_clustering = 1,
  56. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  57. .max_sectors = ZFCP_MAX_SECTORS,
  58. },
  59. .driver_version = ZFCP_VERSION,
  60. };
  61. /* Find start of Response Information in FCP response unit*/
  62. char *
  63. zfcp_get_fcp_rsp_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  64. {
  65. char *fcp_rsp_info_ptr;
  66. fcp_rsp_info_ptr =
  67. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  68. return fcp_rsp_info_ptr;
  69. }
  70. /* Find start of Sense Information in FCP response unit*/
  71. char *
  72. zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  73. {
  74. char *fcp_sns_info_ptr;
  75. fcp_sns_info_ptr =
  76. (unsigned char *) fcp_rsp_iu + (sizeof (struct fcp_rsp_iu));
  77. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  78. fcp_sns_info_ptr = (char *) fcp_sns_info_ptr +
  79. fcp_rsp_iu->fcp_rsp_len;
  80. return fcp_sns_info_ptr;
  81. }
  82. static fcp_dl_t *
  83. zfcp_get_fcp_dl_ptr(struct fcp_cmnd_iu * fcp_cmd)
  84. {
  85. int additional_length = fcp_cmd->add_fcp_cdb_length << 2;
  86. fcp_dl_t *fcp_dl_addr;
  87. fcp_dl_addr = (fcp_dl_t *)
  88. ((unsigned char *) fcp_cmd +
  89. sizeof (struct fcp_cmnd_iu) + additional_length);
  90. /*
  91. * fcp_dl_addr = start address of fcp_cmnd structure +
  92. * size of fixed part + size of dynamically sized add_dcp_cdb field
  93. * SEE FCP-2 documentation
  94. */
  95. return fcp_dl_addr;
  96. }
  97. fcp_dl_t
  98. zfcp_get_fcp_dl(struct fcp_cmnd_iu * fcp_cmd)
  99. {
  100. return *zfcp_get_fcp_dl_ptr(fcp_cmd);
  101. }
  102. void
  103. zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  104. {
  105. *zfcp_get_fcp_dl_ptr(fcp_cmd) = fcp_dl;
  106. }
  107. /*
  108. * note: it's a bit-or operation not an assignment
  109. * regarding the specified byte
  110. */
  111. static inline void
  112. set_byte(int *result, char status, char pos)
  113. {
  114. *result |= status << (pos * 8);
  115. }
  116. void
  117. set_host_byte(int *result, char status)
  118. {
  119. set_byte(result, status, 2);
  120. }
  121. void
  122. set_driver_byte(int *result, char status)
  123. {
  124. set_byte(result, status, 3);
  125. }
  126. static int
  127. zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  128. {
  129. struct zfcp_adapter *adapter;
  130. struct zfcp_unit *unit;
  131. unsigned long flags;
  132. int retval = -ENXIO;
  133. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  134. if (!adapter)
  135. goto out;
  136. read_lock_irqsave(&zfcp_data.config_lock, flags);
  137. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  138. if (unit && atomic_test_mask(ZFCP_STATUS_UNIT_REGISTERED,
  139. &unit->status)) {
  140. sdp->hostdata = unit;
  141. unit->device = sdp;
  142. zfcp_unit_get(unit);
  143. retval = 0;
  144. }
  145. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  146. out:
  147. return retval;
  148. }
  149. /**
  150. * zfcp_scsi_slave_destroy - called when scsi device is removed
  151. *
  152. * Remove reference to associated scsi device for an zfcp_unit.
  153. * Mark zfcp_unit as failed. The scsi device might be deleted via sysfs
  154. * or a scan for this device might have failed.
  155. */
  156. static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  157. {
  158. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  159. if (unit) {
  160. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  161. sdpnt->hostdata = NULL;
  162. unit->device = NULL;
  163. zfcp_erp_unit_failed(unit, 12, NULL);
  164. zfcp_unit_put(unit);
  165. } else
  166. ZFCP_LOG_NORMAL("bug: no unit associated with SCSI device at "
  167. "address %p\n", sdpnt);
  168. }
  169. /*
  170. * called from scsi midlayer to allow finetuning of a device.
  171. */
  172. static int
  173. zfcp_scsi_slave_configure(struct scsi_device *sdp)
  174. {
  175. if (sdp->tagged_supported)
  176. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, ZFCP_CMND_PER_LUN);
  177. else
  178. scsi_adjust_queue_depth(sdp, 0, 1);
  179. return 0;
  180. }
  181. /**
  182. * zfcp_scsi_command_fail - set result in scsi_cmnd and call scsi_done function
  183. * @scpnt: pointer to struct scsi_cmnd where result is set
  184. * @result: result to be set in scpnt (e.g. DID_ERROR)
  185. */
  186. static void
  187. zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  188. {
  189. set_host_byte(&scpnt->result, result);
  190. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  191. zfcp_scsi_dbf_event_result("fail", 4,
  192. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  193. scpnt, NULL);
  194. /* return directly */
  195. scpnt->scsi_done(scpnt);
  196. }
  197. /**
  198. * zfcp_scsi_command_async - worker for zfcp_scsi_queuecommand and
  199. * zfcp_scsi_command_sync
  200. * @adapter: adapter where scsi command is issued
  201. * @unit: unit to which scsi command is sent
  202. * @scpnt: scsi command to be sent
  203. * @timer: timer to be started if request is successfully initiated
  204. *
  205. * Note: In scsi_done function must be set in scpnt.
  206. */
  207. int
  208. zfcp_scsi_command_async(struct zfcp_adapter *adapter, struct zfcp_unit *unit,
  209. struct scsi_cmnd *scpnt, int use_timer)
  210. {
  211. int tmp;
  212. int retval;
  213. retval = 0;
  214. BUG_ON((adapter == NULL) || (adapter != unit->port->adapter));
  215. BUG_ON(scpnt->scsi_done == NULL);
  216. if (unlikely(NULL == unit)) {
  217. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  218. goto out;
  219. }
  220. if (unlikely(
  221. atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status) ||
  222. !atomic_test_mask(ZFCP_STATUS_COMMON_RUNNING, &unit->status))) {
  223. ZFCP_LOG_DEBUG("stopping SCSI I/O on unit 0x%016Lx on port "
  224. "0x%016Lx on adapter %s\n",
  225. unit->fcp_lun, unit->port->wwpn,
  226. zfcp_get_busid_by_adapter(adapter));
  227. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  228. goto out;
  229. }
  230. tmp = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, use_timer,
  231. ZFCP_REQ_AUTO_CLEANUP);
  232. if (unlikely(tmp == -EBUSY)) {
  233. ZFCP_LOG_DEBUG("adapter %s not ready or unit 0x%016Lx "
  234. "on port 0x%016Lx in recovery\n",
  235. zfcp_get_busid_by_unit(unit),
  236. unit->fcp_lun, unit->port->wwpn);
  237. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  238. goto out;
  239. }
  240. if (unlikely(tmp < 0)) {
  241. ZFCP_LOG_DEBUG("error: initiation of Send FCP Cmnd failed\n");
  242. retval = SCSI_MLQUEUE_HOST_BUSY;
  243. }
  244. out:
  245. return retval;
  246. }
  247. static void
  248. zfcp_scsi_command_sync_handler(struct scsi_cmnd *scpnt)
  249. {
  250. struct completion *wait = (struct completion *) scpnt->SCp.ptr;
  251. complete(wait);
  252. }
  253. /**
  254. * zfcp_scsi_command_sync - send a SCSI command and wait for completion
  255. * @unit: unit where command is sent to
  256. * @scpnt: scsi command to be sent
  257. * @use_timer: indicates whether timer should be setup or not
  258. * Return: 0
  259. *
  260. * Errors are indicated in scpnt->result
  261. */
  262. int
  263. zfcp_scsi_command_sync(struct zfcp_unit *unit, struct scsi_cmnd *scpnt,
  264. int use_timer)
  265. {
  266. int ret;
  267. DECLARE_COMPLETION_ONSTACK(wait);
  268. scpnt->SCp.ptr = (void *) &wait; /* silent re-use */
  269. scpnt->scsi_done = zfcp_scsi_command_sync_handler;
  270. ret = zfcp_scsi_command_async(unit->port->adapter, unit, scpnt,
  271. use_timer);
  272. if (ret == 0)
  273. wait_for_completion(&wait);
  274. scpnt->SCp.ptr = NULL;
  275. return 0;
  276. }
  277. /*
  278. * function: zfcp_scsi_queuecommand
  279. *
  280. * purpose: enqueues a SCSI command to the specified target device
  281. *
  282. * returns: 0 - success, SCSI command enqueued
  283. * !0 - failure
  284. */
  285. static int
  286. zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  287. void (*done) (struct scsi_cmnd *))
  288. {
  289. struct zfcp_unit *unit;
  290. struct zfcp_adapter *adapter;
  291. /* reset the status for this request */
  292. scpnt->result = 0;
  293. scpnt->host_scribble = NULL;
  294. scpnt->scsi_done = done;
  295. /*
  296. * figure out adapter and target device
  297. * (stored there by zfcp_scsi_slave_alloc)
  298. */
  299. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  300. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  301. return zfcp_scsi_command_async(adapter, unit, scpnt, 0);
  302. }
  303. static struct zfcp_unit *
  304. zfcp_unit_lookup(struct zfcp_adapter *adapter, int channel, unsigned int id,
  305. unsigned int lun)
  306. {
  307. struct zfcp_port *port;
  308. struct zfcp_unit *unit, *retval = NULL;
  309. list_for_each_entry(port, &adapter->port_list_head, list) {
  310. if (!port->rport || (id != port->rport->scsi_target_id))
  311. continue;
  312. list_for_each_entry(unit, &port->unit_list_head, list)
  313. if (lun == unit->scsi_lun) {
  314. retval = unit;
  315. goto out;
  316. }
  317. }
  318. out:
  319. return retval;
  320. }
  321. /**
  322. * zfcp_scsi_eh_abort_handler - abort the specified SCSI command
  323. * @scpnt: pointer to scsi_cmnd to be aborted
  324. * Return: SUCCESS - command has been aborted and cleaned up in internal
  325. * bookkeeping, SCSI stack won't be called for aborted command
  326. * FAILED - otherwise
  327. *
  328. * We do not need to care for a SCSI command which completes normally
  329. * but late during this abort routine runs. We are allowed to return
  330. * late commands to the SCSI stack. It tracks the state of commands and
  331. * will handle late commands. (Usually, the normal completion of late
  332. * commands is ignored with respect to the running abort operation.)
  333. */
  334. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  335. {
  336. struct Scsi_Host *scsi_host;
  337. struct zfcp_adapter *adapter;
  338. struct zfcp_unit *unit;
  339. struct zfcp_fsf_req *fsf_req;
  340. unsigned long flags;
  341. unsigned long old_req_id;
  342. int retval = SUCCESS;
  343. scsi_host = scpnt->device->host;
  344. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  345. unit = (struct zfcp_unit *) scpnt->device->hostdata;
  346. ZFCP_LOG_INFO("aborting scsi_cmnd=%p on adapter %s\n",
  347. scpnt, zfcp_get_busid_by_adapter(adapter));
  348. /* avoid race condition between late normal completion and abort */
  349. write_lock_irqsave(&adapter->abort_lock, flags);
  350. /* Check whether corresponding fsf_req is still pending */
  351. spin_lock(&adapter->req_list_lock);
  352. fsf_req = zfcp_reqlist_find(adapter,
  353. (unsigned long) scpnt->host_scribble);
  354. spin_unlock(&adapter->req_list_lock);
  355. if (!fsf_req) {
  356. write_unlock_irqrestore(&adapter->abort_lock, flags);
  357. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
  358. retval = SUCCESS;
  359. goto out;
  360. }
  361. fsf_req->data = 0;
  362. fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  363. old_req_id = fsf_req->req_id;
  364. /* don't access old fsf_req after releasing the abort_lock */
  365. write_unlock_irqrestore(&adapter->abort_lock, flags);
  366. fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
  367. if (!fsf_req) {
  368. ZFCP_LOG_INFO("error: initiation of Abort FCP Cmnd failed\n");
  369. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  370. old_req_id);
  371. retval = FAILED;
  372. goto out;
  373. }
  374. __wait_event(fsf_req->completion_wq,
  375. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  376. if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  377. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
  378. retval = SUCCESS;
  379. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  380. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
  381. retval = SUCCESS;
  382. } else {
  383. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
  384. retval = FAILED;
  385. }
  386. zfcp_fsf_req_free(fsf_req);
  387. out:
  388. return retval;
  389. }
  390. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  391. {
  392. int retval;
  393. struct zfcp_unit *unit = scpnt->device->hostdata;
  394. if (!unit) {
  395. WARN_ON(1);
  396. return SUCCESS;
  397. }
  398. retval = zfcp_task_management_function(unit,
  399. FCP_LOGICAL_UNIT_RESET,
  400. scpnt);
  401. return retval ? FAILED : SUCCESS;
  402. }
  403. static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
  404. {
  405. int retval;
  406. struct zfcp_unit *unit = scpnt->device->hostdata;
  407. if (!unit) {
  408. WARN_ON(1);
  409. return SUCCESS;
  410. }
  411. retval = zfcp_task_management_function(unit, FCP_TARGET_RESET, scpnt);
  412. return retval ? FAILED : SUCCESS;
  413. }
  414. static int
  415. zfcp_task_management_function(struct zfcp_unit *unit, u8 tm_flags,
  416. struct scsi_cmnd *scpnt)
  417. {
  418. struct zfcp_adapter *adapter = unit->port->adapter;
  419. struct zfcp_fsf_req *fsf_req;
  420. int retval = 0;
  421. /* issue task management function */
  422. fsf_req = zfcp_fsf_send_fcp_command_task_management
  423. (adapter, unit, tm_flags, 0);
  424. if (!fsf_req) {
  425. ZFCP_LOG_INFO("error: creation of task management request "
  426. "failed for unit 0x%016Lx on port 0x%016Lx on "
  427. "adapter %s\n", unit->fcp_lun, unit->port->wwpn,
  428. zfcp_get_busid_by_adapter(adapter));
  429. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  430. retval = -ENOMEM;
  431. goto out;
  432. }
  433. __wait_event(fsf_req->completion_wq,
  434. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  435. /*
  436. * check completion status of task management function
  437. */
  438. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  439. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  440. retval = -EIO;
  441. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  442. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  443. retval = -ENOTSUPP;
  444. } else
  445. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  446. zfcp_fsf_req_free(fsf_req);
  447. out:
  448. return retval;
  449. }
  450. /**
  451. * zfcp_scsi_eh_host_reset_handler - handler for host reset
  452. */
  453. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  454. {
  455. struct zfcp_unit *unit;
  456. struct zfcp_adapter *adapter;
  457. unit = (struct zfcp_unit*) scpnt->device->hostdata;
  458. adapter = unit->port->adapter;
  459. ZFCP_LOG_NORMAL("host reset because of problems with "
  460. "unit 0x%016Lx on port 0x%016Lx, adapter %s\n",
  461. unit->fcp_lun, unit->port->wwpn,
  462. zfcp_get_busid_by_adapter(unit->port->adapter));
  463. zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
  464. zfcp_erp_wait(adapter);
  465. return SUCCESS;
  466. }
  467. int
  468. zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  469. {
  470. int retval = 0;
  471. static unsigned int unique_id = 0;
  472. if (adapter->scsi_host)
  473. goto out;
  474. /* register adapter as SCSI host with mid layer of SCSI stack */
  475. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  476. sizeof (struct zfcp_adapter *));
  477. if (!adapter->scsi_host) {
  478. ZFCP_LOG_NORMAL("error: registration with SCSI stack failed "
  479. "for adapter %s ",
  480. zfcp_get_busid_by_adapter(adapter));
  481. retval = -EIO;
  482. goto out;
  483. }
  484. ZFCP_LOG_DEBUG("host registered, scsi_host=%p\n", adapter->scsi_host);
  485. /* tell the SCSI stack some characteristics of this adapter */
  486. adapter->scsi_host->max_id = 1;
  487. adapter->scsi_host->max_lun = 1;
  488. adapter->scsi_host->max_channel = 0;
  489. adapter->scsi_host->unique_id = unique_id++; /* FIXME */
  490. adapter->scsi_host->max_cmd_len = ZFCP_MAX_SCSI_CMND_LENGTH;
  491. adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
  492. /*
  493. * save a pointer to our own adapter data structure within
  494. * hostdata field of SCSI host data structure
  495. */
  496. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  497. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  498. scsi_host_put(adapter->scsi_host);
  499. retval = -EIO;
  500. goto out;
  501. }
  502. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  503. out:
  504. return retval;
  505. }
  506. void
  507. zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  508. {
  509. struct Scsi_Host *shost;
  510. struct zfcp_port *port;
  511. shost = adapter->scsi_host;
  512. if (!shost)
  513. return;
  514. read_lock_irq(&zfcp_data.config_lock);
  515. list_for_each_entry(port, &adapter->port_list_head, list)
  516. if (port->rport)
  517. port->rport = NULL;
  518. read_unlock_irq(&zfcp_data.config_lock);
  519. fc_remove_host(shost);
  520. scsi_remove_host(shost);
  521. scsi_host_put(shost);
  522. adapter->scsi_host = NULL;
  523. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  524. return;
  525. }
  526. /*
  527. * Support functions for FC transport class
  528. */
  529. static struct fc_host_statistics*
  530. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  531. {
  532. struct fc_host_statistics *fc_stats;
  533. if (!adapter->fc_stats) {
  534. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  535. if (!fc_stats)
  536. return NULL;
  537. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  538. }
  539. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  540. return adapter->fc_stats;
  541. }
  542. static void
  543. zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  544. struct fsf_qtcb_bottom_port *data,
  545. struct fsf_qtcb_bottom_port *old)
  546. {
  547. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset -
  548. old->seconds_since_last_reset;
  549. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  550. fc_stats->tx_words = data->tx_words - old->tx_words;
  551. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  552. fc_stats->rx_words = data->rx_words - old->rx_words;
  553. fc_stats->lip_count = data->lip - old->lip;
  554. fc_stats->nos_count = data->nos - old->nos;
  555. fc_stats->error_frames = data->error_frames - old->error_frames;
  556. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  557. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  558. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  559. fc_stats->loss_of_signal_count = data->loss_of_signal -
  560. old->loss_of_signal;
  561. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts -
  562. old->psp_error_counts;
  563. fc_stats->invalid_tx_word_count = data->invalid_tx_words -
  564. old->invalid_tx_words;
  565. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  566. fc_stats->fcp_input_requests = data->input_requests -
  567. old->input_requests;
  568. fc_stats->fcp_output_requests = data->output_requests -
  569. old->output_requests;
  570. fc_stats->fcp_control_requests = data->control_requests -
  571. old->control_requests;
  572. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  573. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  574. }
  575. static void
  576. zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  577. struct fsf_qtcb_bottom_port *data)
  578. {
  579. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  580. fc_stats->tx_frames = data->tx_frames;
  581. fc_stats->tx_words = data->tx_words;
  582. fc_stats->rx_frames = data->rx_frames;
  583. fc_stats->rx_words = data->rx_words;
  584. fc_stats->lip_count = data->lip;
  585. fc_stats->nos_count = data->nos;
  586. fc_stats->error_frames = data->error_frames;
  587. fc_stats->dumped_frames = data->dumped_frames;
  588. fc_stats->link_failure_count = data->link_failure;
  589. fc_stats->loss_of_sync_count = data->loss_of_sync;
  590. fc_stats->loss_of_signal_count = data->loss_of_signal;
  591. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  592. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  593. fc_stats->invalid_crc_count = data->invalid_crcs;
  594. fc_stats->fcp_input_requests = data->input_requests;
  595. fc_stats->fcp_output_requests = data->output_requests;
  596. fc_stats->fcp_control_requests = data->control_requests;
  597. fc_stats->fcp_input_megabytes = data->input_mb;
  598. fc_stats->fcp_output_megabytes = data->output_mb;
  599. }
  600. /**
  601. * zfcp_get_fc_host_stats - provide fc_host_statistics for scsi_transport_fc
  602. *
  603. * assumption: scsi_transport_fc synchronizes calls of
  604. * get_fc_host_stats and reset_fc_host_stats
  605. * (XXX to be checked otherwise introduce locking)
  606. */
  607. static struct fc_host_statistics *
  608. zfcp_get_fc_host_stats(struct Scsi_Host *shost)
  609. {
  610. struct zfcp_adapter *adapter;
  611. struct fc_host_statistics *fc_stats;
  612. struct fsf_qtcb_bottom_port *data;
  613. int ret;
  614. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  615. fc_stats = zfcp_init_fc_host_stats(adapter);
  616. if (!fc_stats)
  617. return NULL;
  618. data = kzalloc(sizeof(*data), GFP_KERNEL);
  619. if (!data)
  620. return NULL;
  621. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  622. if (ret) {
  623. kfree(data);
  624. return NULL; /* XXX return zeroed fc_stats? */
  625. }
  626. if (adapter->stats_reset &&
  627. ((jiffies/HZ - adapter->stats_reset) <
  628. data->seconds_since_last_reset)) {
  629. zfcp_adjust_fc_host_stats(fc_stats, data,
  630. adapter->stats_reset_data);
  631. } else
  632. zfcp_set_fc_host_stats(fc_stats, data);
  633. kfree(data);
  634. return fc_stats;
  635. }
  636. static void
  637. zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  638. {
  639. struct zfcp_adapter *adapter;
  640. struct fsf_qtcb_bottom_port *data, *old_data;
  641. int ret;
  642. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  643. data = kzalloc(sizeof(*data), GFP_KERNEL);
  644. if (!data)
  645. return;
  646. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  647. if (ret) {
  648. kfree(data);
  649. } else {
  650. adapter->stats_reset = jiffies/HZ;
  651. old_data = adapter->stats_reset_data;
  652. adapter->stats_reset_data = data; /* finally freed in
  653. adater_dequeue */
  654. kfree(old_data);
  655. }
  656. }
  657. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  658. {
  659. rport->dev_loss_tmo = timeout;
  660. }
  661. struct fc_function_template zfcp_transport_functions = {
  662. .show_starget_port_id = 1,
  663. .show_starget_port_name = 1,
  664. .show_starget_node_name = 1,
  665. .show_rport_supported_classes = 1,
  666. .show_rport_maxframe_size = 1,
  667. .show_rport_dev_loss_tmo = 1,
  668. .show_host_node_name = 1,
  669. .show_host_port_name = 1,
  670. .show_host_permanent_port_name = 1,
  671. .show_host_supported_classes = 1,
  672. .show_host_supported_speeds = 1,
  673. .show_host_maxframe_size = 1,
  674. .show_host_serial_number = 1,
  675. .get_fc_host_stats = zfcp_get_fc_host_stats,
  676. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  677. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  678. /* no functions registered for following dynamic attributes but
  679. directly set by LLDD */
  680. .show_host_port_type = 1,
  681. .show_host_speed = 1,
  682. .show_host_port_id = 1,
  683. .disable_target_scan = 1,
  684. };
  685. /**
  686. * ZFCP_DEFINE_SCSI_ATTR
  687. * @_name: name of show attribute
  688. * @_format: format string
  689. * @_value: value to print
  690. *
  691. * Generates attribute for a unit.
  692. */
  693. #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value) \
  694. static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev, struct device_attribute *attr, \
  695. char *buf) \
  696. { \
  697. struct scsi_device *sdev; \
  698. struct zfcp_unit *unit; \
  699. \
  700. sdev = to_scsi_device(dev); \
  701. unit = sdev->hostdata; \
  702. return sprintf(buf, _format, _value); \
  703. } \
  704. \
  705. static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
  706. ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n", zfcp_get_busid_by_unit(unit));
  707. ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
  708. ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
  709. static struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
  710. &dev_attr_fcp_lun,
  711. &dev_attr_wwpn,
  712. &dev_attr_hba_id,
  713. NULL
  714. };
  715. #undef ZFCP_LOG_AREA